
function fbs_click(){
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function twitter_click(status){
    u=location.href;
    t=document.title;
    window.open("http://twitter.com/home?status="+status,'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

$(document).ready( function(){


    // -- My Annual Report Dropdown
    // --------------------------------------------------------------
    var timeout_down;
    var timeout_up;

    $('#pagecart-dropdown-primary').hover(
        function(){
            timeout_down = setTimeout(function() {
                clearTimeout(timeout_up);
                $(this).addClass('down');
                $('#pagecart-dropdown-primary ul:hidden').slideDown('fast');
            }, 100);
        },
        function(){
            timeout_up = setTimeout(function() {
                clearTimeout(timeout_down);
                $(this).removeClass('down');
                $('#pagecart-dropdown-primary ul:visible').slideUp('fast');
            }, 100);
        }
    );

    $('#pagecart-dropdown-primary-link').click(
        //function() { return false; }
    );

    $('#pagecart-dropdown-save a').click(
        function () {
            if ($(this).hasClass('disabled')) {
                return false;
            }
            
            $.getJSON( make_ajax_url($(this).attr('href')), {}, function(data) {
                $('#above-pagecart-dropdown').html(data.message).removeClass('hidden').show().fadeOut(3000);
                if (page_added == 0) {
                    cur_pages = $('#changeme .cufon cufontext').html();
                    cur_pages = parseInt(cur_pages);
                    $('#changeme .cufon cufontext').html(cur_pages + 1);
                    page_added=1;
                }
                
                Cufon.refresh();
            });
            Cufon.refresh();
            return false;
            
        }
      
    );

    function savePage(id){
        $.getJSON( make_ajax_url(workspace.index + '/' + page_slug + '/add/' + id.replace("id:","")), {}, function(data) {
            return true; // do nothing
        });
        return false;
    }

    function removePage(id){
        $.getJSON( make_ajax_url(workspace.index + '/' + page_slug + '/delete/' + id.replace("id:","")), {}, function(data) {
            return true; // do nothing
        });
        return false;
    }

    function removeAllPages(){
        $('#pagecart-report-form .success').hide();
        $('#pagecart-report-form .error').hide();
        $.getJSON( make_ajax_url(workspace.index + '/' + page_slug + '/deleteall/' ), {}, function(data) {
            $('#pagecart-report-form').prepend('<p class="success">' + data.message + '</p>');
        });
        return false;
    }

    // -- My Annual Report
    // --------------------------------------------------------------

    $('#pagecart-wrapper p').css('cursor', 'pointer');

    // >> click on p (highlight, check/uncheck box)
    $('#pagecart-wrapper p').click( function(e){

       var box = $(this).find('input:checkbox');
       var li = $(this).parent();

       if( box.attr('checked') == false ){
           box.attr('checked', 'checked');
           $(this).addClass('selected');
           savePage($(this).attr("id"));
           pageSavedMsg(li);
       } else {
            box.attr('checked', '');
            $(this).removeClass('selected');
            removePage($(this).attr("id"));
            pageRemovedMsg(li);
       }

       return false;
    });


    $('#pagecart-wrapper p').hover(
        function(){
            var linkto = $(this).find('a').attr('href');
            $(this).append($('#pagecart-messages .viewpage').html());
            $(this).find('.ar-view').attr('href', linkto);

            $(this).find('.ar-view').click( function(e){
                window.location.href = $(this).attr('href');
                e.originalEvent.cancelBubble = true; // don't bubble up
            });
        },
        function(){
            //$(this).find('.ar-view').fadeOut();
          $(this).find('.ar-view').remove();
        }
    );

    // >> click on checkbox
    $('#pagecart-wrapper input:checkbox').click( function(e){
        var li = $(this).parent().parent();

        if( $(this).attr('checked') == true ){
            $(this).parent().addClass('selected');
            savePage($(this).parent().attr("id"));
            pageSavedMsg(li);
        } else {
            $(this).parent().removeClass('selected');
            removePage($(this).parent().attr("id"));
            pageRemovedMsg(li);
        }

       e.originalEvent.cancelBubble = true; // don't bubble up
    });

    /*
    $('#pagecart-wrapper p a').click( function(e){
        window.location.href = $(this).attr('href');
    });
    */

    function pageSavedMsg( li ){
        li.find('p:first .msg').remove();
        $('#pagecart-messages .pageadded').clone().addClass('msg').prependTo( li.find('p:first') ).hide().fadeIn('slow');
        setTimeout(function(){ removeMsg(li.find('p:first .msg')) }, 2000);
    }

    function pageRemovedMsg( li ){
        li.find('p:first .msg').remove();
        $('#pagecart-messages .pageremoved').clone().addClass('msg').prependTo( li.find('p:first') ).hide().fadeIn('slow');
        setTimeout(function(){ removeMsg(li.find('p:first .msg')) }, 2000);
    }

    function removeMsg( msg ){
        msg.fadeOut();
    }


    // remove all pages
    $('#centrecolumn .pagecart-controls .removepages').click( function(){
        if(!confirm($('#pagecart-messages .confirmremove').html())){
            return false;
        }
        removeAllPages();
        $('#pagecart-wrapper input:checkbox').attr('checked', '');
        $('#pagecart-wrapper p').removeClass('selected');
        return false;
    });



    // -- Thickbox
    // --------------------------------------------------------------

    $('.thickbox').hover(
        function(){
            $(this).prepend('<div class="magnify"></div>');
            $(this).find('.magnify').hide().fadeIn('fast');
        },
        function(){
            $(this).find('.magnify').remove();
        }
    );


});

function make_ajax_url(url) {
    return url.replace(workspace.root_url, workspace.root_url + '/xml.php');
}