(function(){

	//Create Namespace
	if(!window.DP) {window['DP'] = {}}	

	//Helper function to check if an element (i.e. div) exists. Argument is element id
	var exists = function(element) {
		if (!document.getElementById(element)) {
			return false;
		}
		else {
			return true;
		}
	}
	window['DP']['exists'] = exists;
	
	//
	var updateComments = function(element) {
		jQuery.ajaxSetup({
			'beforeSend': function(xhr) {xhr.setRequestHeader("Accept","text/javascript")}
		})
		$('#comment-form').submit(function(){
			$.post($(this).attr("action"), $(this).serialize(), null, "script");
			return false;
		})
	}
	window['DP']['updateComments'] = updateComments;
	
	//Inline validation for Registration form
	var commentFormValidation = function(){
		$('#comment-form').validate({
			rules: {
				'comment[name]': {
			  	required: true,
					minlength: 1,
			  },
				'comment[email]': {
	        required: true,
	        email: true
			  },
        'comment[comment]': {
            required: true
			  },
			 'recaptcha_response_field':{
						required: true
			  }
			}, //end rules
			messages: {
				'comment[name]': {
					required: "What's your name?"
				},
				'comment[email]': {
					required: "What's your email address?",
					email: "I need a valid email address"
				},
			  'comment[comment]': {
			  	required: "What's your comment?"
			  },
				'recaptcha_response_field':{
					required: "Are you a human?"
				}
			} //end messages
			
		});
	}
	window['DP']['commentFormValidation'] = commentFormValidation;
	
	//Shows or hides div when element is clicked, depending on action parameter (can be show or hide)
	var popup = function() {		
		$('.portfolio-item').click(function(){
			var expandedSnapshotSmall = $(this).attr('rel');	
			var baseURL = window.location.host;
			var expandedSnapshotLink = "http://" + baseURL + "/images/PORTFOLIO/" + expandedSnapshotSmall
			var expandedSnapshotLarge = $(this).next().find('img');
			$(expandedSnapshotLarge).attr('src',expandedSnapshotLink);
			var imageLarge = '<img class="portfolio-item-expanded" src="'+expandedSnapshotLink+'" />';
			// $.prompt(imageLarge,{ 
			// 	// buttons: { Delete: true, Close: false }, 
			// });
		});		
	}
	window['DP']['popup'] = popup;
	
	//Opens all hrefs with rel="external" attribute in separate window - target=_blank doesn't validate
	var externalLinks = function(){
		
		if (!document.getElementsByTagName) { 
			return; 
		}
		
		var anchors = document.getElementsByTagName("a");
		
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		} 
	}
	window['DP']['externalLinks'] = externalLinks;

})();
		

//All functions that need to be executed after page load go here
$(document).ready (function() {
	
	//Preload all images
	// if(DP.exists('content-block')){
	// 	$.preloadCssImages();
	// }
	
	//If there's a comment form on the page, validate it
	if(DP.exists('comment-form')){
		DP.commentFormValidation();
		// DP.updateComments();
	}
	
	//If page is Portfolio page, execute the following function
	if (DP.exists('portfolio')) {
		DP.popup();
	}
	
	//open hrefs in external tab/page
	DP.externalLinks();
	
});