var newsInterval;
function news_scroll(bUp) {
	try
	{
		clearInterval(autoInterval);
	}
	catch (e)
	{
	}
	
	var oNewsItems = document.getElementById("news-items");
		var oChildrens = $(oNewsItems).children();
	newsInterval = setInterval(function() {

		if (bUp && oNewsItems.scrollTop < (oChildrens.length-1)*80)
			oNewsItems.scrollTop += 2;
		else if (!bUp && oNewsItems.scrollTop > 0)
			oNewsItems.scrollTop -= 2;
		else
			clearInterval(newsInterval);

	},20);
}

function SendShortContactForm() {
	var oName = document.getElementById("sc_sName"),
		oEmail = document.getElementById("sc_sEmail"),
		oContent = document.getElementById("sc_sContent");

	var aFields = [oName, oEmail, oContent];
	for (var i = 0; i < aFields.length; i++) {
	    if (aFields[i].value.replace(/\s/gi, "") == "") {
	        if (location.href.indexOf("/he/") > -1)
	             alert("חובה למלא את כל השדות");
	        else
	             alert("there are empty fields");
	       
	        return false;
	    }
	}

	var sq;
	if (oEmail.value.indexOf("@") > -1) {
	    sq = "sEmail=" + escape(oEmail.value);
	} else {
	sq = "sPhone=" + escape(oEmail.value);
	}
	

	$.ajax({
	    type: "POST",
	    cache: false,
	    url: "getXML.aspx",
	    dataType: "xml",
	    data: "sFunc=SendContactForm&obj=oMail&sFullName=" + escape(oName.value) + "&sMessage=" + escape(oContent.value) + "&" + sq,
	    success: function(xml) {
	        if (location.href.indexOf("/he/") > -1)
	            alert("הטופס נשלח בהצלחה, תודה על פנייתך");
	        else
	            alert("we got your request, thank you");
	        location.href = location.href;
	    }
	});
}

function MenuOut(oCurrentEl,oEvent) {
    var oEl = window.addEventListener ? oEvent.relatedTarget : oEvent.toElement;

        var bFound = false;
        $(oEl).parents().andSelf().each(function() {
            if (this == oCurrentEl) {
                bFound = true;
                return false;
            }
        });

        if (!bFound) {
            setTimeout(function() {
                $(oCurrentEl).children('.submenu:first').css('display', 'none');
            }, 140);
        }

}

function ShowBreadCrumbs() {

	var sHome = location.href.indexOf("/he/") > -1 ? "מו\"פ ערדום" : "MOP Ardom";
	var sFloat = location.href.indexOf("/he/") > -1 ? "right" : "left";
	var sStart = location.href.indexOf("/he/") > -1 ? "he" : "en";

	var iPosToTake =  location.href.lastIndexOf(".aspx") > -1 ? location.href.lastIndexOf(".aspx") : location.href.length;
	var sUrl =  decodeURIComponent(location.href.substring(location.href.lastIndexOf("/")+1, iPosToTake).replace(/\.aspx/gi,"")).replace(/\_/gi," ");

	document.write("<div class=\"breadcrumbs\" style=\"float:"+sFloat+";margin-"+sFloat+":3px;overflow:hidden;text-align:"+sFloat+"\"><a href=\"/"+sStart+"/\">"+sHome+"</a> &gt; <span style=\"font-weight:bold;\">" + sUrl+"</span></div><div style=\"clear:both;margin-bottom:-12px;\">&nbsp;</div>");
}

function auto_scroll() {

	var oNewsItems = document.getElementById("news-items");
	oNewsItems.onmouseover = function() {
		try	{clearInterval(autoInterval);}
		catch (e){}
	}

	var bUp = false;
	var bWait = false;
	var oChildrens = $(oNewsItems).children();
		
	autoInterval = setInterval(function() {
		if (!bWait)
		{
			if (bUp && oNewsItems.scrollTop < (oChildrens.length-1)*80)
				oNewsItems.scrollTop += 1;
			else if (!bUp && oNewsItems.scrollTop > 0)
				oNewsItems.scrollTop -= 1;
			else {
				bUp = !bUp;
				bWait = true;
				setTimeout(function() {bWait = false},3400);
			}
		}
	},30);	
}

function news_scrollstop() {
	try {clearInterval(newsInterval);}
	catch (e){}
}

var GlobalSlider = {bInAction:false}
function Slider(sID,iDuration) {
	var This = this;
	//this.iMaxHeight = iHeight || 0;
	this.iDuration = iDuration || 400;
	this.oElOpened = null;
	this.sElToSlide = sID;
	this.sEasePlugin = "swing";
	this.bOneActionAtTime = false;
	this.OpenPanel = function(oEl) {

		if (!GlobalSlider.bInAction || !This.bOneActionAtTime)
		{
			GlobalSlider.bInAction = true;
			setTimeout(function() { GlobalSlider.bInAction = false },This.iDuration+20);
			if (oEl != This.oElOpened)
			{
				if (This.oElOpened)
					$(This.oElOpened).next().animate({ height: "0px" },This.iDuration,This.sEasePlugin);
				$(oEl).next().animate({ height: This.iMaxHeight+"px" },This.iDuration,This.sEasePlugin);

				This.oElOpened = oEl;
			} else {
				//$(This.oElOpened).next().animate({ height: "0px" },This.iDuration,This.sEasePlugin);
				//This.oElOpened = null;
			}
		}
	}

	this.Apply = function() {

		var oElToSlide = $("#"+This.sElToSlide);
		var oItems = oElToSlide.find(".panelitem");

		this.iMaxHeight = oElToSlide.get(0).offsetHeight+4 - ((oItems.get(0).offsetHeight+1)*oItems.length);
		oItems.each(function() {
			this.onclick = function() {
				This.OpenPanel(this);
			}
		});
			
		if (oItems.length) {
			This.oElOpened = oItems.get(0);
			$(This.oElOpened).next().css("height", This.iMaxHeight+"px").css("display","block");
			
		}
	}
}

function ContactValidation() {
	var aAllFields = "sFullName,sEmail,sPhone,sMessage".split(",");
	var aReq = "sFullName,sEmail".split(",");
	var bErrros = false;

	for (var i=0;i<aReq.length;i++)
	{
		if (document.getElementById(aReq[i]).value.replace(/\s/gi,"") == "")
		{
			bErrros = true;
			break;
		}
	}

	if (bErrros)
	{
		if (location.href.indexOf("/he/") > -1)
			alert("ישנם שדות חובה שלא מולאו");
		else
			alert("there is empty required fields");
	} else {
		var aFields = [];
		for (var i=0;i<aAllFields.length;i++)
			aFields.push(aAllFields[i]+"="+escape(document.getElementById(aAllFields[i]).value));
		
	   $.ajax({
		   type: "POST",
		   cache:false,
		   url: "getXML.aspx",
		   dataType:"xml",
		   data: "sFunc=SendContactForm&obj=oMail&"+aFields.join("&"),
		   success: function(xml){
				if (location.href.indexOf("/he/") > -1)
					alert("הטופס נשלח בהצלחה, תודה על פנייתך");
				else
					alert("we got your request, thank you");
				location.href = location.href;
		   }
	   });

	}
}
