var taskBarHeight = 0;

//preload mouseover images to ensure transition is smooth
var cachedImage = new Image();
var img = new Array();
var cachedImages = new Array();
cachedImages[0] = "images/WebOSLoginButtonHover.png";                

for (var imgStart = 0; imgStart < cachedImages.length; imgStart++){
    img[imgStart] = new Image();
    img[imgStart].src = cachedImages[imgStart];
}

function focus() {
    if (window.setFocus) {
        setFocus( 'TEXT', 'PASSWORD' );
    } else if (window.bpSetFocus) {
        bpSetFocus( "BPLoginFlash" );
    }
}

function toggleHover(element) {
    if (element.className.indexOf("hover") == -1) {
        element.className = "loginForm-login-button-hover loginForm-login-button";
    } else {
        element.className = "loginForm-login-button";                
    }
}

function focusStyle(element) {
    element.className = "loginForm-text-input-hover loginForm-text-input";
}

function blurStyle(element) {
    element.className = "loginForm-text-input";                
}

dojo.addOnLoad( focus );  
dojo.addOnLoad( checkSystemLanuch );     
dojo.addOnLoad( checkLoginBoxHeight );

function checkWidth( width ) {
    if ( width > document.body.clientWidth ) {
        width = document.body.clientWidth;
    } else if ( width == 0 ) {
        width = dojo.html.getViewport().width;
    }
    return width;
}

function checkHeight( height ) {
    if ( height > document.body.clientHeight ) {
        height = document.body.clientHeight;
    } else if ( height == 0 ) {
        height = dojo.html.getViewport().height;
    }
    return height;
}



function getLeft( windowWidth ) {
    var cW = document.body.clientWidth;
    return ( cW - windowWidth ) / 2;
}
function getTop( windowHeight ) {
    var cH = document.body.clientHeight;
    return ( cH - windowHeight ) / 2;
}

function createIframe( url, name ) {
    var iframe;
    if (dojo.render.html.ie) {
        iframe = document.createElement("<IFRAME name='" + name + "'>"); //IE 'name property' bug workaround
    } else {
        iframe = document.createElement("IFRAME");
        iframe.name = name;
    }

    iframe.style.height = "100%";
    iframe.style.display = "block";
    iframe.style.width = "100%";
    iframe.style.backgroundColor = "#fff";
    iframe.style.overflow = "auto";
    if (dojo.render.html.mozilla) {
        iframe.src = "about:blank"; //if we load a url, dojo will load it 2-4 times during widget constuction.
    } else {
        iframe.src = url;
    }

    iframe.frameBorder=0;

    return iframe;
}


function launchSystemCheck(){  
   if (!dojo.render.html.ie && typeof BPLoginFlash_DoFSCommand != 'undefined') {
        return true;
   }
    
   var url = "/apps/systemCheck/systemCheck.jsp";
   var name = "SystemCheck";
   var id = "SystemCheck";
   var title = "System Check";
   iconSrc = "/images/tool.png";

   var srcNode = createIframe( url, name );
   srcNode.widgetID = id;
   srcNode.scrolling = "yes";

   var div = document.createElement("div");
   div.appendChild(srcNode);
   document.body.appendChild(div);

   var width = 800;
   var height = 600;
   width = checkWidth( width );
   height = checkHeight( height );

   var left = getLeft(width);
   var top = getTop(height);

   div.style.height = "100%"
   div.style.width = "100%";
   div.style.padding = "0px";
   div.style.margin = "0px";
   var tempWin;

   var floatWin = dojo.widget.createWidget("swdojo:FloatingPaneWebOS", {  id : id,
                                                                title : title,
                                                                iconSrc : iconSrc,
                                                                toggle : "explode",
                                                                executeScripts : true,
                                                                hasShadow : true,
                                                                resizable : true,
                                                                windowState : "normal", 
                                                                url : url,
                                                                constrainToContainer: false,
                                                                iframeName : name,
                                                                displayRefreshAction : true,
                                                                displayCloseAction : true,
                                                                displayMinimizeAction : false, 
                                                                displayMaximizeAction : true},
                                                                div);
   dojo.html.setStyleAttributes(floatWin.domNode,"position:absolute;top:" + top + "px;left:" + left + "px;width: " + width + "px; height: " + height + "px;z-index:1005");

   floatWin.onShow();
   document.body.appendChild(floatWin.domNode);
   if (window.fixPngs) {
        fixPngs(); //for tools.png
   }
   return false;
}

function checkSystemLanuch() {
    if (document.cookie) {
        //alert(document.cookie);
        var startIndex = document.cookie.indexOf("systemcheck=");
        if (startIndex > -1) {
            var endIndex = document.cookie.indexOf(";", startIndex);
            //alert(startIndex);
            //alert(endIndex);
            if (endIndex == -1) {
                endIndex = document.cookie.length;
            }
            var shouldLauchSystemCheck = document.cookie.substring(startIndex + 12,endIndex);
            //alert("shouldLauchSystemCheck: " + shouldLauchSystemCheck);
            if (shouldLauchSystemCheck == 1) {
                var lastYear = new Date();
                lastYear.setFullYear(lastYear.getFullYear() - 1);
                document.cookie = "systemcheck=0;expires=" + lastYear.toGMTString();
                setTimeout("launchSystemCheck();", 1000);
            }
        }
    }
}

function checkLoginBoxHeight() {
    var availHeight = document.body.clientHeight;

    var loginWidget = dojo.widget.getWidgetById("LoginBox");
    var mb = dojo.html.getMarginBox(loginWidget.domNode);
    if (mb.height > availHeight) {
        //find space between links and footer
        var linksContainer = loginWidget.domNode.getElementsByTagName("P");
        if (linksContainer != null && linksContainer.length > 0) {
            if (linksContainer[0].className == "loginBlock") {
                var offsetHeight = linksContainer[0].offsetTop + linksContainer[0].offsetHeight;
                var footer = document.getElementById("loginFooter");
                if (footer) {
                    var shrink = footer.offsetTop - offsetHeight;
                    if (shrink > 15) {
                        loginWidget.resizeTo(mb.width,mb.height - shrink + 15);
                        loginWidget.center();
                    }
                }
            }
        }
    }
    
}