var meneNez25 = null;
var viceNez25 = null;
var meneNez25Left = 0;
var viceNez25Left = 0;
var arrowSign = $("#arrowSign");
var leftSign = $(".left .sign", arrowSign);
var rightSign = $(".right .sign", arrowSign);
var leftBack = $(".left .back", arrowSign);
var rightBack = $(".right .back", arrowSign);
var womanYoung = null;
var womanMature = null;
var womanYoungLeft = 0;
var womanMatureLeft = 0;
var handShadow = null;
var handShadowLeft = 0;
var viewportWidth = $(window).width();
var boxType = "";
var selectedOption1 = 0;
var selectedOption2 = 0;

$( document ).ready(function()
{
	meneNez25 = $("#meneNez25");
	viceNez25 = $("#viceNez25");

	womanYoung = $("#womanYoung");
	womanMature = $("#womanMature");

	handShadow = $("#handShadow");

	placeVisuals();
	setArrowSigns();
	Boxes.setUp();
});

window.onload = function()
{
	new JsSmoothResizer( document.getElementById( 'handShadow' ), {
		randomize: true,
		centering: false,
		sizeCoeficient: 0.1,
		time: 4000
	} );

	new JsParallax( document.getElementById( 'dermAdvice' ), document.getElementById( 'paralDermAdviceBoxCont' ), {
		//movementEnabled: 'horizontal',
		moveCoeficient: 0.15
	} );
	new JsParallax( document.getElementById( 'winTour' ), document.getElementById( 'paralWinTourBoxCont' ), {
		moveCoeficient: 0.15
	});
};



$(window).resize(function()
{
	delay(function()
	{
		placeVisuals();
	}, 500);
});


function placeVisuals()
{
	var viewportWidth = $(window).width();

	if ($.browser.msie /*&& $.browser.version == '6.0'*/)
	{
		meneNez25Left = ((viewportWidth / 2) + 1);
		viceNez25Left = ((viewportWidth / 2) + 2) - viceNez25.width();

		womanYoungLeft = ((viewportWidth / 2) - 490);
		womanMatureLeft = ((viewportWidth / 2) + 71);

		handShadowLeft = ((viewportWidth / 2) - 900);
	}
	else
	{
		meneNez25Left = ((viewportWidth / 2) + 0);
		viceNez25Left = ((viewportWidth / 2) - 0) - viceNez25.width();

		womanYoungLeft = ((viewportWidth / 2) - 490);
		womanMatureLeft = ((viewportWidth / 2) + 71);

		handShadowLeft = ((viewportWidth / 2) - 900);
	}

	meneNez25.css(
	{
		left: meneNez25Left
	});

	viceNez25.css(
	{
		left: viceNez25Left
	});

	womanYoung.css(
	{
		display: "block",
		left: womanYoungLeft
	});

	womanMature.css(
	{
		display: "block",
		left: womanMatureLeft
	});

	handShadow.css(
	{
		right: handShadowLeft
	});


	if (viceNez25.width() > 0)
	{
		var viceNez25Width = viceNez25Left - viceNez25.width();

		viceNez25.width( viceNez25Width  > 700 ? 700 : viceNez25Width );
	}

}

function toggleVisual(type, animate, maxWidth, state)
{
	if (typeof animate == "undefined")
		animate = true;

	if (typeof maxWidth == "undefined" || maxWidth == "max")
		maxWidth = 800;

	var speed = animate ? 600 : 0;
	var targetWidth = 0;

	if (type == "meneNez25")
	{
		targetWidth = viewportWidth - parseInt(meneNez25.css("left"));
		targetWidth = targetWidth > maxWidth ? maxWidth : targetWidth;

		if (state == "hide")
		{
			targetWidth = 0;
		}
		else if (state == "show")
		{
			targetWidth = targetWidth;
		}
		else if (meneNez25.width() > 0)
		{
			targetWidth = 0;
		}

		meneNez25.stop(true, false).animate(
		{
			width: targetWidth
		}, speed);
	}
	else if (type == "viceNez25")
	{
		targetWidth = parseInt(viceNez25.css("left")) + 50;
		targetWidth = targetWidth > maxWidth ? maxWidth : targetWidth;

		var left = viceNez25Left - targetWidth;

		if (state == "hide")
		{
			targetWidth = 0;
			left = viceNez25Left;
		}
		else if (state == "show")
		{
			targetWidth = targetWidth;
			left = left;
		}
		else if (viceNez25.width() > 0 || state == "hide")
		{
			targetWidth = 0;
			left = viceNez25Left;
		}

		viceNez25.stop(true, false).animate(
		{
			width: targetWidth,
			left: left
		}, speed);
	}
}


var Boxes =
{
	fadeSpeed : 300,

	setUp : function()
	{
		var boxes = $("div.box");
		var optionHolders = $(".options .pracuji", boxes);
		var options = $("a", optionHolders);

		options.click(function(e)
		{
			e.preventDefault();

			optionHolders.removeClass("active");

			$(this).parent().addClass("active");

			var box = $(this).parent().parent().parent().parent();

			if (box.attr("id") == "boxStep1")
			{
				selectedOption1 = $($(this).parent(), $(this).parent().parent()).index() +1
			}
			else if (box.attr("id") == "boxStep2")
			{
				selectedOption2 = $($(this).parent(), $(this).parent().parent()).index() +1
			}

			$("a.buttonNext", box).css({"opacity":1});
		});

		$("#boxStep1 a.buttonNext").click(function(e)
		{
			e.preventDefault();

			if (selectedOption1 > 0)
			{
				Boxes.showBox2();

				var configuratorAge = (viceNez25.width() > 200) ? 'vice_nez_25' : 'mene_nez_25';

				if (selectedOption1 == 1)
					_gaq.push(['_trackEvent', configuratorAge, 'spise_rukama']);
				else if (selectedOption1 == 2)
					_gaq.push(['_trackEvent', configuratorAge, 'spise_hlavou']);
				else if (selectedOption1 == 3)
					_gaq.push(['_trackEvent', configuratorAge, 'spise_nepracuji']);

			}
		});

		$("#boxStep2 a.buttonNext").click(function(e)
		{
			e.preventDefault();

			if (selectedOption2 > 0)
			{
				Boxes.showBox3();

				var configuratorAge = (viceNez25.width() > 200) ? 'vice_nez_25' : 'mene_nez_25';

				if (selectedOption2 == 1)
					_gaq.push(['_trackEvent', configuratorAge, 'hodne']);
				else if (selectedOption2 == 2)
					_gaq.push(['_trackEvent', configuratorAge, 'spise_mene']);
				else if (selectedOption2 == 3)
					_gaq.push(['_trackEvent', configuratorAge, 'nenamaham_je']);

			}
		});
	},

	hideAllBoxes : function(callback)
	{
		var boxes = $("div.box:visible");

//		console.log("selectedOption1="+ selectedOption1 +", selectedOption2="+ selectedOption2);

		boxes.fadeOut(this.fadeSpeed, function()
		{
			boxes.removeClass("brown");
			boxes.removeClass("blue");

			if (typeof callback != "undefined")
				callback();
		});
	},

	showBox1 : function()
	{
		var boxStep1 = $("#boxStep1");

		boxStep1.addClass(boxType);
		boxStep1.fadeIn(this.fadeSpeed);
	},

	showBox2 : function()
	{
		var boxStep2 = $("#boxStep2");

		this.hideAllBoxes(function()
		{
			boxStep2.addClass(boxType);
			boxStep2.fadeIn(this.fadeSpeed);
		});
	},

	showBox3 : function()
	{
		var boxStep3 = $("#boxStep3");

		this.hideAllBoxes(function()
		{
			boxStep3.addClass(boxType);

			var ajaxContent = $("#boxStep3AjaxContent");

			ajaxContent.load(ajaxContent.attr("class"), {index0: (viceNez25.width() > 200 ? 1 : 0), index1:selectedOption1, index2:selectedOption2}, function()
			{
				boxStep3.fadeIn(this.fadeSpeed);
			});

		});
	},

	clearSelects : function()
	{
		selectedOption1 = 0;
		selectedOption2 = 0;
		$("div.box .options .pracuji").removeClass("active");
	}
}



function setLightShowVisual()
{
	leftSign.mouseover(function()
	{
		toggleVisual("meneNez25", true, 50, "show");
		toggleVisual("viceNez25", false, 50, "hide");
	});

	leftSign.mouseout(function()
	{
		toggleVisual("meneNez25", true, 50, "hide");
	});

	rightSign.mouseover(function()
	{
		toggleVisual("viceNez25", true, 50, "show");
		toggleVisual("meneNez25", false, 50, "hide");
	});

	rightSign.mouseout(function()
	{
		toggleVisual("viceNez25", true, 50, "hide");
	});
}

function disableLightShowVisual()
{
	leftSign.unbind("mouseover");
	leftSign.unbind("mouseout");
	rightSign.unbind("mouseover");
	rightSign.unbind("mouseout");
}



function setArrowSigns()
{
	var speedSign = 700;
	var speedSlide = 200;
	var speedOpacity = 100;
	var actualPosition = "center";

	setLightShowVisual();

	$("a", leftSign).click(function(e)
	{
		e.preventDefault();

		if (actualPosition != "center")
		{
			return;
		}

		disableLightShowVisual();

		hideRightSign(function()
		{
			$(".right", arrowSign).width(104);

			arrowSign.animate({left:676}, speedSign, function()
			{
				showRightBack();
				Boxes.showBox1();
			});

		});

		womanMature.fadeOut(200);
		toggleVisual("meneNez25", true, "max", "show");
		$(this).blur();

		actualPosition = "right";
		boxType = "brown";

		_gaq.push(['_trackEvent', 'homepage', 'mene_nez_25']);
	});


	$("a", rightSign).click(function(e)
	{
		e.preventDefault();

		if (actualPosition != "center")
		{
			return;
		}

		disableLightShowVisual();

		hideLeftSign(function()
		{
//			$(".left", arrowSign).width(104);

			arrowSign.animate({left: -75}, speedSign, function()
			{
				showLeftBack();
				Boxes.showBox1();
			});

		});

		womanYoung.fadeOut(200);
		toggleVisual("viceNez25", true, "max", "show");
		$(this).blur();

		actualPosition = "left";
		boxType = "blue";

		_gaq.push(['_trackEvent', 'homepage', 'vice_nez_25']);
	});

	$("a", rightBack).click(function(e)
	{
		e.preventDefault();

		Boxes.hideAllBoxes();
		Boxes.clearSelects();

		hideRightBack(function()
		{
			arrowSign.animate({left:301}, speedSign, function()
			{
				showRightSign();
				womanMature.fadeIn(200);
				setLightShowVisual();
			});
		});

		toggleVisual("meneNez25");
		$(this).blur();

		actualPosition = "center";
		boxType = "";
	});

	$("a", leftBack).click(function(e)
	{
		e.preventDefault();

		Boxes.hideAllBoxes();
		Boxes.clearSelects();

		hideLeftBack(function()
		{
//			$(".left", arrowSign).width(179);

			arrowSign.animate({left:301}, speedSign, function()
			{
				showLeftSign();
				womanYoung.fadeIn(200);
				setLightShowVisual();
			});
		});

		toggleVisual("viceNez25");
		$(this).blur();

		actualPosition = "center";
	});


	var showRightSign = function(callback)
	{
		rightSign.animate({width: 179}, speedSlide, function()
		{
			$("a", rightSign).animate({opacity: 1}, speedOpacity, function()
			{
				if (typeof callback != "undefined")
					callback();
			});
		});
	}

	var hideRightSign = function(callback)
	{
		$("a", rightSign).animate({opacity: 0}, speedOpacity, function()
		{
			rightSign.animate({width: 0}, speedSlide, function()
			{
				if (typeof callback != "undefined")
					callback();
			});
		});
	}

	var showLeftSign = function(callback)
	{
		leftSign.animate({width: 179, left: 0}, speedSlide, function()
		{
			$("a", leftSign).animate({opacity: 1}, speedOpacity, function()
			{
				if (typeof callback != "undefined")
					callback();
			});
		});
	}

	var hideLeftSign = function(callback)
	{
		$("a", leftSign).animate({opacity: 0}, speedOpacity, function()
		{
			leftSign.animate({width: 0, left: 179}, speedSlide, function()
			{
				if (typeof callback != "undefined")
					callback();
			});
		});
	}

	var showRightBack = function()
	{
		rightBack.show();
		rightBack.animate({width: 104}, speedSlide, function()
		{
			$("a", rightBack).animate({opacity: 1}, speedOpacity);
		});
	}

	var hideRightBack = function(callback)
	{
		$("a", rightBack).animate({opacity: 0}, speedOpacity, function()
		{
			rightBack.animate({width: 0}, speedSlide, function()
			{
				rightBack.hide();

				if (typeof callback != "undefined")
					callback();
			});
		});
	}

	var showLeftBack = function()
	{
		leftBack.show();
		leftBack.animate({width: 104}, speedSlide, function()
		{
			$("a", leftBack).animate({opacity: 1}, speedOpacity);
		});
	}

	var hideLeftBack = function(callback)
	{
		$("a", leftBack).animate({opacity: 0}, speedOpacity, function()
		{
			leftBack.animate({width: 0}, speedSlide, function()
			{
				leftBack.hide();

				if (typeof callback != "undefined")
					callback();
			});
		});
	}
}




