// remap jQuery to $
(function($){})(window.jQuery);

/* trigger when page is ready */
$(document).ready(function (){
	jQuery.preLoadImages(
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide1.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide2.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide3.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide4.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide5.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide6.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide7.jpg",
		"http://dev.squadcentral.com.au/barkala/holding-page/_holding/img/slide8.jpg"
	);
	siteHeightResize();
	rotatorResize();
	imgResize();
	imgResize();
	centerGallery();
	initRotator();
	
	$("#showHide").click(function(){
		$(".bg").toggle();
		$(".content-holder").toggle();
	});
});

function siteHeightResize() { $( "#site" ).height( ( $( window ).height() ) - 320 ); }

//ROTATOR

//variables
var t
var slideDelay = 3000
var currSlide = ".current"

//Initialise rotator

function initRotator(){
	$("#rotator .slides").each(function(){
		//Set first and last slides
		$(this).children("LI:first").addClass("first");
		$(this).children("LI:last").addClass("last");
		//Give each slide an ID
		var i = 1
		$(this).children("LI").each(function(){
			var slideID = "slide"+i
			$(this).attr("id", slideID).hide();
			i++;
		});
		$(".current").show();
		$(".next").show();

	});
	//Start auto rotate timer
	t = setTimeout("autoRotate();",slideDelay);
}

//Auto-Rotate
function autoRotate(){
	//Reset timer
	clearTimeout(t);
	var nextSlide
	//If current slide is the last the next slide will be the first, Otherwise the nest slide is the next numerically
	if ($(currSlide).hasClass("last")){
		nextSlide ="#slide1"
	}else{
		var nextSlide = $(currSlide).next();
	}
	//Rotate to the next slide
	rotate(nextSlide);
}

//Rotate
function rotate(nextSlide){
	//Give the next slide the class "next"
	$(".next").removeClass("next");
	$(nextSlide).addClass("next").show();
	//Fade out current slide
	$(currSlide).fadeOut(800, function(){
		//Rename the next slide to be the current slide after previous slide fades out
		$(currSlide).removeClass("current").hide();;
		$(nextSlide).addClass("current").removeClass("next");

		//Restart auto Rotate timer
		t = setTimeout("autoRotate();",slideDelay);
	});
}

function rotatorResize(){
	var windowWidth = $(window).width()
	$("#rotator").width(windowWidth);
	var windowHeight = $(window).height()
	$("#rotator").height(windowHeight);
}

function imgResize(){
	$("#rotator .slides LI IMG").each(function(){
		var windowWidth = $(window).width()
		var windowHeight = $(window).height()
		var imgWidth = $(this).width()
		var imgHeight = $(this).height()
		var ratio = imgWidth / imgHeight;
		if ((windowHeight <= 721) && (windowWidth<=1280)){
			$(this).css("width", 1280);
			$(this).css("height", 721);
		}else{
			if ((imgWidth >= windowWidth) && (imgHeight <= windowHeight)){
				$(this).css("height", windowHeight);
				$(this).css("width", (windowHeight * ratio));
			}else {
				$(this).css("width", windowWidth);
				$(this).css("height", (windowWidth / ratio));
			}
		}
	});
}

function centerGallery() {
	var vDisplacement = (($("#rotator").height())-($("#rotator LI").height()))/2
	var hDisplacement = (($("#rotator").width())-($("#rotator LI").width()))/2
	$("#rotator UL").css("top", vDisplacement);
	$("#rotator UL").css("left", hDisplacement);
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(window).resize(function() {
	siteHeightResize();
	rotatorResize();
	imgResize();
	imgResize();
	imgResize();
	imgResize();
	centerGallery();
});






