var m_place=m_check='';
var m_place_obj=m_place_in_obj='';
var cm_ar = new Array();
$(document).ready(function(){
	create_mplace(); // Инициализируем диалог при загрузке
});
function create_mplace()
{
  if(m_place==undefined || m_place=='') // Формируем окно
  {
    var st = document.createElement("link");// подключаем css
    st.setAttribute("rel","stylesheet");
    st.setAttribute("href","includes/js/control_menu/dialog.css");
    document.body.insertBefore(st, document.body.firstChild);
    m_place='<div id="mask"><div class="dialog_close">Закрыть</div><div id="dialog">'
    + '<div class="dialog_close">Закрыть</div>'
    + '<div id="m_place_in"></div>'
    + '</div></div>';
    document.body.insertAdjacentHTML("beforeEnd", m_place);
  }
}
function get_control_menu(c_link, type)
{
    create_mplace();
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'min-height':maskHeight});//

		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.9);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
     
    //if close button is clicked
    $('.dialog_close').click(function (e) {
      //Cancel the link behavior
      e.preventDefault();
      $('#mask, #dialog').hide();
    });		

    //if mask is clicked
    /*
    $('#mask').click(function () {
      $(this).hide();
      $('#dialog').hide();
    });*/
    $('#dialog').width(winW/6*4);// Высоту не указываем - пусть будет тянущаяся
		//Set the popup window to center
		$('#dialog').css('margin-top',  winH/6);
		$('#dialog').css('margin-left', winW/6);
		//transition effect
		$('#dialog').fadeIn(2000);
		if(type=='ajax')
		{
			$('#m_place_in').html('<div id="control_mnu"></div>');
			show_aj_place('control_mnu',c_link, '', 'GET', 0);
		} else if(type=='img')
		{
			$('#m_place_in').html('<div align=center><img src="'+c_link+'" border=0></div>');
		} else {
			$('#m_place_in').html('<a href="'+c_link+'" target=_blank>'+c_link+'</a>');
		}
}

function getDocumentSize(){ 
    try{ 
            //frame = document.getElementById(frameId); 
            //innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document; 
            objToResize = (frame.style) ? frame.style : frame;
	         	objToResize.height = innerDoc.body.scrollHeight + 20;
       	   	objToResize.width = innerDoc.body.scrollWidth + 20;
	} 
    catch(err){ 
            window.status = err.message; 
		} 
} 

/**
 * Emulates insertAdjacentHTML(), insertAdjacentText() and
 * insertAdjacentElement() three functions so they work with Netscape 6/Mozilla
 * by Thor Larholm me@jscript.dk
 */
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
    HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) {
      switch (where){
        case 'beforeBegin':
            this.parentNode.insertBefore(parsedNode,this);
            break;
        case 'afterBegin':
            this.insertBefore(parsedNode,this.firstChild);
            break;
        case 'beforeEnd':
            this.appendChild(parsedNode);
            break;
        case 'afterEnd':
            if (this.nextSibling) {
                this.parentNode.insertBefore(parsedNode,this.nextSibling);
            }
            else {
                this.parentNode.appendChild(parsedNode);
            }
            break;
      }
    };

    HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var parsedHTML = r.createContextualFragment(htmlStr);
        this.insertAdjacentElement(where,parsedHTML);
    };


    HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
        var parsedText = document.createTextNode(txtStr);
        this.insertAdjacentElement(where,parsedText);
    };
}
function isMSIE()
{
	navigator.appName == "Microsoft Internet Explorer" ? true : false
}

//Отображаем контрол меню
function movieControlMenu(obj)
{
    var el_span = $(obj).find('span');
    var rel_id = $(obj).attr('rel_id');
    if(!cm_ar[rel_id])
    {
	    if(el_span.html() == "")
	    {
			$.ajax(
			{
			    url: 'index.php',
			    type:'get',
			    dataType:'html',
			    data:
			    {
					request: "controller",
					action: "control_menu",
					family: $(obj).attr('family'),
					obj_type: $(obj).attr('type'),
					rel_id: $(obj).attr('rel_id'),
					blid: $(obj).attr('block_id'),
					item_edit:$(obj).attr('item_edit'),
					item_delete:$(obj).attr('item_delete'),
					item_open:$(obj).attr('item_open'),
					obj_id:$(obj).attr('obj_id')
				},
			    success: function(data)
			    {
				    el_span.html(data);
					el_span.slideDown('fast');
			    }
			});
		}
		else
		{
			$(obj).find('span').slideDown('fast');
		}
		cm_ar[rel_id] = true;
	}
	else
	{
		$(obj).find('span').slideUp('fast');
		cm_ar[rel_id] = false;
	}
}

function actionControlMenuItem(block_id,rel_id,family,action,obj_id,obj_type)
{
	$.ajax(
	{
	    url: 'index.php',
	    type:'get',
	    dataType:'json',
	    data:
	    {
			request: "controller",
			action: action,
			family: family,
			obj_type: obj_type,
			rel_id: rel_id,
			blid: block_id,
			obj_id: obj_id
		},
	    success: function(data)
	    {
	    	if(data.reload)
	    	{
	    		window.location.reload();
	    		return;
	    	}
	    	if(data.reload_icon)
	    	{
				//Ищем все кнопки контрол-меню и смещаем их в положение по умолчанию
				$('div.cm_button').css("background-position","0px 0px");
				//Ищем кнопку контрол меню, по которой кликнул пользователь перед тем как выбрать действие
				$('div.cm_button[rel_id='+ rel_id + ']').css("background-position","-12px 0px");
			}
			
			alert(data.message)
	    }
	});
}
/*---------------*/
var exp_status_ar=new Array();
/**
 * Развернуть/свернуть html-элемент
 *
 * @param string plid уникальный идентификатор объекта
 */
function expand_me(plid)
{
    if(exp_status_ar[plid] == undefined)
    {
      exp_status_ar[plid]=0;
    }
    if(exp_status_ar[plid] == 0)
    {
      $('#'+plid).slideDown('slow');
      exp_status_ar[plid]=1;
    } else {
      $('#'+plid).slideUp('fast');
      exp_status_ar[plid]=0;
    }
    return;
}
