﻿/// <reference path="jquery.js" />
/// <reference path="utils.js" />
/// <reference path="lw.js" />

lw.AppendInit(function () {
	var lw = this.lw || {};


	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; i >= 1; i--) {
	            var li = $("<li />");
	            var a = $("<a />");
	            a.addClass("carousel-index-" + i);
	            a[0].href = "#";
	            a.html(i);
	            a.click(function () {
	                clearTimeout(lw.automateNews);
	                var $this = $(this);
	                if ($this.html() == $("._active").html())
	                    return;
	                $("._active").removeClass("_active");
	                $this.addClass("_active");
	                $(".carousel").jcarousel("scroll", parseInt($this.html()));

	                return false;
	            });
	            li.click(function () {
	                clearTimeout(lw.automateNews);
	            });
	            if (i == 1)
	                a.addClass("_active");
	            li.append(a);
	            lw.newsButtons.push(a);
	            ol.append(li);
	        }
	        $(".testimonials .f").append(ol);
	        return false;
	    }
	    $(".carousel").jcarousel({
	        scroll: 1,
	        initCallback: mycarousel_initCallback,
	        // This tells jCarousel NOT to autobuild prev/next buttons
	        buttonNextHTML: null,
	        buttonPrevHTML: null,
	        circular: true
	    });
	    lw.lastNewsButton = 1;
	    function AutomateNews() {
	        if (!lw.automateNews)
	            return;
	        lw.lastNewsButton++;
	        if (lw.lastNewsButton == lw.newsButtons.length + 1)
	            lw.lastNewsButton = 1;
	        $(".carousel").jcarousel("scroll", lw.lastNewsButton);

	        $("._active").removeClass("_active");
	        $(".carousel-index-" + lw.lastNewsButton).addClass("_active");
	        lw.automateNews = setTimeout(AutomateNews, 5000);
	    }
	    lw.automateNews = setTimeout(AutomateNews, 5000);
	}

	/* slide show (home page) */
	lw.slideShow = $(".slide-show");
	if (lw.slideShow.length == 0)
		return;

	for (var i = 2; i <= 5; i++) {
		var im = $("<img src=\"images/header/home/{1}.jpg\" />".Format(lw.vroot, i));
		im.css("opacity", 0);
		lw.slideShow.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",
					"z-index": 1
				});
			}
			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);
});



//menu
lw.AppendInit(function () {
	var dur = 300;
	var easing = "easeOutQuad";

	$(".menu>li").each(function (i, el) {
		el.menu = createMenu(el, null, i);
	});

	function createMenu(el, parent) {
		var m = {};
		m.el = $(el);
		m._a = $(el).children("a");

		m.child = $(el).children("ul");
		if (m._a.length == 0 || m.child.length == 0)
			return;

		m.a = m._a[0];
		m.ul = m.child[0];

		m.li = m.child.children("li");
		m.maxHeight = m.child.outerHeight() + 0; //m.li.length * 23 + 22 + ($.browser.safari && $.browser.version < 526 ? (-2) : 0);
		m.maxWidth = m.child.outerWidth();

		m.visible = false;
		m.hideMe = false;
		m.parent = parent;
		m.a.menu = m;
		m.child[0].menu = m;

		m.show = function () {
			this.hideMe = false;
			if (this.visible)
				return;
			this.visible = true;

			this.child.css({
				overflow: "visible",
				visibility: "visible",
				height: 0,
				top: this.parent === null ?
				20 :
				this._a.position().top - $(this.a.parentNode.parentNode).position().top,
				left: this.parent === null ?
					this._a.position().left:
					this._a.outerWidth() + 2,
				width: this.child.width() < this._a.width() ? this._a.width() - 4 : this.child.width()
			});
			this.child.stop();
			var animateTo = { height: this.maxHeight };
			//if (!$.browser.msie)
			$.extend(animateTo, { opacity: 1 });
			this.child.animate(animateTo, { duration: dur, easing: easing });
			this._a.addClass("_hover");
		};
		m.hide = function () {
			this.hideMe = true;
			window.setTimeout(function () {
				m._hide();
			}, 50);
		}
		m._hide = function () {
			if (!this.hideMe)
				return;
			this.visible = false;
			for (var i = 0; i < this.children.length; i++) {
				if (this.children[i].visible)
					return;
			}
			this.child.stop();
			var animateTo = { height: 0 };
			//if (!$.browser.msie)
			$.extend(animateTo, { opacity: 0 });

			this.child.animate(animateTo, { duration: dur / 2, easing: easing,
				complete: function () {
					if (!this.menu.hideMe)
						return;
					this.menu.child.css("visibility", "hidden");
					this.menu._a.removeClass("_hover");
				}
			});
		}
		m.children = [];
		m.li.each(function (i, el) {
			m.children.push(el);
			el.menu = createMenu(el, this);
			var $a = $($(this).children("a"));
			if ($(el).children("ul").length == 0)
				$a.addClass("arrow1");
		});
		if (m.children.length > 0 && m.parent != null) {
			m._a.addClass("arrow");
		}
		m._a.bind("mouseover", function () {
			if (this.menu) {
				this.menu.show();
			}
		});
		m._a.bind("mouseout", function () {
			if (this.menu)
				this.menu.hide();
		});
		m.child.bind("mouseover", function () {
			if (this.menu)
				this.menu.show();
		});
		m.child.bind("mouseout", function () {
			if (this.menu)
				this.menu.hide();
		});
		return m;
	}
	function hideMenu(el) {
		window.setTimeout(function () {
			_hideMenu(el);
		}, 200);
	}
	function _hideMenu(el) {
		if (el.childVisible)
			return;
		el._sub.css("display", "none");
		el._ol.css("height", 0)
		el._a.removeClass("_hover");
	}
});



function ShowPopup(id) {
	var p = $(id);
	//debugger;
	var h = $(document).height();
	var w = $(document).width();

	var overlays = new Array(4);
	lw.overlays = overlays;
	var body = $(document.body);
	body.css("overflow", "hidden");
	for (var i = 0; i < overlays.length; i++) {
		overlays[i] = $(document.createElement("div"));
		var obj = {
			position: "absolute",
			width: 0,
			height: 0,
			background: "#000",
			opacity: 0,
			top: i < 2 ? 0 : h,
			left: i % 2 == 0 ? 0 : w,
			"z-index": 900
		};
		overlays[i].css(obj);
		body.append(overlays[i]);
		obj = {
			width: w / 2,
			height: h / 2,
			left: i % 2 == 0 ? 0 : w / 2,
			top: i < 2 ? 0 : h / 2,
			opacity: .6
		};
		overlays[i].animate(obj, {
			duration: 600,
			complete: function () {
				if (typeof lw.popupShown == typeof lw.balloutUndefined) {
					lw.popupShown = true;
					obj = {
						position: "absolute",
						left: $(window).width() / 2 - p.width() / 2,
						top: $(window).height() / 2 - p.height() / 2,
						opacity: 0,
						display: "block",
						"z-index": 9000
					};
					p.css(obj);
					p.animate({ opacity: 1 }, 600);
				}
				p.children("#close").css("cursor", "pointer");
				p.click(function () {
					ClosePopup(p);
				});
			}
		}
		);
	}
}
function ClosePopup(p) {
	p.animate({ opacity: 0 }, {
		duration: 300, complete: function () {
			var h = $(document).height();
			var w = $(document).width();
			p.css("display", "none");
			var overlays = lw.overlays;
			for (var i = 0; i < overlays.length; i++) {
				obj = {
					width: 0,
					height: 0,
					left: i % 2 == 0 ? 0 : w,
					top: i < 2 ? 0 : h,
					opacity: 0
				};
				overlays[i].animate(obj, {
					duration: 600,
					complete: function () {
						$(document.body).css("overflow", "auto");
					}
				});
			}
		}
	});
}
