/**
 * Common funcs
 *
 * @copyright Atarim Group LTD.
 * @version 2.0
 */

/** check Browsers **/
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isFF = (navigator.appName.indexOf("Netscape") != -1) ? true : false;

/** Used when processing AJAX responses */
var resultRegexp = /<(error|result)>(.*?)<\/(error|result)>/

/** Shortcut for document.getElementById - now widespread */
function $(objId) { return document.getElementById(objId); }

/** Create and return a new element
 * 	spawns a new element and attached it with properties
 *  basically, allows a shorter syntax for document.createElement
 *  
 *  @author Oded Idan
 *  @example syntax:
 *	var newDiv = getElement("DIV",
 *		{
 *			innerHTML : "I am the DIV's inner HTML",
 *			className : "I am the DIV's class",
 *			style : {
 *				backgroundColor : "#F00",
 *				border : "1px solid #FFFFFF"
 *			}
 *		}
 *	);
**/
function getElement(tag, attr) {
//var getElement = function(tag, attr) {
	var e = document.createElement(tag);
	for (i in attr) {
		switch (i) {
			case 'innerHTML':
				e.innerHTML = attr[i];
			break;
			case 'className':
			case 'class':
				e.className = attr[i];
			break;
			case 'style':
				for (j in attr[i]) {
					e.style[j] = attr[i][j];
				}
			break;
			default:
			e.setAttribute(i, attr[i]);
			break;
		}
	}
	return e;
}

/** Gets the position of an element relative to the document body
 *
 * @return object dimension
 */
function getRealOffset(element)
{
	var totalOffset = new Object();
	totalOffset.x = totalOffset.y = 0;

	do
	{
		totalOffset.x += element.offsetLeft;
		totalOffset.y += element.offsetTop;
		element = element.offsetParent;
	} while (element != null);

	return totalOffset;
}

/** For onmouseout events */
function getEventTo(evt)
{
	var toElement;
	if (window.event) toElement = window.event.toElement;
	else if (evt.relatedTarget) toElement = evt.relatedTarget;
	else return null;
	//opera assigns text nodes as toElements
	while (!toElement.tagName) toElement = toElement.parentNode;
	return toElement;
}
function getEventTarget(evt)
{
	if (window.event) return window.event.srcElement;
	return evt.target;
}
function getEventKeyCode(evt)
{
	// IE
	if(window.event) return window.event.keyCode;
	// Netscape/Firefox/Others
	if (evt.which) return evt.which;
	return false; // unknown
}

/** Courtesy of http://www.quirksmode.org/js/events_properties.html */
function getEventPos(evt)
{
	var pos = new Object();
	if (!evt) evt = window.event;
	if (evt.pageX || evt.pageY)
	{
		pos.x = evt.pageX;
		pos.y = evt.pageY;
	}
	else if (evt.clientX || evt.clientY)
	{
		pos.x = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		pos.y = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return pos;
}
/** Just stops event propagation. Surprisinglyl useful */
function stopPropagate(evt)
{
	(window.event) ? window.event.cancelBubble = true : evt.cancelBubble = true;
}

/** Simplified function for getting random numbers between (And including) min and max */
function getRand(min,max)
{
	max = max+1-min;
	return Math.floor(Math.random()*max)+min;
}

function setWindowCursorWait()
{
	document.documentElement.style.cursor = 'wait';
}
function setWindowCursorDefault()
{
	document.documentElement.style.cursor = 'default';
}
function setWindowCursor(cursorType)
{
	document.documentElement.style.cursor = cursorType;
}

/** A personal favorite of mine. Sets one element (DIV usually) display as 'none', the other 'block' */
function switchElements(elementId1,elementId2)
{
	$(elementId1).style.display = 'none';
	$(elementId2).style.display = 'block';
}

/** The by now famous substituion for using POST with links 
 * @param {string} args_string Should be http POST style (key=value&key2=value2)
 */ 
function doPostBack(args_string,action)
{
	if (!args_string) return;
	// create the form that will be sent
	var keyValuePair;
	var deForm = createForm();
	if (action) deForm.action = action;
	else deForm.action = window.location;
	var args = args_string.split('&');
	for (var i=0;i<args.length;i++)
	{
		keyValuePair = args[i].split('=');	
		appendHiddenToForm(deForm,keyValuePair[0],keyValuePair[1]);
	}
	// eventually - send the bastard
	deForm.submit();
}

// creates a form object and appends it to document. Then returns a reference to it
function createForm()
{
	var deForm = document.createElement("FORM");
	deForm.enctype = 'application/x-www-form-urlencoded';
	deForm.method='POST';
	// deForm.action= ''; // if you need it to go to another page
	document.body.appendChild(deForm);
	return deForm;
}

// appends a new hidden input field to a form
function appendHiddenToForm(deForm,deName,deValue)
{
	var deElement = document.createElement("INPUT");
	deElement.type = "hidden";
	deElement.name = deName;
	deElement.value = deValue;
	deForm.appendChild(deElement);
}

// implements maxlength for textarea
function checkMaxLength(evt,textarea,limit)
{
	if (textarea.value.length >= limit)
	{
		var keyCode= getEventKeyCode(evt);
		// non chars and delete are allowed
		if (!keyCode || keyCode == 8 || keyCode == 45) return true;
		return false;
	}
	return true;
}

/**  Allows only numeric values in a textfield
 * 
 * Parts of this are from http://www.w3schools.com/
 */
function checkDecimal(evt)
{
	var keyCode=false;
	if (window.event) // IE / Opera 9.0
		keyCode = window.event.keyCode;
	else if (evt.which) // Netscape/Firefox/Others
		keyCode = evt.which;

	// Some non chars and delete are allowed
	if (!keyCode || keyCode == 8 || keyCode == 45) return true;

	var keychar = String.fromCharCode(keyCode);
	var numcheck = /\d/;
	return numcheck.test(keychar);
}

// trim functions for text
function ltrim(str)
{
	return str.replace(/^\s*/g,"");
}
function rtrim(str)
{
	return str.replace(/\s*$/g,"");
}
function trim(str)
{
  return str.replace(/^\s*|\s*$/g,"");
}

/** Inserts html breaks before newlines */
function nl2br(text)
{
	return text.replace(/\n/gi,"<br />\n");
}
/** Turns all html breaks to newlines */
function br2nl(text)
{
	return text.replace(/<br(.\/)?>/gi,"\n");
}
/** Changes < > & and " to their HTML counterparts */
function htmlSpecialChars(text)
{
	function innerFunc(match)
	{
		switch (match)
		{
			case '<': return '&lt;';
			case '>': return '&gt;';
			case '"': return '&quot;';
			case '&': return '&amp;';
		}
	}
	return text.replace(/[&<>"]/g,innerFunc);
}
/** Undos &amp;,&gt;,&lt; and such for textareas/textfields */
function deHtmlSpecialChars(text)
{
	function innerFunc(match)
	{
		switch (match)
		{
			case '&lt;': return '<';
			case '&gt;': return '>';
			case '&quot;': return '"';
			case '&amp;': return '&';
		}
	}
	return text.replace(/(&amp;|&lt;|&gt;|&quot;)/g,innerFunc);;
}

function setShinHomePage(element)
{
	element.style.behavior = 'url(#default#homepage)';
	element.setHomePage("http://www.shin1.co.il/");
}

/**
 * For tables whose rows light up on mouseover
 * @param {string} tableId
 * @param {string} colorHighlight
 */
function RowLighter(tableId,colorHighlight)
{
	var me = this;

	this.colorHighlight = colorHighlight;

	this.table = $(tableId);
	if (this.table == null) return; // Element not found
	this.tableRows = this.table.getElementsByTagName('TR');
	this.rowColors = new Array(this.tableRows.length);
	// Assign event handlers to rows. Start from 1, to skip header row.
	for (var i=1;i<this.tableRows.length;i++)
	{
		this.tableRows[i].onmouseover = highlight;
		this.tableRows[i].onmouseout = dim;
		this.rowColors[i] = this.tableRows[i].style.backgroundColor;
	}

	/* ### Event handlers ### */

	function highlight(evt)
	{
		(window.event) ? window.event.cancelBubble = true : evt.cancelBubble = true;
		this.style.backgroundColor = me.colorHighlight;
	}
	function dim(evt)
	{
		(window.event) ? window.event.cancelBubble = true : evt.cancelBubble = true;
		for (var i=0;i<me.tableRows.length;i++)
		{
			if (me.tableRows[i] == this)
			{
				this.style.backgroundColor = me.rowColors[i];
				break;
			}
		}
	}	
}

/**
 * parse common date
 * @param string thedate
 * @param string thetime
 * @return string
 */
function parseDateTime(thedate, thetime) {
	if (thedate.indexOf('-') > 0) {
		thedate = thedate.split('-');
		thedate = thedate[1]+'/'+thedate[0]+'/'+thedate[2] + ' ';
	} else
		thedate = thedate + ' ';
	
	if (thetime.indexOf('AM') > 0)
		thedate = thedate + thetime.replace("AM", "");
	else {
		var thetimeArr = thetime.split(':');
		var hr = new Number(thetimeArr[0]);
		hr = hr + 12; //make evening
		thedate = thedate + hr + ':' + thetimeArr[1]; 
	}
	return thedate;
}

/**
 * SWFObject 1.5 http://blog.deconcept.com/swfobject/
 * SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
if(typeof deconcept=="undefined"){var deconcept=new Object();}if(typeof deconcept.util=="undefined"){deconcept.util=new Object();}if(typeof deconcept.SWFObjectUtil=="undefined"){deconcept.SWFObjectUtil=new Object();}deconcept.SWFObject=function(_1,id,w,h,_5,c,_7,_8,_9,_a){if(!document.getElementById){return;}this.DETECT_KEY=_a?_a:"detectflash";this.skipDetect=deconcept.util.getRequestParameter(this.DETECT_KEY);this.params=new Object();this.variables=new Object();this.attributes=new Array();if(_1){this.setAttribute("swf",_1);}if(id){this.setAttribute("id",id);}if(w){this.setAttribute("width",w);}if(h){this.setAttribute("height",h);}if(_5){this.setAttribute("version",new deconcept.PlayerVersion(_5.toString().split(".")));}this.installedVer=deconcept.SWFObjectUtil.getPlayerVersion();if(!window.opera&&document.all&&this.installedVer.major>7){deconcept.SWFObject.doPrepUnload=true;}if(c){this.addParam("bgcolor",c);}var q=_7?_7:"high";this.addParam("quality",q);this.setAttribute("useExpressInstall",false);this.setAttribute("doExpressInstall",false);var _c=(_8)?_8:window.location;this.setAttribute("xiRedirectUrl",_c);this.setAttribute("redirectUrl","");if(_9){this.setAttribute("redirectUrl",_9);}};deconcept.SWFObject.prototype={useExpressInstall:function(_d){this.xiSWFPath=!_d?"expressinstall.swf":_d;this.setAttribute("useExpressInstall",true);},setAttribute:function(_e,_f){this.attributes[_e]=_f;},getAttribute:function(_10){return this.attributes[_10];},addParam:function(_11,_12){this.params[_11]=_12;},getParams:function(){return this.params;},addVariable:function(_13,_14){this.variables[_13]=_14;},getVariable:function(_15){return this.variables[_15];},getVariables:function(){return this.variables;},getVariablePairs:function(){var _16=new Array();var key;var _18=this.getVariables();for(key in _18){_16[_16.length]=key+"="+_18[key];}return _16;},getSWFHTML:function(){var _19="";if(navigator.plugins&&navigator.mimeTypes&&navigator.mimeTypes.length){if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","PlugIn");this.setAttribute("swf",this.xiSWFPath);}_19="<embed type=\"application/x-shockwave-flash\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" style=\""+this.getAttribute("style")+"\"";_19+=" id=\""+this.getAttribute("id")+"\" name=\""+this.getAttribute("id")+"\" ";var _1a=this.getParams();for(var key in _1a){_19+=[key]+"=\""+_1a[key]+"\" ";}var _1c=this.getVariablePairs().join("&");if(_1c.length>0){_19+="flashvars=\""+_1c+"\"";}_19+="/>";}else{if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","ActiveX");this.setAttribute("swf",this.xiSWFPath);}_19="<object id=\""+this.getAttribute("id")+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" style=\""+this.getAttribute("style")+"\">";_19+="<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />";var _1d=this.getParams();for(var key in _1d){_19+="<param name=\""+key+"\" value=\""+_1d[key]+"\" />";}var _1f=this.getVariablePairs().join("&");if(_1f.length>0){_19+="<param name=\"flashvars\" value=\""+_1f+"\" />";}_19+="</object>";}return _19;},write:function(_20){if(this.getAttribute("useExpressInstall")){var _21=new deconcept.PlayerVersion([6,0,65]);if(this.installedVer.versionIsValid(_21)&&!this.installedVer.versionIsValid(this.getAttribute("version"))){this.setAttribute("doExpressInstall",true);this.addVariable("MMredirectURL",escape(this.getAttribute("xiRedirectUrl")));document.title=document.title.slice(0,47)+" - Flash Player Installation";this.addVariable("MMdoctitle",document.title);}}if(this.skipDetect||this.getAttribute("doExpressInstall")||this.installedVer.versionIsValid(this.getAttribute("version"))){var n=(typeof _20=="string")?document.getElementById(_20):_20;n.innerHTML=this.getSWFHTML();return true;}else{if(this.getAttribute("redirectUrl")!=""){document.location.replace(this.getAttribute("redirectUrl"));}}return false;}};deconcept.SWFObjectUtil.getPlayerVersion=function(){var _23=new deconcept.PlayerVersion([0,0,0]);if(navigator.plugins&&navigator.mimeTypes.length){var x=navigator.plugins["Shockwave Flash"];if(x&&x.description){_23=new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));}}else{if(navigator.userAgent&&navigator.userAgent.indexOf("Windows CE")>=0){var axo=1;var _26=3;while(axo){try{_26++;axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+_26);_23=new deconcept.PlayerVersion([_26,0,0]);}catch(e){axo=null;}}}else{try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");}catch(e){try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");_23=new deconcept.PlayerVersion([6,0,21]);axo.AllowScriptAccess="always";}catch(e){if(_23.major==6){return _23;}}try{axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash");}catch(e){}}if(axo!=null){_23=new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));}}}return _23;};deconcept.PlayerVersion=function(_29){this.major=_29[0]!=null?parseInt(_29[0]):0;this.minor=_29[1]!=null?parseInt(_29[1]):0;this.rev=_29[2]!=null?parseInt(_29[2]):0;};deconcept.PlayerVersion.prototype.versionIsValid=function(fv){if(this.major<fv.major){return false;}if(this.major>fv.major){return true;}if(this.minor<fv.minor){return false;}if(this.minor>fv.minor){return true;}if(this.rev<fv.rev){return false;}return true;};deconcept.util={getRequestParameter:function(_2b){var q=document.location.search||document.location.hash;if(_2b==null){return q;}if(q){var _2d=q.substring(1).split("&");for(var i=0;i<_2d.length;i++){if(_2d[i].substring(0,_2d[i].indexOf("="))==_2b){return _2d[i].substring((_2d[i].indexOf("=")+1));}}}return "";}};deconcept.SWFObjectUtil.cleanupSWFs=function(){var _2f=document.getElementsByTagName("OBJECT");for(var i=_2f.length-1;i>=0;i--){_2f[i].style.display="none";for(var x in _2f[i]){if(typeof _2f[i][x]=="function"){_2f[i][x]=function(){};}}}};if(deconcept.SWFObject.doPrepUnload){if(!deconcept.unloadSet){deconcept.SWFObjectUtil.prepUnload=function(){__flash_unloadHandler=function(){};__flash_savedUnloadHandler=function(){};window.attachEvent("onunload",deconcept.SWFObjectUtil.cleanupSWFs);};window.attachEvent("onbeforeunload",deconcept.SWFObjectUtil.prepUnload);deconcept.unloadSet=true;}}if(!document.getElementById&&document.all){document.getElementById=function(id){return document.all[id];};}var getQueryParamValue=deconcept.util.getRequestParameter;var FlashObject=deconcept.SWFObject;var SWFObject=deconcept.SWFObject;

/**
 * TopHeader functions
 */
/*docReady.push(function() {*/
divReady = function() {
	/*var bodyWidth = jQ('body').width();
	var header    = jQ('#mid_head').width();
	var left 	  = bodyWidth/2-header/2; 
	jQ('#mid_head').css('left' ,left);*/
	 
	openMyShin = 0;
	lockMyShin = 0;
	
	function slide(dir) {
		if(dir == 'Up') {
			if(lockMyShin == 1) {
				jQ('#slideIn_container').slideUp('fast' ,function () {
					openMyShin = 0;
					lockMyShin = 0;
				}); 
			}
		} else if (dir == 'Down') { 
			if (lockMyShin == 0)  {
				jQ('#slideIn_container').slideDown('fast' ,function() {
					openMyShin = 1; 
					lockMyShin = 1;
				});	
			}
		} else {
			if(openMyShin == 1) {
				slide('Up');
			} else {
				slide('Down');
			}	
			return;
		}
	}
		/** 
		 * alocation of the position of the link and allign the drop down with it. 
		 */
	jQ("#myshin").hover(function () {
		var offset  = jQ(this).find('a.menu_link').offset({ scroll: false });
		offset.left = offset.left + jQ(this).find('a.menu_link').width() - jQ('#slideIn_container').width();
		offset.top  = offset.top  + jQ(this).find('a.menu_link').height();
		jQ('#slideIn_container').css({'left':(offset.left)+'px' ,
									  'top' :(offset.top-2)+'px'});
		slide('Down');
	}, function () {
		slide('Up');
	});
	
	if (typeof shinLogoFile == "undefined") shinLogoFile = "shin";
	var shinLogo = new SWFObject("/static/images/topheader/"+shinLogoFile+".swf?v=1&clickTAG=/&clickTARGET=_self", "logo" ,"64" ,"64" ,"6" ,"transparent");
	shinLogo.addParam("wmode", "transparent");
	shinLogo.write('logoPlaceholder');
	shinLogo = null;
};

/**
 * previously "document.ready.js"
 */
docReady = [];
jQ(document).ready(function () {
	jQ.each(docReady ,function () {
		this.call();
	});
});


