//HOME PAGE HEADER IAMGES
// Author: http://www.marcofolio.net/webdesign/advanced_jquery_background_image_slideshow.html
// Also: http://plungjan.name/test/js_background_rotator

(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);

jQuery.preLoadImages("assets/images/homepage/img1.jpg", "assets/images/homepage/img2.jpg", "assets/images/homepage/img3.jpg", "assets/images/homepage/img4.jpg", "assets/images/homepage/img5.jpg", "assets/images/homepage/img6.jpg");

var photos =[{"image":"img1.jpg", "transitionTime":5 }, 
					{"image":"img2.jpg", "transitionTime":5 }, 
					{"image":"img3.jpg", "transitionTime":5 },
					{"image":"img4.jpg", "transitionTime":5 },
					{"image":"img5.jpg", "transitionTime":5 },
					{"image":"img6.jpg", "transitionTime":5 }];
					
var stopped = false; // autoplaying if false; manual start if true

$(document).ready(function() {

	var timeout;
	var activeContainer = 1;  
	var currentImg = 0;
	var animating = false;
	
	var navigate = function(direction, override) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		var photoObj = photos[currentImg]; 
		
		if (photoObj.timeLeft == undefined) { // first time
		  photoObj.timeLeft=photoObj.transitionTime;
		  showImage(photoObj);
		}
		else if (photoObj.timeLeft==0 || override) { 
		  // Check which current image we need to show
			if (direction == "next") {
				currentImg++;
				if(currentImg >= photos.length) {
					currentImg = 0;
				}
			} 
		  
			photoObj = photos[currentImg]; // new image
			photoObj.timeLeft=photoObj.transitionTime;
		  
			showImage(photoObj);
		}
		
		if (!stopped) { photoObj.timeLeft--; }
		timeout = setTimeout(function() {navigate("next")},1000);
	};
	 
		var currentZindex = -1;
	  
		var showImage = function(photoObject) {
		animating = false;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
		  activeContainer = 2;
		} else {
		  activeContainer = 1;
		}
	  
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(assets/images/homepage/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut('slow', function() {
			// Animation complete.
		});
	  };
	  
	  // We should statically set the first image
	  currentImg = 0; 
	  navigate("next");
  
  
});


// -----------------------------------------------------------------------------------------
	
// IE6 Hack
// Main Menu display drop down menu when mousing over the li and not the link

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


// -----------------------------------------------------------------------------------------


// HOME PAGE Main big button
function moveBackground(backHeight) {
	var posHeight = "0px " + backHeight + "px";
	$('#mainOnes').css('backgroundPosition', posHeight);
}


// -----------------------------------------------------------------------------------------
// HOME PAGE TESTIMONIALS SCROLLER
// http://flowplayer.org/tools/index.html
$(document).ready(function() {

	$("a[rel^='prettyPhoto']").prettyPhoto({
			keyboard_shortcuts: false
	});
	
	if($("#browsable").length) {
		// initialize scrollable
		$("#browsable").scrollable({ circular: true, mousewheel: false });
		
		// return flase when click the next / prev buttons
		$("a.next").click(function() { return false; });
		$("a.prev").click(function() { return false; });
	}

});


// -----------------------------------------------------------------------------------------

