var last = "";
var last2 = "";

var divs = 
{
	loadPic: function( id, url )
	{
		document.getElementById(id).src = url;
		divs.show("previwpiccont");
		divs.show("bigpiccont");
	},
		
	show: function( id )
	{
		var s = document.getElementById(id).style;
		
		//alert(id);
		//param(s);
				
		if( last2 != "" )
		{
			last2.display = 'none';
			last2 = "";
		}
				
		if (s.display == 'block')
		{
			s.display = 'none';
			last = "";
			//if( document.getElementById("bigpic") )
				//document.getElementById("bigpic").src = "/files/image/loading.png";
			//alert("!");
		}
		else 
		{
			if( last != "" )
			{
				if (last.display == 'block')
				{
					last.display = 'none';
				}
				else 
				{
					last.display = 'block';
				}
			}
		
			s.display = 'block';
			last = s;
		}
		s = null;
		
		return false;
	},
	
	show3: function( id )
	{
		var s = document.getElementById(id).style;
		
		//alert(id);
		//param(s);
				
		if (s.display == 'block')
		{
			s.display = 'none';
			last2 = "";
		}
		else 
		{
		if( last2 != "" )
		{
			if (last2.display == 'block')
			{
				last2.display = 'none';
			}
			else 
			{
				last2.display = 'block';
			}
		}
		
			s.display = 'block';
			last2 = s;
		}
		s = null;
		
		return false;
	},
	
	show2: function( id )
	{
		var s = document.getElementById(id).style;
		s.display = 'block';
		s = null;
		
		return false;
	},
	
	hide: function( id )
	{
		var s = document.getElementById(id).style;
		s.display = 'none';
		s = null;
		
		return false;
	},
		
	setClass: function( id, name )
	{
		document.getElementById(id).className=name;
	},
	
	selMenu: function(obj, url)
	{
		obj.src = url;
	},
	
	UselMenu: function(obj, url)
	{
		obj.src = url;
	},
	
	setLT: function( id, left, top, zindex )
	{
		var s = document.getElementById(id).style;
		s.position = "absolute";
		s.left = left+"px";
		s.top = top+"px";
		s.zIndex = zindex;
	},
	
	setRT: function( id, right, top, zindex )
	{
		var s = document.getElementById(id).style;
		s.position = "absolute";
		s.right = right+"px";
		s.top = top+"px";
		s.zIndex = zindex;
	},
	
	setRB: function( id, right, bottom, zindex )
	{
		var s = document.getElementById(id).style;
		s.position = "absolute";
		s.right = right+"px";
		s.bottom = bottom+"px";
		s.zIndex = zindex;
	},
	
	
	setLB: function( id, left, bottom, zindex )
	{
		var s = document.getElementById(id).style;
		s.position = "absolute";
		s.left = left+"px";
		s.bottom = bottom+"px";
		s.zIndex = zindex;
	},
	
	setPositionByMouse: function( id, event )
	{
		var event = event || window.event;
		var x = divs.getMousePosition(event).x;
		var y = divs.getMousePosition(event).y + 5;
		//alert("x="+x+"\ny="+y);
		var s = document.getElementById(id).style;
		s.left = x+"px";
		s.top = y+"px";
	},
	
	getMousePosition: function(event) 
	{
    	var x = y = 0;
		if (document.attachEvent != null) { // Internet Explorer & Opera
        	x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	        y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
		} else if (!document.attachEvent && document.addEventListener) { // Gecko
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
    	} else {
        	// Do nothing
    	}
    	return {x:x, y:y};
	}
}

mwindow =
{
	popup: function( url, width, height, event ) 
	{
		var event = event || window.event;
		if (!width) width = 450;
		if (!height) height = 100;
		
		var s = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		
		var x = divs.getMousePosition(event).x;
		var y = divs.getMousePosition(event).y - s;
		wnd = window.open( url, "_blank", "top="+y+", left="+x+", width="+width+", height="+height+", directories=0,location=0,status=0,toolbar=0,resizable=0,scrollbars=0,menubar=0");
		wnd.focus();
		
		//param( wnd );
		
		return wnd;
	}
}

param = window.param = function (obj, name) 
{
    var div = document.createElement("PRE"); 
    div.style.border = "1px solid red", div.style.padding = "5px";
    
    if ( typeof obj != 'object' )
        div.innerHTML = obj;
    else {
        var name = (name == undefined) ? 'object.' : name + '.'
        for (var i in obj) 
        {
            try { 
                div.innerHTML += name + i + " = " + obj[i] + "<br />";
            }
            catch( err ) {
                /* Auaia n niiauaieai ia ioeaea */
                div.innerHTML += "object.line = ERROR<br />";
            }
        }
    }
    
    if (document.body == null) 
    {
        function func() {document.body.appendChild(div)};
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        }
        else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    } 
    else {
        document.body.appendChild(div);
    }
}



ajax = 
{	
	getXmlHttpRequestObject: function () 
	{
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			return false;
		}
	},
	
	get: function( url, func, key ) 
	{
		if ( typeof receiveReq != 'object' || receiveReq == null || receiveReq == undefined ) receiveReq = new Array();
		
		// ПРОЦЕСС АЯКСА БУДЕТ ЗАПУЩЕН:
        // 1. Если нет такого объекта
        // 2. Если объект уже вернул результат
        var aStart = false;
            
        if ( typeof receiveReq[key] == 'undefined' || receiveReq[key] == null) 
        {
			aStart = true;
        } 
        else if ( typeof receiveReq[key] == 'object' && receiveReq[key].readyState == 4 ) 
        {
			aStart = true;
        }
		
		if( aStart )
		{
			receiveReq[key] = this.getXmlHttpRequestObject();
	
			if (receiveReq[key].readyState == 4 || receiveReq[key].readyState == 0) 
			{
				receiveReq[key].open("GET", url, true);
				receiveReq[key].onreadystatechange = func; 
				receiveReq[key].send(null);
			} 
		}
	},
	
	go2: function( id )
	{
		divs.show("bigproductcont");
		var url = "/get_product.php?id=" + id;
		return ajax.send( url, "bigproductcont" );
		//return false;
	},
	
	go3: function( id )
	{
		divs.show("bigpiccont");
		var url = "/get_pic.php?id=" + id;
		return ajax.send( url, "bigpiccont" );
		//return false;
	},
		
	send: function(obj, idcont)
	{
		if( obj.href ) curUrl = obj.href;
		else curUrl = obj;
		
		ajax.get( curUrl, function()
		{
			if(receiveReq[idcont].readyState == 4 && receiveReq[idcont].status == 200 )
			{
				if( receiveReq[idcont].responseText != '' )
				{
					if( document.getElementById("p_loading") ) divs.hide("p_loading");
					if( document.getElementById("p_loading2") ) divs.hide("p_loading2");
					
					document.getElementById(idcont).innerHTML = ajax.checkEvalTags( receiveReq[idcont].responseText );
				}
			}
			else if(receiveReq[idcont].readyState == 3 || receiveReq[idcont].readyState == 1 )
			{
				//document.getElementById(idcont).innerHTML = "<div style='padding: 50px;' align='center'><img alt='Загрузка' src='/images/loading.gif'></div>";
				if( document.getElementById("p_loading") ) divs.show2("p_loading");
				if( document.getElementById("p_loading2") ) divs.show2("p_loading2");
			}
		}, idcont );
		return false;
	},
	
	checkEvalTags: function( content )
	{
		var tags = ['andycms'];
					
		for( var tag in tags ) //param( tag );
		{
			for( var i=0; i<tags.length; i++ ) 
			{
				var reg = new RegExp("<"+tags[i]+">(.*)</"+tags[i]+">", "im");
				res = content.match(reg);
				//alert(res);
				if ( res != null && res[1].length > 0 ) 
				{ 
					//alert( res );
					content = content.replace(res[0],""); 
					eval(res[1]);
					//ekv('#'+tags[i]).html('[' + res[1] + ']'); num++;
				}
			}
		}
		return content;
	}
}

