// JavaScript Document

var currentWelkom = 1;
var welkomSwitch = 10; //seconds

$(document).ready(function(){
	
	var subColumnWidth = 170;
	
	$("#topnav div.subHolder").each(function(){
		$(this).css("width", $(this).find("ul").length * subColumnWidth);
	});
	
	$("#topnav li").hover(
		function(){
			$(this).find('div.subHolder:first').stop(true, true);
			$(this).find('div.subHolder:first').show(300);
		},
		function(){
			$(this).find('div.subHolder:first').hide(200);
		}
	);
	
	$("#whiteDiv").css("height", $(document).height() - 95).show().fadeTo(0, 0).hide();
	if ($.browser.msie) {
		$("#registerOfferForm").hide();
		$("#imagePreviewPopup").hide();
	} else {
		$("#registerOfferForm").show().fadeTo(0, 0).hide();
		$("#imagePreviewPopup").show().fadeTo(0, 0).hide();
	}
	
	$("div.accordionsHolder a.item").prepend("<span class=\"right\"></span><span class=\"left\"></span>")
	$("div.accordionsHolder a.item").hover(
		function() {
			$(this).find("span.left").css("background-position", "0px -22px");
			$(this).find("span.right").css("background-position", "0px -22px");
		}, 
		function() {
			$(this).find("span.left").css("background-position", "0px 0px");
			$(this).find("span.right").css("background-position", "0px 0px");
		}
	);
	
	prepareWelkom();
			
});

function openRegisterForm() {
	if ($.browser.msie) {
		$("#registerOfferForm").show();
	} else {
		$("#registerOfferForm").show().fadeTo(300, 1);
	}
	$("#whiteDiv").show().fadeTo(300, 0.85);
}

function closeRegisterForm() {
	if ($.browser.msie) {
		$("#registerOfferForm").hide();
	} else {
		$("#registerOfferForm").fadeTo(0, 0).hide();
	}
	$("#whiteDiv").fadeTo(0, 0).hide();
}

function openAccordion(tableId) {
	$("#" + tableId).slideToggle(200);
}

function openImagePreview(imgPath) {
	$("#imagePreviewPopup img.bigImage").attr("src", imgPath);
	if ($.browser.msie) {
		$("#imagePreviewPopup").show();
	} else {
		$("#imagePreviewPopup").show().fadeTo(300, 1);
	}
	$("#whiteDiv").show().fadeTo(300, 0.85);
}

function closeImagePreview() {
	if ($.browser.msie) {
		$("#imagePreviewPopup").hide();
	} else {
		$("#imagePreviewPopup").fadeTo(0, 0).hide();
	}
	$("#whiteDiv").fadeTo(0, 0).hide();
}

function prepareWelkom() {
	$("div.welkomDivsHolder > div").hide();
	
	$("#welkombuttons a:nth-child(" + currentWelkom + ")").find("img").css("top", -10);
	
	$("div.welkomDivsHolder > div:nth-child(" + currentWelkom + ")").fadeIn(300);
	
	if ($("div.welkomDivsHolder > div").length > 0) welkomTimer = setTimeout(changeWelkom, welkomSwitch * 1000);
	
	/*
	$("#welkombuttons a").hover(
		function() {
			$(this).find("img").css("top", -10);
		},
		function() {
			$(this).find("img").css("top", 0);			
		}
	);
	*/
	
	$("#welkombuttons a").each(function(i){
		$(this).bind("click", function() {
			clearTimeout(welkomTimer);
			$("div.welkomDivsHolder > div:nth-child(" + currentWelkom + ")").fadeOut(300, function() {
				$("#welkombuttons a:nth-child(" + currentWelkom + ")").find("img").css("top", 0);
				currentWelkom = i + 1;
				$("#welkombuttons a:nth-child(" + currentWelkom + ")").find("img").css("top", -10);
				$("div.welkomDivsHolder > div:nth-child(" + currentWelkom + ")").fadeIn(300);
				welkomTimer = setTimeout(changeWelkom, welkomSwitch * 1000);
			});
		});
	});
}

function changeWelkom() {
	clearTimeout(welkomTimer);
	$("div.welkomDivsHolder > div:nth-child(" + currentWelkom + ")").fadeOut(300, function() {
		$("#welkombuttons a:nth-child(" + currentWelkom + ")").find("img").animate({top:"0px"}, 300);
		currentWelkom++;
		if (currentWelkom > 3) currentWelkom = 1;
		$("#welkombuttons a:nth-child(" + currentWelkom + ")").find("img").animate({top:"-10px"}, 300);
		$("div.welkomDivsHolder > div:nth-child(" + currentWelkom + ")").fadeIn(300);
		welkomTimer = setTimeout(changeWelkom, welkomSwitch * 1000);
	});
}