AJAX_GATEWAY="ajaxGateway.php";
$(function() {
    initSearchHistory();
    initDocumentHistory();
    initFavButton();
    doFavourite('list','');
});

function initSearchHistory(){
    $("#SearchHistoryHandler a").click(function(e){
        e.preventDefault();
        $('#SearchHistoryHandler').removeClass('menuinact');
        $('#DocumentHistoryHandler').removeClass('menuact');
        $('#SearchHistoryHandler').addClass('menuact');
        $('#DocumentHistoryHandler').addClass('menuinact');        
        $.get(AJAX_GATEWAY, {
            action: 'ShowSearchHistory'
        }, function(data){                        
            $("#SearchHistoryPopup").html(data);            
            $("#SearchHistoryPopup").fadeIn('fast');
            initPopupHide('list_search_content','SearchHistoryPopup');
        });
    });
}

function initDocumentHistory(){
    $("#DocumentHistoryHandler a").click(function(e){
        e.preventDefault();
        $('#SearchHistoryHandler').removeClass('menuact');
        $('#DocumentHistoryHandler').removeClass('menuinact');
        $('#SearchHistoryHandler').addClass('menuinact');
        $('#DocumentHistoryHandler').addClass('menuact');        
        $.get(AJAX_GATEWAY, {
            action: 'ShowDocumentHistory'
        }, function(data){    
            
            $("#DocumentHistoryPopup").html(data);            
            $("#DocumentHistoryPopup").fadeIn('fast');
            initPopupHide('list_doc_content','DocumentHistoryPopup');
        });
    });
}

function initFavourites(){
    
}



function initPopupHide(popupDivId,popupContainerId){
    $("#"+popupDivId).mouseleave(function(){
     
        clearTO();
        setTO("popupHide('"+popupContainerId+"')");
        return false;
    });
}


function initFavButton(){
    $('#favButton').bind('mouseover', menu_open)
    $('#favButton').bind('mouseout',  menu_timer)
    $('#all_fav').bind('mouseover',   menu_canceltimer)
    $('#all_fav').bind('mouseout',    menu_timer)
}



function listFavourite(num){
    var username=document.getElementById('userName').innerHTML;
    $.get(AJAX_GATEWAY, {
        action: "doFavourite",
        username: username,
        doing: 'list',
        data: num
    },function(data){

        });
    return false;
}










function doFavourite(doing,data){
    $.get(AJAX_GATEWAY, {
        action: "doFavourite",
        doing: doing,
        data: data
    },function(data){
       
        $("#all_fav").html(data);
    });
    return false;
}


var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;



function menu_open(){
    menu_canceltimer();
    ddmenuitem = $('#all_fav').slideDown("slow");
}

function menu_close(){
    if(ddmenuitem){
        $('#all_fav').slideUp("slow");
    }
}

function menu_timer(){
    closetimer = window.setTimeout(menu_close, timeout);
}

function menu_canceltimer(){
    if(closetimer){
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}




document.onclick = menu_close;
