﻿$(document).ready(function ()
{
	//init
	$.fx.interval = 66;
	//Start siddur navigation
	$.history.init(Citra.Navigation.Navigate, { unescape: "" });

	$("#product_list li .item").bind('click', Citra.Navigation.Toggle);
	$("#product_list li li a").bind('click', Citra.Gallery.Load);
	$(".menu li a").bind('click', Citra.Pages.Load);
	$("#text_page .content .close").bind('click', Citra.Pages.Close);
	$(".footer-menu li a").bind('click', Citra.Info.Load);

	$("div#mainGallery").slideView({ uiPreviousNext: true, width: 540, height: 473, easeFunc: null });
});


var Citra = {
	Gallery:
	{
		Load: function (event)
		{
			Citra.Navigation.GoTo($(this).attr('href'));
			return false;
		}
	},
	Info:
	{
		ActiveItem: null,
		Load: function (event)
		{
			if ($(this).siblings("div").length == 0)
			{
				return Citra.Pages.Load.apply(this, [event].concat());
			}
			if (Citra.Info.ActiveItem !== null)
			{
				Citra.Info.Close.apply(Citra.Info.ActiveItem, [event].concat());
				if (Citra.Info.ActiveItem === this)
				{
					Citra.Info.ActiveItem = null;
					return false;
				}
			}

			Citra.Info.ActiveItem = this;
			$(this).siblings("div").css("visibility", "visible");
			$(this).siblings("div").stop().animate({ 'opacity': 1 }, 500);
			return false;
		},
		Close: function (event)
		{
			$(this).siblings("div").stop().animate({ 'opacity': 0 }, 500, null, function ()
			{
				$(this).css("visibility", "hidden");
			});
		}
	},
	Pages:
	{
		ActiveItem: null,
		Load: function (event)
		{
			var href = $(this).attr('href');
			if (href.charAt(0) != "/")
				return true;

			if (Citra.Pages.ActiveItem !== null)
			{
				Citra.Pages.Close.apply(Citra.Pages.ActiveItem, [event, (Citra.Pages.ActiveItem !== this), this].concat());
				if (Citra.Pages.ActiveItem === this)
				{
					Citra.Pages.ActiveItem = null;
					return false;
				}
				Citra.Pages.ActiveItem = this;
				return false;
			};
			var obj = this;
			//Citra.Pages.ActiveItem = this;

			if (href.substring(0, 1) === "/")
			{
				$("#text_page .content").stop().animate({ 'clip': 'rect(0px 544px 520px 0px)', opacity: 1 }, 500, null, function ()
				{
					Citra.Pages.ActiveItem = obj;
				});
				Citra.Navigation.GoTo(href);
				return false;
			}

			return false;
		},
		Close: function (event, reopen, activeItem)
		{
			if (Citra.Pages.ActiveItem === null)
				return;


			if (reopen)
				$("#text_page .content").stop().animate({ 'clip': 'rect(260px, 272px, 260px, 272px)', opacity: 0 }, 500).animate({ 'clip': 'rect(0px 544px 520px 0px)', opacity: 1 }, 500, null, function ()
				{
					Citra.Pages.ActiveItem = activeItem;
				});
			else
				$("#text_page .content").stop().animate({ 'clip': 'rect(260px, 272px, 260px, 272px)', opacity: 0 }, 500);
		}
	},
	Navigation:
	{
		Navigate: function (targetPage)
		{
			if (targetPage != "")
			{
				Citra.Pages.Close();
				if (targetPage.substring(0, 1) != "!")
					return;
				targetPage = targetPage.substring(1);

				$.get('/webservices/ContentLoader',
					targetPage,
					function (data, textStatus, jqXHR)
					{
						switch (jqXHR.status)
						{
							case 200:
								$("div#mainGallery").html("<ul class='slider'><div class=\"previous\"></div><div class=\"next\"></div><\/ul>");
								$("div#mainGallery").next(".switcher").remove();
								for (var i = 0; i < data.length; i++)
								{
									$("div#mainGallery ul").append("<li><a href='#'><img src='" + data[i].Href + "' alt='" + data[i].AlternateText + "' width='" + data[i].Width + "' height='" + data[i].Height + "' \/><\/a>" + (data[i].AlternateText != "" ? "<div class=\"caption\">" + data[i].AlternateText + "</div>" : "") + "<\/li>");
								}
								$("div#mainGallery").find("div.caption").css("display", "none");
								$("div#mainGallery").slideView({ uiPreviousNext: true, width: 540, height: 473, easeFunc: null });
								break;
							case 204:
								$("div#mainGallery").html("<div class='noitems'><\/div>");
								$("div#mainGallery").next(".switcher").remove();
								break;
							default:
								break;
						}
					});
			}
		},
		Toggle: function (event)
		{
			if (!$(this).parent().hasClass("active"))
			{
				if ($(this).siblings("ul").length == 0)
					Citra.Navigation.GoTo($(this).attr('href'));
				$(this).parent().addClass("active");
				$(this).siblings("ul").first().animate({ 'height': '20px', 'opacity': '1' }, 250);
			}
			else
			{
				if ($(this).siblings("ul").length == 0)
				{
					$(this).parent().removeClass("active");
				}
				else
				{
					$(this).siblings("ul").first().animate({ 'height': '0px', 'opacity': '0' }, 250, null, function (event)
					{
						$(this).parent().removeClass("active");
					});
				}
			}
			return false;
		},
		GoTo: function (page)
		{
			window.location.hash = '#!' + page;
		}
	}
};
