// Javascript overlay code

function loadShadowURL(uri){
    document.getElementById('hiabshadow').className = 'hiabshadow';
    $.get(uri, function(data){
            //alert("Shadow Data Loaded: " + data);
            document.body.style.overflow="hidden";
            document.getElementById('hiabshadowContent').innerHTML = data;
            document.getElementById('hiabshadowContent').className = 'hiabshadowContent';
    });
}

function loadShadowHTML(html){
    document.body.style.overflow="hidden";
    document.getElementById('hiabshadow').className = 'hiabshadow';
    document.getElementById('hiabshadowContent').innerHTML = html;
    document.getElementById('hiabshadowContent').className = 'hiabshadowContent';
}

function loadShadowImage(path,imageWidth,imageHeight){
    window.scroll(0,0);
    document.body.style.overflow="hidden";
    document.getElementById('hiabshadow').className = 'hiabshadow';
    var w = $(window).width()-100;
    var h = $(window).height()-100;
    leftPos = 50;
    topPos = 50;
    
    if(imageWidth < w)
        {
        w = imageWidth;
        leftPos = ($(window).width() - w) / 2;
        }
    if(imageHeight < h)
        {
        h = imageHeight;
        topPos = ($(window).height() - h) / 2;
        }
    
    $('#hiabshadowContent').css({'left':leftPos+'px','top':topPos+'px','margin-left': '0px','padding':'6px','border':'none','background':'none'});
    
    $('#hiabshadowContent').width(w);
    $('#hiabshadowContent').height(h);
    html = '<html><head>';
    html += '</head><body>';
    html += '<img src="'+path+'"/>';
    html += '<div id="shadowClose" onclick="closeShadow()" style="cursor: pointer; width: 20px; height: 20px; background: url(./modules/imageMgr/images/zoomClose.png);position: absolute; top: 0px; right: 0px"></div>';
    html += '</body></html>';
    
    
    document.getElementById('hiabshadowContent').innerHTML = html;
    document.getElementById('hiabshadowContent').className = 'hiabshadowImage';
}

function closeShadow(){
    document.body.style.overflow="auto";
    document.getElementById('hiabshadowContent').className = 'hidden';
    document.getElementById('hiabshadow').className = 'hidden';
    document.getElementById('hiabshadowContent').innerHTML = '';
}


// Login functions

function hiabDoLogin(){
    /*
     This function expects some form elements to be present on the page
     that calls it.  These include
     
     Required Values:
     hiabLoginUser: Username of user logging in
     hiabLoginPass: Password of user logging in
     
     Optional Values (One success and one failure field is required):
     hiabLoginSuccessURI: Page Redirect Location once user is logged in
     hiabLoginSuccessFunc: Function to run when login complete
     hiabLoginSuccessFrameURI: where to send the hidden iframe
     hiabLoginFailureURI: Page redirect in case of login failure
     hiabLoginFailureFunc: Function to run in case of login failure
    */
    
    var username = document.getElementById('hiabLoginUser').value;
    var password = document.getElementById('hiabLoginPass').value;
    
    // Now we check the user/pass combo
    $.post("/class/publicAuth.php", { user: username, pass: password },
        function(data){
            var resultdata = data.split(':');
            var resultcode = resultdata[0];
            var resultmsg = resultdata[1];
          if (resultcode === 'OK'){
            if (document.getElementById('hiabLoginSuccessURI')){
                var redirect = document.getElmentById('hiabLoginSuccessURI').value;
                closeShadow();
                document.location.href = hiabLoginSuccessURI;
            }
            else if (document.getElementById('hiabLoginSuccessFunc')){
                var func = document.getElementById('hiabLoginSuccessFunc').value;
                closeShadow();
                eval(hiabLoginSuccessFunc());
            }
            else if (document.getElementById('hiabLoginSuccessFrameURI')){
                var uri = document.getElementById('hiabLoginSuccessFrameURI').value;
                closeShadow();
                document.getElementById('hiabScriptFrame').src = uri;
                
            }
            else {
                window.location.reload();
            }
          }
          else {
            alert(resultmsg);
          }
        });
    
}
