var flashCallCloseVideoPopup;
(function()
{
	var b_inAnim = false;
	
	$(document).ready(init);
	
	function init()
	{
		g8_share._browser.init();
		if(g8_share._browser.browser != "Firefox" && g8_share._browser.browser != "Safari" && g8_share._browser.browser != "Chrome")
			return;
		if(g8_share._browser.OS.match("iPhone"))
			return;
		
		g8_share.appendOnLoadOverlay(c_onLoadMsg);
		$(".ctrlImageLoadContainer > img").each(
			function()
			{
				$(this).attr("src", $(this).attr("title"));
			}
		);
		$(window).bind("load",
			function()
			{
				g8_share.removeOnLoadOverlay();
			}
		);
		
		refresh();
		
		$("#fvcPopup").flashembed(
			{
				src:"/res/" + c_resActual + "themes/portal/home/flash/imagevideo_player.swf", 
				allowNetworking: "all",
				allowFullscreen: "true",
				allowScriptAccess: "always",
				wmode:"transparent",
				version:[9]
			}
		);
		$("#frmVideoCover").bind("click", ctrlWatchVideoCloseClick);
		
		$(window).resize(
			function()
			{
				if(($(window).width() / $(window).height()) >= (675 / 560))
				{
					var i_height = $(window).height() - 50 > 560 ? 560 : $(window).height() - 50;
					var i_width = (675 / 560) * ($(window).height() - 50);
					$("#fvcPopup").css("height", i_height).css("top", ($(window).height() - i_height) / 2);
					$("#fvcPopup").css("width", i_width);
					$("#fvcPopup").css("left", ($(window).width() - i_width) / 2);
				}
				else
				{
					var i_width = $(window).width() - 50 > 675 ? 675 : $(window).width() - 50;
					var i_height = (560 / 675) * ($(window).width() - 50);
					$("#fvcPopup").css("width", i_width).css("left", ($(window).width() - i_width) / 2);
					$("#fvcPopup").css("height", i_height);
					$("#fvcPopup").css("top", ($(window).height() - i_height) / 2);
				}
			}
		);
	}
	
	function refresh()
	{
		//$(".ctrlLinkContent").unbind("click", ctrlLinkContentClick);
		//$(".ctrlLinkContent").bind("click", ctrlLinkContentClick);
		//$(".ctrlLinkContentMain").unbind("click", ctrlLinkContentMainClick);
		//$(".ctrlLinkContentMain").bind("click", ctrlLinkContentMainClick);
		
		$(".ctrlWatchVideo").unbind("click", ctrlWatchVideoClick);
		$(".ctrlWatchVideo").bind("click", ctrlWatchVideoClick);
		$(".ctrlHomeChangeTheme").unbind("click", ctrlHomeChangeThemeClick);
		$(".ctrlHomeChangeTheme").bind("click", ctrlHomeChangeThemeClick);
		$(".ctrlImageBoxView").unbind("click", ctrlImageBoxViewClick);
		$(".ctrlImageBoxView").bind("click", ctrlImageBoxViewClick);
		$(".ctrlPayInfoPaypal").unbind("click", ctrlPayInfoPaypalClick);
		$(".ctrlPayInfoPaypal").bind("click", ctrlPayInfoPaypalClick);
		$(".ctrlPayInfoClibuy").unbind("click", ctrlPayInfoClibuyClick);
		$(".ctrlPayInfoClibuy").bind("click", ctrlPayInfoClibuyClick);
		
		$(".demoForm").unbind("submit", demoFormSubmit);
		$(".demoForm").bind("submit", demoFormSubmit);
		$(".whoisForm").unbind("submit", whoisFormSubmit);
		$(".whoisForm").bind("submit", whoisFormSubmit);
		$(".orderForm").unbind("submit", orderFormSubmit);
		$(".orderForm").bind("submit", orderFormSubmit);
		$(".inputFault").unbind("click", inputFaultTipClick);
		$(".inputFault").bind("click", inputFaultTipClick);
		$(".inputFault > input,select").unbind("focus", inputFaultTipFocus);
		$(".inputFault > input,select").bind("focus", inputFaultTipFocus);
		_mtt_extern_refresh();
		_mtt_extern_postRefresh();
		
		b_inAnim = false;
	}
	
	function startAnimation()
	{
		if(!$("#contentHome").length)
			return false;
		
		var i_h = $("#contentHomeMain").height();
		$("#contentHomeMain")
			.animate({height:i_h}, {duration: "250", complete:
				function()
				{
					ctrlHomeChangeThemeClick();
					$(this).animate({height:i_h}, {duration: "250", complete:
						function()
						{
							ctrlHomeChangeThemeClick();
							$(this).animate({height:i_h}, {duration: "250", complete:
								function()
								{
									ctrlHomeChangeThemeClick();
									var i_left = $(".linkHomeChangeTheme").css("left");
									$(".linkHomeChangeTheme")
										.css("left", - $(".linkHomeChangeTheme").width())
										.css("visibility", "visible")
										.animate({left: i_left}, 500,
											function()
											{
												setTimeout(animationSlogan, 250);
											}
										);
								}
							});
						}
					});
				}
			});
	}
	
	function animationSlogan()
	{
		var i_width = $(window).width();
		$("#contentHomeMainLeftTitle")
			.css("position", "relative")
			.css("left", - $("#contentHomeMainLeftTitle").width())
			.css("visibility", "visible")
			.animate({left: 0}, 500,
				function()
				{
					$("#contentHomeMainLeftSubtitle")
						.css("position", "relative")
						.css("left", - $("#contentHomeMainLeftSubtitle").width())
						.css("visibility", "visible")
						.animate({left: 0}, 500,
							function()
							{
								$("#contentHomeMainLeftText")
									.css("position", "relative")
									.css("left", - $("#contentHomeMainLeftText").width())
									.css("visibility", "visible")
									.animate({left: 0}, 500,
										function()
										{
											animationTeaserItem($("#contentHomeTeaser > .contentHomeTeaserItem").eq(0));
										}
									);
							}
						);
				}
			);
	}
	
	function animationTeaserItem(p_teaserItem)
	{
		var i_top = p_teaserItem.offset().top;
		p_teaserItem
			.css("position", "relative")
			.css("top", - i_top + 100)
			.css("opacity", 0)
			.css("visibility", "visible")
			.animate({opacity: 1}, 1000,
				function()
				{
					p_teaserItem.animate({top:0}, 350);
					if(p_teaserItem.next().next().length == 1)
						animationTeaserItem(p_teaserItem.next().next());
					else
						setTimeout(animationLinks, 250);
				}
			);
	}
	
	function animationLinks()
	{
		$("#contentHomeMainLeft > .ctrlLinkContent").eq(0)
			.css("position", "relative")
			.css("left", - $("#contentHomeMainLeft > .ctrlLinkContent").eq(0).width())
			.css("visibility", "visible")
			.animate({left: 0}, 250,
				function()
				{
				$("#contentHomeMainLeft > .ctrlLinkContent").eq(1)
						.css("position", "relative")
						.css("left", - $("#contentHomeMainLeft > .ctrlLinkContent").eq(1).width())
						.css("visibility", "visible")
						.animate({left: 0}, 250,
							function()
							{
								var i_right = $("#contentHomeMain > .ctrlLinkContent").eq(1).css("right");
								$("#contentHomeMain > .ctrlLinkContent").eq(1)
									.css("right", $(window).width() + $("#contentHomeMain > .ctrlLinkContent").eq(1).width())
									.css("visibility", "visible")
									.animate({right: i_right}, 750,
										function()
										{
											$("#contentHomeMain > .ctrlLinkContent").eq(0)
												.css("opacity", 0)
												.css("visibility", "visible")
												.animate({opacity: 1}, 500);
										}
									);
							}
						);
				}
			);
	}
	
	function ctrlLinkContentClick()
	{
		if(b_inAnim)
			return false;
		
		var b_get_demo = $(this).attr("href").match("content=get");
		if(!b_get_demo)
			b_get_demo = $(this).attr("href").match("content=demo");
		var b_https = location.protocol.match("https:");
		if(b_get_demo && !b_https)
		{
			location.replace("https://portal.eightpage.com/" + $(this).attr("href"));
			return false;
		}
		else if(!b_get_demo && b_https)
		{
			location.replace("http://portal.eightpage.com/" + $(this).attr("href"));
			return false;
		}
		
		$(".topBodyNaviItem").removeClass("active");
		loadContent($(this).attr("href"), ".content");
		$(".topBodyNaviItem[href=" + $(this).attr("href") + "]").addClass("active");
		return false;
	}
	
	function ctrlLinkContentMainClick()
	{
		if(b_inAnim)
			return false;
		
		$(".contentNaviItem").removeClass("active");
		loadContent($(this).attr("href"), ".contentMain");
		$(".contentNaviItem[href=" + $(this).attr("href") + "]").addClass("active");
		return false;
	}
	
	function loadContent(c_href, c_sel)
	{
		if(c_href)
		{
			b_inAnim = true;
			
			g8_share.appendWaitOverlay();
			
			var c_content = "home";
			var i_section = 1;
			var c_temp = c_href.replace("?lang=" + c_lang + "&", "");
			var i_temp = c_temp.indexOf("&section=");
			if(i_temp == -1)
			{
				c_content = c_temp.replace("content=", "");
				i_section = 1;
			}
			else
			{
				c_content = c_temp.substr(0, i_temp).replace("content=", "");
				i_section = c_temp.substr(i_temp).replace("&section=", "");
			}
			$("head > title").text(c_title_[c_content][i_section] ? c_title_[c_content][i_section] : "eightpage.com");
			
			var p_temp = $("<div><div>");
			p_temp.insertAfter(c_sel);
			p_temp.load(c_href + " " + c_sel,
				function()
				{
					$(this).children(c_sel).removeClass("active");
					$(this).children(c_sel).insertAfter(c_sel + ".active");
					$(this).remove();
					if(c_sel == ".content")
					{
						$(".contentNaviItemBox, .contentNaviItem").fadeOut(450);
						$(c_sel + ".active").slideUp(450,
							function()
							{
								$(this).remove();
								$(c_sel).slideDown(600,
									function()
									{
										$(this).addClass("active");
										$(".contentNaviItemBox, .contentNaviItem").fadeIn(600);
										langSwitchRefresh(c_href);
										analyticsRefresh();
										refresh();
										
										$("#frmOverlayAlpha").remove();
									}
								);
							}
						);
					}
					else if(c_sel == ".contentMain")
					{
						$(c_sel + ".active").fadeOut(450,
							function()
							{
								$(this).remove();
								$(c_sel).find(".getEightpageTeaser").hide();
								$(c_sel).fadeIn(600,
									function()
									{
										$(this).addClass("active");
										$(this).find(".getEightpageTeaser").fadeIn(450);
										langSwitchRefresh(c_href);
										analyticsRefresh();
										refresh();
										
										$("#frmOverlayAlpha").remove();
									}
								);
							}
						);
					}
					else if(c_sel == ".whoisFormResult")
						$(c_sel + ".active").slideUp(450,
							function()
							{
								$(this).remove();
								$(c_sel).slideDown(600,
									function()
									{
										$(this).addClass("active");
										langSwitchRefresh(c_href);
										analyticsRefresh();
										refresh();
										
										$("#frmOverlayAlpha").remove();
										$("input[type=submit]").blur();
										
										$(".orderForm > input[name=domain]").val($(".whoisForm > div > input[name=domain]").val());
									}
								);
							}
						);
				}
			);
		}
	}
	
	function langSwitchRefresh(c_href)
	{
		if(c_lang == "de")
		{
			$(".linkLangSwitchDE").attr("href", c_href);
			$(".linkLangSwitchEN").attr("href", c_href.replace("lang=de", "lang=en"));
		}
		else if(c_lang == "en")
		{
			$(".linkLangSwitchEN").attr("href", c_href);
			$(".linkLangSwitchDE").attr("href", c_href.replace("lang=en", "lang=de"));
		}
	}
	
	function whoisFormSubmit()
	{
		var c_href = $(this).attr("action");
		$.each($(this).find("input[type!=submit], select"),
			function()
			{
				c_href += "&" + $(this).attr("name") + "=" + $(this).val();
			}
		);
		loadContent(c_href, "." + $(this).find("input[name=formResult]").attr("value"));
		return false;
	}
	
	function demoFormSubmit()
	{
		var c_href = $(this).attr("action");
		$.each($(this).find("input[type!=submit], select"),
			function()
			{
				c_href += "&" + $(this).attr("name") + "=" + $(this).val();
			}
		);
		
		if(c_href)
		{
			b_inAnim = true;
			
			g8_share.appendWaitOverlay();
			
			var p_temp = $("<div><div>").css("display", "none");
			p_temp.insertAfter(".demoForm");
			p_temp.load(c_href + " .demoForm, .demoFormSuccess",
				function()
				{
					$(this).children(".demoForm, .demoFormSuccess").removeClass("active");
					if($(".demoFormSuccess").length)
					{
						$(".demoFormSuccess").insertAfter(".demoForm");
						$(this).remove();
						$(".demoForm").parent().parent().children(".contentMainGetBoxHead").children(".contentMainGetBoxHeadTeaser").fadeOut(450,
							function()
							{
								$(this)
									.attr("src", "/res/" + c_resActual + "/themes/portal/home/img/batch_try_ok.png")
									.fadeIn(600);
							}
						);
						$(".demoForm").slideUp(450,
							function()
							{
								$(this).remove();
								$(".demoFormSuccess").slideDown(600,
									function()
									{
										$(this).addClass("active");
										refresh();
										
										$("#frmOverlayAlpha").remove();
									});
							});
					}
					else
					{
						$(".demoForm.active").html($(this).children(".demoForm").html());
						$(this).remove();
						refresh();
						
						$("#frmOverlayAlpha").remove();
					}
				}
			);
		}
		
		return false;
	}
	
	function orderFormSubmit()
	{
		var c_href = $(this).attr("action");
		$.each($(this).find("input[type!=submit], select"),
			function()
			{
				if($(this).attr("name") == "agb" || $(this).attr("name") == "paytype" || $(this).attr("name") == "tld")
				{
					if($(this).filter(":checked").length)
						c_href += "&" + $(this).attr("name") + "=" + $(this).val();
				}
				else
					c_href += "&" + $(this).attr("name") + "=" + $(this).val();
			}
		);
		
		if(c_href)
		{
			b_inAnim = true;
			
			g8_share.appendWaitOverlay();
			
			var p_temp = $("<div><div>").css("display", "none");
			p_temp.insertAfter(".orderForm");
			p_temp.load(c_href + " .whoisForm, .orderForm, .orderFormSuccess",
				function()
				{
					$(this).children(".whoisForm, .orderForm, .orderFormSuccess").removeClass("active");
					if($(".orderFormSuccess").length)
					{
						$(".orderFormSuccess").insertAfter(".orderForm");
						$(this).remove();
						$(".orderForm").parent().parent().children(".contentMainGetBoxTail").text(" ");
						$(".orderForm").parent().parent().children(".contentMainGetBoxHead").children(".contentMainGetBoxHeadTeaser").fadeOut(450,
							function()
							{
								$(this)
									.attr("src", "/res/" + c_resActual + "/themes/portal/home/img/batch_buy_ok.png")
									.fadeIn(600);
							}
						);
						$("<div id=\"orderCreateSuccessOverlay\">&nbsp;</div>").css("display", "none").prependTo($("#mid")).fadeIn(750);
						$('html, body').animate({scrollTop:0}, 750,
							function()
							{
								$(".orderForm").slideUp(450,
									function()
									{
										$(this).remove();
									}
								);
								$(".whoisForm").slideUp(500,
									function()
									{
										$(this).remove();
										$(".orderFormSuccess").slideDown(600,
											function()
											{
											$(this).addClass("active");
												refresh();
												
												$("#frmOverlayAlpha").remove();
											}
										);
									}
								);
							}
						);
					}
					else
					{
						$(".orderForm.active").html($(this).children(".orderForm").html());
						$(".whoisForm.active").html($(this).children(".whoisForm").html());
						$(this).remove();
						refresh();
						
						$("#frmOverlayAlpha").remove();
					}
				}
			);
		}
		
		return false;
	}
	
	function ctrlHomeChangeThemeClick()
	{
		if($("#contentHomeMainTheme > img").length > 1)
			return;
		
		var c_back = $("#contentHomeMainTheme > img").attr("src");
		var c_end = c_back.substr(c_back.length - 13);
		var c_no = c_end.substr(0, 2);
		var i_newNo = parseInt(c_no) + 1;
		if(i_newNo == 5)
			i_newNo = 1;
		var c_newNo = i_newNo < 10 ? "0" + i_newNo : "" + i_newNo;
		
		$("#contentHomeMainTheme > img").fadeOut(450,
			function()
			{
				$(this)
					.attr("src", "/res/" + c_resActual + "themes/portal/home/img/theme_" + c_newNo + "_teaser.png")
					.fadeIn(600);
			}
		);
	}
	
	function ctrlImageBoxViewClick()
	{
		var p_this = $(this);
		var p_img = p_this.children("img");
		var i_w = p_img.width();
		var i_h = p_img.height();
		
		var p_imgBig = $("<div id=\"imageBoxView\"><img src=\"" + p_this.attr("href") + "\" /></div>")
			.css("width", i_w)
			.css("height", i_h)
			.css("top", p_img.offset().top - $(window).scrollTop())
			.css("left", p_img.offset().left - $(window).scrollLeft())
			.css("position", "fixed")
			.css("z-index", "3")
			.css("background", "no-repeat")
			.appendTo("body");
		
		var p_imgBigObj = new Image();
		p_imgBigObj.src = p_this.attr("href");
		i_w = p_img.width();
		i_h = p_img.height();
		if((i_w / i_h) < 1.876)
		{
			i_w = (i_w / i_h) * 533;
			i_h = 533;
		}
		else
		{
			i_h = (i_h / i_w) * 1000;
			i_w = 1000;
		}
		
		$(window).resize(
			function()
			{
				if(($(window).width() / $(window).height()) >= (i_w / i_h))
				{
					var i_height = $(window).height() - 50 > i_h ? i_h : $(window).height() - 50;
					var i_width = (i_w / i_h) * i_height;
					p_imgBig.css("height", i_height).css("top", ($(window).height() - i_height) / 2);
					p_imgBig.css("width", i_width);
					p_imgBig.css("left", ($(window).width() - i_width) / 2);
				}
				else
				{
					var i_width = $(window).width() - 50 > i_w ? i_w : $(window).width() - 50;
					var i_height = (i_h / i_w) * i_width;
					p_imgBig.css("width", i_width).css("left", ($(window).width() - i_width) / 2);
					p_imgBig.css("height", i_height);
					p_imgBig.css("top", ($(window).height() - i_height) / 2);
				}
			}
		);
		
		if(($(window).width() / $(window).height()) >= (i_w / i_h))
		{
			var i_newH = $(window).height() - 50 > i_h ? i_h : $(window).height() - 50;
			var i_newW = (i_w / i_h) * i_newH;
		}
		else
		{
			var i_newW = $(window).width() - 50 > i_w ? i_w : $(window).width() - 50;
			var i_newH = (i_h / i_w) * i_newW;
		}
		var i_newTop = ($(window).height() - i_newH) / 2;
		var i_newLeft = ($(window).width() - i_newW) / 2;
		
		g8_share.appendImageBoxViewOverlay(
			function()
			{
				p_imgBig.animate({width: i_newW, height: i_newH, top: i_newTop, left: i_newLeft}, 600);
			}
		);
		
		$("#frmOverlayAlpha, #imageBoxView").bind("click",
			function()
			{
				$("#imageBoxView").animate({width: 0, height: 0, top: ($(window).height() / 2), left: ($(window).width() / 2)}, 450,
					function()
					{
						$(this).remove();
						$("#frmOverlayAlpha").fadeOut(150,
							function()
							{
								$(this).remove();
							}
						);
					}
				);
			}
		);
		
		return false;
		//$("#frmOverlayAlpha").remove();
	}
	
	function inputFaultTipClick()
	{
		$(this).removeClass("inputFault");
	}
	
	function inputFaultTipFocus()
	{
		$(this).parent().removeClass("inputFault");
	}
	
	function ctrlPayInfoPaypalClick()
	{
		$(".ctrlPayInfoPaypalContent").slideToggle(450);
		$(this).toggleClass("active");
		return false;
	}
	
	function ctrlPayInfoClibuyClick()
	{
		$(".ctrlPayInfoClibuyContent").slideToggle(450);
		$(this).toggleClass("active");
		return false;
	}
	
	function ctrlWatchVideoClick()
	{
		if($("#fvcPopup").find("object,embed").length == 0)
		{
			g8_share.blackPopup("alert", "Hinweis", "Du hast kein Flash installiert oder zurzeit aktiviert. Um dir das Video anzuschauen musst Flash installiert und aktiviert haben.", g8_share.blackConfirm, false);
			return false;
		}
		
		$("#fvcPopup").find("object,embed").bind("focus",
			function()
			{
				this.blur();
			}
		);
		
		$(window).trigger("resize");
		$("#frmVideoCover").fadeIn(600,
			function()
			{
				$("#fvcPopup").css("display", "block");
			}
		);
		
		return false;
	}
	function ctrlWatchVideoCloseClick()
	{
		$("#fvcPopup").css("display", "none");
		$("#frmVideoCover").fadeOut(450);
		return false;
	}
	flashCallCloseVideoPopup = ctrlWatchVideoCloseClick;

})();
