$.clientCoords = function() { // Taken from http://www.mail-archive.com/discuss@jquery.com/msg02537.html
     var dimensions = {width: 0, height: 0};
     if (document.documentElement) {
         dimensions.width = document.documentElement.offsetWidth;
         dimensions.height = document.documentElement.offsetHeight;
     } else if (window.innerWidth && window.innerHeight) {
         dimensions.width = window.innerWidth;
         dimensions.height = window.innerHeight;
     }
     return dimensions;
};

function confirmDelete()
{
  var agree = confirm("Are you sure you want to delete?");
  if (agree)  return true ; else  return false ;
}


$.Utils = {
    local_time_diff : 0,
    title : "",
    new_msgs : 0,
    auto_height : new Array(),
    auto_width : new Array(),
    pages_callback : 0,
    pages : 0,
    page : 1,

    get_int : function( value ) {
        if( !value ) {
            return 0;
        }
        return parseInt(value);
    },
    simple_ajax : function( url, variables, result_dest, reload_on_success, callback ) {
        return $.simple_ajax( url, variables, result_dest, reload_on_success, callback );
    },
   /* 
    show_centered_element : function( element, center_in ) {
        var all_width = center_in.width();
        var width = element.width();

        var left = (all_width - width) / 2 - this.get_int(element.css("border-left-width")) - this.get_int(element.css("padding-left"));

        // HACK for ie6
        if( $.browser.msie && !(typeof document.body.style.maxHeight != "undefined") ) {
            left -= all_width/2;
        }

        element.css("left", left );
        element.show();
    },

    show_dark_layer : function () {
        $("#dark-layer").show();
        if( $.browser.msie ) {
            $("#dark-layer").css("width", $("html").get(0).offsetWidth + "px" );
            $("#dark-layer").css("height", $("html").get(0).offsetHeight + "px" );
        }
    },
    
    hide_dark_layer : function () {
        $("#dark-layer").hide();
    },
*/
    // server_time formatted "HH:MM"
    set_time_diff : function( server_time ) {
        var now = new Date();
        
        var s_t = server_time.split(":");
        var s_min = parseInt(s_t[0]) * 60 + parseInt(s_t[1]);
        var c_min = now.getHours() * 60 + now.getMinutes(); 

        this.local_time_diff = parseInt( Math.round((c_min - s_min) / 60) );
    },

    // time formatted "HH:whatever_you_want"
    get_user_time : function( time ) {
        var h = parseInt( time.substr(0,2) );
        var rest = time.substr(3);

        var new_hour = ( h + this.local_time_diff + 24 ) % 24;

        return (new_hour>9?"":"0") + new_hour + ":" + rest;
    } ,
    
    update_title_set : function( number ) {
        this.new_msgs = number;
        var new_title = "";

        if( this.new_msgs > 0 )
            new_title = " (" + this.new_msgs + ")";

        document.title = this.title + new_title;
    } ,

    update_title : function( number ) {
        if( number )
            this.new_msgs += number;
        else
            this.new_msgs = 0;
        this.update_title_set( this.new_msgs );
    } ,
/*
    resize : function( ) {
        var coords = $.clientCoords();
        var h = coords.height - ( $("#header").height() + $("#footer").height() );
        var w = coords.width;
                
       //$("#content").height( h );

        for( selector in this.auto_height )
            $( selector ).height( h + this.auto_height[selector] );
        
        for( selector in this.auto_width )
            $( selector ).width( w + this.auto_width[selector] );
    } ,

    set_height : function( selector, val ) {
        this.auto_height[ selector ] = val;
    } ,

    set_width : function( selector, val ) {
        this.auto_width[ selector ] = val;
    } ,
*/

    trim : function( text ) {
        return text.replace(/^\s*/, "").replace(/\s*$/, "");
    } ,

    dec_value : function( selector ) {
        var num_el = $(selector);
        if( num_el.length > 0 ) {
            var new_num = parseInt(num_el.html()) - 1;
            num_el.html( new_num );
            return new_num;
        }
        return null;
    } ,

    page_click : function( page_num ) {
        return this.pages_callback( page_num );
    } ,
    
    update_pages : function(all_pages, this_page, pages_callback) {
        var str = "";
        this.pages = all_pages;
        this.page = this_page;
        if( pages_callback != undefined )
            this.pages_callback = pages_callback;

        // Don't show paging in case of single page
        if( all_pages <= 1 ) {
            $("#bottom_page").hide();
            return;
        } 
        $("#bottom_page").show();

        if( this_page == all_pages || all_pages == 0 )
            $("#load-next-page").hide();
        else
            $("#load-next-page").show();


        if( this_page == 1 )
            $("#load-prev-page").hide();
        else
            $("#load-prev-page").show();

        var page_link = function(num) {
            return ' <div class="page_number" onclick="$.Utils.page_click(' + num + ');"><a href="javascript:void(0);">' + num + "</a> </div>";
        }

        var start = this_page   > 4         ? this_page-2 : 1;
        var stop =  this_page+4 < all_pages ? this_page+3 : all_pages;

        if( start > 1 ) {
            str += page_link( 1 );
            str += "<div class='pages_more'>... </div>";
        }

        for( var i=start; i<=stop; i++ ) {
            if( i == this_page )
                str += "<div class='this_page'>" + i + "</div> ";
            else
                str += page_link(i);
        }

        if( stop != all_pages ) {
            str += "<div class='pages_more'>... </div>";
        }

        $("#page_nr").html( str );
    } ,

    init_pages : function ( onclick_callback ) {
        this.pages_callback = onclick_callback;
        $("#load-prev-page").click( function() { onclick_callback($.Utils.page-1); } );
        $("#load-next-page").click( function() { onclick_callback($.Utils.page+1); } );
    } ,

    init : function () {
        this.title = document.title;
    }
};

$(document).ready( function() { $.Utils.init(); } );
