
$(document).ready(function(){
    $('#login-username').each(function() {
        var default_value = this.value;
        $(this).css('color', '#666');
        $(this).focus(function() {
            if(this.value == default_value) {
                this.value = '';
                $(this).css('color', '#333');
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                $(this).css('color', '#666');
                this.value = default_value;
            }
        });
    });

    $('.body_health').each(function() {
        var default_value = this.value;
        $(this).css('color', '#CCC');
        $(this).focus(function() {
            if(this.value == default_value) {
                this.value = '';
                $(this).css('color', '#000');
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                $(this).css('color', '#CCC');
                this.value = default_value;
            }
        });
    });

    $('#password-clear').css('color', '#666');

    $('#password-clear').show();
    $('#password-password').hide();
 
    $('#password-clear').focus(function() {
        $('#password-clear').hide();
        $('#password-password').show();
        $('#password-password').focus();
    });
    $('#password-password').blur(function() {
        if($('#password-password').val() == '') {
            $('#password-clear').show();
            $('#password-password').hide();
        }
    });

    $("select#filter-country").change(function(){
        if ($(this).val() != 0)
        {
            $('#filter-loading-cities').show();
            $.post(site_url + 'ajax/json_city_by_country', {
                id: $(this).val()
            },
            function(j){
                var options = '<option value="0">' + lang_global_city +'</option>';
                for (var i = 0; i < j.length; i++) {
                    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                }
                $("select#filter-city").html(options);
                $('#filter-loading-cities').hide();
            }, "json");
        }
        else
        {
            $("select#filter-city").html('<option value="0" selected="selected">' + lang_global_city  + '</option>');
        }
			
    })

    var allowedcodes = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,8,9,46,13,17,18,37,38,39,40];
    $(".numericonly").keydown(function(event) {
        //alert(event.keyCode);
        if ($.inArray(event.keyCode, allowedcodes)===-1){
            //alert(event.keyCode);
            return false;

        }
    });

    $(".numericanddot").keydown(function(event) {
        //alert(event.keyCode);
        if ($.inArray(event.keyCode, allowedcodes)=== -1 && event.keyCode != 110 && event.keyCode != 190 ){
            //alert(event.keyCode);
            return false;

        }
    });

    $('input').keypress(function(event) {
        if($(this).attr('name') != "expr") {
            if (event.keyCode == 13) {
                $(this).parents('form').submit();
            }
        }
    });

    $('.fakefile input[type="button"]').mouseover(function(){
        $(this).parents('.fileinputs').children('input[type="file"]').css('right', "40px");
    });

    $('.fakefile input#fakefile').mouseover(function(){
        $(this).parents('.fileinputs').children('input[type="file"]').css('right', "0px");
    });
    
    $('input.file').change(function(){
        $(this).next("div.fakefile").children("input#fakefile").val($(this).val());
    });


    $("div.fakefile input").focus(function(){
        $(this).parent().prev('input.file').focus();
    });

    $("div.fakefile input").click(function(){
        $(this).parent().prev('input[type=file]').click();
    });

      
    // Caching the jQuery selectors:
    var panel = $('.panel');
    var timeout;

    $('.label').click(
        function(){
            // Setting a custom 'open' event on the sliding panel:

            clearTimeout(timeout);
            timeout = setTimeout(function(){
                panel.trigger('open');
            },500);
        });
     $('.label').mouseout(
        function(){
            // Custom 'close' event:

            clearTimeout(timeout);
            timeout = setTimeout(function(){
                panel.trigger('close');
            },500);
        });
        
    var loaded=false;	// A flag which prevents multiple ajax calls to geodata.php;

    // Binding functions to custom events:

    panel.bind('open',function(){
        panel.slideDown(function(){
            if(!loaded)
            {
                // Loading the countries and the flags once the sliding panel is shown:
                panel.load(site_url+'index.php/statistics/online');
                loaded=true;
            }
        });
    }).bind('close',function(){
        panel.slideUp();
    });


});
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name)
{
    createCookie(name,"",-1);
}
// /cookie functions

// Local vars for toggle
var availableStylesheets = [];
var activeStylesheetIndex = 0;

// To loop through available stylesheets
$.stylesheetToggle = function()
{
    activeStylesheetIndex ++;
    activeStylesheetIndex %= availableStylesheets.length;
    $.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
};

// To switch to a specific named stylesheet
$.stylesheetSwitch = function(styleName)
{
    $('link[@rel*=style][title]').each(
        function(i)
        {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) {
                this.disabled = false;
                activeStylesheetIndex = i;
            }
        }
        );
    createCookie('style', styleName, 365);
};

// To initialise the stylesheet with it's
$.stylesheetInit = function()
{
    $('link[rel*=style][title]').each(
        function(i)
        {
            availableStylesheets.push(this.getAttribute('title'));
        }
        );
    var c = readCookie('style');
    if (c) {
        $.stylesheetSwitch(c);
    }
};

$.stylesheetInit();


