var imageRotateTimeout = 5 * 1000;
var autoRotateTimeout = 30 * 1000;
var autoRotatePause = 3 * autoRotateTimeout;
var autoRotate = null;
var imageRotate = null;
var autoRotateIndex = 0;
var htmlContent = new Array();
var fadeSpeed = 300;
var contentFadeSpeed = 750;
var showSiteTimer = null;
var showSiteTimeout = 500;
$(document).ready(function() {
	$("#grouppicker a").click(showSiteDetailsClick);
	$("#grouppicker a img").mouseenter(showHover);
	$("#grouppicker a img.hoverImg").live("mouseleave",hideHover);
	$("#grouppicker a img.activeImg").live("mouseenter",resetHover);
	$(".career a img").mouseenter(showLinkImgHover);
	$(".career a img.hoverImg").live("mouseleave",hideLinkImgHover);
	initAutoRotate();
});

showSiteDetailsClick = function(e) {
	if(e.target == this) {
		showSiteDetails($(this),e);
	} else {
		parent.location = $(this).attr("rel");
		e.preventDefault();
	}
	
}
showSiteDetails = function(sender,e) {
	if(typeof(htmlContent[$(sender).attr("href")]) != "undefined") {
		showContent(htmlContent[$(sender).attr("href")]);
	} else {
		var reqUrl = $(sender).attr("href");
		$.get("/ajax.php?page="+reqUrl,null,function(response) {
			htmlContent[$(this).attr("url").substr($(this).attr("url").lastIndexOf("=")+1)] = response;
			showContent(response);
		});
	}
	$(".activeLi").removeClass("activeLi");
	sender.parent("li").addClass("activeLi");
	markInactive($("#grouppicker .active"));
	markActive($("img",sender));
	if(typeof(e) != "undefined") e.preventDefault();
}

showContent = function(html) {
	var oldNode = $("#content .sitediv");
	var newNode = $("#blanksitediv").clone();
	$(newNode).removeAttr("id");
	$(newNode).html(html);
	$(newNode).css("z-index",2);
	$("#content").append(newNode);
	$(".imagerotator ul li",newNode).first().show();
	$(".imagerotator ul li",newNode).first().addClass("visibleImage");
	setSiteLinkHoverEvent(newNode);
	resetImageRotate();
	$(newNode).fadeIn(contentFadeSpeed,function() {
		$(newNode).css("z-index",1);
		$(oldNode).remove();
	});
}

setSiteLinkHoverEvent = function(newNode) {
	$("a.sitelink img",newNode).mouseenter(showLinkImgHover);
	$("a.sitelink img.hoverImg",newNode).live("mouseleave",hideLinkImgHover);
}

showHover = function(e) {
	resetHover();
	if($(this).hasClass("active")) return;
	//$(this).attr("src",$(this).attr("src").replace(".off.png",".hover.png"));
	var newNode = $(this).clone();
	$(newNode).attr("src",$(this).attr("src").replace(".off.png",".hover.png"));
	$(newNode).addClass("hoverImg");
	$(this).parent().append(newNode);
	$(newNode).stop(true);
	$(newNode).fadeIn(fadeSpeed);
	
	var sender = $(this);
	showSiteTimer = setTimeout(function() {
		showSiteDetails(sender.closest("a"));
	},showSiteTimeout);
}

hideHover = function() {
	if($(this).hasClass("active")) return;
	clearTimeout(showSiteTimer);
	//$(this).attr("src",$(this).attr("src").replace(".hover.png",".off.png"));
	$(".hoverImg",$(this).parent()).fadeOut(fadeSpeed,function() {
		$(this).remove();
	})
}

resetHover = function() {
	$("#grouppicker a img.hoverImg").each(hideHover);
}

markActive = function(node) {
	pauseAutoRotate();
	$(node).each(function() {
		if($(this).hasClass("hoverImg")) return;
		$(this).addClass("active");
		//$(node).attr("src",$(node).attr("src").replace(".off.png",".active.png"));
		var newNode = $(this).clone();
		$(newNode).attr("src",$(this).attr("src").replace(".off.png",".active.png"));
		$(newNode).addClass("activeImg");
		$(this).parent().append(newNode);
		$(newNode).fadeIn(fadeSpeed,function() {
			$(".hoverImg",$(this).parent()).remove();
		});
	});
}

markInactive = function(node) {
	if(!node.length) return;
	$(node).removeClass("active");
	$(node).each(function() {
		if(!$(this).hasClass("activeImg")) return;
		//$(this).attr("src",$(node).attr("src").replace(".active.png",".off.png"));
		$(this).fadeOut(fadeSpeed,function() {
			$(this).remove();
		});
	});
}

initAutoRotate = function() {
	$("#grouppicker a").first().click();
	//autoRotateIndex++;
	resetAutoRotate();
}

/*showNextCompany = function() {
	var node = $("#grouppicker a")[autoRotateIndex];
	$(node).click();
	autoRotateIndex++;
	if(autoRotateIndex >= $("#grouppicker a").length) autoRotateIndex = 0;
	resetAutoRotate();
}*/

showNextCompany = function() {
	var nextCompany = $("#grouppicker li.activeLi").next("li");
	$("#grouppicker li.activeLi").removeClass("activeLi");
	if(!nextCompany.length) {
		$("a",$("#grouppicker li").first()).click();
	} else {
		$("a",nextCompany).click();
	}
	resetAutoRotate();
}

resetAutoRotate = function() {
	clearTimeout(autoRotate);
	autoRotate = setTimeout(showNextCompany,autoRotateTimeout);
}

pauseAutoRotate = function() {
	clearTimeout(autoRotate);
	autoRotate = setTimeout(showNextCompany,autoRotatePause);
}

showNextImage = function() {
	var nextImage = $(".imagerotator ul li.visibleImage").next("li");
	if(!nextImage.length) {
		nextImage = $(".imagerotator ul li").first();
		nextImage.show();
		$(".imagerotator ul li.visibleImage").fadeOut();
		$(".imagerotator ul li.visibleImage").removeClass("visibleImage");
		nextImage.addClass("visibleImage");
	} else {
		nextImage.fadeIn(function() {
			$(".imagerotator ul li.visibleImage").hide();
			$(".imagerotator ul li.visibleImage").removeClass("visibleImage");
			$(this).addClass("visibleImage");
		});
	}
	resetImageRotate();
}

resetImageRotate = function() {
	clearTimeout(imageRotate);
	imageRotate = setTimeout(showNextImage,imageRotateTimeout);
}

showLinkImgHover = function() {
	resetHover();
	var newNode = $(this).clone();
	$(newNode).attr("src",$(this).attr("src").replace(".off.gif",".hover.gif"));
	$(newNode).addClass("hoverImg");
	$(this).parent().append(newNode);
	$(newNode).stop(true);
	$(newNode).fadeIn(fadeSpeed);
}

hideLinkImgHover = function() {
	$(".hoverImg",$(this).parent()).fadeOut(fadeSpeed,function() {
		$(this).remove();
	})
}
