// BUILT ON TOP OF JQUERY /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
/*************************************************************************************************/

$(document).ready(function() {
						   
	$("ul li:first-child").addClass("first-child");
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// dynamically move some assets...
	
	// move the images from slideshowContent div into slideshow
	// this is so that any extra markup/content stays within slideshowContent & doesn't affect the slideshow (all slideshow children are 'slides')
	$(".slideshowContent img").appendTo(".slideshow");
	
	// same approach for the gallery, with the extra <li> wrap around each <img> too
	$(".properties.detail #gallery img").appendTo(".properties.detail #gallery ul.gallery");
	$(".properties.detail #gallery ul.gallery img").wrap("<li></li>");
	
	// and again, for each instance...
	$('.grabTheImage').each(function() {
		if ($(this).find('img')) {
			$(this).find('img').eq(0).appendTo($(this).parent().find('.grabbedImage a'));
		}
	});

	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// wrap maincol images in their frames
	
	if ($("body").hasClass("landingPage")) {
		$(".maincol img:eq(0)").wrap("<div class='hero-image slim-hero'><div class='slim-inner'></div></div>");
	} else {
		$(".explore-belize .maincol img:eq(0)").wrap("<div class='small-hero'></div>");
		$(".regions .maincol img:eq(0)").wrap("<div class='small-hero'></div>");
		$(".about-belize .maincol img:eq(0)").wrap("<div class='small-hero'></div>");
		$(".fiesta .maincol img:eq(0)").wrap("<div class='small-hero'></div>");
	}
	
	//$(".landingPage .maincol img:eq(0)").wrap("<div class='hero-image slim-hero'><div class='slim-inner'></div></div>"); // set above
	$(".properties.region .maincol img:eq(0)").wrap("<div class='hero-image slim-hero'><div class='slim-inner'></div></div>");
	$(".properties.resort .maincol img:eq(0)").wrap("<div class='hero-image slim-hero'><div class='slim-inner'></div></div>");
	$(".properties.detail .maincol img:eq(0)").wrap("<div class='hero-image'></div>");
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// setup the slideshow
	// fade up the first image too
	// and make it a random one
	// from http://malsup.com/jquery/cycle/fade-in-first2.html
	// see more at http://malsup.com/jquery/cycle/more.html?v2.23
	
	//$(".slideshow p").addClass("slideshow");
	
	$('.slideshow img').css({
        opacity: 0
    });
	
	setTimeout(function() {
        $('.slideshow').cycle({
            fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed:  3000,
			timeout: 3000,
			random: 1, // set randomness to true
            delay:  -5000
        });

	    $('.slideshow img').css({
	        opacity: 0
	    });

    }, 0);
	
	///////////////////////////////////////////////////////////////////////////////////////////////

	
	// init fancybox, see for more http://fancybox.net/api
	$(".region-excerpt a.map").fancybox({
		'titlePosition' : 'inside'
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// simple galleria gallery
	//jQuery(function($) { $('.gallery .maincol ul').galleria(); }); 
	
	// more advanced galleria gallery
	jQuery(function($) {
		
		//$('.gallery .subcol ul.gallery').addClass('gallery'); // adds new class name to maintain degradability
		$(".properties.detail #gallery ul.gallery li:first-child").addClass("active") // makes first image active
		
		
		$('.properties.detail #gallery ul.gallery').galleria({
			//history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			//insert    : '#main_image', // the containing selector for our main image
			insert	  : 'undefined',
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				/***********************************************************************************************************/
				// put the gallery thumbnails on top of the main image
				/*$(".galleria_container").insertAfter("ul.galleria");
				
				// and then this little routine wraps a parent div round the galleria ul
				// and then works out the necessary margin-left in order to centre the ul within its parent
				$("ul.galleria").wrap("<div class='galleria_thumbs'></div>");
				$("ul.galleria").css('margin-left', ( (601-$("ul.galleria").width()) /2 ));*/
				/***********************************************************************************************************/
				
				// remove the right margin of every third thumbnail
				$(".galleria li:nth-child(3)").css('margin-right','0');
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.6);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.6';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
				)
			}
		});

	});

	// remove the margin from the end of the row list elements
	$("ul.galleria li:nth-child(3n)").addClass("noMarginRight");

	
});
