﻿/// <reference path="dev-scripts/jquery.js" />
/// <reference path="utils.js" />
/// <reference path="liteweb.js" />

liteweb.AppendInit(function () {
	var lw = this.lw || {};
	lw.wrapper = $(".wrapper");
	lw.footer = $(".footer");
	lw.window = $(window);
	lw.onResize = function () {
		return;
		var size = ___getPageSize();

		//set footer at the bottom if the wrapper height < body height
		if (lw.wrapper.height() < size[3] - lw.footer.height()) {
			var h = lw.wrapper.outerHeight(true) - lw.wrapper.height();
			if ($(".inside").length == 0)
				lw.wrapper.animate({ height: size[3] - lw.footer.height() - h });
			else {
				$(".inside .content .b").animate({ height: size[3] - lw.footer.height() - lw.navigation.outerHeight() - $(".header-image").outerHeight() - 24 });
			}
		}
		//remove scrolling incase window width < 1000
		if (size[0] < 1014) {
			lw.wrapper.width = 1000;
		}
	}
	lw.topMenu = $(".top-menu");
	lw.topMenu.bound = false;
	lw.navigation = $(".navigation");
	lw.menuContainer = $(".menu-container");
	lw.preOpenMenu = function () {
		if (lw.closeMenuTimeout) {
			clearTimeout(lw.closeMenuTimeout);
			lw.closeMenuTimeout = null;
		}
		lw.openMenutimeOut = setTimeout(lw.openMenu, 500);
	}
	lw.openMenu = function () {
		if (lw.menuOpened)
			return;
		lw.menuOpened = true;
		lw.navigation.bind("mouseover", lw.preOpenMenu);
		if (!lw.topMenu.bound) {
			lw.topMenu.bound = true;
			lw.topMenu.whites = lw.topMenu.children("div.white");
			lw.topMenu.menu = lw.topMenu.children("div.menu");
			lw.topMenu.whites.each(function () {
				var $el = $(this);
				this.widthFrom = $el.width();
			});
			lw.ulPositions = [];
			var ul = $(".menu-container>ul>li");
			ul.each(function () {
				lw.ulPositions.push($(this).position().left);
			});
			lw.headers = $(".top-menu ol li a");
		}
		lw.topMenu.whites.stop().animate({ width: 0 });
		lw.menuContainer.stop().animate({ top: 30 }, { duration: 600, easing: "easeOutCubic" });
		lw.headers.each(function (i) {
			var animateTo = $(this).position().left - lw.ulPositions[i] - 8;
			$(this).animate({ left: -1 * animateTo }, { duration: 600, easing: "easeOutCubic" });
		});
	}
	lw.restoreMenu = function () {
		lw.topMenu.whites.each(function () {
			$(this).stop().animate({ width: this.widthFrom }, { duration: 300, easing: "swing" });
		});
		lw.menuContainer.stop().animate({ top: -400 }, { duration: 600, easing: "easeOutCubic" });
		lw.headers.animate({ left: 0 }, { duration: 600, easing: "easeOutCubic" });

		lw.menuOpened = false;
		lw.navigation.unbind("mouseover");
	}
	lw.preRestoreMenu = function () {
		if (lw.openMenutimeOut) {
			clearTimeout(lw.openMenutimeOut);
			lw.openMenutimeOut = null;
		}
		if (!lw.menuOpened)
			return;
		lw.closeMenuTimeout = setTimeout(lw.restoreMenu, 505);
	}
	lw.window.bind("resize", lw.onResize);
	lw.topMenu.bind("mouseover", lw.preOpenMenu);
	lw.navigation.bind("mouseout", lw.preRestoreMenu);

	liteweb.AppendLoad(lw.onResize);
	lw.home = $(".home");
	if (lw.home && lw.home.length > 0) {
		lw.newsButtons = [];
		function mycarousel_initCallback(carousel) {
			var len = $(".carousel ul li").length;
			var ol = $("<ol />");
			for (var i = len - 1; i >= 0; i--) {
				var li = $("<li />");
				var a = $("<a />");
				a[0].href = "#";
				a.html(i + 1);
				a.click(function () {
					var $this = $(this);
					if ($this.html() == $("._active").html())
						return;
					$("._active").removeClass("_active");
					$this.addClass("_active");
					carousel.scroll(parseInt($this.html()));
					return false;
				});
				li.click(function () {
					lw.automateNews = false;
				});
				if (i == 0)
					a.addClass("_active");
				li.append(a);
				lw.newsButtons.push(a);
				ol.append(li);
			}
			$(".news .f").append(ol);
			return false;
		}
		$(".carousel").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		lw.automateNews = true;
		lw.lastNewsButton = lw.newsButtons.length - 1;
		function AutomateNews() {
			if (!lw.automateNews)
				return;
			lw.lastNewsButton--;
			if (lw.lastNewsButton == -1)
				lw.lastNewsButton = lw.newsButtons.length - 1;
			lw.newsButtons[lw.lastNewsButton].click();
			setTimeout(AutomateNews, 5000);
		}
		setTimeout(AutomateNews, 5000);
	}


	var language = "en";
	if (location.href.indexOf("/de/") > 0)
		language = "de";
	else if (location.href.indexOf("/jp/") > 0)
		language = "jp";

	var languageIndex = location.href.indexOf("/" + language + "/");
	if (languageIndex > 0) {
		var link = location.href.substr(location.href.indexOf("/" + language + "/") + 4);
		$(".en")[0].href = liteweb.vroot + "/en/" + link;
		$(".de")[0].href = liteweb.vroot + "/de/" + link;
	}

	var selector = ".languages a:not(.{0})".Format(language);
	var languages = $(selector);
	languages.animate({ opacity: .5 });
	languages.bind("mouseover", function () {
		$(this).stop().animate({ opacity: 1 });
	});
	languages.bind("mouseout", function () {
		$(this).stop().animate({ opacity: 0.5 });
	});


	var subMenu = $(".sub-menu");
	if (subMenu.length > 0) {
		var content = $(".final-content h1");
		var el = $("<div />").html(subMenu[0].innerHTML);
		el.addClass("sub-menu");

		var li = el.children("ol").children("li");
		var isHide = false;
		li.each(function () {
			if (this.className == "sep")
				isHide = true;
			if (isHide)
				this.style.display = "none";
		});

		content.after(el);
	}



	/* slide show (home page) */
	if ($(".slide-show").length == 0)
		return;

	for (var i = 2; i <= 4; i++) {
		var im = $("<img src=\"{0}/images/slide-show/{1}.jpg\" />".Format(liteweb.vroot, i));
		im.css("opacity", 0);
		$(".slide-show").append(im);
	}

	var slInit = false;
	var lastImage = 0;
	function SlideShow() {

		var images = $(".slide-show img");
		if (!slInit) {
			for (var i = 1; i < images.length; i++) {
				$(images[i]).css({
					"opacity": 0,
					"position": "absolute"
				});
			}
			slInit = true;
			//return;
		}
		var temp = lastImage;
		lastImage++;
		if (lastImage == images.length)
			lastImage = 0;
		$(images[temp]).animate({ opacity: 0 }, 1500);
		$(images[lastImage]).animate({ opacity: 1 }, 1500);
		setTimeout(SlideShow, 8000);
	}
	setTimeout(SlideShow, 8000);
});
