var AjaxStarted = false;
var AjaxFIFO = new Array();

var str_you_voite 		= 'Спасибо! Ваш голос учтен!';
var str_already_voite 	= 'Вы уже проголосовали 1 раз!';

function AjaxInit() {
	var ajax_obj = false;
	/* Create a new XMLHttpRequest object to talk to the Web server */
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		ajax_obj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			ajax_obj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			ajax_obj = false;
		}
	}
	@end @*/

	if (!ajax_obj && typeof XMLHttpRequest != 'undefined') {
		ajax_obj = new XMLHttpRequest();
	}
	return ajax_obj;
}

function AjaxReq (url, target, ext_block) {
	ext_block = ext_block || null;
	xml_http = AjaxInit();
	xml_http.open("GET", url, true);
	xml_http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xml_http.send(null);
	xml_http.onreadystatechange = function () {
		if (xml_http.readyState == 4) {
			if (xml_http.status == 200) {
				text = xml_http.responseText;
				if (text) {
					document.getElementById(target).style.display = 'block';
					if (ext_block)
						document.getElementById(ext_block).style.display = 'block';
				}
				document.getElementById(target).innerHTML = text;
				doAjaxFIFO();
			}
		}
	}
}

function BannerAjaxReq (url, target, ext_block, width, height) {
	ext_block = ext_block || null;
	xml_http = AjaxInit();
	xml_http.open("GET", url, true);
	xml_http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xml_http.send(null);
	xml_http.onreadystatechange = function () {
		if (xml_http.readyState == 4) {
			if (xml_http.status == 200) {
				text = xml_http.responseText;
				if (text) {
					document.getElementById(target).style.display = 'block';
					if (ext_block)
						document.getElementById(ext_block).style.display = 'block';
				}
				document.getElementById(target).innerHTML 	= text;
				if(width) document.getElementById(target).style.width		= width;
				if(height) document.getElementById(target).style.height		= height;
				doAjaxFIFO();
			}
		}
	}
}

function AjaxEval (url) {
	xml_http = AjaxInit();
	xml_http.open("GET", url, true);
	xml_http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xml_http.send(null);
	xml_http.onreadystatechange = function () {
		if (xml_http.readyState == 4) {
			if (xml_http.status == 200) {
				eval(xml_http.responseText);
				doAjaxFIFO();
			}
		}
	}

}

function click (id) {
	AjaxEval(home() + "iframe.php?page=bzone_portal_banners&do=click&id="+id);
}

function openBan(url){
	if(!url) url = home();
	open(url, '_blank');
	return;
}

function addToAjaxFIFO (ev) {
	AjaxFIFO.push(ev);
}

function doAjaxFIFO() {
	if (AjaxFIFO.length > 0) {
		if(Gtimer) window.clearTimeout(Gtimer);
		expr = AjaxFIFO.shift();
		eval(expr);
	} else {
		Gtimer = window.setTimeout("doAjaxFIFO()", 1000);
	}
}

function ChangeBlock(url, target) {
	xml_http = AjaxInit();
	xml_http.open("GET", url, true);
	xml_http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xml_http.send(null);
	xml_http.onreadystatechange = function () {
		if (xml_http.readyState == 4) {
			if (xml_http.status == 200) {
				text = xml_http.responseText;
				document.getElementById(target).innerHTML = text;
				doAjaxFIFO();
			}
		}
	}
}

function changeImage(id, pid, url, clas) {
	addToAjaxFIFO("ChangeBlock('iframe.php?page=" + clas + "&do=getImg&id=" + id + "&pid=" + pid + "&url=" + url + "', 'image_ajax')");
	imgs = document.getElementById('gallery_rows').getElementsByTagName('div');
	for (i = 0; i < imgs.length; i++) {
		if(imgs[i].className.indexOf('clear') == -1) imgs[i].className = "l";
	}
	document.getElementById(clas + id).className = "l sel";
	doAjaxFIFO();
	return false;
}

function changeGal(id, pid, url, clas, tml) {
	addToAjaxFIFO("ChangeBlock('iframe.php?page=" + clas + "&do=gettextImg&id=" + id + "&pid=" + pid + "&url=" + url + "', '" + tml + "')");
	doAjaxFIFO();
	return false;
}

function addReiting (id, reiting) {
	addToAjaxFIFO("AjaxEval('iframe.php?page=content_rating&do=Add_rate&id=" + id + "&rate=" + reiting + "')");
	doAjaxFIFO();
/*	if(!already)
		window.alert(str_you_voite);
	else
		window.alert(str_already_voite);
	already = true;
	return false;
*/	
}


// JavaScript Document
/* for rating */
var defrates 	= new Object();
var rates 		= new Array();
var already		= false;
var Gtimer		= '';

function sratingset(id, num){
	ids 			= document.getElementById(id).getElementsByTagName('div');
	rates 		= new Array();
	for (i = 0; i < ids.length; i++) {
		rates[i] = ids[i].className;
		if(i < num) {
			ids[i].className 	= "star full";
			ids[i].style.cursor = 'pointer';
		} else {
			ids[i].className 	= "star empty";
		}
	}
	if(!defrates[id]) {
		defrates[id] = new Object(rates);
	}
	return;
}

function sratingnull(id){
	ids = document.getElementById(id).getElementsByTagName('div');
	if(defrates[id] && !already){
	   for (i = 0; i < ids.length; i++) {
	      ids[i].className = defrates[id][i];
	   }
	}
	return;
}


function ShowComments(){
	el = document.getElementById('content_comments');
	if(el.className == 'tab_close') el.className="tab_normal";
	else  el.className="tab_close";
	return false;
}
function GalleryChangeBlock(url, target) {
	xml_http = AjaxInit();
	xml_http.open("GET", url, true);
	xml_http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xml_http.send(null);
	xml_http.onreadystatechange = function () {
		if (xml_http.readyState == 4) {
			if (xml_http.status == 200) {
				text = xml_http.responseText;
				document.getElementById(target).innerHTML = text;
				doAjaxFIFO();
			}
		}
	}
}

function changeImage(id, pid, url) {
    addToAjaxFIFO("GalleryChangeBlock('"+document.getElementsByTagName('base')[0].href+"iframe.php?page=content_gallery&do=getImg&id=" + id + "&pid=" + pid + "&url=" + url + "', 'image_ajax')");
	imgs = document.getElementById('gallery_rows').getElementsByTagName('div');
	for (i = 0; i < imgs.length; i++) {
		if(imgs[i].className.indexOf('clear') == -1) imgs[i].className = "l";
	}
	document.getElementById('content_gallery' + id).className = "l sel";
	doAjaxFIFO();
	return false;
}
