function global_cancelInvite() {
    var inviteCount = $("#inviteCount").html();
    if (inviteCount == 0) {
        $("#invitation_form ul li label, #invitation_form ul li input, #invitation_form ul li button").hide();
    } else {
        $("#inviteeEmailAddressInput").focus();
        $("#inviteeEmailAddressInput").attr("value", "");
    }
}

/**
 * Cookie functions
 *
 * By Peter-Paul Koch:
 * http://www.quirksmode.org/js/cookies.html
 */
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);
}

$(document).ready(function(){
    // Button click indicatior
    $('.popupContent button:not(#btnCancel)').live('click', function(){
        if ( $(this).parents("#make_history_form").length > 0 ) {
            $(this).remove('span#preloader').after('<span id="preloader">Creating new History...</span>');
        }else {
            $(this).remove('span#preloader').after('<span id="preloader">Please wait</span>');
        }
    });
    $('form').attr('autocomplete', 'off');


    // Put this somewhere meaningful
    if( $('ul.notifications').length != 0) $("abbr.timeago").timeago();

    if(readCookie('invites') == null){createCookie('invites', 'turned_on');}

    // Invites JS
    if($('#invite-holder').length != 0) {
        $('#pageTop').addClass('invites');
        $('.wideInfoBox').addClass('invites');
    }
    if(readCookie('invites') == "turned_off"){
        $('#pageTop').removeClass('invites');
        $('#invite-holder').remove();
    }

    $('#close-invites').live('click', function(){
        createCookie('invites', 'turned_off');
        $("#box p").slideToggle('fast');
        $(this).toggleClass('active');

        $('#pageTop').removeClass('invites');
        $('#invite-holder').slideUp('fast', function () {
            $('#invite-holder').remove();
        });
    });
    $('#send-invite').click( function(){
        $('#invite-block p').animate({width: 'toggle'}, 300);
        $('#invite-block form').fadeIn(1000);

        return false;
    });

    // Private post background toggle
    $('.popupContent .privatePost span').live('click', function(){
        alert('weee');
        $(this).parentsUntil('.body').parent().toggleClass('private');
    });

    // Adds custom background to finish registration page
    if($('#finishRegistration').length != 0) {
        $('#content').addClass('finish-registration');
    }

    // Removes margins on last 3 featured posts
    $('ul.featuredPosts li:gt(2)').css({'marginBottom': 0});

    $("input[type='text'], input[type='password']").live('focus', function () {$(this).addClass("selectedField");});
    $("input[type='text'], input[type='password']").live('blur', function () {$(this).removeClass("selectedField");});

    $("#pageTop input[type='text']").live('focus', function () {if(this.value==this.defaultValue)this.value='';});
    $("#pageTop input[type='text']").live('blur', function () {if(this.value=='')this.value=this.defaultValue;});

    /* Make links underlined on history image thumbnail hover. */
    $(".homeColumnImg").live("mouseover", function() {
        $(this).next().find(".featuredStoryTitle a:first").addClass("underLined");
    });

    $(".homeColumnImg").live("mouseout", function() {
        $(this).next().find(".featuredStoryTitle a:first").removeClass("underLined");
    });
    $(".homeColumnThumb").live("mouseover", function() {
        $(this).next().find("a:first").addClass("underLined");
    });

    $(".homeColumnThumb").live("mouseout", function() {
        $(this).next().find("a").removeClass("underLined");
    });

    $("#pageTop fieldset input").focus(function () {$("#pageTop fieldset").addClass("selectedSearch");});
    $("#pageTop fieldset input").blur(function () {$("#pageTop fieldset").removeClass("selectedSearch");});

    $(".commentForm fieldset span textarea").focus(function () {$(this).parent().addClass("selectedextarea");});
    $(".commentForm fieldset span textarea").blur(function () {$(this).parent().removeClass("selectedextarea");});

    $("button").focus(function () {$(this).blur();});
    $("textarea").css("resize","none");
/*
    $(".entryOptionsBox ul li a").toggle(
        function () {$(this).addClass("activeOption");},
        function () {$(this).removeClass("activeOption");});
    */

    $(".galleryMainImg").hover(
        function () {$(".galleryMainImg span").css("backgroundPosition","0 -280px");$(".galleryMainImg ul").css("display","block");},
        function () {$(".galleryMainImg span").css("backgroundPosition","0 0");$(".galleryMainImg ul").css("display","none");});

    $(".userPhoto").hover(
        function () {$(".userPhoto span").css("backgroundPosition","0 -280px");$(".userPhoto ul").css("display","block");},
        function () {$(".userPhoto span").css("backgroundPosition","0 0");$(".userPhoto ul").css("display","none");});

    $(".galleryThumbsHolder ul li a").click(function() {
        GalleryHelper.setActiveImage($(this), get_resize_url($(this).attr("id")), $(this).attr("id").charAt($(this).attr("id").length-1));
        return false;
    });
/*
    $(".galleryThumbsHolder ul li a").click(
        function () {
            var main_image_url = get_resize_url($(this).attr("id"));
            $("#galleryMainImage").attr("src", main_image_url);
            $(".galleryThumbsHolder ul li a").removeClass("activeThumb");
            $(this).addClass("activeThumb");
            return false
        });
*/
    $(".galleryThumbsHolder ul li:last").css("margin","0");

    $(".commentSendButton button").hover(
        function () {$(this).addClass(".hoverButton");$(".commentSendButton button").addClass("hoverButton");},
        function () {$(this).removeClass(".hoverButton");$(".commentSendButton button").removeClass("hoverButton");});

    $('input[type=checkbox], input[type=radio]').checkbox();
//  $("select.topSearch, .wideSelectHolder select, .wideSelectHolderThin select").jdrop();

    $(".top-news-carousel").jcarousel({scroll: 4, visible: 4});
//  $(".side-news-carousel").jcarousel({scroll: 1, visible: 2});
//  $(".top-news-carousel").jcarousel({scroll: 1, visible: 4});


    $(".topCommBoxContent").hide();
    $(".topCommBoxTriger").toggle(
      function () {
        $(this).addClass('shrink');
        $(".topCommBoxContent").slideDown();
        $(".topCommBoxTriger").addClass("activeTopCommTriger");
      },
      function () {
        $(this).removeClass('shrink');
        $(".topCommBoxContent").slideUp();
        $(".topCommBoxTriger").removeClass("activeTopCommTriger");
      }
    );

//  $('a.openup').facebox()

    $('ul.hiddenArrows').parent().parent().children().first().addClass('dontShow').next().addClass('dontShow');

    var statboxes = $(".statistics-bar");
    var max = 0;
    
    statboxes.each(function(){
        var val = parseInt($(this).find("span").html());
        if (val > max) {
            max = val;
        }
    });
    var step = Math.round(145 / max);

    statboxes.each(function(){
       var val = parseInt($(this).find("span").html()); 
       var width = val * step;
       $(this).animate({"width": width}, 1400);
    });

});

