﻿var koura = {
    currentAlbum: 0,
    albumsLength: 0,
    albumsList: null,
    albumLinks: null,
    preparegallery: function () {
        var _ = koura;
        var list = _.albumsList = $(".gallery a>img");
        _.albumLinks = $(".gallery a");
        _.albumsLength = list.length;
        if (_.albumsLength == 1) {
            $("a[title='Previous Album']").css("visibility", "hidden");
            $("a[title='Next Album']").css("visibility", "hidden");
            return;
        }
        list.each(function (i, el) {
            if (i > 0) {
                $(el).css("opacity", 0);
            }
        });
        $("a[title='Previous Album']").click(function () {
            _.changePhoto(-1);
        });
        $("a[title='Next Album']").click(function () {
            _.changePhoto(1);
        });
    },
    changePhoto: function (step) {
        var _ = koura;
        var temp = _.currentAlbum;
        _.currentAlbum += step;
        _.currentAlbum = _.currentAlbum == _.albumsLength ? 0 : _.currentAlbum < 0 ? _.albumsLength - 1 : _.currentAlbum;
        $(_.albumsList[temp]).animate(
			{ left: step * 300, opacity: 0 },
			{ duration: 300, complete: function () {
			}
			});
        $(_.albumsList[_.currentAlbum]).css("left", 0);
        $(_.albumsList[_.currentAlbum]).animate(
			{ opacity: 1 },
			{ duration: 300, complete: function () {
			}
			});
        $(".galleryholder .album-title").attr("href",
			$(_.albumLinks[_.currentAlbum]).attr("href")
		);
        $(".galleryholder .album-title").attr("title",
			$(_.albumLinks[_.currentAlbum]).attr("title")
		);
        $(".galleryholder .footer-title").text(
			$(_.albumLinks[_.currentAlbum]).attr("title")
		);
    },
    preparenews: function () {
        var _ = koura;
        liteweb.animDuration = 600;
        liteweb.news = $("div.newslist div.news");
        liteweb.news.css({ position: "absolute", display: "none" });
        var ul = document.createElement("ul");
        var ar = [];
        liteweb.news.each(function (i, el) {
            ar.push("<li><a href=\"javascript:\" onclick=\"koura.NewsItem({0}, this)\"{2}>{1}</a></li>".Format(i, i + 1, i == 0 ? " class=\"selected\"" : ""));
        });
        ul.innerHTML = ar.join("");
        $(".news-links").append(ul);
        if (liteweb.news.length == 0)
            return;
        liteweb.lastNewsItem = $(liteweb.news[0]);
        liteweb.lastNewsItem.css({ display: "block" });
        var $ul = $(ul);
        var offset = $ul.offset();
        $ul.css({
            position: "absolute",
            top: offset.top,
            left: offset.left,
            zIndex: 100
        });
        var newsslider = liteweb.newsslider = $(document.createElement("div"));
        var $a = $($ul.children("li").children("a")[0]);
        newsslider.addClass("newsslider");
        newsslider.css({
            position: "absolute",
            top: $a.offset().top + 1,
            left: $a.offset().left + 1,
            zIndex: 99
        });
        $(".news-links").append(newsslider);
        liteweb.oldNewsNav = $a;
    },
    NewsItem: function (i, el) {
        if (el.className.indexOf("selected") >= 0)
            return;
        liteweb.oldNewsNav.removeClass("selected");
        var $el = $(el);

        var animateTo = {
            left: $el.offset().left + 1
        };

        liteweb.newsslider.animate(animateTo, { duration: liteweb.animDuration });

        liteweb.oldNewsNav = $el;
        liteweb.oldNewsNav.addClass("selected");

        if (liteweb.lastNewsItem != undefined) {
            liteweb.lastNewsItem.fadeOut(liteweb.animDuration);
        }
        liteweb.lastNewsItem = $(liteweb.news[i]);
        liteweb.lastNewsItem.fadeIn(liteweb.animDuration);
    },
    preparetestimonials: function () {
        var _ = koura;
        liteweb.animDuration = 600;
        liteweb.testimonials = $("div.testimonialslist div.testimonial");
        liteweb.testimonials.css({ position: "absolute", display: "none" });
        var ul = document.createElement("ul");
        var ar = [];
        liteweb.testimonials.each(function (i, el) {
            ar.push("<li><a href=\"javascript:\" onclick=\"koura.TestimonialsItem({0}, this)\"{2}>{1}</a></li>".Format(i, i + 1, i == 0 ? " class=\"selected\"" : ""));
        });
        ul.innerHTML = ar.join("");
        $(".testimonial-links").append(ul);
        if (liteweb.testimonials.length == 0)
            return;
        liteweb.lastTestimonialsItem = $(liteweb.testimonials[0]);
        liteweb.lastTestimonialsItem.css({ display: "block" });
        var $ul = $(ul);
        var offset = $ul.offset();
        $ul.css({
            position: "absolute",
            top: offset.top,
            left: offset.left,
            zIndex: 100
        });
        var slider = liteweb.slider = $(document.createElement("div"));
        var $a = $($ul.children("li").children("a")[0]);
        slider.addClass("slider");
        slider.css({
            position: "absolute",
            top: $a.offset().top + 1,
            left: $a.offset().left + 1,
            zIndex: 99
        });
        $(".testimonial-links").append(slider);
        liteweb.oldTestimonialsNav = $a;
    },
    TestimonialsItem: function (i, el) {
        if (el.className.indexOf("selected") >= 0)
            return;
        liteweb.oldTestimonialsNav.removeClass("selected");
        var $el = $(el);

        var animateTo = {
            left: $el.offset().left + 1
        };

        liteweb.slider.animate(animateTo, { duration: liteweb.animDuration });

        liteweb.oldTestimonialsNav = $el;
        liteweb.oldTestimonialsNav.addClass("selected");

        if (liteweb.lastTestimonialsItem != undefined) {
            liteweb.lastTestimonialsItem.fadeOut(liteweb.animDuration);
        }
        liteweb.lastTestimonialsItem = $(liteweb.testimonials[i]);
        liteweb.lastTestimonialsItem.fadeIn(liteweb.animDuration);
    }
}


liteweb.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.div = $(el).children("div.menu-sub");
		m._ol = m.div.children("ul");
		if (m._a.length == 0 || m._ol.length == 0)
			return;
		
		m._a.addClass("haschildren");

		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");
		m.maxHeight = m._ol.outerHeight() + 2; //m.li.length * 23 + 22 + ($.browser.safari && $.browser.version < 526 ? (-2) : 0);
		m.maxWidth = m._ol.outerWidth() + 2;
		
		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",
				width: 0,
				top: this._a.offset().top,
				left: this._a.offset().left + this._a.width() + 16
				//top: this._a.offset().top,
				//left: this._a.offset().left + this._a.width()
			});
			this.div.stop();
			var animateTo = { width: this.maxWidth};
			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 = { width: 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("ul.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");
	}
});
