var Custom = {

	Settings: {
		MainImageInterval: 8,
		NewsSliderInterval: 7,
		NewsSliderSpeed: 0.5,
		ItemBigOpen: 0.3,
	},

	MenuInterval: null,
	LastHoveredEl: null,
	OutMenu: function(oEl) {

				$(oEl).removeClass("hover");
				if ($(oEl).hasClass("first"))
					$(oEl).removeClass("hover-first");
				else if (getIndex(oEl) != 1)
					$(oEl).removeClass("hover-small");

	},

    LogOut: function() {
        $.ajax({
            type: "POST",
            url: "getXML.aspx",
            dataType: "xml",
            data: "sFunc=LogOut&obj=Admin_Login",
            success: function(xml) {
                location.href = location.href;
            }
        });
    },

	InitMenu: function() {
		$("#lstMainMenu").children().bind("mouseover", function() {

			try {clearInterval(Custom.MenuInterval);}
			catch (e) {}

			if (Custom.LastHoveredEl == this && $(this).hasClass("hover"))
				return;

			$("#lstMainMenu").children().each(function() {
				Custom.OutMenu(this);
			});

			$(this).addClass("hover");
			if ($(this).hasClass("first"))
				$(this).addClass("hover-first");
			else if (getIndex(this) != 1)
				$(this).addClass("hover-small");

			Custom.LastHoveredEl = this;

		}).bind("mouseout", function() {

			try {clearInterval(Custom.MenuInterval);}
			catch (e) {}

			Custom.MenuInterval = setInterval(function() {
				clearInterval(Custom.MenuInterval);
				$("#lstMainMenu").children().each(function() {
					Custom.OutMenu(this);
				});
			},100);

		});

		var oMenuProductList = $("#menu-product-list").find("LI");
		Custom.BigMenuItem.Init();
	},

	ContactForm: {

		Validate: function() {

			var bErrors = false;
			var oInputs = $("#contact-form").find("INPUT.required").filter(function() {
				if ($(this).val().replace(/\s/gi,"") == "")
				{
					bErrors = true;
					return false;
				}
			});

			if (bErrors)
			{
				alert("There are missing required fields");
				return;
			}

            $.ajax({
                type: "POST",
                cache: false,
                url: "/he/getXML.aspx",
                dataType: "xml",
                data: "obj=MailSender&sFunc=SendContactForm&" + $("#contact-form").serialize(),
                success: function(xml) {

					location.href = "/thankyou.aspx";

                }
            });			

		}
	},

	BigMenuItem: {

		Page: 1,
		PageCount: 0,
		SliderWidth: 191,
		SelectItem: function(iPage) {

			var iLastPage = Custom.BigMenuItem.Page;
			if (!iPage) iPage = Custom.BigMenuItem.Page;
			Custom.BigMenuItem.Page = iPage;

			var oSlider = $("#product-list-slider").get(0);

			

			Custom.Tween((iLastPage-1)*Custom.BigMenuItem.SliderWidth*-1,(iPage-1)*Custom.BigMenuItem.SliderWidth*-1,function(event) {
				var iPos = event.target._pos;
				oSlider.style.marginLeft = iPos+"px";
			},null,Custom.Settings.ItemBigOpen);

		},

		Next: function() {
			
			if (Custom.BigMenuItem.Page > (Custom.BigMenuItem.PageCount-5))
			{
				var oLIs = $("#product-list-slider").find("LI");
				oLIs.filter(":first").insertAfter(oLIs.filter(":last"));
				Custom.BigMenuItem.Page--;
			}
			Custom.BigMenuItem.SelectItem(Custom.BigMenuItem.Page+1);

		},

		Prev: function() {
			if (Custom.BigMenuItem.Page == 1)
			{
				var oLIs = $("#product-list-slider").find("LI");
				oLIs.filter(":last").insertBefore(oLIs.filter(":first"));
				Custom.BigMenuItem.Page++;
			}
			Custom.BigMenuItem.SelectItem(Custom.BigMenuItem.Page-1);
		},

		Init: function() {

			Custom.BigMenuItem.PageCount = $("#product-list-slider").find("LI").length;

			if (Custom.BigMenuItem.PageCount > 5)
			{
				$("#product-list-prev").css("visibility","visible");
				$("#product-list-next").css("visibility","visible");
			}

		}

	},

	/*
		Tween.regularEaseIn
		Tween.regularEaseOut
		Tween.regularEaseInOut
		Tween.strongEaseIn
		Tween.strongEaseOut
		Tween.strongEaseInOut
		Tween.backEaseOut
		Tween.backEaseIn
		Tween.backEaseInOut
		Tween.bounceEaseOut
		Tween.bounceEaseIn
		Tween.bounceEaseInOut
		Tween.elasticEaseIn
		Tween.elasticEaseOut
		Tween.elasticEaseInOut
	*/

    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();
    },

	News: {
		Page: 1,
		PageCount: 0,
		ItemHeight: 0,
		ElContainer: null,
		ElToSlide: null,
		SlideInterval: null,
		bInAction: false,
		Next: function(bStopInterval) {

			if (bStopInterval)
				Custom.News.StopAutoSlide();

			if (Custom.News.bInAction)
				return;
			
			Custom.News.bInAction = true;
			setTimeout(function() {
				Custom.News.bInAction = false;
			},Custom.Settings.NewsSliderSpeed+500);

			if (Custom.News.Page == Custom.News.PageCount) {
				Custom.News.Page--;
				Custom.News.ElContainer.find("LI:first").insertAfter(Custom.News.ElContainer.find("LI:last"));
			}
				
			Custom.Tween(Custom.News.ItemHeight*(Custom.News.Page-1),Custom.News.ItemHeight*Custom.News.Page,function(event) {
				var iPos = event.target._pos;
				$(Custom.News.ElToSlide).css("marginTop",(iPos*-1)+"px");
			},null,Custom.Settings.NewsSliderSpeed);

			Custom.News.Page++;
			
		},
		Prev: function(bStopInterval) {

			if (bStopInterval)
				Custom.News.StopAutoSlide();

			if (Custom.News.bInAction)
				return;

			Custom.News.bInAction = true;
			setTimeout(function() {
				Custom.News.bInAction = false;
			},Custom.Settings.NewsSliderSpeed+500);

			if (Custom.News.Page == 1) {
				Custom.News.Page++;
				Custom.News.ElContainer.find("LI:last").insertBefore(Custom.News.ElContainer.find("LI:first"));
			}
				
			Custom.Tween(Custom.News.ItemHeight*(Custom.News.Page-1),Custom.News.ItemHeight*(Custom.News.Page-2),function(event) {
				
				var iPos = event.target._pos;
				$(Custom.News.ElToSlide).css("marginTop",(iPos*-1)+"px");

			},null,Custom.Settings.NewsSliderSpeed);



			Custom.News.Page--;

		},
		StartAutoSlide: function() {
			Custom.News.SlideInterval = setInterval(Custom.News.Next,Custom.Settings.NewsSliderInterval*1000);
		},
		StopAutoSlide: function() {
			try {clearInterval(Custom.News.SlideInterval);}
			catch (e) {}
		},
		Init: function() {
			var oEl = $("#lstNewsItems");
			Custom.News.PageCount = oEl.find("LI").length;
			if (Custom.News.PageCount)
				Custom.News.ItemHeight = oEl.get(0).offsetHeight;
			
			Custom.News.ElContainer = oEl;
			Custom.News.ElToSlide = oEl.find("UL:first");
			Custom.News.StartAutoSlide();
		}
	},

	CenteredBoxOptions: {
		Page: 1,
		PageCount: 0,

		SelectItem: function(iPage) {

			var iLastPage = Custom.CenteredBoxOptions.Page;
			if (!iPage) iPage = Custom.CenteredBoxOptions.Page;
			Custom.CenteredBoxOptions.Page = iPage;

			var oBuxaChilds = $("#centeredbuxacontainer").children();
			oBuxaChilds.filter(":eq("+(iLastPage-1)+")").fadeOut(200);
			oBuxaChilds.filter(":eq("+(iPage-1)+")").fadeIn(200);
			$("#lstCenteredBuxa").children().removeClass("selected").filter(":eq("+(iPage-1)+")").addClass("selected");

		},

		Next: function() {
			Custom.CenteredBoxOptions.SelectItem(Custom.CenteredBoxOptions.Page == Custom.CenteredBoxOptions.PageCount ? 1 : Custom.CenteredBoxOptions.Page+1);
		},

		Prev: function() {
			Custom.CenteredBoxOptions.SelectItem(Custom.CenteredBoxOptions.Page == 1 ? Custom.CenteredBoxOptions.PageCount : Custom.CenteredBoxOptions.Page-1);
		},

		Init: function() {
			Custom.CenteredBoxOptions.PageCount = $("#lstCenteredBuxa").children("LI").bind("click",function() {
				Custom.CenteredBoxOptions.SelectItem(getIndex(this)+1);
			}).length;
			
			var oBuxaChilds = $("#centeredbuxacontainer").children();
			oBuxaChilds.filter(":first").css("display","block");
			$("#lstCenteredBuxa").children(":first").addClass("selected");

		}
	},


	RightBoxOptions: {
		Page: 1,
		PageCount: 0,
		SliderWidth: 238,
		SelectItem: function(iPage) {

			var iLastPage = Custom.RightBoxOptions.Page;
			if (!iPage) iPage = Custom.RightBoxOptions.Page;
			Custom.RightBoxOptions.Page = iPage;

			var oSlider = $("#rightboxslider").get(0);

			Custom.Tween((iLastPage-1)*Custom.RightBoxOptions.SliderWidth*-1,(iPage-1)*Custom.RightBoxOptions.SliderWidth*-1,function(event) {
				var iPos = event.target._pos;
				oSlider.style.marginLeft = iPos+"px";
			},null,Custom.Settings.NewsSliderSpeed);			

		},

		Next: function() {

			if (Custom.RightBoxOptions.Page == Custom.RightBoxOptions.PageCount)
			{
				$("#rightboxslider").children(":first").insertAfter($("#rightboxslider").children(":last"));
				Custom.RightBoxOptions.Page--;
			}

			Custom.RightBoxOptions.SelectItem(Custom.RightBoxOptions.Page+1);
			Custom.RightBoxBottom.Next();

		},

		Prev: function() {

			if (Custom.RightBoxOptions.Page == 1)
			{
				$("#rightboxslider").children(":last").insertBefore($("#rightboxslider").children(":first"));
				Custom.RightBoxOptions.Page++;
			}

			Custom.RightBoxOptions.SelectItem(Custom.RightBoxOptions.Page-1);
			Custom.RightBoxBottom.Prev();
		},

		Init: function() {

			Custom.RightBoxOptions.PageCount = $("#rightboxslider").find(".productimage").length;
			if (Custom.RightBoxOptions.PageCount <= 1)
			{
				$("#right-box-arrowleft").css("display","none");
				$("#right-box-arrowright").css("display","none");
			}


			Custom.RightBoxBottom.Init();

		}
	},

	RightBoxBottom: {
		Page: 1,
		PageCount: 0,
		SliderWidth: 290,
		SelectItem: function(iPage) {

			var iLastPage = Custom.RightBoxBottom.Page;
			if (!iPage) iPage = Custom.RightBoxBottom.Page;
			Custom.RightBoxBottom.Page = iPage;

			var oSlider = $("#lstHomny").get(0);
			Custom.Tween((iLastPage-1)*Custom.RightBoxBottom.SliderWidth*-1,(iPage-1)*Custom.RightBoxBottom.SliderWidth*-1,function(event) {
				var iPos = event.target._pos;
				oSlider.style.marginLeft = iPos+"px";
			},null,Custom.Settings.NewsSliderSpeed);			

		},

		Next: function() {

			if (Custom.RightBoxBottom.Page == Custom.RightBoxBottom.PageCount)
			{
				$("#lstHomny").children(":first").insertAfter($("#lstHomny").children(":last"));
				Custom.RightBoxBottom.Page--;
			}

			Custom.RightBoxBottom.SelectItem(Custom.RightBoxBottom.Page+1);
			

		},

		Prev: function() {

			if (Custom.RightBoxBottom.Page == 1)
			{
				$("#lstHomny").children(":last").insertBefore($("#lstHomny").children(":first"));
				Custom.RightBoxBottom.Page++;
			}

			Custom.RightBoxBottom.SelectItem(Custom.RightBoxBottom.Page-1);
		},

		Init: function() {
			Custom.RightBoxBottom.PageCount = $("#lstHomny").children().length;
		}
	},

	MainImage: {
		Banners: null,
		Switch: function() {

			var iSelectedIndex = getIndex(Custom.MainImage.Banners.filter(function() {
				return $(this).css("display") == "block";	
			}).get(0));
			var iToShow = iSelectedIndex+1;

			if (iSelectedIndex == (Custom.MainImage.Banners.length-1))
				iToShow = 0;

			Custom.MainImage.Banners.filter(":eq("+iSelectedIndex+")").fadeOut(1200);
			Custom.MainImage.Banners.filter(":eq("+iToShow+")").fadeIn(1200);

		},

		Init: function() {
			Custom.MainImage.Banners = $("#banner-container").find(".banner");
			if (Custom.MainImage.Banners == null || Custom.MainImage.Banners.length == 0)
			    return;
			Custom.MainImage.Banners.filter(":first").css("display","block");
			setInterval(Custom.MainImage.Switch, Custom.Settings.MainImageInterval*1000);
		}
	},
    Product: {

		Filter: function(source, sCat) {
		    $(source).siblings().removeClass("selected");
		    $(source).addClass("selected");
		    
			Custom.Tween(0,170,function(event) {
				var iPos = event.target._pos;
				$("#product-list-slider").css("marginTop",(iPos*-1)+"px");
		    },Tween.regularEaseOut,0.15);

            setTimeout(function(event) { 
                $("#menu-product-list li").css("display","none");
		        var oItems=$(sCat=='Default'?"#menu-product-list li":"#menu-product-list li[data-"+sCat+"='true']");
		        $(oItems).css("display","block");
		        $(oItems).each(function () {
    		        var oItem=this;
	    	        $(oItem).find("a").each(function (index) {
		                var sLink = $(this).attr("href");
						var aLink = sLink.split('/');
						if (aLink.length<4)
							aLink.push('');
						aLink[aLink.length - 1] = sCat!='Default'? sCat : $(oItem).attr('data-default');
						sLink = aLink.join('/');
						$(this).attr("href", sLink);
		            });
		        });

	            Custom.Tween(170,0,function(event) {
			        var iPos = event.target._pos;
			        $("#product-list-slider").css("marginTop",(iPos*-1)+"px");
	            },Tween.regularEaseOut,0.15);
            },700);
		    
		}
	},


	InitTooltip: function() {
        
		var sHtml = "<div id=\"tooltip\" class=\"tooltip-element\"><div class=\"top\"></div><div class=\"middle\" id=\"tooltip-info\"></div><div class=\"bottom\"></div></div>";
		$(document.body).append(sHtml);
	
		$(".tooltip").bind("mouseover",function() {
			$("#tooltip").css({"display":"block","left":(getLeft(this)-20)+"px"});
			$("#tooltip").css("top",(getTop(this) - $("#tooltip").get(0).offsetHeight)+"px");
			$("#tooltip-info").html($(this).attr("title"));

			if ($(this).attr("href") == "#")
			{
				$(this).bind("click",function() {
					return false;
				});
			}

		}).bind("mouseout",function() {
			$("#tooltip").css("display","none");
		});
		
	},

	LoginCheck: function() {

		$.ajax({
			type: "POST",
			cache: false,
			url: "/he/getXML.aspx",
			dataType: "xml",
			data: "obj=LoginArea&sFunc=Login&" + $("#frmLogin").serialize(),
			success: function(xml) {

				var sUsername = $("#login-sUsername").val();
				var sPassword = $("#login-sPassword").val();
				if (sUsername.replace(/\s/gi,"") == "" || sPassword.replace(/\s/gi,"") == "")
				{
					$("#login-validation").css("color","red").html("* please fill username<br/> and password");
					return;
				}

				if ($(xml).find("succeed") && $(xml).find("succeed").text() == "true")
				{
					location.href='/en/membersarea.aspx';
				} else {
					$("#login-validation").css("color","red").html("* Username or password<br/>incorrect");
				}

			}
		});

	},

	forgotpassword: function() {

		var sUsername = $("#login-sUsername").val();
		var bEmail = /^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/.test(sUsername);

		if (sUsername.replace(/\s/gi,"") == "")
		{
			$("#login-validation").css("color","red").html("* Please fill your username");
		} else if (!bEmail)
		{
			$("#login-validation").css("color","red").html("* Username not valid");
		} else {
			$("#login-validation").css("color","red").html("");
			$.ajax({
				type: "POST",
				cache: false,
				url: "/he/getXML.aspx",
				dataType: "xml",
				data: "obj=LoginArea&sFunc=ForgotPassword&user="+sUsername,
				success: function(xml) {

					if ($(xml).find("succeed") && $(xml).find("succeed").text() == "true")
					{
						$("#login-validation").css("color","black").html("Password sent!");
						$("#login-sUsername").val("");


					} else {
						alert("Username not found or error occured while sending password, please try again later");
					}

				}
			});
		}

	},
			
	Sitemap: {
		Create: function() {
			$.ajax({
				type: "GET",
				url: "/sitemap/sitemap.xml",
				dataType: "xml",
				success: function(xml) {
						var sUrl,sPage, aPath, aItems = [];
						$(xml).find("loc").each(function () {
							sUrl=$(this).text().replace("http://www.beammed.com/","/").replace("http://beammed.com/","/");
							sPage = sUrl.split("/")[sUrl.split("/").length-1].split(".aspx")[0];
							Custom.Sitemap.RecursiveCreate(sUrl, aItems);

						});
						Custom.Sitemap.RecursiveShow(aItems,$("#sitemap"));
					}
			});
		},

		RecursiveCreate: function (sUrl,oObj) {
			aPath = sUrl.replace(/^\/+/,"").split('/');
			for (var i=0;i<aPath.length;i++)
			{
				if(typeof(oObj[aPath[i]]) == "undefined")
					oObj[aPath[i]] = [];
				oObj = oObj[aPath[i]];
			}
		},
		RecursiveShow: function (aItems,oObj) {
			for (var sItem in aItems)
			{
				if (sItem != "en" && sItem != "indexOf" && sItem != "insert" && sItem != "indexOfPartial" && sItem != "getPartialMatched")
				{
					var oNew=$("<li><a>/"+sItem+"</a><ul></ul></li>");
					$(oObj).append(oNew);
					var sLink = "/"+sItem;
					$(oNew).parents("li").each(function() { 
						sLink=$(this).children("a").text()+sLink;
					});
					$(oNew).children("a").attr("href",sLink);
					oNew = $(oNew).children("ul");
					Custom.Sitemap.RecursiveShow(aItems[sItem],oNew);
				}
			}
		}
	}

}


