var Custom = {

	Settings: {
		GallerySliderSpeed: 0.3
	},

    Tween: function(iFrom, iTo, fCallback, oTween, iSpeed) {
        tweenTA = new Tween(new Object(), 'prop', oTween || Tween.regularEaseOut, iFrom, iTo, iSpeed || 0.3);
        tweenTA.onMotionChanged = fCallback;
        tweenTA.start();
    },

	SendContactForm: function() {

		var bErrors = false;
		$("#contact-form").find(".requiredfield").each(function() {
			if ($(this).val().replace(/\s/gi,"") == "")
			{
				bErrors = true;
				return false;
			}
		});

		if (bErrors)
		{
			if ($(document.body).hasClass("direction-rtl"))
				alert("ישנם שדות חובה שלא מילאת");
			else
				alert("please fill all required fields");
			return;
		}

		$.ajax({
			type: "POST",
			url: "getXml.aspx",
			dataType: "xml",
			data: "obj=MailSender&sFunc=SendContactForm&" + $("#frmContactForm").serialize(),
			success: function(xml) {

				if ($(document.body).hasClass("direction-rtl"))
					alert("הטופס נשלח בהצלחה, תודה על פנייתך");
				else
					alert("Form sent successfully, thank you");

				
				location.href = location.href;
			}
		});

	},



	GalleryImage: {
		Page: 1,
		PageCount: 0,
		SliderWidth: 870,
		Direction: "ltr",
		SelectItem: function(iPage,bFast) {

			var iLastPage = Custom.GalleryImage.Page;
			if (!iPage) iPage = Custom.GalleryImage.Page;
			Custom.GalleryImage.Page = iPage;

			var oSlider = $("#gallery-slider").get(0);
			$("#gallery-items").children("IMG").each(function() {
				this.src = "/images/gallery-item"+ ((($(this).attr("data-page")*1)==Custom.GalleryImage.Page)?"-selected":"") +".png";
			})

			var iiPos = (iPage-1)*Custom.GalleryImage.SliderWidth*-1;

			if (bFast)
			{

					if (Custom.GalleryImage.Direction == "ltr")
						oSlider.style.marginLeft = iiPos+"px";
					else {
						oSlider.style.right = (iiPos*-1)+"px";
					}	

			} else {

				Custom.Tween((iLastPage-1)*Custom.GalleryImage.SliderWidth*-1,iiPos,function(event) {
					var iPos = event.target._pos;
					if (Custom.GalleryImage.Direction == "ltr")
						oSlider.style.marginLeft = iPos+"px";
					else {
						oSlider.style.right = (iPos*-1)+"px";
					}
				},null,Custom.Settings.GallerySliderSpeed);	

			}

			$("#gallery-items-prev").css("visibility","visible");
			$("#gallery-items-next").css("visibility","visible");
			
			if (Custom.GalleryImage.Page == Custom.GalleryImage.PageCount)
			{
				$("#gallery-items-next").css("visibility","hidden");
			} else if (Custom.GalleryImage.Page == 1)
			{
				$("#gallery-items-prev").css("visibility","hidden");
			}

		},

		Next: function() {
	
			/*
			{
				$("#gallery-slider").children(":first").insertAfter($("#gallery-slider").children(":last"));
				Custom.GalleryImage.Page--;
			}*/

			Custom.GalleryImage.SelectItem(Custom.GalleryImage.Page+1);
		},

		Prev: function() {
			/*
			{
				$("#gallery-slider").children(":last").insertBefore($("#gallery-slider").children(":first"));
				Custom.GalleryImage.Page++;
			}
			*/
			Custom.GalleryImage.SelectItem(Custom.GalleryImage.Page-1);
		},

		Init: function(options) {

			Custom.GalleryImage.Direction = $(document.body).hasClass("direction-rtl") ? "rtl" : "ltr";

			if (Custom.GalleryImage.Direction == "rtl")
			{
				$("#gallery-items-prev").html("<img src=\"/images/gallery-arrow-right.png\" alt=\"\" \/>").css({position:'relative',right:'8px'});
				$("#gallery-items-next").html("<img src=\"/images/gallery-arrow-left.png\" alt=\"\" \/>").css({position:'relative',left:'8px'});
			}


			Custom.GalleryImage.PageCount = $("#gallery-slider").children(".img").length;

			$("#gallery-items-prev").css("visibility","hidden");
			$("#gallery-items-next").css("visibility","hidden");

			if (Custom.GalleryImage.PageCount > 1)
				$("#gallery-items-next").css("visibility","visible");

			for (var i=0;i<Custom.GalleryImage.PageCount;i++)
				$("#gallery-items").append("<img data-page=\""+(i+1)+"\" src=\"/images/gallery-item"+(i==0?"-selected":"")+".png\" alt=\"\" \/>");

			if (options && options.Direction == "rtl")
				Custom.GalleryImage.Direction = "rtl";
			
			$("#gallery-items").children("IMG").bind("click",function() {
				var bFast = $(this).attr("fast") == "true";
				$(this).attr("fast","false");
				Custom.GalleryImage.SelectItem($(this).attr("data-page")*1,bFast);
			});

			if (location.href.indexOf("#"))
			{
				var sPageID = location.href.substring(location.href.indexOf("#")+1,location.href.length);
				
				if (!isNaN(sPageID))
				{
					var oImg = $("#gallery-slider").children("DIV[data-id='"+sPageID+"']");
					if (oImg.length)
					{
						var iIndex = getIndex(oImg.get(0)) / 2;
						$("#gallery-items").children("IMG:eq("+iIndex+")").attr("fast","true").trigger("click");
					}
					
				}
			}

		}
	},

	MenuSide: {

		oTween: null,
		oLastEl: null,
		bInAction: false,
		IntervalAction: null,
		ChildsCount: 0,

		SetImage: function(sSrc) {
			$("#MainImg").css("display","none").attr("src",sSrc).fadeIn(200);
		},

		Next: function() {
			if (Custom.MenuSide.bInAction)
				return;
			var oImagesStrip = $(".images-strip");
			var oDirSide = $(document.body).hasClass("direction-rtl") ? "Right" : "Left";
			
			Custom.MenuSide.IntervalAction = setInterval(function() {
				var oIma = (parseInt(oImagesStrip.css("margin"+oDirSide).replace(/px/gi,"")))-3;
				oImagesStrip.css("margin"+oDirSide,oIma+"px");
				
				var oDis = Custom.MenuSide.ChildsCount*94*-1+116+5;
				if (oIma < oDis)
				{
					oImagesStrip.css("margin"+oDirSide,oDis+"px");
					clearInterval(Custom.MenuSide.IntervalAction);
				}
			},4);
		},

		Prev: function() {
			if (Custom.MenuSide.bInAction)
				return;
			var oImagesStrip = $(".images-strip");
			var oDirSide = $(document.body).hasClass("direction-rtl") ? "Right" : "Left";

			Custom.MenuSide.IntervalAction = setInterval(function() {
				var oIma = (parseInt(oImagesStrip.css("margin"+oDirSide).replace(/px/gi,"")))+3;
				oImagesStrip.css("margin"+ oDirSide,oIma+"px");
				if (oIma > 0)
				{
					oImagesStrip.css("margin"+ oDirSide,0+"px");
					clearInterval(Custom.MenuSide.IntervalAction);
				}
			},4);
		},

		Out: function() {
			try
			{
				clearInterval(Custom.MenuSide.IntervalAction);
				Custom.MenuSide.bInAction = false;
			}
			catch (e)
			{
			}
		},

		IntervalOut: null,
		OnHover: function() {

			try	{clearInterval(Custom.MenuSide.IntervalOut);}
			catch (e) {}

			$("#browser-frame").css("display","block");

		},
		OnOut: function() {

			try	{clearInterval(Custom.MenuSide.IntervalOut);}
			catch (e) {}
			Custom.MenuSide.IntervalOut = setInterval(function() {
				$("#browser-frame").css("display","none");
			},200);

		},

		PutElements: function(oEl) {

			$("#images-strip").html($(oEl).find(".images-list").html());

			var sRef = $(oEl).find("A:first").attr("href");
			


			$("#images-strip").find("IMG").each(function() {
				var This = this;
				$(this).prev().bind("click", function() {

					location.href = sRef+"#"+$(This).attr("data-id");
				});
			});

			var oDirSide = $(document.body).hasClass("direction-rtl") ? "Right" : "Left";

			$(".images-strip").css("margin"+oDirSide,"0px");
			Custom.MenuSide.ChildsCount = $("#images-strip").find("LI").length;
			Custom.MenuSide.oLastEl = oEl;

			if (Custom.MenuSide.ChildsCount < 2)
			{
				$("#pres-left-arrow").css("visibility","hidden");
				$("#pres-right-arrow").css("visibility","hidden");
			} else {
				$("#pres-left-arrow").css("visibility","visible");
				$("#pres-right-arrow").css("visibility","visible");
			}

		},
		Init: function() {
			
			$("#menu-side").bind("mouseover",function() {
				var oElEvent = window.addEventListener ? arguments[0].target : event.srcElement;
				if (oElEvent.id != "menu-side")
				{
					var oEl = null;
					if (oElEvent.parentNode.id == "menu-side")
					{
						oEl = oElEvent;
					} else {
						oEl = $(oElEvent).parents("LI.side-menu-item:first").get(0);
					}

					if (Custom.MenuSide.oLastEl == oEl)
						return;

					if (Custom.MenuSide.oTween)
						Custom.MenuSide.oTween.stop();
					
					Custom.MenuSide.PutElements(oEl);

					var iFrom, sFrom = $("#browser-frame").css("marginTop").replace(/px/gi,"");
					if (sFrom == "auto")
					{
						iFrom = 36;
					} else {
						iFrom = sFrom * 1;
					}

					Custom.MenuSide.oTween = new Tween(new Object(), 'prop', Tween.None, iFrom, getTop(oEl)-214+((oEl.offsetHeight-30)/2), 0.1);
					Custom.MenuSide.oTween.onMotionChanged = function(event) {
						var iPos = event.target._pos;
						$("#browser-frame").css("marginTop",iPos+"px");
					};

					Custom.MenuSide.oTween.start();
				}
			});

			var oFirst = $("#menu-side").children("LI:first").get(0);
			if (oFirst != null)
				Custom.MenuSide.PutElements(oFirst);

			var bRtl = $(document.body).hasClass("direction-rtl");
			if (!bRtl)
			{
				$("#pres-left-arrow").bind("mouseover",Custom.MenuSide.Prev);
				$("#pres-right-arrow").bind("mouseover",Custom.MenuSide.Next);
			} else {
				$("#pres-left-arrow").bind("mouseover",Custom.MenuSide.Next);
				$("#pres-right-arrow").bind("mouseover",Custom.MenuSide.Prev);
			}



		}
	}

	

}


