// JavaScript Document

$(document).ready(function() {

	var index = 0;
	var originalSliderHeight = 0;
	var currentSliderHeight = 0;

	if ($('#intro-logo').length > 0) { // Homepage only
		$('#intro-logo').css({ "display":"block" });
		setHomepageLogoPosition();	
		$(window).resize(function() {
				setHomepageLogoPosition();
		})
		$.backstretch(myImages[index], {speed: 750});
	}

	$("#page-intro").imageLoader({ // None of the images will fade until they have all loaded
			images: myImages,
			splashScreenImage: false,
			enableSplashScreenImage: false,
			enableSplashScreenText: false,
			enableSplashScreen: false
		}, function(){
				setInterval(function() {
					index = (index >= myImages.length - 1) ? 0 : index + 1;
					$.backstretch(myImages[index]);
				}, 7000);
		});

	function setHomepageLogoPosition() {
		var windowHeight = $(window).height();
		var introLogoHeight = $('#intro-logo').height();
		var topPos = Math.ceil((windowHeight/2) - (introLogoHeight/2));
		$('#intro-logo').css({ "top": topPos + "px" });
	}

	$("a.trigger").click(function (event) {
		event.preventDefault();
		$('#slides').cycle('pause');

		if (originalSliderHeight == 0) {
			originalSliderHeight = $('#slides').height();
		}

		$(this).toggleClass('open');
		$(this).parents('.info-container').children('.more-info').show();
		$(this).hide();

		var myInfoHeight = $(this).parents('.info-container').children('.more-info').height();

		if ((myInfoHeight + originalSliderHeight) > currentSliderHeight) {
			currentSliderHeight = originalSliderHeight + myInfoHeight;
			$('#slides').height(currentSliderHeight);
		}
	});

  	// Do preload, after all images loaded, start the cycle
	var objSlideshow = $('#slides');
	var imagesSlideshow = $('img', objSlideshow);
	var totalImages = imagesSlideshow.length;
	var iTotalImage = 0;

	imagesSlideshow.each(function(){
		$('<img />').attr('src', jQuery(this).attr('src')).load(function(){
			iTotalImage++;
			if (totalImages == iTotalImage) {
				// Start the cycle
				$('#slides').cycle({
					fx: 'scrollHorz',
					prev: '#btn-prev', 
					next: '#btn-next',
					timeout: 6000,
					speed: 500,
					pause: 1
				});
				// Show the btns
				$('#btn-prev').show();
				$('#btn-next').show();
			}
		});
	});

	$('img').bind('contextmenu', function(e){ // Remove right click functionality
		e.preventDefault();
		return false;
	});

});
