﻿
// ------------------------------------------------------------------------
// detekcija browsera kroz css


var css_browser_selector = function() {
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}();


// ----------------------------------------------------------------
// pronalazenje stvarne pozicije objekta na ekranu
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// -----------------------------------------------------------------------
// pozicija kurzora u okviru elementa
function findElementPos( e ) 
{
    return [ (e && e.layerX ) || window.event.offsetX, ( e && e.layerY ) || window.event.offsetY ];
}
        


// ----------------------------------------------------------------
// prikazivanje objekta
function showHideAtPos(show, objId, objIdAtPos, x, y)
{
    var o1 = document.getElementById(objId);
    var o2 = document.getElementById(objIdAtPos);

    if(o1 != null && o2 != null)
    {
        if(show == 1)
        {
            var pos = findPos(o2);
            var leftPos = pos[0] + x;
            var topPos = pos[1] + y;
            
            o1.style.position = "absolute";
            o1.style.left = parseInt(leftPos)+"px";
            o1.style.top = parseInt(topPos)+"px";
            
            o1.style.visibility = "visible";
            o1.style.zindex = "1000";
        }
        else if(show == 0)
        {
            o1.style.visibility = "hidden";
        }
        else if(show == 2)
        {
            showHideAtPos(o1.style.visibility == "visible" ? 0 : 1,  objId, objIdAtPos, x, y);
        }
    }
}


function showHideObj(show, objId)
{
    var o1 = document.getElementById(objId);

    if(o1 != null)
    {
        o1.style.visibility = 
            show == 2 ? (o1.style.visibility == "visible" ? "hidden" : "visible" ) :
            ( show == 1 ? "visible" : "hidden" );
    }
}


function dropdownPut(objId, objValId, val)
{
    var o1 = document.getElementById(objId);
    var o2 = document.getElementById(objValId);

    if(o1 != null)
    {
        o1.value = val;
        o2.value = val;
    }
}


// **********************************************************************
// niz otvorenih modalnih popup-ova
var modals = new Array();

function unshrinkWnd(from, to)
{
    
}

// ----------------------------------------------------------------------
// otvaranje i zatvaranje popup-a
function modalPopup(wnd, objIdAtPos, x, y, shade, display)
{
    var modalBackground = document.getElementById("_modalBackground");
    
    // zatvaramo prethodni
    if(wnd == null)
    {
        var wndObj = modals[modals.length-1];
    
        wndObj.wnd.style.visibility = "hidden";
        modalBackground.style.visibility = "hidden";
        
        if(wndObj.objAtPos != null)
        {
            wndObj.objAtPos.style.backgroundColor = wndObj.objAtPosStyle[0];
            wndObj.objAtPos.style.borderBottomWidth = wndObj.objAtPosStyle[1];
            wndObj.objAtPos.style.borderBottomColor = wndObj.objAtPosStyle[2];
        }
        
        // vracamo prethodni popup
        modals.length --;
        if(modals.length > 0)
        {
            wndObj = modals[modals.length-1];
            modalBackground.style.zindex = ""+(10000+parseInt(wndObj.wnd.style.zindex)-1)+"";
            modalBackground.style.visibility = "visible";
        }
    }
    else
    {
        var wndObj = new ModalPopupObject(wnd);
       
        if(display)
           wndObj.wnd.style.display = display; 
        
        if(objIdAtPos != null)
        {
            var o2 = document.getElementById(objIdAtPos);

            var pos = findPos(o2);
            var leftPos = pos[0] + x;
            var topPos = pos[1] + y;
            
            if(shade)
            {
                wndObj.objAtPos = o2;
                wndObj.objAtPosStyle = [ o2.style.backgroundColor, o2.style.borderBottomWidth, o2.style.borderBottomColor ];
                
                o2.style.backgroundColor = "#f7dddd";
                o2.style.borderBottom = "solid 1px #cc3333";
            }
        
            wndObj.wnd.style.left = parseInt(leftPos)+"px";
            wndObj.wnd.style.top = parseInt(topPos)+"px";
        }
        else
        {
            var sW = window.screen.availWidth;
            var sH = window.screen.availHeight;
            
            var wW = wndObj.wnd.offsetWidth ? wndObj.wnd.offsetWidth : ( wndObj.wnd.style.pixelWidth ? wndObj.wnd.style.pixelWidth : 0); 
            var wH = wndObj.wnd.offsetHeight ? wndObj.wnd.offsetHeight : ( wndObj.wnd.style.pixelHeight ? wndObj.wnd.style.pixelHeight : 0); 
            
            //alert(sW+" "+sH+" "+wW+" "+wH);
            
            wndObj.wnd.style.left = parseInt((sW-wW)/2)+"px";
            wndObj.wnd.style.top = parseInt((sH-wH)/2-80)+"px";

        }
        
        modals[modals.length] = wndObj;

        wndObj.wnd.style.position = "absolute";
        wndObj.wnd.style.visibility = "visible";

        modalBackground.style.zindex = ""+(10000+parseInt(wndObj.wnd.style.zindex)-1)+"";
        modalBackground.style.visibility = "visible";
    }
    
}

// ----------------------------------------------------------------------
// kreiranje novog popup objekta
function ModalPopupObject(wnd)
{
    this.wnd = document.getElementById(wnd);
    this.objAtPos = null;
    this.objAtPosStyle = null;
    this.wnd.style.zindex = ""+(10000+modals.length*2)+"";
}


// ----------------------------------------------------------------------
// menjanje stila objekta u fokusu
function focusObj(objId, css)
{
    var obj = document.getElementById(objId);
    if(obj != null)
        obj.className = css;
}


function submitLogin(textUser, textPass, login)
{
    var oUser = document.getElementById(textUser);
    var oPass = document.getElementById(textPass);
    
    var oLogin = document.getElementById(login);
    var oLUser = document.getElementById(login+"_UserName");
    var oLPass = document.getElementById(login+"_Password");
    var oLButton = document.getElementById(login+"_LoginButton");

    oLUser.value = oUser.value;
    oLPass.value = oPass.value;
    
   
    //alert(oLButton.name+"\r"+login+"\r"+login.replace(/_/g, "$"));
       
    //var result = Page_ClientValidate(oLogin.name);
    //__doPostBack("'"+oLButton.name+"'", '');
    //alert(typeof(oLButton.onclick));
    //eval(oLButton.onclick);
    //oLButton.click();
    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("'"+oLButton.name+"'", '', true, "'"+login.replace(/_/g, "$")+"'", '', false, false));
}

