var Keam = {
    Direction: "Rtl", // default direction of keam
    oElShowed: null,
    //BarInterval: null,
    SubMenuInterval: null,

    SetDirection: function(sDirection) {
        document.body.className += " Site" + sDirection;
        Keam.Direction = sDirection;
    },

    BarHover: function(oEl, oEvent) {
        var sourceElement = window.addEventListener ? oEvent.target : event.srcElement;
		$(oEl).find(".dd-menu").css("display","block"); //("display","block");
    },

    IsCurrentMenu: function(oEl) {
        var tElement = oEl;
        var bFound = false;
        for (var i = 0; i < 4; i++) {
            if (tElement) {
                if (tElement.className) {
                    if (tElement.className == "PopupMenu")
                        tElement = $(tElement).prev().get(0);

                    if (tElement.className == "item") {
                        bFound = true;
                        break;
                    }
                }
            } else {
                bFound = false;
                break;
            }

            if (!tElement.parentNode)
                break;
            tElement = tElement.parentNode;
        }

        if (!bFound)
            tElement = oEl; 	//return to default

        if (tElement != Keam.oElShowed)
            return false;
        else
            return true;
    },

    BarOut: function(oEl, oEvent) {

		$(oEl).find(".dd-menu").css("display","none");

        //if (!Keam.IsCurrentMenu(tElement)) {
            //try { clearInterval(Keam.BarInterval); }
            //catch (e) { }



            // Keam.BarInterval = setInterval(function() {
            //   clearInterval(Keam.BarInterval);
           // Keam.HideMenu(oEl);
            // }, 170);

       // }
    },

    HideMenu: function(oEl) {
        if (typeof (oEl) == "undefined" || oEl == null)
            return;

        var oImages = oEl.getElementsByTagName("IMG");
        for (var i = 0; i < oImages.length; i++)
            oImages[i].src = oImages[i].src.replace(/\-hover\./gi, ".");
        var oSpan = oEl.getElementsByTagName("SPAN")[0];
        oSpan.style.background = "none";

        if (Keam.Direction == "Rtl") {
            oEl.style.background = "url(/images/bar/seperator.gif) 0px no-repeat";
            oEl.style.marginRight = "0px";
            oSpan.style.paddingRight = "27px";
        } else {
            oEl.style.background = "url(/images/bar/seperator.gif) 100% no-repeat";
            oEl.style.marginLeft = "0px";
            oSpan.style.paddingLeft = "27px";
        }

        if (oEl == Keam.oElShowed)
            Keam.oElShowed = null;

        var oPopup = $(oEl).next().get(0);
        if (oPopup && oPopup.className == "PopupMenu")
            oPopup.style.display = "none";
    },

    iIndexedViewed: 0,
    ShowInfo: function(oEvent) {
        var oEl = window.addEventListener ? oEvent.target : event.srcElement;
        if (oEl.tagName == "H2")
            oEl = oEl.parentNode;

        if (oEl.tagName == "LI") {
            var oLis = oEl.parentNode.getElementsByTagName("LI");
            for (var i = 0; i < oLis.length; i++) {
                if (oEl == oLis[i]) {
                    var oCurrent = $("#ServicesInfoContainer").children(":eq(" + Keam.iIndexedViewed + ")").get(0),
                        oNew = $("#ServicesInfoContainer").children(":eq(" + i + ")").get(0);

                    if ((oCurrent) && (oNew)) {
                        oCurrent.style.display = "none";
                        oNew.style.display = "block";
                        $("#ServicesAnchors").children().get(Keam.iIndexedViewed).className = "";
                        oEl.className = "selected";
                        Keam.iIndexedViewed = i;
                    }
                    break;
                }
            }
        }
    },

    PopupHover: function(oEvent) {
        var oEl = window.addEventListener ? oEvent.target : event.srcElement;
        if (oEl.tagName == "A")
            oEl.parentNode.className = "selected";

        //try { clearInterval(Keam.BarInterval); }
        //catch (e) { }

        try { clearInterval(Keam.SubMenuInterval); }
        catch (e) { }

    },
    PopupOut: function(oEl, oEvent) {

        var oElHovered = window.addEventListener ? oEvent.target : event.srcElement;
        if (oElHovered.tagName == "A")
            oElHovered.parentNode.className = "";

        var tElement = window.addEventListener ? oEvent.relatedTarget : event.toElement;

        if ((!Contains(oEl, tElement)) && (!Contains(Keam.oElShowed, tElement))) {
            Keam.SubMenuInterval = setInterval(function() {
                try { clearInterval(Keam.SubMenuInterval); }
                catch (e) { }

                Keam.HideMenu(Keam.oElShowed);
            }, 100);
        }
    },

    StarsInit: function(sStarsContID) {

        $("#" + sStarsContID).children().each(function() {
            this.setAttribute("OriginalSrc", this.src);
        });

        $("#" + sStarsContID).each(function() {
            this.onmouseover = function() {
                var oEvent = window.addEventListener ? arguments[0] : event;
                var oEl = window.addEventListener ? oEvent.target : oEvent.srcElement;
                var oImgs = $("#" + sStarsContID).get(0).getElementsByTagName("IMG");
                if (oEl.tagName == "IMG") {
                    var bNotEqual = false;
                    for (var i = 0; i < oImgs.length; i++) {
                        if (bNotEqual) {
                            oImgs[i].src = "/images/content/vote-stars/star4.gif";
                        } else {
                            if (oImgs[i] != oEl) {
                                oImgs[i].src = "/images/content/vote-stars/star3.gif";
                            } else {
                                bNotEqual = true;
                                oImgs[i].src = "/images/content/vote-stars/star3.gif";
                            }
                        }
                    }
                }
            }

            this.onmousedown = function() {
                var oEvent = window.addEventListener ? arguments[0] : event;
                var oEl = window.addEventListener ? oEvent.target : oEvent.srcElement;
                var oImgs = $("#" + sStarsContID).get(0).getElementsByTagName("IMG");
                if (oEl.tagName == "IMG") {
                    for (var i = 0; i < oImgs.length; i++) {
                        if (oImgs[i] == oEl) {
                            alert("You voted: " + (i + 1) + "!");
                            break;
                        }
                    }
                }
            }

            this.onmouseout = function() {
                var oEvent = window.addEventListener ? arguments[0] : event;
                var oEl = window.addEventListener ? oEvent.target : oEvent.srcElement;
                var oElTo = window.addEventListener ? oEvent.relatedTarget : oEvent.toElement;
                if (!Contains(oEl, oElTo)) {
                    var oImgs = $("#" + sStarsContID).get(0).getElementsByTagName("IMG");
                    for (var i = 0; i < oImgs.length; i++)
                        oImgs[i].src = oImgs[i].getAttribute("OriginalSrc");
                }
            }
        });

    },

    LinkTo: function(sUrl) {
        if (sUrl != "None")
            open(sUrl);
        else
            alert("אתר פנימי, לא ניתן לספק קישור");
    },

    InitMenu: function() {
        setTimeout(function() {

            $("#BarInner").find(".mainitem").each(function() {

				if ($(this).find(".middle").html().replace(/\s/gi,"").toLowerCase() != "<ul></ul>")
				{
					this.onmouseover = function() { Keam.BarHover(this, window.addEventListener ? arguments[0] : event); }
					this.onmouseout = function() { Keam.BarOut(this, window.addEventListener ? arguments[0] : event) }
				} else {
					$(this).find(".arrow1").remove();
				}
            });


        }, 100);
    },

    SendQuickContactForm: function() {
        var aFields = [["sName", "שם"], ["sContactWay", "טלפון / מייל"], ["sContent", "תוכן"]];
        for (var i = 0; i < aFields.length; i++) {
            if (document.getElementById(aFields[i][0]).value == aFields[i][1]) {
                alert("חובה למלא את כל השדות");
                return;
            }
        }
        document.getElementById("sContent").value += "\r\n\r\n\r\n\r\n\r\n<div style='text-align:left;direction:ltr;'><br/><br/>from:" + location.href + "</div>";
        document.getElementById("sContent").scrollTop = 0;
        document.getElementById("sContent").style.overflow = "hidden";
        alert("תודה על פנייתך");
        document.getElementById('frmContact').submit();
    },

    ArticlesShowDate: function(iID, sDate) {

        var arrMonths = ["jan","feb","mar","apr","may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];

        if (sDate.match(/\d{2}\/\d{2}\/\d{4}/gi)) {
            var iMonth = sDate.split("/")[1] * 1 - 1;
            var iDay = sDate.split("/")[0] * 1;

            document.getElementById("DateBox_Month_" + iID).style.background = "URL(/images/content/dates/months/" + arrMonths[iMonth] + ".png)";
            if ($.browser.msie && jQuery.browser.version.toString() == "6.0")
                document.getElementById("DateBox_Month_" + iID).style.filter = "_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/content/dates/months/" + arrMonths[iMonth] + ".png',sizingMethod='image');";
            document.getElementById("DateBox_Day_" + iID).src = "/images/content/dates/days/" + iDay + ".png";
        }
    },

	MiniPortfolio: {

		Items: [],

		SelectedItem: 0,
		InAction: false,
		ShowLogo: function(iLogo) {

			if (Keam.MiniPortfolio.InAction)
				return;

			Keam.MiniPortfolio.InAction = true;

			var oCurrentItem = Keam.MiniPortfolio.Items[iLogo];
			$('#MiniPortfolio_Progress').css("width","0px");

			setTimeout(function() {
				 $('#MiniPortfolio_Progress').animate({
					width: '260px'
				  }, 4000, function() {
					  Keam.MiniPortfolio.InAction = false;
					  if (Keam.MiniPortfolio.SelectedItem == Keam.MiniPortfolio.Items.length-1)
					  {
						  Keam.MiniPortfolio.ShowLogo(0);
					  } else {
						Keam.MiniPortfolio.ShowLogo(Keam.MiniPortfolio.SelectedItem+1);
					  }
					
				  });	
			},500);

			$("#logo-limit").children(":eq("+Keam.MiniPortfolio.SelectedItem+")").fadeOut(500).parent().children(":eq("+iLogo+")").fadeIn(500).css("cursor",oCurrentItem.Url ? "pointer" : "default").attr("obj_info","MiniPortfolio,"+oCurrentItem.Id).get(0).onclick = function() {
				if (oCurrentItem.Url)
					open(oCurrentItem.Url);	
			};

			var oEl = $("#MiniPortfolio_lstItems").children(":eq("+Keam.MiniPortfolio.SelectedItem+")").removeClass("selected").parent().children(":eq("+iLogo+")").addClass("selected");
		

			$("#MiniPortfolio_SiteName").html((oCurrentItem.Url ? "<a href=\""+oCurrentItem.Url+"\">" : "") + oCurrentItem.Description + (oCurrentItem.Url ? "</a>" : "")).css("display","none").fadeIn(500);
			
			$('#MiniPortfolio_Arrow').animate({
				left: (oEl.get(0).offsetLeft + oEl.get(0).offsetWidth / 2 + 1)+'px'
			  }, 300, function() {
					setTimeout(function() {
						Keam.MiniPortfolio.InAction = false; 
					},300);
			  });	


			

			Keam.MiniPortfolio.SelectedItem = iLogo;



		},

		Init: function() {

			if (Keam.MiniPortfolio.Items.length > 0)
			{
				$("#MiniPortfolio_lstItems").html(new Array(Keam.MiniPortfolio.Items.length+1).join("<li></li>")).get(0).onclick = function() {
					if (!Keam.MiniPortfolio.InAction)
					{
						$('#MiniPortfolio_Progress').stop();
						var oEl = window.addEventListener ? arguments[0].target : event.srcElement;
						Keam.MiniPortfolio.ShowLogo(getIndex(oEl));
					}
				};

				var aLogos = [];
				$.each(Keam.MiniPortfolio.Items,function(idx) {
					aLogos.push(this.Logo);
				});

				$("#logo-limit").append("<img src=\"" + aLogos.join("\" alt=\"\"/><img src=\"") + "\" alt=\"\"/>");
				Keam.MiniPortfolio.ShowLogo(0);

			}

		}

		

	}

}


onload = function() {
	$("#ServicesInfoGradiant").pngFix(); 
	$("#Footer").pngFix();
	/*
	if (!window.addEventListener)
	{
		$("#BarInner").children("DIV").children("IMG").each(function() {
			var oDiv = document.createElement("DIV");
			oDiv.className = "footer";
			$(oDiv).insertBefore(this);
			$(this).remove();
		});
	}
	*/
}
