var xml=false;
var version = navigator.userAgent;
var findIE6 = version.indexOf('MSIE 6');
var findIE7 = version.indexOf('MSIE 7');
function makeXml(){
   var temp=false;
   if (window.XMLHttpRequest){
      try {
         temp = new XMLHttpRequest();
      } catch(e) {
         temp = false;
      }
   } else if(window.ActiveXObject){
      try{
         temp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
         try{
            temp = new ActiveXObject("Msxml2.HTMLHTTP");
         } catch(e) {
            temp = false;
         }
      }
   }
   return temp;
}
function xmlRequest(method, url, toSend,handler){
   xml = makeXml();
   if(xml){
      xml.open(method, url, true);
      xml.setRequestHeader("content-type","application/x-www-form-urlencoded");
      if(handler){
         xml.onreadystatechange = handler;
      }
      if(method == "GET"){
         xml.send(null);
      }else{
         xml.send(toSend);
      }
   }else{
      alert("Your browser does not seem to support XMLHttpRequest.");
   }
}
function updateLocation(){
   xmlRequest("post","http://www.vins.co.il/index.php?action=ajax","place=global&requested=updatelocation");
}
window.onload = function(){
   window.setInterval(updateLocation,280000);
   if(document.getElementById("objectSpan")){
      document.getElementById("objectSpan").className = "shown";
      document.getElementById("loadingSpan").className = "hidden";
   }
}
function createCookie(name,value,days) {
   if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
   }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
   var nameEQ = name + "=";
   var ca = document.cookie.split(";");
   for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==" ") c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return "";
}
function setToFavorites(title,url){
   if (window.sidebar)
      window.sidebar.addPanel(title, url, "");
   else if(window.opera && window.print){
      var elem = document.createElement("a");
      elem.setAttribute("href",url);
      elem.setAttribute("title",title);
      elem.setAttribute("rel","sidebar");
      elem.click();
   }else if(document.all)
      window.external.AddFavorite(url, title);
}
var lastSearchType="searchByGoogle";
function changeSearchType(to){
   if(to != lastSearchType){
      document.getElementById(lastSearchType).className = "searchbuttonoff";
      document.getElementById(to).className = "searchbuttonon";
      lastSearchType = to;
      document.getElementById("searchTypeInput").value = to;
   }
   if (to == "searchByGoogle") {
   		document.getElementById("GoogleFields").innerHTML = 
        '<input type="hidden" name="cx" value="partner-pub-7469287236839067:jcjb80jdiu4" />'+
  		'<input type="hidden" name="cof" value="FORID:9" />'+
    	'<input type="hidden" name="ie" value="windows-1255" />'+
    	'<!--<script type="text/javascript" src="http://www.google.co.il/coop/cse/brand?form=cse-search-box&amp;lang=iw"></script>-->';
		document.getElementById("searchInput").name = 'q';
	}else{
		document.getElementById("GoogleFields").innerHTML = '';
		document.getElementById("searchInput").name = 'search';
	}
}

var FavoriteType = null;
function AddToFavorites(id,type){
	FavoriteType = type;
   xmlRequest("post","http://www.vins.co.il/index.php?action=ajax","place=global&requested=addFavorite&type="+type+"&id="+id,AddToFavoritesFB);
}

function AddToFavoritesFB(){
	if(xml.readyState == 4){
		if(xml.status == 200){
			type = FavoriteType;
			var code = xml.responseText;
			if (code == "LOGIN") {
				alert("רק משתמשים רשומים יכולים להשתמש באפשרות זו");
			}else if (code == "EXIST") {
				if (type == "game") {
					alert("המשחק קיים במועדפים שלך");
				}else if (type == "funny") {
					alert("התמונה קיימת במועדפים שלך");
				}else if (type == "download") {
					alert("ההורדה קיימת במועדפים שלך");
				}else if (type == "video") {
					alert("הסרטון קיים במועדפים שלך");
				}
			}else if (code == "ADDED") {
				if (type == "game") {
					document.getElementById("remmemberDiv").innerHTML = "המשחק נשמר במועדפים";
				}else if (type == "funny") {
					document.getElementById("remmemberDiv").innerHTML = "התמונה נשמרה במועדפים";
				}else if (type == "download") {
					document.getElementById("remmemberDiv").innerHTML = "ההורדה נשמרה במועדפים";
				}else if (type == "video") {
					document.getElementById("remmemberDiv").innerHTML = "הסרטון נשמר במועדפים";
				}
			}else{
				alert(code);
			}
			FavoriteType = null;
		}
	}
}


function reportUsHandler(){
   if(xml.readyState == 4){
      if(xml.status == 200){
         document.getElementById("reportUs").innerHTML = xml.responseText;
      }
   }
}
function reportUs(where,number,messege){
   xmlRequest("post","http://www.vins.co.il/index.php?action=ajax","place=global&requested=report&where="+where+"&number="+number+"&messege="+messege,reportUsHandler);
}
function moveToSearch(){
   if(document.getElementById('searchInput').value != "" &&  document.getElementById("searchTypeInput").value != "searchByGoogle"){
      location.href='http://www.vins.co.il/search/'+document.getElementById('searchTypeInput').value+'/1/vote/'+document.getElementById('searchInput').value;
	  return false;
   }
	return true;
}
function openFunc(domain) {
   location.href = domain;
}
function setHomePage() {
   document.body.style.behavior='url(#default#homepage)';
   document.body.setHomePage(window.location.href);
}
function log_out(B){var A=document.getElementsByTagName("html")[0];A.style.filter="progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";if(confirm(B)){return true}else{A.style.filter="";return false}}

function facebook (){
	window.open ("http://www.facebook.com/vins.co.il");
}

// New Version -  Comments Games And Music Clips
function elm(Id) {
	return document.getElementById(Id);
}

var CommentsGameId = null;
function AddComment(GameId){
	CommentsGameId = GameId;
	var comment = elm("comment").value;
	xmlRequest("post","http://www.vins.co.il/index.php?action=ajax","place=game&requested=add_comment&comment="+comment+"&game="+GameId,CommentFeedBack);
}

function ShowComments() {
	if ((xml.readyState == 4) && (xml.status == 200)) 
		elm("comments").innerHTML = xml.responseText;
}

function GetComments(GameId,Page){
	elm("comments").innerHTML = '<div class="cloading"><img src="http://www.vins.co.il/style/loading.gif" alt="טוען..." /></div>';
	if (Page == undefined)
		Page = 1;
	xmlRequest("post","http://www.vins.co.il/index.php?action=ajax","place=game&requested=comments&GameId="+GameId+"&Page="+Page,ShowComments);
}

function CommentFeedBack() {
	if ((xml.readyState == 4) && (xml.status == 200)) {
		var code = xml.responseText;
		if (code == "OK") {
			elm("comment").value = "";
			elm("comment").disabled = "disabled";
			elm("comment").style.cssText  = "background:#f6f6f6";
			elm("csend").disabled = "disabled";
			GetComments(CommentsGameId);
			CommentsGameId = null;
		}else{
			if (code =="LOGIN") {
				alert("עלייך להיות מחובר לאתר וינס כדי להוסיף תגובה.");
			}else if (code =="TOOSHORT") {
				alert("תגובתך קצרה מדי, כתוב תגובה ארוכה יותר ונסה שנית.");
			}else if (code =="TOOLONG") {
				alert("תגובתך ארוכה מדי, כתוב תגובה הכוללת פחות מילים ונסה שוב.");
			}else if (code =="BADWORD") {
					elm("comment").disabled = "disabled";
					elm("csend").disabled = "disabled";
			}else if (code =="LONGWORD") {
					elm("comment").disabled = "disabled";
					elm("csend").disabled = "disabled";
			}else if (code =="TIME") {
				alert("יש להמתין מספר דקות בין פירסום תגובות.");
			}else if (code =="ISLAST") {
				alert("לא ניתן להגיב פעמיים ברצף.");
			}else if (code =="BANNED") {
				alert("חשבונך נחסם מכיוון שעברת על חוקי האתר, אנא פנה למנהל האתר לבירורים נוספים.");
			}else if (code =="HTML") {
				elm("comment").disabled = "disabled";
				elm("comment").style.cssText  = "background:#f6f6f6";
				elm("csend").disabled = "disabled";
				alert("תגובה לא חוקית.");
			}else if (code =="RCHAR") {
				alert("התגובה מכילה מילים ארוכות מדי.");
			}else{
				alert(code);
			}
		}
	}
}