var cInd = new Array();
var fObj = new Array();

var ajaxConReq = null;
var curajaxConReq = 0;
var curProcImg = null;
var allCountClips = 0;

var cDid;
var cGid;
var cPos;
var isPending = 0;

var isAXObj = false;
isAXObj = window.ActiveXObject ? true : false;

function getAObj()
{
	var tmp = isAXObj == true ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	return tmp;
}

function getAllcount()
{
	var i = 0 ;
	for(var did in fObj)
	{
		for(var gid in fObj[did])
		{
			for(var pos in fObj[did][gid]) i++;
		}	
	}
	allCountClips = i;
}
		
function processPending()
{
	var i = 0;
	isPending = 0;
	
	if(curajaxConReq == 0) getAllcount();
	
	for(var did in fObj)
	{
		for(var gid in fObj[did])
		{
			for(var pos in fObj[did][gid])
			{
				i++;
				
				if(fObj[did][gid][pos]['status'] != 1)
				{
					isPending = 1;
					
					if(i > curajaxConReq)
					{
						curajaxConReq = i;
						try{
							curProcImg = document.images["i"+did+"_"+gid+"_"+pos];
						}catch(Exception)
						{
							curProcImg = null;
						}
															
						cDid = did;
						cGid = gid;
						cPos = pos;
						
						ajaxConReq = getAObj(); 
						
						ajaxConReq.onreadystatechange = function()
						{		
							if(ajaxConReq.readyState == 4)
							{
								if(ajaxConReq.status)
								{
									if(ajaxConReq.status == 200)	
									{
										fObj[cDid][cGid][cPos]['status'] = 1;
										
										if(curProcImg != null)
										{
											curProcImg.src = fObj[cDid][cGid][cPos]['src'];
											
											if(is_nav == true) curProcImg.title = fObj[cDid][cGid][cPos]['title'];	
											else curProcImg.alt = fObj[cDid][cGid][cPos]['title'];	
										}
									}
								}
								processPending();
							}
						}	
						
						ajaxConReq.open("GET",fObj[did][gid][pos]['src'], true);
						ajaxConReq.send(null);	
						
						return false;
					}	
				}
				
				if(i == allCountClips && isPending == 1)
				{
					curajaxConReq = 0;
					setTimeout("processPending()",5000);
				}
			}
		}
	}
}

var ajax = function()
{
	this.request = null;
	this.callback = null;
	this.cbparams = null;
	
	this.getData = function(path, toObj)
	{
		this.request = getAObj();
		var selfObj = this;
		
		this.request.onreadystatechange = function() 
		{
			if(selfObj.request.readyState == 4 && selfObj.request.status == 200)
			{
				if(typeof(selfObj.callback) == 'function') 
				{
					if(selfObj.callback(selfObj.request, selfObj.cbparams)) toObj.innerHTML = selfObj.request.responseText;
				}
				else toObj.innerHTML = selfObj.request.responseText;	
			}
		}
		
		try
		{
			this.request.open("GET", path, true);
			this.request.send(null);	
			this.loadingFunc(toObj);
		}
		catch(Exception){alert(Exception.message);}
	}
	
	this.postData = function(path, data, toObj)
	{
		this.request = getAObj();
		var selfObj = this;
		
		this.request.onreadystatechange = function() 
		{
			if(selfObj.request.readyState == 4 && selfObj.request.status == 200)
			{
				if(typeof(selfObj.callback) == 'function') 
				{
					if(selfObj.callback(selfObj.request, selfObj.cbparams)) toObj.innerHTML = selfObj.request.responseText;
				}
				else toObj.innerHTML = selfObj.request.responseText;	
			}
		}
		
		try
		{
			this.request.open("POST", path, true);
			this.request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.request.send(data);	
			this.loadingFunc(toObj);
		}
		catch(Exception){alert(Exception.message);}
	}
	
	this.loadingFunc = function(toObj)
	{
		toObj.innerHTML = '<div align="center" style="padding:20px"><b>Загрузка...</b></div>';
	}
}


function checkXmlErrors(o, callback)
{
    var err_mes = '';
    var tmp = o.responseXML.getElementsByTagName('error_messages');
    if(tmp.length > 0)
	{
		tmp = tmp[0];
		for(i=0;i<tmp.childNodes.length;i++) err_mes += (isAXObj == true ? tmp.childNodes[i].text : tmp.childNodes[i].textContent) + "\n";
		if(err_mes != '') 
		{
			alert(err_mes);
			if(callback && typeof(callback) == 'function') callback();
		}	
		return true;
	}	
	return false;
}
