﻿/// <reference path="jquery-vsdoc.js" />
/// <reference path="lw.js" />


lw.AppendInit(function () {
	var slides = lw.slides = $(".accordian div.slide");
	var easing = "";
	slides.map = {};
	var dur = 800;
	slides.each(function (i, el) {
		el._Id = "slide_{0}".Format(i);
		var $el = $(el);
		var slide = {
			el: $el,
			overlay: $el.children(".overlay"),
			menu: $el.children(".info-box"),
			side: $el.children(".side"),
			img: $el.children("img"),
			index: i,
			left: true
		};
		slides.map[el._Id] = slide;
		if (i > 0)
			slide.overlay.css({
				"opacity": 0.3,
				"cursor": "pointer"
			});
		else {
			slide.overlay.css({
				"cursor": "pointer"
			});
			lw.lastSlide = slide;
		}
	});

	slides.bind("mouseover", function (e) {
		var slide = slides.map[this._Id];
		slide.overlay.stop().animate({ opacity: 0 }, { duration: 600 });
	});
	slides.bind("mouseout", function (e) {
		var slide = slides.map[this._Id];
		slide.overlay.stop().animate({ opacity: 0.3 }, { duration: 600 });
	});
	slides.click(function (e) {
		var slide = slides.map[this._Id];
		if (slide.el.hasClass("current"))
			return;

		var oldSlide = lw.lastSlide;

		oldSlide.el.removeClass("current");
		slide.el.addClass("current");

		oldSlide.menu.stop().animate({
			top: oldSlide.menu.hasClass("down") ? 320 : -300
		}, {
			duration: dur,
			easing: "easeOutCirc",
			complete: function () {
				oldSlide.menu.css("display", "none");
			}
		});



		lw.lastSlide = slide;

		slide.overlay.css("display", "none");
		if (slide.left) {
			for (var i = 0; i <= slide.index; i++) {
				var s = slides.map["slide_{0}".Format(i)];
				s.el.animate({ left: (120 * s.index) }, { duration: dur, easing: easing });
				s.left = false;
			}
		}
		else {
			for (var i = slide.index + 1; i < slides.length; i++) {
				var s = slides.map["slide_{0}".Format(i)];
				s.el.animate({ left: (120 * s.index) + 500 }, { duration: dur, easing: easing });
				s.left = true;
			}
		}

		slide.menu.css({ display: "block" });
		slide.menu.animate({
			top: slide.menu.hasClass("down") ? parseInt(slide.menu.css("top")) - 100 : 25
		}, {
			duration: dur * 2,
			easing: "easeOutCirc",
			complete: function () {
			}
		});

		setTimeout(function () {
			oldSlide.overlay.css({ "display": "block", opacity: 0 });
			oldSlide.overlay.stop().animate({ opacity: 0.3 }, { duration: 600 });
		}, dur);

	});

	//$(".spotlight").trigger("click");
});


var qolo = {
	currentAlbum: 0,
	albumsLength: 0,
	albumsList: null,
	albumLinks: null,
	prepareGallery: function () {
		var _ = qolo;
		var list = _.albumsList = $(".gallery a>img");
		_.albumLinks = $(".gallery a");
		_.albumsLength = list.length;
		list.each(function (i, el) {
			if (i > 0) {
				$(el).css("opacity", 0);
			}
		});
		$(".g-pages a.previous").click(function () {
			_.changePhoto(-1);
		});
		$(".g-pages a.next").click(function () {
			_.changePhoto(1);
		});
	},
	changePhoto: function (step) {
		var _ = qolo;
		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")
		);
	}
}

lw.AppendInit(function () {
	$(".menu>ul>li").menu({ parent: $(".menu") });
});

lw.AppendInit(function () {
	var lw = this.lw || {};

	lw.imagePlace = $(".image-place");

	lw.home = $("#home");
	if (lw.home && lw.home.length > 0) {
		lw.ArticlesButtons = [];
		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[0]._index = i;
				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;

					if (!this._img) {
						var li = $(".carousel ul li")[this._index - 1];
						this._img = $(li).children("img");
						this._img.css({
							"opacity": 0,
							display: "block"
						});

						lw.imagePlace.append(this._img);
					}

					if (lw.lastNewsImage)
						lw.lastNewsImage.stop().animate({ "opacity": 0 }, 500);
					lw.lastNewsImage = this._img;

					this._img.stop().animate({ "opacity": 1 }, 500);

					$("._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.ArticlesButtons.push(a);
				ol.append(li);
			}
			$(".news .f").append(ol);
			return false;
		}
		try {
			$(".carousel").jcarousel({
				scroll: 1,
				initCallback: mycarousel_initCallback,
				// This tells jCarousel NOT to autobuild prev/next buttons
				buttonNextHTML: null,
				buttonPrevHTML: null,
				circular: true
			});
		}
		catch (e) { }
		lw.lastNewsButton = 1;
		function AutomateNews() {
			return;
			if (!lw.automateNews)
				return;
			lw.lastNewsButton++;
			if (lw.lastNewsButton == lw.ArticlesButtons.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);

		var li = $(".carousel ul li")[0];
		this._img = $(li).children("img");
		this._img.css({
			"opacity": 1,
			display: "block"
		});

		lw.imagePlace.append(this._img);
	}
});

