
var _addSootiMsg = null;
var _cf = null;

HTMLElement.prototype.getOpacity = function() {
	var s = this.style;
	var o = s.opacity;
	if (o === null)
		return 0;
	return parseFloat(o, 10);
};

HTMLElement.prototype.setOpacity = function(o) {
	var s = this.style;
	var old = s.opacity;
	s.opacity = o;
	return old;
};

HTMLElement.prototype.fadein = function(t, d, start, end, callback) {
	if (start == null)
		start = 0;
	if (start >= 0)
		this.setOpacity(start);
	if (end == null)
		end = 1;
	var _e = this;
	var _i = setInterval(function() {
		var o = _e.getOpacity();
		o += d;
		if (o > 1)
			o = 1;
		_e.setOpacity(o);
		if (o >= end) {
			clearInterval(_i);
			if (callback)
				callback();
		}
	}, t);
};

HTMLElement.prototype.fadeout = function(t, d, start, end, callback) {
	if (start == null)
		start = 1;
	if (start >= 0)
		this.setOpacity(start);
	if (end == null)
		end = 0;
	var _e = this;
	var _i = setInterval(function() {
		var o = _e.getOpacity();
		o -= d;
		if (o < 0)
			o = 0;
		_e.setOpacity(o);
		if (o <= end) {
			clearInterval(_i);
			if (callback)
				callback();
		}
	}, t);
};

function e(s) {
	return document.getElementById(s);
}

function sugCloseButOver()	// BY ALI3d
{
	var but = e("sugCloseBut");
	but.style.color = "#990000";
	but.style.backgroundColor = "#777777";
}

function sugCloseButOut()	// BY ALI3d
{
	var but = e("sugCloseBut");
	but.style.color = "#FFFFFF";
	but.style.backgroundColor = "#555555";
}

var loaded = [];

function preload() {
	var link = e("LINK");
	link.rel = "stylesheet";
	link.type = "text/css";
	link.href = "/themes/default/ghooti.css";
	loaded.push(link);

	var image = e("IMG");
	image.src = "/themes/default/ghooti.jpg";
	loaded.push(image);
}

function l(nesfe) {
	_addSootiMsg = e("addSootiMsgDiv");
	_cf = e("cf");
	if (!nesfe) {
		var _addSootiButton = e("addSootiButton");
		_addSootiButton.focus();
	}

	// TODO
//	preload();
}

function hideAddSooti(_addSooti) {
	_addSooti.style.display = "none";
	e("addSootiButton").innerHTML = "يه سوتی جديد";
}

function showAddSooti(_addSooti) {
	_addSooti.style.display = "";
	e("addSootiSender").focus();
	e("addSootiButton").innerHTML = "بی خيال سوتی";
}

function toggleAddSooti() {
	var _addSooti = e("addSooti");
	if (_addSooti.style.display == "none") {
		var _search = e("search");
		if (_search.style.display != "none")
			toggleSearch();
		showAddSooti(_addSooti);
	}
	else {
		hideAddSooti(_addSooti);
	}
}

function hideSearch(_search) {
	_search.style.display = "none";
	e("searchButton").innerHTML = "جستجو";
}

function showSearch(_search) {
	_search.style.display = "";
	e("searchSender").focus();
	e("searchButton").innerHTML = "بی خيال جستجو";
}

function toggleSearch() {
	var _search = e("search");
	if (_search.style.display == "none") {
		var _addSooti = e("addSooti");
		if (_addSooti.style.display != "none")
			toggleAddSooti();
		showSearch(_search);
	}
	else {
		hideSearch(_search);
	}
}

function s() {
	var _x = e("addSootiSooti");
	var _y = e("addSootiCorrect");
	if (_y.value === "")
		_y.value = _x.value;
}

var _errorNumbers = ["اولا ", "حالا اون هيچی، ", "ثالثن "];

function showMsg() {
	if (_addSootiMsg.style.display == "none")
		_addSootiMsg.style.display = "";
	_addSootiMsg.setOpacity(1);
}

function clearMsg() {
	// TODO .removeChilds()???
	_addSootiMsg.innerHTML = "";
	_addSootiMsg.style.display = "none";
}

function clearErrors() {
	clearMsg();
	_errorNumber = 0;
}

function setTitle(_title, _class) {
	clearErrors();
	showMsg();
	var _div = document.createElement("DIV");
	if (_class === null)
		_class = "title";
	_div.className = _class;
	_div.align = "center";
	_div.innerHTML = _title;
	_addSootiMsg.appendChild(_div);
}

function loading() {
	clearErrors();
	setTitle("لطفا صبر کنيد...");
}

function setErrorTitle(_title) {
	setTitle(_title, "errorTitle");
}

var _errorNumber = 0;

function addError(_error, _dontUseNum) {
	var _e = "";
	if (!_dontUseNum)
		_e = _errorNumbers[_errorNumber++];
	_e = _e + _error;
	showMsg();
	var _div = document.createElement("DIV");
	_div.className = "errorMsg";
	_div.align = "center";
	_div.innerHTML = _e;
	_addSootiMsg.appendChild(_div);
}

function ch() {
	loading();

	var _form = e("sootiForm");
	_form.action = "add_js.php";
//	var _from = e("addSootiFrom").value;
	var _sender = e("addSootiSender").value;
	var _sooti = e("addSootiSooti").value;
	var _correct = e("addSootiCorrect").value;
	
	var _errors = [];
	
//	if (_from == "")
//		_errors.push("نگفتي صاحب سوتي کي بود!");
	if (_sender === "")
		/*_errors.push_back("")*/;
	if (_sooti === "")
		_errors.push("خود سوتي چي شد؟!!");
	if (_correct === "")
		_errors.push("منظورش از اين سوتي چي بود؟");
	
	if (_errors.length > 0) {
		setErrorTitle(_errors.length + " خطا رخ داد:");
		if (_errors.length == 1)
			addError(_errors[0], true);
		else {
			for (var _index in _errors) {
				var _error = _errors[_index];
				addError(_error);
			}
		}
		return false;
	}
	
	return true;
}

function ch2() {
	var _iframe = e("hif");
	var _d = _iframe.contentWindow.document;
	var _status = _d.getElementById("status").innerHTML;
	if (_status == "Error") {
		var _count = _d.getElementById("num").innerHTML;
		setErrorTitle(_count + " خطا رخ داد:");
		var _errors = _d.getElementsByTagName("DIV");
		var _dontUseNum = false;
		if (_count == 1)
			_dontUseNum = true;
		for (var _i = 0; _i < _errors.length; _i++) {
			var _error = _errors[_i];
			if (_error.getAttribute("NAME") != "error")
				continue;
			addError(_error.innerHTML, _dontUseNum);
		}
	}
	else if (_status == "Success") {
		setTitle("دست شما درد نکنه! اين سوتی هم اضافه شد! اگر يه مدت صبر کنيد، احتمالا اون رو توی ليست سوتی ها می بينيد!");
		toggleAddSooti();
		var _form = e("sootiForm");
		_form.reset();
		var _addSootiButton = e("addSootiButton");
		_addSootiButton.innerHTML = "بازم يه سوتي ديگه";
		_addSootiButton.focus();
		setTimeout(function() { _addSootiMsg.fadeout(30, 0.04); }, 2500);
	}
}



function showMsg2() {
	var _addSootiMsg2 = e("sug_title");
	if (_addSootiMsg2.style.display == "none")
		_addSootiMsg2.style.display = "";
}

function clearMsg2() {
	var _addSootiMsg2 = e("sug_title");
	// TODO .removeChilds()???
	_addSootiMsg2.innerHTML = "";
	_addSootiMsg2.style.display = "none";
}

function clearErrors2() {
	clearMsg2();
	_errorNumber2 = 0;
}

function setTitle2(_title, _class) {
	var _addSootiMsg2 = e("sug_title");
	clearErrors2();
	showMsg2();
	var _div = document.createElement("DIV");
	if (_class === null)
		_class = "title";
	_div.className = _class;
	_div.align = "center";
	_div.innerHTML = _title;
	_addSootiMsg2.appendChild(_div);
}

function loading2() {
	clearErrors2();
	setTitle2("لطفا صبر کنيد...");
}

function setErrorTitle2(_title) {
	setTitle2(_title, "errorTitle");
}

var _errorNumber2 = 0;

function addError2(_error, _dontUseNum) {
	var _addSootiMsg2 = e("sug_title");
	var _e = "";
	if (!_dontUseNum)
		_e = _errorNumbers[_errorNumber2++];
	_e = _e + _error;
	showMsg2();
	var _div = document.createElement("DIV");
	_div.className = "errorMsg";
	_div.align = "center";
	_div.innerHTML = _e;
	_addSootiMsg2.appendChild(_div);
}

function sug(_e) {
	page_dis(_e);
	var _div = e("sug_table");
	_div.style.display = "";
	var _input = e("sugName");
	_input.focus();
}

function sug2() {
	loading2();
/*	var _div = e("sug_title");
	_div.innerHTML = "لطفا صبر کنيد...";
	_div.style.display = "";*/
	
	var _name = e("sugName").value;
	var _sug = e("sugSug").value;
	
	var _errors = [];
	
	if (_name === "")
		/*_errors.push("شما کی بودی؟!")*/;
	if (_sug === "")
		_errors.push("نظرتو نگفتی!");
	
	if (_errors.length > 0) {
		setErrorTitle2(_errors.length + " خطا رخ داد:");
		if (_errors.length == 1)
			addError2(_errors[0], true);
		else {
			for (var _index in _errors) {
				var _error = _errors[_index];
				addError2(_error);
			}
		}
		return false;
	}
	
	return true;
}

function sug3() {
	var _iframe = e("hif");
	var _d = _iframe.contentWindow.document;
	var _status = _d.getElementById("status").innerHTML;
	if (_status == "Error") {
		var _count = _d.getElementById("num").innerHTML;
		setErrorTitle2(_count + " خطا رخ داد:");
		var _errors = _d.getElementsByTagName("DIV");
		var _dontUseNum = false;
		if (_count == 1)
			_dontUseNum = true;
		for (var _i = 0; _i < _errors.length; _i++) {
			var _error = _errors[_i];
			if (_error.getAttribute("NAME") != "error")
				continue;
			addError2(_error.innerHTML, _dontUseNum);
		}
	}
	else if (_status == "Success") {
		sug_close();
		
		setTitle("نظر شما ثبت شد، از شما بسيار متشکريم!");
	}
}

function page_dis(_e) {
	var _x = e("disIMG");
	_x.style.display = "";
	window.scroll(1000, 0);
	var _b = document.body;
	_b.style.overflow = "hidden";
}

function page_en() {
	var _x = e("disIMG");
	_x.style.display = "none";
	var _b = document.body;
	_b.style.overflow = "auto";
}

function sug_close() {
	var _div = e("sug_table");
	_div.style.display = "none";

	clearErrors2();

	_cf.reset();
	
	page_en();
}

function cont() {
	var _page = e("page");
	var _b = e("b");
	var _table = document.createElement("TABLE");
	_page.style.display = "none";
	_b.appendChild(_table);
	_table.style.position = "absolute";
	_table.style.top = "0px";
	_table.style.left = "0px";
	_table.style.width = "100%";
	_table.style.height = "100%";
	_table.style.backgroundColor = "#999999";
//	_table.style.backgroundColor = "Blue";
	var _tbody = document.createElement("TBODY");
	_table.appendChild(_tbody);
	var _tr = document.createElement("TR");
	_tbody.appendChild(_tr);
	var _td = document.createElement("TD");
	_td.align = "center";
	_tr.appendChild(_td);

	var _div2 = document.createElement("DIV");
	_div2.style.width = "400px";
	_div2.style.height = "300px";
	_div2.style.backgroundColor = "#DDDDDD";
	_td.appendChild(_div2);

	var _cf = e("cf");
	_cf.style.display = "";
	_div2.appendChild(_cf);
}

function u(id) {
	loading();
	var _form = e("sootiForm");
	var _sender = e("sender" + id).value;
	var _sooti = e("sooti" + id).value;
	var _correct = e("correct" + id).value;
	_form.action = "update_js.php";
	_form._id.value = id;
	_form.sender.value = _sender;
	_form.sooti.value = _sooti;
	_form.correct.value = _correct;
	
	_form.submit();
}

function u2(id) {
	setTitle("ورودی شماره " + id +  "با موفقيت بروزرسانی شد!");
}

function d(id) {
	if (confirm('مطمئن؟')) {
		loading();

		
	}
}

function toggleSokhan() {
	var _sokhan = e("sokhanTable");
	_sokhan.style.display = "";
}

function theme_mouseOver() {
	e("themeMenu").style.display = "block";
//	e("themeMenu").style.visibility = "visible";
	e("themeMenuArrow").color = "#eeeeee";
}

function theme_mouseOut() {
	e("themeMenu").style.display = "none";
//	e("themeMenu").style.visibility = "hidden";
	e("themeMenuArrow").color = "#111111";
}

function changeTheme(name) {
//	document.cookie = "theme=" + name + "; expires=Mon, 27 Feb 2007 12:01:10 GMT; path=/";
	document.cookie = "theme=" + name + "; path=/";
	e("stylesheet").href = "/themes/" + name + "/ghooti.css";
	e("headerImage").src = "/themes/" + name + "/ghooti.jpg";
	
}

function err(_e) {
	alert("An error occured:\n" + e);
}

//window.onerror = err;

