var a = 0;
var b = 0;
var timer;
var backgrounds = [];
var pictures = [];
var cache = [];
var initLoad = true;
var mobile = false;

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

/***** GALLERY PAGE ****/

function setupGalleryPage() {
		isMobile();
		backgrounds = [];
		$("ul.gallery").find("a").each(function(i) {
			backgrounds.push(this);	
			$(this).attr("index",i);		
			cache.push($(this).attr("href"));
		}).click(function() {
			if (a!==($(this).attr("index")*1)) {
				showGalleryBackgroundImage($(this).attr("index")*1);
			}
			return false;
		}).hover(function() {
			if (a!==($(this).attr("index")*1)) {
				$(this).css("opacity", 1);	
			}
		}, function() {
			if (a!==($(this).attr("index")*1)) {
				$(this).css("opacity", 0.4);
			}
		})
		
		
		
		$("ul.smallGallery").find("a").each(function(i) {
			
			$(this).attr("index",i);
			pictures.push(this);	
			cache.push($(this).attr("href"))
			if (i>0) $(this).css("opacity", 0.4);
			
			
		}).mouseover(function() {
			if (b!==($(this).attr("index")*1)) {
				showSmallImage($(this).attr("index")*1);
			}
			return false;
		}).click(function() {
			return false;
		
		});
		
		jQuery.preLoadImages(cache);
		
		//start gallery
		$(document).pngFix(); 
		
		showGalleryBackgroundImage(a);
	
	
}

function showSmallImage(index) {
		index = index*1;
		$("ul.smallGallery").find("a").css("opacity", 0.4);
		$("#smallGalleryPic").attr("src", $(pictures[index]).attr("href"))
		$(pictures[index]).css("opacity", 1);
		
		b = index;
}

function showGalleryBackgroundImage(index) {
		index = index*1;
		clearTimeout(timer);
		$("ul.gallery").find("a").css("opacity", 0.4);
		var speed = 1500;
		if (initLoad) {
			initLoad = false;
			speed = 0;
		}
		
		
		$('#imgContainer').npFullBgImgInline($($(backgrounds[index]).attr("href")).get(0), {fadeInSpeed: speed, center: true, callback:function(){startGalleryTimer()}});
		$(backgrounds[index]).css("opacity", 1);
		
		a = index;
}	
function next() {
		a++;
		if(a == backgrounds.length) a=0;
		showGalleryBackgroundImage(a);	
}
function startGalleryTimer() {
		//set up timer for gallery, set here to 10 seconds.
		clearTimeout(timer);
		timer = setTimeout("next()", 5000);	
}

function isMobile() {

	var deviceAgent = navigator.userAgent.toLowerCase();

	var agentID = deviceAgent.match(/(iphone|ipod|ipad|android|blackberry|blazer|fennec)/);

	if (agentID) {
		mobile = true;
	}

}


/****** GENERAL PAGE ***/
	function setupPage() {
		$(document).pngFix();
		isMobile();
		backgrounds = [];
		$('#imgContainer').find("img").each(function() {
			backgrounds.unshift(this);
			$(this).attr("fixedwidth", $(this).width());
			$(this).attr("fixedheight", $(this).height());
			
		});
		showBackground();
	}
	
	function showBackground() {
		var speed = 1500;
		if (initLoad) {
			initLoad = false;
			speed = 0;
		}
		var params = {fadeInSpeed: speed, center: true};
		if (backgrounds.length>1) params.callback = function(){startPageTimer()};
		$('#imgContainer').npFullBgImgInline(backgrounds[a], params);
		a++;
	
		if(a == backgrounds.length) a=0;
	}
	
	function startPageTimer() {
	
		
		setTimeout("showBackground()", 8000);
	}
