var dt = 5000;
var ft = 1000;
var pos = -1;
var anim = true;
var photoPos = -1;
var photoFT = 500;

if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
{
     var chromeCss = document.createElement("link");

     chromeCss.rel = "stylesheet";
     chromeCss.href = "css/chromeStyles.css";

     document.getElementsByTagName("head")[0].appendChild(chromeCss);
}

$.fn.centernav = function() {
	
	// defaults
	fs = parseInt($(this).find("a").css("font-size")) + 1;
	ml = -1;
	
	// check for oversize
	while (ml < 0) {
		
		// decrement font-size
		fs--;
		
		// decrease zoom parameter until nav fits
		$(this).find("a").css("font-size",fs);
		
		// cumulative element width plus horizontal paddings
		w = 0;
		$(this).find("a").each(function(i,e){
			w += $(e).width();
			w += parseInt($(e).css("padding-left"));
			w += parseInt($(e).css("padding-right"));
		});

		// set up a left margin
		ml = ($(this).width() - w) / 2;
		
	}
	
	// center
	$(this).find("li:eq(0)").css("margin-left",ml);

	// show
	$(this).css({opacity:1});
	
}

$.fn.gallerize = function() {
		
	$(this).bind("click",function(e){

		// get large photo
		var el = $("#large-photos div.photo:eq(" + $(this).index() + ")");

		myFT = 0;
		if (photoPos != -1) myFT = photoFT;
		else photoPos = 0;
				
		// is this the current pos?
		if (photoPos != $(this).index()) {
			
			// fade out old pos
			$("#large-photos div.photo:eq(" + photoPos + ")").fadeOut(myFT);
			$("#small-photos img:eq(" + photoPos + ")").removeClass("selected");
			
		}
		
		
		// center
		$(el).show();
		marginTop = -1 * $(el).find("img").height()/2 - 36; // compensate for caption
		marginLeft = -1 * $(el).find("img").width()/2;
		$(el).find("img").css("margin-top",marginTop);
		$(el).find("img").css("margin-left",marginLeft);
		$(el).hide();
		
		
		// fade in new
		$(el).fadeIn(myFT);
		$("#small-photos img:eq(" + $(this).index() + ")").addClass("selected");
		
		// set flag
		photoPos = $(this).index();
		
	});
	
	$("#small-photos img:eq(0)").click();
	
}

$.fn.cycle = function(newpos) {

	if (anim) {
		
		var tmpFT = 0;

		// fade out old
		if (pos != -1 && pos != newpos) {
		
			$(this).find("a:eq(" + pos + ")").css({"z-index":900}).fadeOut(ft);
			$("#hero-caption div.caption:eq(" + pos + ")").fadeOut(ft,function(){ $(this).css({opacity:0}); });
			$("#hero-navigation img:eq(" + pos + ")").attr("src","i/hero-dot-off.png");
			tmpFT = ft;
		
		} 

		// increment
		pos++;
		if (pos == $(this).find("a").length) pos = 0;
	
		// fade in new
		/*
		$(this).find("a:eq(" + pos + ")").css({"z-index":1000}).fadeIn(tmpFT);
		$("#hero-caption div.caption:eq(" + pos + ")").fadeIn(tmpFT);
		$("#hero-overlay").fadeIn(tmpFT);
		*/

		$(this).find("a:eq(" + pos + ")").css({"z-index":1000}).fadeIn(tmpFT);
		$("#hero-caption div.caption:eq(" + pos + ")").css({"display":"block"}).animate({opacity:0.99},tmpFT);
		$("#hero-overlay").fadeIn(tmpFT);
		
		$("#hero-navigation img:eq(" + pos + ")").attr("src","i/hero-dot-on.png");

		// delay
		setTimeout(function() { $("#hero").cycle(); },dt);
		
	} else if (newpos != null) {
		
		// fade out old
		if (pos != -1 && pos != newpos) {
		
			$(this).find("a:eq(" + pos + ")").css({"z-index":900}).fadeOut(ft/2);
			$("#hero-caption div.caption:eq(" + pos + ")").fadeOut(ft/2,function(){ $(this).css({opacity:0}); });
			$("#hero-navigation img:eq(" + pos + ")").attr("src","i/hero-dot-off.png");
		
		}

		// set to new position
		pos = newpos;
	
		// fade in new
		$(this).find("a:eq(" + pos + ")").css({"z-index":1000}).fadeIn(ft/2);
		$("#hero-caption div.caption:eq(" + pos + ")").css({"display":"block"}).animate({opacity:0.99},ft/2);
		$("#hero-overlay").fadeIn(ft/2);
		$("#hero-navigation img:eq(" + pos + ")").attr("src","i/hero-dot-on.png");
		
	}
	
}

$.fn.colorBullets = function(bulletClass,textClass) {
	
	$(this).addClass(bulletClass);
	$(this).find("li").wrapInner("<span class=" + textClass + " />");
	
}

$(window).load(function(){
	
	if (pageLabel == "home") {
		
		$("#hero").cycle();
		$("#hero-navigation img").click(function(e) {
			anim = false;
			$("#hero").cycle($(this).index());
		});	
		$("#home-body ul").colorBullets("red","gray");
		
	}
	
	$("#page-content ul").colorBullets("red","gray");
	
	$("#small-photos img").gallerize();

	$("#photo-next").click(function(e){ 
		e.preventDefault();
		myIndex = photoPos + 1;
		if (myIndex == $("#small-photos img").length) myIndex = 0;
		$("#small-photos img:eq(" + myIndex + ")").click();
	});
	
	$("#photo-prev").click(function(e){ 
		e.preventDefault();
		myIndex = photoPos - 1;
		if (myIndex == -1) myIndex = $("#small-photos img").length - 1;
		$("#small-photos img:eq(" + myIndex + ")").click();
	});
	
	$(".fancybox").fancybox({ overlayOpacity: 0.8 });
	
	$("ul#nav").centernav();
	
});

$(window).resize(function() {
	$("ul#nav").centernav();	
});
