﻿/**
 @package 	    Linso
 @subpackage 	Modal
 @copyright 	ООО "Портал" 2008 г.
 @autor   
*/



/*
	Аяксовые функции	
*/ 

function Ajax(address, data, callback, shPrc, method)
{
	if(shPrc) showPopLoader();
	if(method==null) method='GET';
	
	jQuery.ajax({
	  type: method,
	  url: address,
	  data: data,
	  dataType: "json",	  
	  success: function(json){
	    if (typeof(callback) == 'function') 
    	{    		
    		if(shPrc) hidePopWin();			
    		callback(json);
    	}
	  }
	});
}

function AjaxDialog(address , callback)
{		
	showPopLoader();

	if(typeof(callback)=="function")
      {
	    var onreadystatechange = function(json)
	    {
		    showPopupWindow(json.data,json);
			callback(json);	    		
	    }
	  }
	else       
	  { 
	    var onreadystatechange = function(json) 
	    {    	
	   		showPopupWindow(json.data,json);
	    }
	 }
	 	
	jQuery.get(address, {url: window.location.href}, onreadystatechange, "json" );
}	




/* custom ajax dialog*/
function CustomAjaxDialog(e , address)
{
	if (!e) e = window.event;
	
	var x = e.clientX;
    var y = e.clientY;
	
	var onreadystatechange = function(json) 
    {    
	    CustomshowPopWin(json.width, json.height, x , y );
		updatePopWinCustom(json.data);
    }

	jQuery.get(address, {url: window.location.href}, onreadystatechange, "json" );
}	

function AjaxSend(address, form, callback)
{	
	var callbackFunction = function(){};
	if(typeof(callback) == 'function') callbackFunction = callback;
	
	jQuery(form).ajaxSubmit({
		url: address,
		dataType:'json',
		type:'POST',
		success: function(json) {
			callbackFunction(json);
		}
	});	
}

/*
	Стандартные заголовки ответа
*/

function FormStdRedirect(x)
{
	if (x.reload)
	{
		if(x.reloadlink)
		{
			document.location = x.reloadlink;
		}
		else
		{
			document.location.reload();
		}
	}
	else
	{		
		showPopupWindow(x.data,x); 		
	}
}


/*
	Диалог
*/

var gcom_popupMask = null;
var gcom_popupContainer = 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;

flag_init = false;
/**
 * Initializes popup code on load.	
*/

function initPopUp() 
{
	if(flag_init) return false;
	
	theBody = document.getElementsByTagName('BODY')[0];
	
	popmask 		  = document.createElement('div');
	popmask.id 		  = 'com_popupMask';
	popmask.innerHTML = "&nbsp;";
	
	popcont = document.createElement('div');
	popcont.id = 'com_popupContainer';
	popcont.style.zIndex = 999;
	jQuery(popcont).addClass("modalwin_align_center");
	

	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gcom_popupMask = $("com_popupMask");
	gcom_popupContainer = $("com_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 = document.getElementsByTagName('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;
			}
		}
	}
	
	
	flag_init = true;
}

addEvent(window, "load", initPopUp);


function showPopLoader() 
{ 
	showPopupWindow('<div style="text-align:center;"><img src="/mod/_system/style/prtl/img/loader/load_2.gif" title="Загрузка..."  alt="Загрузка..." /></div>');
}

function showPopupWindow(html,options)
{
	if(!options)options={};
	
	updatePopWin(html,options);
	showPopWin(options);
}

function updatePopWin(innerHtml, options) 
{		
	var beginHtml = '';
	if(options)
		if(options.closeButton) 
			beginHtml += '<div id="modalblockContainerClose" onclick="hidePopWin();">x</div>';
	
	popcont.innerHTML =  '' +
		'<div class="modalwin_align_center_to_left">' +
			'<div class="modalwin_align_center_to_right"><div id="popupInner"><div id="modalblockContainer">' +
					beginHtml + innerHtml + 
			'</div></div></div>' +
		'</div>';
}

function updatePopWinCustom(innerHtml) 
{		
	popcont.innerHTML =  innerHtml ;
}



function showPopWin(options) 
{
	
	gPopupIsShown = true;
	//disableTabIndexes();
	
	gcom_popupMask.style.display = "block";
	gcom_popupContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin(options);
	
	setMaskSize(options);
	
	// for IE
	if (gHideSelects == true) hideSelectBoxes();
}

/* custom popup windows */
function CustomshowPopWin(width, height , x , y) 
{
	
	gPopupIsShown = true;
	disableTabIndexes();
	gcom_popupMask.style.display = "block";
	gcom_popupContainer.style.display = "block";
	// calculate where to place the window on screen
	//centerPopWin(width, height);
	
	gcom_popupContainer.style.width = width + "px";
	gcom_popupContainer.style.height = height + "px";
	
	getXY(x , y , gcom_popupContainer, width , height);
		
	// for IE
	if (gHideSelects == true) hideSelectBoxes();
}

//
var gi = 0;

function centerPopWin(options) 
{
	if (gPopupIsShown == true) 
	{
		if(options && options.width) jQuery(gcom_popupContainer).width(options.width);
			else jQuery(gcom_popupContainer).width(450);
			
		width = jQuery(gcom_popupContainer).width();
		height = jQuery(gcom_popupContainer).height(); 
		
		var theBody = document.getElementsByTagName("BODY")[0];
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		setMaskSize();
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
			
		gcom_popupContainer.style.top = (scTop+40)+'px';//(scTop + ((fullHeight - (height)) / 2)) + "px";
		gcom_popupContainer.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(options) 
{
	var theBody = document.getElementsByTagName("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;
	
	gcom_popupMask.style.height = popHeight + "px";
	gcom_popupMask.style.width = popWidth + "px"; /**/
	
	var theBody = document.getElementsByTagName("BODY")[0];

	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
			
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) popHeight = "100%"; else
	popHeight = theBody.scrollHeight+"px";
	
	if (fullWidth > theBody.scrollWidth) popWidth = "100%"; else
	popWidth = theBody.scrollWidth+"px";
	
	gcom_popupMask.style.height = popHeight;
	gcom_popupMask.style.width = popWidth;	
}



function hidePopWin() 
{
	gPopupIsShown = false;
	
	var theBody = document.getElementsByTagName("BODY")[0];
	
	theBody.style.overflow = "";
	restoreTabIndexes();
	
	if (gcom_popupMask == null) return;

	gcom_popupMask.style.display = "none";
	gcom_popupContainer.style.display = "none";
	
	popcont.innerHTML = "";
	
	// 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 = document.getElementsByTagName(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 = document.getElementsByTagName(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)
{
	/* Проверка на браузер */
	/*
	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; */
   
	if( (getViewportWidth() - x ) > w) x +=5;
	else	 x = x - w;
	
	if( ((getScrollTop() + getViewportHeight()) - y) > h  ) y += getScrollTop();
	else y -= h;
	
	
	object.style.left =  x + "px";
    object.style.top =   y + "px";
         
	/*if(is_opera||is_ie6||is_ie7)
	 {
		 object.style.pixelLeft = x;
         object.style.pixelTop = y;
	 }
	 else
	 {
		 object.style.left =  x + "px";
         object.style.top =   y + "px";
	 } */	 
}


