/* Проверка на браузер */
var userag = navigator.userAgent;
is_gecko = /gecko/i.test(userag);
is_opera = /opera/i.test(userag);
is_mac = /mac_powerpc/i.test(userag);
is_ie = /msie/i.test(userag) && !is_opera && !is_gecko && !is_mac;
is_ie5 = is_ie && /msie 5\.[^5]/i.test(userag);
is_ie6 = is_ie && /msie 6\.[^6]/i.test(userag);
is_ie7 = is_ie && /msie 7\.[^7]/i.test(userag);
is_nn4 = document.layers ? true : false;

/*
	Аяксовые функции	
*/

function Ajax(address, data, callback, shPrc)
{
	if(shPrc) showPopLoader();
		
    var req = new AjaxRequest();        
    req.onreadystatechange = function() 
    {    	
    	if (req.readyState == 4 && typeof(callback) == 'function') 
    	{    		
    		if(shPrc) hidePopWin();
    		callback(req.responseJS);
    	}
    }        		
	req.open('GET', address, true);
	req.send(data);		
}

function AjaxDialog(address)
{		
    var req = new AjaxRequest();
    
    showPopLoader();
    
    req.onreadystatechange = function() 
    {    	
    	if (req.readyState == 4)
    	{    		
		    
    		showPopWindow(req.responseJS.width, req.responseJS.height, req.responseJS.data);
    	}
    }

	req.open('GET', address, true);
	req.send( {url: window.location.href} );
}	

/* custom ajax dialog*/
function CustomAjaxDialog(e , address)
{		
    var req = new AjaxRequest();
   	
	if (!e)
        e = window.event;
    var x = e.clientX;
    var y = e.clientY;
	if(is_gecko)
	 {
		  var x = e.pageX;
          var y = e.pageY;
	 }
    req.onreadystatechange = function() 
    {    	
    	if (req.readyState == 4)
    	{    		
		   
		    CustomshowPopWin(req.responseJS.width, req.responseJS.height, x , y );
			updatePopWin(req.responseJS.data);    		
    	}
    }

	req.open('GET', address, true);
	req.send( {url: window.location.href} );
}	

function AjaxSend(address, form, callback)
{		
    var req = new AjaxRequest();
        
    req.onreadystatechange = function() 
    {    	
    	if (req.readyState == 4 && typeof(callback) == 'function') 
    	{    		
    		callback(req.responseJS);
    	}
    }
    		
	req.open('POST', address, true);
	req.send({ q: form });
}

/*
	Стандартные заголовки ответа
*/

function headerFormStd(x)
{
	if (x.reload) 
	{
		document.location.reload();
	}
	else
	{		
		showPopWin(x.width, x.height);
		updatePopWin(x.data); 		
	}
}

/*
	Диалог
*/

var gPopupMask = null;
var gPopupContainer = null;
var gPopupIsShown = false;
var gHideSelects = false;
var gReturnVal = null;
var gLoader = false;
var gTabIndexes = new Array();

// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) document.onkeypress = keyDownHandler;


/**
 * Initializes popup code on load.	
 */
function initPopUp() 
{
	theBody = $$('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	

	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = $("popupMask");
	gPopupContainer = $("popupContainer");
		
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);

	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) gHideSelects = true;

	
	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
	var elms = $$('a');
	
	for (i = 0; i < elms.length; i++) 
	{
		if (elms[i].className.indexOf("submodal") == 0) 
		{ 
			elms[i].onclick = function()
			{
				var width = 400;
				var height = 200;
				
				params = this.className.split('-');
				
				if (params.length == 3) 
				{
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
}
//addEvent(window, "load", initPopUp);


function showPopLoader() 
{ 
	showPopWindow(180, 100, '<img src="/img/wnd/loader.gif" title="Loading..."  alt="Loading..." />');
}


function showPopWindow(width, height, innerHtml) 
{ 
	showPopWin(width, height);
	updatePopWin(innerHtml);
}

function updatePopWin(innerHtml) 
{		
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<table class="flat wdlg" width="100%">' +
				'<tr class="wdlg-br-x"><td class="wdlg-br-y wdlg-ur"></td><td class="wdlg-u"></td><td class="wdlg-br-y wdlg-ul"></td></tr><tr><td class="wdlg-l"></td><td class="wdlg-c">'+
					innerHtml + '</td><td class="wdlg-r"></td></tr>' +
				'<tr class="wdlg-br-x"><td class="wdlg-dr"></td><td class="wdlg-d"></td><td class="wdlg-dl"></td></tr>' +
			'</table>' +
		'</div>';	
}



function showPopWin(width, height) 
{
	
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin(width, height);
	
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height) + "px";
	
	setMaskSize();
	
	// for IE
	if (gHideSelects == true) hideSelectBoxes();
}

/* custom popup windows */
function CustomshowPopWin(width, height , x , y) 
{
	
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	//centerPopWin(width, height);
	
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = height + "px";
	getXY(x , y , gPopupContainer, width , height);
		
	// for IE
	if (gHideSelects == true) hideSelectBoxes();
}

//
var gi = 0;

function centerPopWin(width, height) 
{
	if (gPopupIsShown == true) 
	{
		if (width == null || isNaN(width)) width = gPopupContainer.offsetWidth;
		if (height == null) height = gPopupContainer.offsetHeight;
		
		var theBody = $$("BODY")[0];
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		setMaskSize();
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}
//addEvent(window, "resize", centerPopWin);
//addEvent(window, "scroll", centerPopWin);
//window.onscroll = centerPopWin;


/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() 
{
	var theBody = $$("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) popHeight = fullHeight; else
	popHeight = theBody.scrollHeight;
	
	if (fullWidth > theBody.scrollWidth) popWidth = fullWidth; else
	popWidth = theBody.scrollWidth;
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

function hidePopWin() 
{
	gPopupIsShown = false;
	
	var theBody = $$("BODY")[0];
	
	theBody.style.overflow = "";
	restoreTabIndexes();
	
	if (gPopupMask == null) return;

	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	
	// display all select boxes
	if (gHideSelects == true) displaySelectBoxes();
}


// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() 
{
	if (document.all) 
	{
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) 
		{
			var tagElements = $$(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) 
			{
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() 
{
	if (document.all) 
	{
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) 
		{
			var tagElements = $$(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) 
			{
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() 
{
	for(var i = 0; i < document.forms.length; i++) 
	{
		for(var e = 0; e < document.forms[i].length; e++)
		{
			if(document.forms[i].elements[e].tagName == "SELECT") 
			document.forms[i].elements[e].style.visibility="hidden";
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() 
{
	for(var i = 0; i < document.forms.length; i++) 
	{
		for(var e = 0; e < document.forms[i].length; e++)
		{
			if(document.forms[i].elements[e].tagName == "SELECT") 
			document.forms[i].elements[e].style.visibility="visible";
		}
	}
}

/* Возвращает x и y*/
function getXY(x , y , object , w , h)
{
	
    if (document.body.clientWidth && document.body.clientWidth - (x + document.body.scrollLeft - document.body.clientLeft) < w)
        x = x - w;
    else
        x = x + 5;

    if (document.body.clientHeight && document.body.clientHeight - y < h)
        y = y - h;
    
	
	 if(is_opera)
	 {
		 object.style.pixelLeft = x + document.body.scrollLeft - document.body.clientLeft;
	   	 object.style.pixelTop = y + document.documentElement.scrollTop - document.body.clientTop + 5;
	 }
	 
	 if(is_ie6)
	 {
		 
		 object.style.pixelLeft = x;
         object.style.pixelTop = y + document.documentElement.scrollTop - document.body.clientTop+5;
	 }
	 if(is_gecko)
	 {
		 object.style.left =  x + "px";
         object.style.top =   y + "px";
		
	 }
	 
}



