var resizeTimer;
var fadeTime = 200;
var fadeTimeBg = 400;
var currentPicture = 1;
var autoplayTimer;
var autoplayTime = 3500;
var autoplayChangePictureTime = 2500;

// tab id:		1	2	3	4
var displayTimes = [0,	5000,	5000,	5000,	5000]; // time tab is displayed
var chImgTimes   = [0,	2500,	2500,	2500,	2500]; // time image inside tab is displayed (each tab has 2 images)

$(document).ready(function() {
	resizeWindow();
	$("a.popup").fancybox();

	$("#promo_wrap_content").addClass("active_promo_link_1_1");

	$("#promo_navigation li").click(function() {
		if ($(this).hasClass("active"))
			return;
		$("#promo_navigation li").removeClass("active");
		$(this).addClass("active");
		var id = $(this).attr("id").split("_")[1];

		autoplayTime = ((displayTimes[id] != null) ? displayTimes[id] : 5000);
		autoplayChangePictureTime = ((chImgTimes[id] != null) ? chImgTimes[id] : 2500);

		$(".promo_section:visible").fadeOut(fadeTime, function() {
			$("#promo_" + id).fadeIn(fadeTime);
		});
		$(".promo_wide_bg:visible").fadeOut(fadeTimeBg, function() {
			$("#promo_wrap_content").removeClass();
			$("#promo_wrap_content").addClass("active_promo_link_" + id + "_1");
			$("#promo_bg_" + id + "_1").fadeIn(fadeTimeBg);
		});
		$(".promo_section_link:visible").fadeOut(fadeTimeBg, function() {
			$("#promo_link_" + id).fadeIn(fadeTimeBg);
		});
	});

	autoplayTime = ((displayTimes[1] != null) ? displayTimes[1] : 5000);
	autoplayChangePictureTime = ((chImgTimes[1] != null) ? chImgTimes[1] : 2500);

	$("#promo").mouseover(function() { autoPlayStop(); });
	$("#promo").mouseout(function() { autoPlayStart(autoplayChangePictureTime); });

	autoPlayStart(autoplayChangePictureTime);

});

function autoPlayStart(time) {
	autoplayTimer = setTimeout("autoPlayNext();", time);
}


function autoPlayNext() {
	var active = $("#promo_navigation").find(".active");
	var activeId = $(active).attr("id").split("_")[1];

	if (currentPicture == 1) {
		currentPicture++;
		replaceImage(activeId);
		return;
	}

	else currentPicture = 1;
	var nextId = (activeId >= 4) ? 1 : ++activeId;

	$("#tab_" + nextId).click();

	autoplayTimer = setTimeout("autoPlayNext();", autoplayChangePictureTime);
}

function autoPlayStop() {
	clearTimeout(autoplayTimer);
}

function replaceImage(currentId) {
	$(".promo_wide_bg:visible").fadeOut(fadeTimeBg, function() {
		$("#promo_wrap_content").removeClass();
		$("#promo_wrap_content").addClass("active_promo_link_" + currentId + "_2");
		$("#promo_bg_" + currentId + "_2").fadeIn(fadeTimeBg);
	});
	autoplayTimer = setTimeout("autoPlayNext();", autoplayTime);
}


$(window).resize(function() {
	clearTimeout(resizeTimer);
	resizeTimer = setTimeout("resizeWindow();", 100);

});

function resizeWindow() {
	var window_height = $(window).height();
	var window_width = $(window).width();

	var content_height = $("#page_wrap").height();
	var footer_height = $("#footer_wrap").height();
	var promo_height = $("#promo").height();
	var admin_height = $("#editor_box").height();

	var height_center = ((window_height - content_height - footer_height - promo_height - admin_height) / 2) - 50;
	var width_center = window_width;
	var width_space = 0 - (window_width / 2);
	var top_padding = height_center;
	if (height_center > 70) {
		//alert("asd")
		var top_padding = 70;
	}
	$(".productpage #header").css("padding-top", top_padding);
	$(".frontpage #header").css("padding-top", top_padding);
	$("#promo_wide").css("width", width_center);
	//$(".promo_wide_bg").css("margin-left", width_space);
}
