/* $Id: shared.js,v 1.11 2010/10/24 00:45:54 sap Exp $ */


var LAST_DROP_POS = false;

$(document).ready(function () {
    
    /* navigation */
    $('#header_nav ul li').hover(
        function(){
            var m = $(this);
            m.children('ul').addClass('active');
        },
		/* 
		 * function(){
		 *     var m = $(this);
		 *     setTimeout(function() {
		 *         m.children('ul').removeClass('active');
		 *     }, 2000);
		 * }
		 */
		function(){
		    var m = $(this);
		    m.children('ul').removeClass('active');
		}
    );

    /* popup */
    $('a#login').click(function(){
        $('#popup_o').show();
        $('#popup').show();
        //$('form#login input#password').val('');
    });

    $('#popup_close').click(function(){
        $('#popup_o').hide();
        $('#popup').hide();
        $('form#login input#password').val('');
    });

    /* login stuff */
    $('a#iforgot_link').click(function(){
        $('form#login').hide();
        $('h5#hlogin').hide();
        $('form#iforgot').show();
        $('h5#hiforgot').show();
    });

    // login
    $('a#login_submit').click(function(){
        $.post('/', {
                'mod': 'login',
                'e-login': '1',
                'username': $('form#login input#username').val(),
                'password': $('form#login input#password').val(),
                'remember': $('form#login input[name=remember_u]:checked').val()
            },
            function(data) {
                //alert(data)
                if (data != '-1') {
                    if (typeof (LOGIN_RELOAD) == 'undefined') {
                        $('li#logout_menu').show();
                        $('li#login_menu').hide();
                        $('#popup_o').hide();
                        $('#popup').hide();
                        $('li#logout_menu span').html(data);
                    }
                    else {
                        document.location.href = RETURN_URL;
                    }
                }
                else {
                    $('h5#hlogin').hide();
                    $('h5#login_err').show();
                }
            }
        );
        
        return false;
    });
    
    // logout
    $('a#logout_link').click(function(){
        $.post('/', {
                'mod': 'login',
                'e-logout': '1'
            },
            function(data) {                
                if (data == '1') {
                    if (typeof (LOGIN_RELOAD) == 'undefined') {
                        $('li#logout_menu').hide();
                        $('li#login_menu').show();
                    }
                    else {
                        document.location.href = RETURN_URL;
                    }
                }
            }
        );
        
        return false;
    });
    
    // re-show_login
    $('a#show_login').click(function(){
        $('h5#hiforgot').hide();
        $('p#iforgot_confirm').hide();
        $('h5#hlogin').show();
        $('form#login').show();
        $('form#login input#password').val('');
        return false;
    });
    
    // iforgot
    $('a#iforgot_submit').click(function(){
        $.post('/', {
                'mod': 'login',
                'email': $('form#iforgot input#email').val(),
                'e-iforgot': '1'
            },
            function(data) {                
                if (data == '1') {
                    $('form#iforgot').hide();
                    $('p#iforgot_confirm').show();
                }
            }
        );
        
        return false;
    });
    
    
    /* coerce css for inputs */
    $('input').each(function (i) {
        if (this.getAttribute('type') == 'text' || this.getAttribute('type') == 'password')
            $(this).addClass('text');
    });
    $('input').each(function (i) {
        if (this.getAttribute('type') == 'checkbox')
            $(this).addClass('checkbox');
    });
    $('input').each(function (i) {
        if (this.getAttribute('type') == 'radio')
            $(this).addClass('radio');
    });
    
    // clear newsletter email input
    $('#f-nlemail').focus(function(){
        if ($('#f-nlemail').val() == 'Enter email')
            $('#f-nlemail').val('');
    });
    
    // clear reports search input
    $('#report-kw').focus(function(){
        if ($('#report-kw').val() == 'Enter Keyword(s)')
            $('#report-kw').val('');
    });
    
    
});

    /****************
    *               *
    *   nformat()   *
    *               *
    ****************/
    function nformat (i, p)
    {
    var testnum = parseFloat(i);
    var num = isNaN(testnum) ? 0 : testnum;
    var nstr = num.toFixed(p) + '';
    x = nstr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;

        while (rgx.test(x1)) 
        {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }

    return x1 + x2;
    }
    
    /********************
    *                   *
    *   parse_float()   *
    *                   *
    ********************/
    function parse_float (i)
    {
    var str = i + '';
    str = str.replace(/[^0-9\.]/g, '');
    var n = parseFloat(str);
    
    return isNaN(n) ? 0 : n;
    }

