﻿var lw = {};
var l = {};
liteweb.AppendInit(function() {
	lw.lastNews = 0;
	lw.newsInterval = 7000;
	lw.galleryInterval = 7000;

	function SwitchGalleryImage() {
		with (liteweb) {
			var temp = lastGalleryImage;
			lastGalleryImage++;
			lastGalleryImage = lastGalleryImage >= GalleryImgList.length ? 0 : lastGalleryImage;
			lw.cEl = $(liteweb.GalleryImgList[lastGalleryImage]);
			$(liteweb.GalleryImgList[temp]).animate({ opacity: 0 }, { easing: "easeInCirc", duration: 1000, complete: function() {
			}
			});
			setTimeout(function() {
				lw.cEl.animate({ opacity: 1 }, { easing: "easeInCirc", duration: 1000 });
			}, 500);
		};

		lw.galleryTimeout = setTimeout(SwitchGalleryImage, lw.galleryInterval);
	};

	function showNextNews() {
		var lastNews = $(lw.newsItems[lw.lastNews]);
		lastNews.css("position", "relative");
		lastNews.animate({ "left": -300 }, { duration: 600, easing: "easeInCirc", complete: function() {
			lw.news.animate({ "scrollTop": lw.news.scrollTop() + lastNews.height() }, { duration: 200, complete: function() {
				lastNews.css("left", 0);
			}
			});
		}
		});
		lw.lastNews++;
		if (lw.lastNews == lw.originalLength) {
			lw.lastNews = 0;
			setTimeout(function() {
				lw.news.stop().scrollTop(0)
			}, 1000);
		}
		lw.newsTimeout = setTimeout(showNextNews, lw.newsInterval);
	};

	/* Gallery */

	var list = liteweb.GalleryImgList = $(".spotimages img");
	if (list.length == 0) {
		$(".spotimages").css("display", "none");
		//return;
	}
	if (list.length == 1) {
		//return;
	}
	list.each(function(i, el) {
		if (i == 0)
			return;
		$(el).css("opacity", 0);
	});
	liteweb.lastGalleryImage = 0;
	galleryTimeout = setTimeout(SwitchGalleryImage, lw.galleryInterval);

	/* News */

	lw.news = $(".newshome");
	lw.newsUl = $(".newshome ul");
	lw.newsItems = $(".newshome ul li");
	lw.originalLength = lw.newsItems.length;

	if (lw.newsItems.length == 1)
		return;

	lw.newsItems.each(function() {
		var b = this.cloneNode(true);
		lw.newsUl.append(b);
	});

	lw.newsItems = $(".newshome ul li");

	newsTimeout = setTimeout(showNextNews, lw.newsInterval);

});

liteweb.AppendInit(function() {
	var scrollingDiv = $("#scrollingDiv");
	var doc = $(document);
	var win = $(window);
	lw.headerHeight = $("#header").height() + 10;
	l.ScrollWindow = function() {
		//if (doc.height() <= win.height() + win.scrollTop())
		//	return;
		$(l.images[l.lastImage]).css("position", "relative");
		var scroll = $(window).scrollTop();
		if (scroll <= lw.headerHeight)
			scroll = lw.scollOffset.top;
		if (scroll + scrollingDiv.height() >= doc.height())
			scroll = doc.height() - scrollingDiv.height();
		scrollingDiv
				.stop()
				.animate({ "top": scroll + 10 }, {
					duration: 600,
					complete: function() {
						$(l.images[l.lastImage]).css("position", "absolute");
					}
				});
	};
	lw.scollOffset = scrollingDiv.offset();
	scrollingDiv.css({ "position": "absolute",
	top: lw.scollOffset.top,
	left: lw.scollOffset.left + ($.browser.safari ? -8 : 0)
	});
	$(window).scroll(function() {
		l.scrollTimer = setTimeout(l.ScrollWindow, 10);
	});
});

liteweb.AppendInit(function() {
	l.lastImage = 0;
	l.images;
	l.lastNews = 0;
	l.interval = 6000;

	function displayNextImage() {
		$(l.images[l.lastImage]).fadeOut("slow", function() {
			$(this).css("position", "absolute");
		});
		l.lastImage++;
		l.lastImage = l.lastImage >= l.images.length ? 0 : l.lastImage;
		$(l.images[l.lastImage]).css("position", "absolute");
		$(l.images[l.lastImage]).fadeIn("slow", function() {
		$(this).css("position", "absolute");
		});
		setTimeout(displayNextImage, l.interval);
	};

	l.images = $("#home-gallery img");
	l.images.each(function(i, im) {
		$(im).css({ "position": "absolute"
		});
		if (i == 0) {
			return;
		}
		$(im).css("display", "none");
	});
	setTimeout(displayNextImage, l.interval);
});


liteweb.AppendInit(function() {
	var dur = 300;
	var easing = "easeOutQuad";

	$(".topmenu>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.div = $(el).children("div.topmenu-sub");
		m._ol = m.div.children("ol");
		if (m._a.length == 0 || m._ol.length == 0)
			return;

		var span = m._a.children("span");
		/*if (span.length > 0)
		this._a = span;
		*/
		m.a = m._a[0];
		m.ol = m._ol[0];

		m.li = m._ol.children("li");
		//alert();
		m.maxHeight = m._ol.outerHeight() + 2; //m.li.length * 23 + 22 + ($.browser.safari && $.browser.version < 526 ? (-2) : 0);

		m.visible = false;
		m.hideMe = false;
		m.parent = parent;
		m.a.menu = m;
		m.div[0].menu = m;

		m.show = function() {
			this.hideMe = false;
			if (this.visible)
				return;
			this.visible = true;

			this.div.css({
				overflow: "visible",
				visibility: "visible",
				height: 0,
				top: this.parent === null ?
				17 :
				this._a.offset().top - $(this.a.parentNode.parentNode).offset().top - 6,
				left: this.parent === null ?
					this._a.offset().left - $(this.a.parentNode.parentNode).offset().left + 25 :
					this._a.outerWidth() + 2
			});
			this.div.stop();
			var animateTo = { height: this.maxHeight };
			if (!$.browser.msie)
				$.extend(animateTo, { opacity: 1 });
			this.div.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.div.stop();
			var animateTo = { height: 0 };
			if (!$.browser.msie)
				$.extend(animateTo, { opacity: 0 });

			this.div.animate(animateTo, { duration: dur / 2, easing: easing,
				complete: function() {
					if (!this.menu.hideMe)
						return;
					this.menu.div.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("div.topmenu-sub").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.div.bind("mouseover", function() {
			if (this.menu)
				this.menu.show();
		});
		m.div.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;
		alert(el);
		el._sub.css("display", "none");
		el._ol.css("height", 0)
		el._a.removeClass("_hover");
	}
});
