var ie  = document.all;
var nn6 = document.getElementById&&!document.all;

var in_drag;
var x_pos,y_pos;
var drag_obj;
var tx,ty;

paint_login_box(true);

function paint_login_box(first)
{
    var myimages = get_cookie('myimages');
    if (!myimages || myimages == '')
        // not registered
        on_uname_fail();
    else
    {
        // have cookies
        var user = get_cookie('isUSER');
        if (!user)
        {
            // have cookies but have username is not known yet
             if (first)
             {
                 var callback =
                 {
                     success:  on_uname_ok,
                     failure:  on_uname_fail,
                     argument: null
                 };
                 YAHOO.util.Connect.asyncRequest('GET', '/getusername.php?r=' + Math.random(), callback, null, null);
             }
             else
                 on_uname_fail();
        }
        else
        if (user.indexOf('-') >= 0)
        {
            // have cookies but username is empty
            var html = '<a href="http://my.imageshack.us/registration/logout.php" onClick="pageTracker._trackEvent(\'header-click\',\'old-header-logout\');" class="menu">Logout</a>';
            get('lb_container').innerHTML = html;
            get('loginbox').style.display = 'inline';
        }
        else
        {
           var html = '<span style="font-family:Tahoma,Arial;font-size:12px">User: <a class="menu" href="http://profile.imageshack.us/user/' + user + '" onClick="pageTracker._trackEvent(\'header-click\',\'old-header-username\');">' + user + '</a></span> | <a href="http://my.imageshack.us/registration/logout.php" onClick="pageTracker._trackEvent(\'header-click\',\'old-header-signout\');" class="menu">Logout</a>';
           get('lb_container').innerHTML = html;
           get('loginbox').style.display = 'inline';
        }
    }
}

function on_uname_ok(rsp)
{
    paint_login_box(false);
}

function on_uname_fail(rsp)
{
    var html = '<a href="http://my.imageshack.us/registration" onClick="pageTracker._trackEvent(\'header-click\',\'old-header-register\');" class="menu">SignUp</a> | <a href="javascript:void(0)" onclick="popup_login();pageTracker._trackEvent(\'header-click\',\'old-header-signin\');return false" class="menu">Login</a>';
    get('lb_container').innerHTML = html;
    get('loginbox').style.display = 'inline';
}

function dark()
{
    var mist = get('mist');
    mist.style.width = document.body.scrollWidth + "px";
    mist.style.height = document.body.scrollHeight + "px";
    mist.style.visibility = "visible";
    window.onresize = dark;
}

function tt(html_body, o, e, width, noborder , customhandler)
{

    if(!customhandler) customhandler='';

    var html ='';

    html+='<table border="0" cellspacing="0" cellpadding="0" width="100%">';
    html+='  <tr style="cursor:move">';                             
    html+='   <td>';    
    html+='    <table border="0" cellspacing="0" cellpadding="0" width="100%">';
    html+='         <tr style="cursor:move">';
    html+='             <td style="height:30px;width:30px;background-image: url(' + iurl('lbleft.gif') + ');">&nbsp;</td>';
    html+='             <td style="height:30px;background-image: url(' + iurl('lbcenter.gif') + ');vertical-align: absmiddle; background-repeat: repeat-x;">';
    html+='                 &nbsp;&nbsp;ImageShack&#174;';
    html+='             </td>';
    html+='             <td width="100%" align="right" style="height:30px;width:39px;background-image: url(' + iurl('lbright.gif') + ');">';
    html+='                 <a href="javascript:void(0)" onclick="'+  customhandler  +';hidetip();light();unmask();return false;"><img src="' + iurl('winclose.gif') + '" style="height:28px;border:none"/></a>';
    html+='              </td>';
    html+='         </tr>';
    html+='     </table>';
    html+='  </td>';
    html+='  </tr>';
    html+='  <tr>';
    html+='        <td align="center">';
    html+=              '<table border="0" cellspacing="7" cellpadding="0" id="fxholder" width="100%"><tr><td>';
    html+=                   html_body;
    html+=              '</td></tr></table>';
    html+='        </td>';
    html+='    </tr>';
    html+='</table>';


    var d = get_win_rect();
    if (!d)
    {
        fixedtooltip(html, o, e, width);
        return;
    }
    if (window.event) 
        event.cancelBubble=true;
    else if (e.stopPropagation) 
        e.stopPropagation();

    clearhidetip();
    dropmenuobj=document.getElementById? document.getElementById("fixedtipdiv") : fixedtipdiv;
    dropmenuobj.innerHTML=html;


    dropmenuobj.style.backgroundColor = "transparent";
    dropmenuobj.style.borderTop       = "0px";
    dropmenuobj.style.borderLeft      = "0px";
    dropmenuobj.style.borderRight     = "0px";
    dropmenuobj.style.borderBottom    = "0px";



    dropm  = get('fxholder');
    dropm.style.backgroundColor = '#ffffff';


    if (noborder)
        dropm.style.border = 'none';
    else
    {
        dropm.style.borderTop    = '0px solid gray';
        dropm.style.borderLeft   = '1px solid gray';
        dropm.style.borderRight  = '1px solid gray';
        dropm.style.borderBottom = '1px solid gray';

    }


    if (ie4||ns6)
    {
        showhide(dropmenuobj.style, e, "visible", "hidden", width);
        var eh = dropmenuobj.offsetHeight;
        var ew = dropmenuobj.offsetWidth;
        dropmenuobj.style.left=d[0] + Math.ceil((d[1] - d[0] - ew) / 2) + 'px';
        dropmenuobj.style.top=d[2] + Math.ceil((d[3] - d[2] - eh) / 2) + 'px';
        dropmenuobj.style.zIndex = 10001;
    }
}

function get_win_rect() 
{
    try
    {
        var ver=navigator.appVersion;
        var dom=document.getElementById?1:0;
        var ie6=(ver.indexOf("MSIE 6")>-1 && dom)?1:0;
        var ie7=(ver.indexOf("MSIE 7")>-1 && dom)?1:0;
        var opera5=navigator.userAgent.indexOf("Opera")>-1;

        var w, h, sx, sx; 

        if (ns6 || opera5) 
        { 
            w  = self.innerWidth; 
            h  = self.innerHeight; 
            sx = window.pageXOffset; 
            sy = window.pageYOffset; 
        } 
        else if (ie6 || ie7) 
        { 
            w = document.documentElement.clientWidth; 
            h = document.documentElement.clientHeight; 
            if (w==0) 
            { 
                w = document.body.clientWidth; 
                h = document.body.clientHeight; 
            } 
            sx = document.body.scrollLeft + document.documentElement.scrollLeft; 
            sy = document.body.scrollTop + document.documentElement.scrollTop; 
        } else if (ie4) 
        { 
            w = document.body.clientWidth; 
            h = document.body.clientHeight; 
            sx = document.body.scrollLeft; 
            sy = document.body.scrollTop; 
        } 
        if (isNaN(sx) || isNaN(sy) || isNaN(w) || isNaN(h))
            return null;
        return [sx, w + sx, sy, h + sy];
    }
    catch (err)
    {
        return null;
    }
} 

function popup_login()
{
    dark();

    mask();

    var html = '';
    html+='<table border="0" cellspacing="0" cellpadding="0">';
    html+='    <tr>';
    html+='        <td>&nbsp;</td>';
    html+='        <td align="center">';
    html+='            <table border="0">';
    html+='                <tr>';
    html+='                    <td colspan="2" nowrap="nowrap" style="text-align:center">';
    html+='                        <div id="lb_error" style="display:none;color:red"></div>';
    html+='                    </td>';
    html+='                </tr>';
    html+='                <tr>';
    html+='                    <td>Username/Email:</td>';
    html+='                    <td><input type="text" id="lb_user" name="username" style="width:200px"/></td>';
    html+='                </tr>';
    html+='                <tr>';
    html+='                    <td>Password:</td>';
    html+='                    <td><input type="password" id="lb_pass" name="password" style="width:200px" onkeydown="if(event.keyCode==13) lb_login()"/></td>';
    html+='                </tr>';
    html+='            </table>';
    html+='            <div style="text-align:center" id="lb_controls">';
    html+='                <a href="javascript:void(0)" onclick="lb_login()"><img src="' + iurl('login.gif') + '" style="width:56px;height:23x;border:none" alt="Login" title="Login"/></a>';
    html+='            </div>';
    html+='            <div style="text-align:left">';
    html+='                If you are already registered with ImageShack, but do not have a password, ';
    html+='                please log in using your registration link and click on "Preferences" to create your new password.<br/>';
    html+='            </div>';
    html+='            <div style="text-align:center;">';
    html+='                <a class="ser" href="http://my.imageshack.us/registration/passwordrecovery.php" onClick="pageTracker._trackEvent(\'header-click\',\'login-forgotpass\');">Forgot your password?</a>';
    html+='            </div>';
    html+='        </td>';
    html+='        <td>&nbsp;</td>';
    html+='    </tr>';
    html+='    <tr>';
    html+='        <td>&nbsp;</td>';
    html+='        <td>&nbsp;</td>';
    html+='        <td>&nbsp;</td>';
    html+='    </tr>';
    html+='</table>';
    var e = {type:'click'};
    tt(html, document.body, e, '400px', false);
    get('lb_user').focus();
}

function light()
{
    get('mist').style.visibility = "hidden";
    window.onresize = null;
}

function lb_login()
{
   if( pageTracker )
     pageTracker._trackEvent('header-click','login-submit');
 
    var e = get('lb_error');
    if (e)
        e.style.display = 'none';
    e = get('lb_controls');
    if (e)
        e.style.display = 'none';
    var callback =
    {
        success:  on_login_ok,
        failure:  on_login_fail,
        argument: null
    };
    YAHOO.util.Connect.asyncRequest('POST', '/auth.php', callback, 'username=' + encodeURIComponent(get('lb_user').value) + '&password=' + encodeURIComponent(get('lb_pass').value), null);
}

function on_login_ok(rsp)
{
    if (rsp.responseText != 'OK')
    {
        on_login_fail(rsp);
        return;
    }
    document.location.replace('http://my.imageshack.us/v_images.php');
}

function on_login_fail(rsp)
{
    var e = get('lb_error');
    if (!e)
        return;
    e.innerHTML = 'Username and password you\'ve entered do not match';
    e.style.display = '';
    e = get('lb_controls');
    if (e)
        e.style.display = '';
}

function dodrag(e)
{
    if (in_drag)
    {
        drag_obj.style.left = (nn6 ? tx + e.clientX - x_pos : tx + event.clientX - x_pos) + 'px';
        drag_obj.style.top  = (nn6 ? ty + e.clientY - y_pos : ty + event.clientY - y_pos) + 'px';
        return false;
    }
}

function startdrag(e) 
{
    var fobj = nn6 ? e.target : event.srcElement;
    if (!fobj || fobj.tagName == 'TEXTAREA' || fobj.tagName == 'INPUT' || fobj.tagName == 'SELECT' || fobj.tagName == 'OPTION')
        return true;
    while (fobj && fobj.id != 'fixedtipdiv')
        fobj = nn6 ? fobj.parentNode : fobj.parentElement;

    if (fobj && fobj.id=="fixedtipdiv")
    {
        in_drag = true;
        drag_obj = fobj;
        tx = parseInt(drag_obj.style.left+0);
        ty = parseInt(drag_obj.style.top+0);
        x_pos = nn6 ? e.clientX : event.clientX;
        y_pos = nn6 ? e.clientY : event.clientY;
        document.onmousemove=dodrag;
        return false;
    }
}

function mask()
{
    var o = get('showplayer');
    if (o)
        o.style.display = 'none';
    o = get('fastsmalladdiv');
    if (o)
        o.style.display = 'none';

    var o = get('yieldaddiv');
    if (o)
        o.style.visibility = 'hidden';
    o = get('ymaddiv');
    if (o)
        o.style.visibility = 'hidden';
    o = get('resizeoptions');
    if (o)
        o.style.visibility = 'hidden';
}

function unmask()
{
    var o = get('lb_user');
    if (o)
        o.style.display = 'none';
    o = get('lb_pass');
    if (o)
        o.style.display = 'none';
    o = get('showplayer');
    if (o)
        o.style.display = '';
    o = get('fastsmalladdiv');
    if (o)
        o.style.display = '';
    o = get('yieldaddiv');
    if (o)
        o.style.visibility = 'visible';
    o = get('ymaddiv');
    if (o)
        o.style.visibility = 'visible';
    o = get('resizeoptions');
    if (o)
        o.style.visibility = 'visible';

}

document.onmousedown = startdrag;
document.onmouseup=function () {in_drag=false};
