/********** globals vars ***********/
var Cells=new Array;
var RefreshTimeout=300;
/* popCellMove hold the Drag object */
var popCellMove = null;
document.ondrag=new Function("return false");
document.domain="walla.co.il";
var scriptStr='';

/****** Checks the browser family ******/
function MyWallaCheckBrowser()
{
	var navFamily = navigator.userAgent.toLowerCase();
	if (navFamily.indexOf('msie')!=-1) {
		var ver = parseFloat( navFamily.substring(navFamily.indexOf('msie')+5,
			navFamily.length) );
		scriptStr='SCRIPT';
		navFamily = 'IE6';
	} else if (navFamily.indexOf('gecko')!=-1) {
		var ver = parseFloat( navFamily.substring(navFamily.indexOf('gecko')+6,
			navFamily.length) );
		scriptStr='script';
		navFamily = 'Gecko';
	} else {
		scriptStr='script';navFamily = 'Gecko';
	}
	
	return navFamily;
}
var MyWallaBrowser = MyWallaCheckBrowser();

function printErrorBrowser()
{
	document.open('text/html','replace');
	document.write("<h3>Your browser does not support</h3>");
	document.write("Use the following browsers:<ul>");
	document.write("<li>Gecko browsers which is equivalent with or newer than Mozilla 1.4/Netscape 7.1</li>");
	document.write("<li>Opera 7.5 or newer</li>");
	document.write("<li>Internet Explorer 6</li></ul>");
	document.close();
	return;		
}

/****** globals function******/
function emulateEventHandlers(eventNames) {
   for (var i = 0; i < eventNames.length; i++) {	
      document.addEventListener(eventNames[i], function (e) {
         window.event = e;
      }, true); // using capture
   }
}

if (MyWallaBrowser == 'Gecko') {
	emulateEventHandlers(["click"]);
}

//Can the string be used as variabel name?
String.prototype.isQualifiedVarName = function() {
	var re = /[a-zA-Z_]\w*/;
	var val = this.toString();
	var valid = true;
	var found = re.exec(val);
	if (!found) valid=false;
	else valid = (found[0].length==val.length);
	return valid;
};

function fixInt(i) {
	i = parseInt(i);
	return i.toString() == "NaN" ? 0 : i;
}

function Eve(e){
	var trigger='';
	if(e.srcElement) trigger= e.srcElement;
	else if(e.target) trigger= e.target;
	return trigger;
}

function getOffsets (e) {
  	if (MyWallaBrowser == 'Gecko'){
  
	  var target = Eve(e);
	  if (typeof target.offsetLeft == 'undefined') {
	    target = target.parentNode;
	  }
	  var pageCoords = getPageCoords(target);
	  var eventCoords = { 
	    x: window.pageXOffset + e.clientX,
	    y: window.pageYOffset + e.clientY
	  };
	  var offsets = {
	    offsetX: eventCoords.x - pageCoords.x,
	    offsetY: eventCoords.y - pageCoords.y
	  }
	  return offsets;
	}
	else if(MyWallaBrowser=='IE6')
	{
		var offsets = {
	    	offsetX: e.offsetX,
	    	offsetY: e.offsetY
		}
		return offsets;
	}
}

function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}

function inspect(obj) {
	var str = "";
	for (var prop in obj)
	{
		str += "obj." + prop + " = " + obj[prop] + "\n"
		
	}
	alert(str);
	
}


/***** tool-bar *****/
function showSetting(obj)
{	
	CleanPage();
	AddStuffOpen=false;
	if(typeof obj == 'object')
	{
		var trHtml,start,end;
		trHtml='<div align="center" style="border:1px solid '+MyMainTable.topColor[MyMainTable.selectedColor]+'"><table cellpadding="0" cellspacing="5" width="120px" border="0"><tr><td class="w2" align="right" colspan="4" style="padding-right:3px;" valign="top">מספר עמודות</td></tr><tr>';
		for(var i=0;i<4;i++)
		{
			trHtml+='<td align="center" style="cursor: pointer;background:white;" ><table align="center" width="20px" height="20px" ';
			if(MyMainTable.table.rows[0].cells.length==(i+1))trHtml+='style="border:1px solid green;color:green;" onmouseover="this.style.borderColor=\'green\';this.style.color=\'green\';return false;" onclick="MyMainTable.fixCols('+i+');CloseStyle(document.getElementById(\'tSetting\'));return false;"><tr><td align="center">'+(i+1)+'</td></tr></table></td>';
			else trHtml+='style="border:1px solid #cccccc;color:#489db9;" onmouseover="this.style.borderColor=\'green\';this.style.color=\'green\';return false;" onmouseout="this.style.borderColor=\'#cccccc\';this.style.color=\'#489db9\';return false;" onclick="MyMainTable.fixCols('+i+');CloseStyle(document.getElementById(\'tSetting\'));return false;"><tr><td align="center">'+(i+1)+'</td></tr></table></td>';
		}
		trHtml+='</tr><tr><td class="w2" align="right" colspan="4" style="padding-right:3px;" valign="top"><hr width="98%" size="1px" color="#cccccc" align="center"/>צבעים</td></tr>';
		for(var i=0;i<MyMainTable.setting.length;i++)
		{
			if(i%4==0){trHtml+='<tr>';start=true;end=false;}
			trHtml+='<td align="center" valign="top"><table cellpadding="0" cellspacing="0" border="1px" bordercolor="#cccccc" align="center" width="20px" height="20px" bgcolor="'+MyMainTable.setting[i]+'"  onclick="MyMainTable.fixColor('+i+');CloseStyle(document.getElementById(\'tSetting\'));return false;" style="cursor: pointer;"><tr><td>&nbsp;</td></tr></table></td>';
			//if(MyMainTable.selectedColor==i)trHtml+='o</td></tr>';
			//else trHtml+='&nbsp;</td>';
			if((i+1)%4==0){trHtml+='</tr>';end=true;start=false;}
		}
		if(end==true)trHtml+='</tr>';
		trHtml += '</table></div>';
		obj.innerHTML = trHtml;
		obj.onclick="window.event.cancelBubble = true;return true;";
		document.onclick=CleanPage;
		document.getElementById("tSetting").style.display = 'block';
	}
	return true;
}
/***** End tool-bar *****/


function CleanPage()
{
	CloseStyle(document.getElementById("tSetting"));
	CloseStyle(document.getElementById("topDiv"));
	document.onclick = null;
	return;
}

function CloseStyle(obj)
{
	obj.style.display = 'none';
}

var lastObj = null;
function OpenSetting(obj,oImg,rco)
{
	if(typeof obj == 'object')
	{
		if(obj.style.display == 'none')
		{
			if(lastObj!=null)lastObj.style.display='none';
			obj.style.display='block';
			lastObj=obj;
			document.onmouseup= function() {
				obj.style.display='none';
				document.onmouseup=new Function("return true");
				lastObj = null;
			};
			
		}
		else
		{
			obj.style.display='none';
			lastObj=null;
		}
	}
}

function showFlash(divName,obj) {
	var d=document.getElementById(divName);
	d.innerHTML=obj;
}

function openGame(url)
{
	window.open(url,"game_page","height=600, width=800,toolbar=0, location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
}

/***** cookie ********/

var today = new Date(); 
var zero_date = new Date(0,0,0); 
today.setTime(today.getTime() - zero_date.getTime()); 
var cookie_expire_date = new Date(today.getTime() + (60 * 60 * 24 * 90 * 1000)); 

function Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 

function Set_Cookie(name,value,expires,path,domain,secure) { 
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString;
	return(true); 
} 

function Delete_Cookie(name,path,domain) { 
   if (Get_Cookie(name)) document.cookie = name + "=" + 
      ( (path) ? ";path=" + path : "") + 
      ( (domain) ? ";domain=" + domain : "") + 
      ";expires=Thu, 01-Jan-70 00:00:01 GMT"; 
} 


/****** globals function END ******/