var cropObj;

function GetUID()
{
    return Math.round(Math.random()*10000);
}

function openDialog (dict_id)
{
    $.post(
        "./dict.php",
        {
            getSelect: "1",
            did: dict_id
        },
        onAjaxSuccess
    );

}

function onAjaxSuccess(data)
{
  $("#dialog").html(data);
  $("#dialog").dialog("open");
}


function closeDialog ()
{
    $("#dialog").dialog("close");
}

var coordinats;
$(document).mousemove(function(e)
{
    coordinats = {x:e.clientX, y:e.clientY};
});
var liftMove = false;


function liftm (val)
{
//alert( 'X: '+coordinats.x+'\nY: '+coordinats.y )    
    liftMove = val;
    if (val) lift_unit ();

//alert( 'X: '+coordinats.x+'\nY: '+coordinats.y )
}

function lift_unit ()
{
    var yScroll = 0;
    if (window.pageXOffset)
        yScroll = window.pageYOffset;
    else if (document.documentElement)
        yScroll = document.body.scrollTop + document.documentElement.scrollTop;
    else if (document.body.scrollLeft >= 0)
        yScroll = document.body.scrollTop

    tp = coordinats.y - 290 + yScroll;
    tp2 = $("#lift").css("top");
    tp2 = tp2.substring (0, tp2.length-2)
    tp2 = parseInt (tp2);
//    alert (tp)
//    alert (tp2)
    if (tp < tp2-10)
        tp = tp2 - 10;
    else if (tp > tp2+10)
        tp = tp2 + 10;
//    alert (tp)
    if (tp < -40)
        tp = -40;
        
    if (tp > parseInt($("#menu").attr("offsetHeight"))-60)
        tp = parseInt($("#menu").attr("offsetHeight"))-60;
    
    tp = tp + "px";
    //alert (tp)
    
    
    $("#lift").css("top", tp);
    
    if (liftMove) setTimeout ("lift_unit()", 50);
    
}

function menu_over (menu_el, val)
{
    if (val == 1)
    {
        menu_el.className = "left_menu_hover";
    }
    else
    {
        if ($(menu_el).attr("v") != "1")
            menu_el.className = "left_menu";
    }
    
}

function show_menu (menu_el, menu_id)
{
    
    if ($(menu_el).attr("v") == "1")
    {
        $("#"+menu_id).hide();
        //$("#"+menu_id).attr("v", "0");
        $(menu_el).attr("v", "0")
        $("img.arrow_menu").hide();
    }
    else
    {
        $(".submenu").hide();
        $(".left_menu_hover").attr ("class", "left_menu");
        $(".left_menu").attr("v", "0")
        $("img.arrow_menu").hide();

        $("#"+menu_id).show();
        $("#arrow"+menu_id).show();
        $(menu_el).attr("v", "1")
        menu_el.className = "left_menu_hover";
    }
}

function clickclear(thisfield, defaulttext) 
{
	if (thisfield.value == defaulttext) 
    {
		thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext) 
{
	if (thisfield.value == "") 
    {
		thisfield.value = defaulttext;
	}
}

function selectPhotoOpen ()
{
    window.open ("photos.php?ocd=photos_select&id="+$("#photo_id").attr ("value"), "", "width=600,height=400");
}

function selectPhoto (photo_id)
{
//alert ($("photo_"+photo_id).html ());
    if (window.opener!=null)
    {
        $(window.opener.document).find ("#photo").html ($("#photo_"+photo_id).html ());
        $(window.opener.document).find ("#photo_id").attr ("value", photo_id);
        window.close ();
    }
}

function selectPhotoClear ()
{
    $("#photo").html ("Выберите фото");
    $("#photo_id").attr ("value", "0");
}

function checkPhotoCheckbox (this_el, second_id)
{
    if ($(this_el).attr ("checked"))
    {
        $("#"+second_id).attr ("checked", false);
    }
}

function search (t, f)
{
    if ($("#"+t).attr("value") != "")
        val = $("#"+t).attr("value");
    else
        val = "*";
    location.href=$("#"+f).attr("action")+"/"+val;
}

function companyPhoto (url, w, h)
{
    window.open (url, "", "screenX=0,screenY=0,width="+w+",height="+h+"");
}


function startCrop (perX, perY)
{
    cropObj = new Cropper.Img( 
            'posterImage',
			{
                ratioDim: {x:perX, y:perY},
                displayOnInit: true,
                onEndCrop: function (coords, dimensions) 
	   			{
                    document.getElementById ("coords").value = coords.x1+","+coords.y1+","+coords.x2+","+coords.y2+","+dimensions.width+","+dimensions.height;
                            
		        }
			}
	)
}





