/**
 * устаревшая функция для работы с ajax
 * сохранена для обратной совместимости
 * @param parameters
 */
function AjaxRequest(parameters) {

	var type = parameters.type ? parameters.type : 'POST';
	var data = parameters.data ? parameters.data : {};

	if (!parameters.action) {
		alert('Не указано действие');
		return;
	}

	var success = parameters.success ? parameters.success : NULL;
	var action  = parameters.action  ? parameters.action  : NULL;
	if( typeof( parameters.module )== undefined )
	  var module = NULL;
	else
	  var module  = parameters.module;
	
	$.ajax( {
		"type" : type,
		"url" : "/?r=page/ajax",
		"data" : {
			"action" : action,
			"module" : module,
			"data" : data
		},
		"success" : success,
		"beforeSend" : function() {
			//alert('Отправляем запрос!');
		},
		"complete" : function() {
			//alert('Запрос завершен');
		},
		"error" : function(xmlhttpobj, textStatus, errorThrown) {
			alert('Не удалось обработать запрос');
		},
		"cache" : false
	});
}

/**
 * Улучшенная функция для работы с ajax
 * 
 * Пример вызова:
 * 	ajax2('PageTree','siteeditor',{
 * 		siteid: $siteId
 *  }, function(res){
 *      $('#cattree').remove();
 *      $('#pagetree_title').after(res);
 *  });
 * 
 * @param action  - имя действия
 * @param module  - имя контроллера
 * @param data    - данные
 * @param success - функция, вызываемая когда придет ответ
 * @param type    - (default) POST или GET
 * 
 */
function ajax2(action,module,data,success,type,form){
	data.action = action;
	data.module = module;
	
	var parameters = {
		type : type? type: 'POST',
		url  : "/?r=page/ajax",
		data : data,
		success : success,
		error : function() {
			alert('Не удалось обработать запрос');
		},
		cache : false
	};
	
	if(!$(form).length > 0) $.ajax(parameters);
	else{
		$(form).ajaxForm(parameters);
		$(form).submit();
	}
}

function countPopups(){
	return $('.popupcontainer').length;
}

function popup_open(html,width,title,top,id){
	// обычно top == $(this).offset().top
	var popupsNum = countPopups() + 1;
	
	var fullwidth  = $(document).width();
	var fullheight = $(document).height();

	//var bg = popupsNum == 1 ? '<div class="overlay" id="popup_overlay" style="width:'+fullwidth+'px; height:'+fullheight+'px;"></div>' : '';
	var bg = '';
	
	if(id == undefined) id='';
	$("#popupcontainer"+id).remove();
	
	if(top != undefined) top = 'style="top: ' + top + 'px"';
	else    top = '';
	
	if(!title) title = '';	
	else       title = '<div class="popuptitle" id="popuptitle'+id+'">' + title + '</div>';
	
	if(!html) html = '';
	else html = '<div class="popupbody" id="popupbody'+id+'">'+html+'</div>';
	
	var closeButton = '<div onclick=\'popup_close("'+id+'");\' class="popupclose"></div>';
	
	$(document.body).append(			
			'<div class="popupcontainer" '+top+' id="popupcontainer'+id+'">'
			 +'<div class="align_center"><div class="align_center_to_left"><div class="align_center_to_right">' 
			 +'<div class="popupwindow" id="popupwindow'+id+'" style="width: '+width+'px">'
			   + title
			   + html
			   + closeButton
			 +'</div>'
			 +'</div></div></div>'
			+'</div>'
			+ bg
	);
	
	$('#popupcontainer'+id).draggable();
}

function popup_close(id){
	if(id == undefined) id='';
	var popupsNum = countPopups() - 1;

	$("#popupcontainer"+id).remove();
	if(popupsNum <= 0)  $('#popup_overlay').remove();
}


function ShowBlock(id) {

form = document.getElementById(id);

// Определяем рабочую область экрана пользователя

w = ClientWidth(); // ширина окна пользователя
h = ClientHeight(); // высота окна пользователя

// ширина и высота блока который вставляем по центру страницы

formW = 668;
formH = 300;

width = (w-formW) /2;
height = ((h-formH) / 2) - ((h-formH) / 4);

form.style.left = width + 'px';
form.style.top = height + 'px';
form.style.display = 'block';

}

function ClientWidth() {
return document.compatMode=='CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}

function ClientHeight() {
return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientHeight : document.body.clientHeight;
} 


 function changeView( key, obj )
 {
   if( typeof obj == "undefined" )
   {
     var v1 = document.getElementById('forma');
   }
   else
   {
     var v1 = document.getElementById(obj);
   }

   if( !key )
   {
     v1.style.display='none';
   }
   else
   {
     v1.style.display='block';
   }

   return false;
 }

