/*
	Author: Mads Hartmann Jensen, mads379@gmail.com
	
	Description: This documents makes sure that everything looks & acts as intented.
				 This document gets loaded each time a new page is loaded (not when 
				 the user selects another category or archive).
*/

/*

FUNCTIONS THAT ARE USED IN THE SCRIPS

*/

jQuery.extend(jQuery.expr[":"], { 
        "containsNC": function(elem, i, match, array){ 
                return (elem.textContent || elem.innerText || "").toLowerCase 
().indexOf((match[3]||"").toLowerCase()) >= 0; 
        } 
}); 

jQuery(document).ready(function(){
	
	// hide the other headers
	jQuery('#dusk, #dawn, #night').css('opacity',0);
	
	jQuery('.mainMenuItem').hover(
		function(){
			jQuery(this).find('img').animate({
				'padding-bottom' : '5px'
			},200);
		},
		function(){
			jQuery(this).find('img').animate({
				'padding-bottom' : '0px'
			},200);
		})
	
	// Creates the sliding effect of the to-top link.
	jQuery("#to-top").bind("click", function() {	
     		jQuery('html,body').animate({scrollTop: jQuery("body").offset().top}, 500);
      		return false;
     });
     
     jQuery("#commentLink").bind("click", function() {	
     		jQuery('html,body').animate({scrollTop: jQuery("#respond").offset().top}, 500);
      		return false;
     });
	
	jQuery("#s").attr("autocomplete","off");
	jQuery(".sociable_tagline").remove();


/* CATEGORIES :: 										*/
/* =================================================== 	*/
	
	
	if (window.location == "http://pixelresort.com/") {
		setClicked(jQuery("#submenu li").eq(0));
	}
	if (window.location == "http://pixelresort.com/blog/category/Announcements/") {
		setClicked(jQuery("#submenu li").eq(1));
	}
	if (window.location == "http://pixelresort.com/blog/category/Articles/") {
		setClicked(jQuery("#submenu li").eq(2));
	}
	if (window.location == "http://pixelresort.com/blog/category/Releases/") {
		setClicked(jQuery("#submenu li").eq(3));
	}
	if (window.location == "http://pixelresort.com/blog/category/Showcase/") {
		setClicked(jQuery("#submenu li").eq(4));
	}
	
	/* Creates the hover effect */
	jQuery("#submenu li").hover(
	function() { 
		var listItem = jQuery(this);
		var listSpan = jQuery(this).find("span");
		var listA = jQuery(this).find("a");
		
		jQuery(listItem).addClass("selected");
		jQuery(listSpan).addClass("selected");
		jQuery(listA).addClass("selected");
	}
	,
	function() {
		jQuery(".selected").removeClass("selected");
	});
	
	/* Makes sure that the categories are loaded with ajax.	*/
	if (jQuery("#singlePageSubmenu").size() > 0) {
	
	} else {
	jQuery("#submenu li").bind('click', function() {
		
		jQuery(".clicked").removeClass("clicked");
		
		var listItem = jQuery(this);
		var listSpan = jQuery(this).find("span");
		var listA = jQuery(this).find("a");
		
		jQuery(listItem).addClass("clicked");
		jQuery(listSpan).addClass("clicked");
		jQuery(listA).addClass("clicked");
		
		
				
		var linkHref = jQuery(this).find("a").attr("href");
		var url = linkHref + " #post_container";
			
		jQuery("#image-container").fadeIn(200);		
	
		/* Finds the url and loads it into the content div through ajax */
		jQuery("#content").load(url, function() {
			
				jQuery("#image-container").fadeOut(200);
				jQuery(".shareButton").bind('click', function() {
					jQuery(this).next().toggle(200);
				});			
		});
	return false;
	});
	}
 
/*	SEARCH		 												*/
/*	======================================================== 	*/		

var inputvalue = " ";
var searchfield = jQuery("#s");


jQuery(searchfield).focus(function () {
	searchfield.val("");
});

/*
jQuery(searchfield).blur(function() {
	searchfield.val("Type to search");
});
*/

jQuery(searchfield).keyup(function() {
	var shown;
	var hidden;
	
	
	if (inputvalue.length == searchfield.val().length) {
		// the search string didn't change
	} 
	
	inputvalue = searchfield.val();
	if (inputvalue.length == 0) {
		jQuery("#search-results").fadeOut();
	} else {
		var doesContain = jQuery("#search-results ul li a h1:containsNC('" + inputvalue + "')");
		var doesNotContain = jQuery("#search-results ul li a h1").not(doesContain);
			
		jQuery(doesContain).parent().parent().show();
		jQuery(doesNotContain).parent().parent().hide();
		
		if (jQuery(doesContain).size() == 0 ) {
			jQuery(".noResults").remove();
			jQuery("<p class='noResults'>Sorry, couldn't find any posts with the title <br /> <span>'" + inputvalue +"'</span></p>").appendTo("#search-results");
		} else {
			jQuery(".noResults").remove();
		}
		
		// this will only be trigged when it is hidden.
		jQuery("#search-results").fadeIn();
	}
}); 

 		 
/*	SHARE BUTTONS 												*/
/*	======================================================== 	*/			
		
	jQuery(".shareButton").bind('click', function() {
		jQuery(this).next().toggle(200);
	});

/*	MODAL WINDOW												*/
/*	======================================================== 	*/	
	
	mailFormXHTML = "\
		<h1>Send me an email</h1> \
		<form method='post'> \
			<fieldset> \
				<label for='name'>Name</label> \
				<input name='name' id='name' type='text' /> \
			</fieldset> \
			<fieldset> \
				<label for='email'>Email</label> \
				<input name='email' id='email' type='text' /> \
			</fieldset> \
			<fieldset> \
				<label for='message' id='messageLabel'>Message</label> \
				<textarea name='message' id='message' /> \
			</fieldset> \
				<span class='button'> \
					<input type='submit' value='Send' id='mail-button'></input> \
				</span> \
		</form>";
	
	var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		jQuery(".modal-window").remove();
		jQuery(".modal-overlay").remove();
	},
	open:function()
	{
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		jQuery(this.parent).append(modal);

		jQuery(".modal-window").append("<a class=\"close-window\"></a>");
		jQuery(".close-window").click(function(){modalWindow.close();});
		jQuery(".modal-overlay").click(function(){modalWindow.close();});
		// add the event handler to the send button
		jQuery("#mail-button").bind("click", 
		function() {
			
			if ( jQuery("#name").val().length == 0 || jQuery("#email").val().length == 0 || jQuery("#message").val.length == 0 ) {
				jQuery("#myModal").prepend("<p id='fail'>Alle felter skal udfyldes</p>");  
			} 
			else {
				jQuery.post("/wp-content/themes/PixelResort/phpfiles/mail.php", { 
					navn: jQuery("#name").val(), 
					email: jQuery("#email").val(), 
					besked: jQuery("#message").val()}, 
				function() {
					jQuery("#myModal form").fadeOut("300", function(){
						jQuery("#myModal h1").html("Thanks a lot!").addClass('thankyou');  
						jQuery("#myModal").append("<h2>I'll get back to you as soon as possible.</h2>");
					});
					jQuery("#fail").remove();
					
				});
			}		
		return false; // ignores the normalt event
	});

	}
	};

	var openMyModal = function()  
	{  
	    modalWindow.windowId = "myModal";  
	    modalWindow.width = 435;  
	    modalWindow.height = 310;  
	    modalWindow.content = mailFormXHTML;  
	    modalWindow.open();  
	}; 

	// Add the event handler to the mail me link
	jQuery("#aboutMeLink").bind("click", function() {
		openMyModal();
		return false;	
	});
	
	jQuery('.open_mail').bind('click', function(){
		openMyModal();
		return false;
	});
	
/* FADING OF THE BANNERS :: 							*/
/* =================================================== 	*/
// {'background' : 'url(http://www.pixelresort.com/img/banner-sunrise.png) top center'}


var imgCount = 1;
var prevImgCount = 0;
jQuery('#dusk').css('background', 'url(http://pixelresort.com/img/banner4.jpg) top center');
jQuery('#dawn').css('background', 'url(http://pixelresort.com/img/banner2.jpg) top center');
jQuery('#night').css('background', 'url(http://pixelresort.com/img/banner3.jpg) top center');

var changeHeader = function(){
	
	
	
	jQuery('.background').eq(prevImgCount).animate({'opacity':0}, 5000);
	jQuery('.background').eq(imgCount).animate({'opacity':1}, 5000);
	++imgCount;
	prevImgCount = imgCount - 1;
	if (imgCount == 4) { prevImgCount = 3}
	if (imgCount == 4) { imgCount = 0}

	setTimeout(function(){ changeHeader() }, 10000);
}

setTimeout(function(){

changeHeader();
		
}, 10000); 
	
	



});
	
	