/* Javascript functions for longwood bed and breakfast */
/* Created by http://www.visualchefs.com/ */

/* Image Preloader */
jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// JavaScript Document
$(document).ready(
	function() {

		/* Launch external links in new window */
		$("a[@rel='external']").addClass("external").click(function(){window.open(this.href);return false;});
		
		if($("body#rooms")){

			/* Hide the room images */
			$("div.room_images").hide();
		
			/* Hide the room details */
			$("div.room_details").each(function(i){
				if (!$(this).is('.active')){
						$(this).hide()
				}
			});
			
			/* Preload the imagery */
			$.preloadImages(
				"../images/bedrooms/jarman_1.jpg",
				"../images/bedrooms/jarman_2.jpg",
				"../images/bedrooms/lancaster_1.jpg",
				"../images/bedrooms/grant_1.jpg",
				"../images/bedrooms/grant_2.jpg",	
				"../images/bedrooms/grant_3.jpg",
				"../images/bedrooms/cunningham_1.jpg",
				"../images/bedrooms/blanton-hanbury_1.jpg",
				"../images/bedrooms/blanton-hanbury_2.jpg",			
				"../images/bedrooms/lankford_1.jpg",
				"../images/bedrooms/lankford_2.jpg",
				"../images/bedrooms/lankford_3.jpg",
				"../images/bedrooms/thumbs/jarman_1.jpg",
				"../images/bedrooms/thumbs/jarman_2.jpg",
				"../images/bedrooms/thumbs/lancaster_1.jpg",
				"../images/bedrooms/thumbs/grant_1.jpg",
				"../images/bedrooms/thumbs/grant_2.jpg",			
				"../images/bedrooms/thumbs/cunningham_1.jpg",
				"../images/bedrooms/thumbs/blanton-hanbury_1.jpg",
				"../images/bedrooms/thumbs/blanton-hanbury_2.jpg",			
				"../images/bedrooms/thumbs/lankford_1.jpg",
				"../images/bedrooms/thumbs/lankford_2.jpg",
				"../images/bedrooms/thumbs/lankford_3.jpg"
			);			
		
			/* handle room click */
			$("div.room h3 a").click(function(){
				/* Get room details container */
				el =  $(this).parent().siblings("div.room_details");
				
				/* See if the current room was clicked and if not continue */
				if(!el.is('.active')){
						/* loop through each room */
						$("div.room_details").each(function(i){
							if($(this).is('.active')){
								/* Hide the active room */
								$(this).slideUp();
								$(this).removeClass("active");
								$(this).siblings("h3").children().removeClass("active");
							}
						});	
						
						/* show the current room */
						$(this).addClass("active");
						$(el).addClass("active");
						$(el).slideDown();
						
						/* swap the images */
						$("div.thumbnails").empty();
						
						$(el.siblings("div.room_images").children().children("li")).each(function(i){
							if(i == 0){
								$("img.main_image").attr("src", "images/bedrooms/"+ $(this).html().replace(/^\s+|\s+$/g, '') +".jpg");
								$("div.thumbnails").prepend('<img src="images/bedrooms/thumbs/' + $(this).html().replace(/^\s+|\s+$/g, '') + '.jpg" alt="" class="active" />');
							}else{
								$("div.thumbnails").prepend('<img src="images/bedrooms/thumbs/' + $(this).html().replace(/^\s+|\s+$/g, '') + '.jpg" alt="" />');
							}
						});
						
						/* handle image thumbnail click */
						$("div.thumbnails img").click(function(){
							if(!$(this).is('.active')){
								// Reset active class
								$(this).siblings().removeClass("active");
								$(this).addClass("active");
	
								// Swap image
								$("img.main_image").attr("src", this.src.replace(/thumbs\//,""));
							}
						});						
						
				}else{
						/* hide the clicked room */
						$(el).slideUp();
						$(el).removeClass("active");
						$(el).siblings("h3").children().removeClass("active");
				}
			});
		
			/* handle image thumbnail click */
			$("div.thumbnails img").click(function(){
				if(!$(this).is('.active')){
						// Reset active class
						$(this).siblings().removeClass("active");
						$(this).addClass("active");
	
						// Swap image
						$("img.main_image").attr("src", this.src.replace(/thumbs\//,""));
				}
			});	
		} // End if body#rooms
		
		if($("body#faqs")){
			
			/* Hide the faq details */
			$("div.faq_details").each(function(i){
				if (!$(this).is('.active')){
						$(this).hide()
				}
			});

			/* handle faq click */
			$("div.faq h3 a").click(function(){
				/* Get faq details container */
				el =  $(this).parent().siblings("div.faq_details");
				
				/* See if the current faq was clicked and if not continue */
				if(!el.is('.active')){
						/* loop through each faq */
						$("div.faq_details").each(function(i){
							if($(this).is('.active')){
								/* Hide the active faq */
								$(this).slideUp();
								$(this).removeClass("active");
								$(this).siblings("h3").children().removeClass("active");
							}
						});	
						
						/* show the current faq */
						$(this).addClass("active");
						$(el).addClass("active");
						$(el).slideDown();
				}else{
						/* hide the clicked faq */
						$(el).slideUp();
						$(el).removeClass("active");
						$(el).siblings("h3").children().removeClass("active");
				}
			});
		} // End if body#faqs
		
		if($("body#directions")){
			
			/* Hide the directions details */
			$("div.direction_details").each(function(i){
				if (!$(this).is('.active')){
						$(this).hide()
				}
			});
		} // End if body#directions

		if($("body#photos")){
			
			/* Preload the imagery */
			$.preloadImages(
				"../images/historical/badminton.gif",
				"../images/historical/sitting.gif",
				"../images/historical/game.gif",
				"../images/historical/front.gif",
				"../images/historical/pergola.gif",
				"../images/historical/side_yard.gif",
				"../images/historical/tree_view.gif",
				"../images/historical/garden.gif"
			);			
		
			/* handle thumbnail click */
			/* Need to add title swapping */
			$("ul#photogallery li a").click(function(){
				/* Get parent container */
				el =  $(this).parent();
				obj = $(this).children();
			
				/* See if the current image was clicked and if not continue */
				if(!el.is('.active')){
						/* loop through each thumbnail */
						$("ul#photogallery li").each(function(i){
							if($(this).is('.active')){
								$(this).removeClass("active");
							}
						});	
						
						/* show the current thumb */
						$(el).addClass("active");
						$("div.main_image_box img").attr("src", obj[0].src.replace(/thumbs\//,""));
						
						/* Show the image title */
						$("#maincontent h3").empty();
						$("#maincontent h3").append(obj[0].alt);
				}
			});
		} // End if body#photos
		
		if($("body#directions")){
			
		} // End if body#directions
		
	}
);
