var OnAction = false;
var Ajax;
var SiteUrl = "http://www.sheep.co.il/";
var time  = 10;
function e(element)
{
	return document.getElementById(element);
}

function GetData(File,Action) {
	OnAction = true;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		Ajax = new XMLHttpRequest();
		if (Ajax.overrideMimeType) {
			Ajax.overrideMimeType('text/html;');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try {
				Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){ }
		}
	}
	if (!Ajax) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	Ajax.onreadystatechange = function () { Handler(Action); }
	Ajax.open('GET', File, true);
	Ajax.send(null);
}

function Handler(Action) {
	if (Ajax.readyState == 4 && Ajax.status == 200) {
		if (Ajax.responseText == "E")
			alert("ארעה שגיאה אנא נסה מאוחר יותר. אם שגיאה זו חוזרת אנא דווח לנו.");
		else
		 	Action();
		Ajax = null;
		OnAction = false;
	}
}

function Search(location)
{
	if (e("search").value != "")
		window.location = SiteUrl+location+"/search/1/"+e("search").value+".html";
}

function Vote(type,id,vote,div)
{
	if (type == 'poll')
		for (i=0;i<document.forms['pollform']['option'].length;i++) {
			if (document.forms['pollform']['option'][i].checked) {
				vote = document.forms['pollform']['option'][i].value;
			}
		}
	var Action = function()
	{
		if (Ajax.responseText == "A")
		{
			alert("דירוגך כבר קיים במערכת");
		}
		else
		{
			alert("תודה. דירוגך נקלט במערכת.");
			e(div).innerHTML = Ajax.responseText;
		}
	}
	GetData(SiteUrl+"ajax.php?a=v&t="+type+"&i="+id+"&v="+vote,Action);
}
function Results(id,div)
{
	var Action = function()
	{
		e(div).innerHTML = Ajax.responseText;
	}
	GetData(SiteUrl+"ajax.php?a=v&t=results&i="+id,Action);
}
function ShowUsers(ac)
{
	var Action = function()
	{
		e('users').innerHTML = Ajax.responseText;
	}
	GetData(SiteUrl+"ajax.php?a=users&g="+ac,Action);
}

function SwitchVideo(videoId)
{
	var Action = function()
	{
		e('video').innerHTML = Ajax.responseText;
	}
	GetData(SiteUrl+"ajax.php?a=swichvideo&i="+videoId,Action);
}

function Joke(page)
{
	var Action = function()
	{
		if (Ajax.responseText == "E")
		{
			alert("ארעה שגיאה אנא נסה מאוחר יותר. אם שגיאה זו חוזרת אנא דווח לנו.");
		}
		else
		{
			e("jokebox").innerHTML = Ajax.responseText;
		}
	}
	GetData(SiteUrl+"ajax.php?a=j&p="+page,Action);
}
function LastGames(page)
{
	var Action = function()
	{
		if (Ajax.responseText == "E")
		{
			alert("ארעה שגיאה אנא נסה מאוחר יותר. אם שגיאה זו חוזרת אנא דווח לנו.");
		}
		else
		{
			e("lastgames").innerHTML = Ajax.responseText;
		}
	}
	GetData(SiteUrl+"ajax.php?a=lastgames&page="+page,Action);
}
function Skip()
{
	e("preloader").style.display = "none";
	e("game").style.display = "block";
}

function Preload()
{
	if (time > 4)
		e("skip").style.display = "none";
	else
		e("skip").style.display = "";
		
	e("time").innerHTML = time;
	time = time - 1;
	if (time != -1)
		setTimeout(function() { Preload(); },1000);
	else
		Skip();
}

function Request(userid)
{
	var Action = function()
	{
		if (Ajax.responseText == "REQUEST")
		{
			e("req").innerHTML = "";
			alert("בקשתך לחברות נשלחה.");
		}
		if (Ajax.responseText == "FRIEND")
		{
			e("req").innerHTML = "";
			alert("החברות בינכם אושרה.");
		}
	}
	GetData(SiteUrl+"ajax.php?a=request&friend="+userid,Action);
}
function OverVote(num)
{
	for (var i=0; i < 5 ; i++)
		document.getElementById("vote_"+(i+1)).setAttribute("class", "vote_0");
	for (var i=0; i < num ; i++)
		document.getElementById("vote_"+(i+1)).setAttribute("class", "vote_1");
}
function OutVote()
{
	OverVote(original);
}

tinyMCE.init({
	// General options
	language: "he",
	mode : "exact",
	elements : "extended",
	theme : "advanced",
	skin : "o2k7",
	plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,autosave",

	// Theme options
	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
	theme_advanced_buttons5 : "insertdate,inserttime,preview,|,ltr,rtl,|,link,unlink,anchor,image,cleanup,help,code,|,print,|,fullscreen",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true
});

function LogOut()
{
	if(confirm("האם הינך בטוח שברצונך להתנתק מהמערכת?"))
	{
		window.location = SiteUrl+"users/logout.php";
	}
}
function OpenHide(id)
{
	if (e("message_"+id).style.display == "none")
	{
		e("message_"+id).style.display = "";
		UpdateUnrededMessages(id);
	}
	else
	{
		e("message_"+id).style.display = "none";
	}

}
function UpdateUnrededMessages(id)
{
	var Action = function()
	{
		if (Ajax.responseText == "OK")
		{
			e("newmessages").innerHTML = parseInt(e("newmessages").innerHTML)-1;
		}
	}
	GetData(SiteUrl+"ajax.php?a=messages&i="+id,Action);
}

function Fullscreen(game)
{
	window.open(SiteUrl+'games/fullscreen/'+game+'.html','GameWindow','width=800,height=600');
}
