$(document).ready(function() {
	defaultTexts();
	logoSlider();
	/*$('#coda-slider-1').codaSlider({dynamicTabsPosition:"bottom", dynamicTabs: false, slideEaseFunction: "easeInOutQuint", slideEaseDuration: 3000});*/
	//startPageFader();
	//brandPopup();
	$(".fancy").fancybox();
	$(".popup-chart-fancy").fancybox();
	$(".popup-table-fancy").fancybox();
	$(".popup-image-fancy").fancybox();
	$(".popup-general-fancy").fancybox();
});

var panels, numPanels, currentpanel, timeoutID, sliderContainer, sliderObj, sliderContainerWidth, keepSliding;

function startPageFader() {
	$("#coda-nav-right-1 a, #coda-nav-left-1 a").hover(function() {
		$(this).animate({ opacity: 1.0 }, 200);
	}, function() {
		$(this).animate({ opacity: 0.8 }, 200);
	});
	panels = $('#coda-slider-1 .panel');
	numPanels = panels.length;
	currentpanel = 0;

	panels.css("position", "absolute").css("top", "0px").each(function(n) {
		//$(this).css("z-index", numPanels - n );
		if (n > 0) {
			$(this).fadeOut(0);
		}
	});

	$("#coda-nav-right-1 a").click(function() {
		startPanelRightClick();
	});
	$("#coda-nav-left-1 a").click(function() {
		startPanelLeftClick();
	});

	timeoutID = setInterval("StartAutoPaging()", 100);
}

function StartAutoPaging() {
	clearInterval(timeoutID);
	timeoutID = setInterval("startPanelRight()", 3000);
}

function startPanelRightClick() {
	clearInterval(timeoutID);
	timeoutID = setInterval("StartAutoPaging()", 7000);
	startPanelRight();
}

function startPanelLeftClick() {
	clearInterval(timeoutID);
	timeoutID = setInterval("StartAutoPaging()", 7000);
	startPanelLeft();
}

function startPanelRight() {
	$('#coda-slider-1 .panel:eq(' + currentpanel + ')').fadeOut(2000);
	currentpanel++;
	currentpanel = currentpanel % numPanels;
	$('#coda-slider-1 .panel:eq(' + currentpanel + ')').fadeIn(2000);
}

function startPanelLeft() {
	$('#coda-slider-1 .panel:eq(' + currentpanel + ')').fadeOut(2000);
	currentpanel--;
	if (currentpanel < 0) currentpanel = numPanels - 1;
	$('#coda-slider-1 .panel:eq(' + currentpanel + ')').fadeIn(2000);
}

function defaultTexts() {
	$(".defaultText").focus(function(srcc) {
		if ($(this).val() == $(this)[0].value) {
			$(this).removeClass("defaultTextActive");
			$(this).val("");
		}
	});

	$(".defaultText").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("defaultTextActive");
			$(this).val($(this)[0].value);
		}
	});

	$(".defaultText").blur();
	$('input').customInput();
}

var logoCounter = 0;

function logoSlider() {
	var isSet = false;

	var isHovering = false;
	keepSliding = true;
	sliderContainer = $("#logo_sider_container");
	sliderTable = $("#logo_sider_container table");
	if (sliderTable.width() != null) {
		if (!isSet) {
			isSet = true;
			var initialLeft = Math.random();
			initialLeft = initialLeft * 300;
			sliderTable.animate({ left: -initialLeft }, 5, "linear", function() { });
		}
		sliderTable.animate({ left: "-=1" }, 40, "linear", function() { });
		sliderContainerWidth = sliderContainer.width();
		setTimeout("moveLogoSlider()", 3000);
	}

	$("#banner .title").show();
	$("#banner #BrandImagePanel-default").show();

	sliderContainer.hover(function() {
		keepSliding = false;
		$("#banner .title").hide();
	}, function() {
		$("#banner .title").show();
		$("#banner #BrandImagePanel-default").show();
		keepSliding = true;
		moveLogoSlider();
	});

	sliderTable.find("td").hover(function() {
		$(this).siblings().stop().animate({ opacity: 0.25 }, 200);
		var id = $(this).attr("class").split(" ");
		for (i = 0; i < id.length; i++) {
			if (id[i].indexOf("BrandLogo-") > -1) {
				var localID = id[i].split("-");
				if (localID.length == 2) {
					$("#BrandImagePanel-" + localID[1]).show();
				}
			}
		}
	}, function() {
		$("#banner .panel").hide();
		$(this).siblings().stop().animate({ opacity: 1.0 }, 100);
	});
}

function moveLogoSlider() {
	sliderTable.animate({ left: "-=1" }, 40, "linear", function() {
		if ($(this).css("left").replace(/[^-\d\.]/g, '') > -(sliderTable.width() - sliderContainerWidth)) {
			if (keepSliding) {
				moveLogoSlider();
			}
		}
	});
}


function brandPopup() {
	$("ul.brandlist a").click(function(event) {
		var newPos = $(event.currentTarget).offset();
		var $anchor = $(this);
		$("#brand_popup .wrapper").hide(200, function() {
			$("#brand_popup").offset(newPos);
			$("#brand_content_placeholder").load($anchor.attr("href") + ' #brand_info', function() {
				$("#brand_popup .wrapper").show(200, function() { $.scrollTo($("#brand_popup .wrapper"), 400); });
			});
		});
		return false;
	});
	$("#brand_popup a.close").click(function() {
		$("#brand_popup .wrapper").hide(100);
		return false;
	});
}
