//refresh banners every 30 seconds
var totalRef = -1;
var lastTime = new Date();
var BANNER_REFRESH_TIME = 31500;
var GA_CATEGORY_SEARCH = "Search";
var GA_CATEGORY_NAVIGATION = "Navigation";

var GA_EVENT_TOP_MOVIES = "TopMovies";
var GA_EVENT_TOP_SERIES = "TopSeries";
var GA_EVENT_FEED = "Feed";

var GA_NAVIGATION_HOME = "Home";
var GA_NAVIGATION_CONTACT = "Contact";
var GA_NAVIGATION_NEWS = "News";
var GA_NAVIGATION_SUGGEST_IDEA = "Suggest an idea";
var GA_NAVIGATION_FORUM = "Forum";
var GA_NAVIGATION_HELP = "Help";
var GA_NAVIGATION_KEY_CLOUD = "Keyword Cloud";
var GA_NAVIGATION_KEY_STATS = "Keyword Stats";
var GA_NAVIGATION_ENGINE_STATS = "Engine Stats";
var GA_NAVIGATION_USER_PROFILE = "User Profile";
var GA_NAVIGATION_USER_LOGIN = "User Login";
var GA_NAVIGATION_USER_LOGOUT = "User Logout";
var GA_NAVIGATION_USER_LOGIN_FB = "User Login Facebook";
var GA_NAVIGATION_USER_SIGNUP = "User Signup";

var GA_LABEL_KEYWORD_PREFIX = "KW";

var AUTOCOMPLETE_MIN_LENGTH = 3;

var QUANTSERVE_URL = 'http://edge.quantserve.com/quant.js';
var WIBIYA_TOOLBAR_URL = 'http://cdn.wibiya.com/Toolbars/dir_0690/Toolbar_690613/Loader_690613.js';

$(document).ready(function() {
 
 $(function() {
            $('#e_ulist').sortable({
                containment: '#engine_list',
                opacity: 0.75,
                distance: 0.25,
                axis: 'y',
                stop: function(event, ui) {
                    reorderEngineList('#e_ulist');
                }
            });
    });
 resizeEngineList('#e_ulist');
 
    $("#collapse a, #collapse2 a").click(function() {
        collapseSidebar();
    });
    $('#collapse a, #collapse2 div a').mouseover(function(){
        if ($('#collapse').is(':visible')) {
            tooltip('#collapse', '#sidebar_tooltip', false);
        } else {
            tooltip('#arrow-resize', '#sidebar_tooltip', true,'show',-parseInt($('#arrow-resize').css('padding-top')));
        }
    });

    $('#collapse a, #collapse2 a').mouseout(function(){
        if ($('#collapse').is(':visible')) {
            tooltip('#collapse', '#sidebar_tooltip', false, 'hide');
        } else {
            tooltip('#collapse2', '#sidebar_tooltip', false, 'hide');
        }
    });
    $(window).resize(function() {
        if ($('#main-container').css('margin-left') == "22px") {
            collapseSidebar();
        }
        resizeIframe();
		 resizeEngineList('#e_ulist');
        $("#leftcol").css("min-height", $(window).height());
        //resize the banner section also
        $('#banner-section').width($(window).width() - 227);
        
        hideBodyScroll();
        resizeEngineList('#e_ulist');
        resizeEngineList('#e_blist');
    });
    resizeIframe();
 resizeEngineList('#e_ulist');
   


  
 
    //adding submenu functionality for stats menu
    $("ul.subnav").parent().append("<span></span>");
    $("#a_stats, #a_account, #a_help").mouseover(function() { //When trigger is clicked...
            //Following events are applied to the subnav itself (moving subnav up and down)
            $(this).parent().find("ul.submenu").slideDown('fast').show(); //Drop down the subnav on click

            $(this).parent().hover(function() {
            }, function(){
                    $(this).parent().find("ul.submenu").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
            });

            //Following events are applied to the trigger (Hover events for the trigger)
            }).hover(function() {
                    $(this).addClass("subhover"); //On hover over, add class "subhover"
            }, function(){	//On Hover Out
                    $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });
 

    if ($('#maincol').height() > $(window).height() - $('#topmenu').height()) {
        $('#maincol').height($(window).height() - $('#topmenu').height());
    }


});

function switch_engine(id, noSubmit) {
    if ($('input[name="data[query]"]').attr("value")) {
        $("#popup").hide();
        $('.engine').removeClass('selected');
        $('#engine_' + id).addClass('selected');
        $('input[name="data[engine_id]"]').attr("value", id);
        if (!noSubmit && $('input[name="data[query]"]').attr("value")) {
            $('#search_form').submit();
        }
    } else {
        $("#popup").show();
    }
}

// removes scrollbars of the body, adds scrollbar to the main content, to the engine list,removes overflow of content if in search mode
//resize engine iframe depending on window size
function resizeEngineList(list) {
    if ($(list).length) {
        $(list).css("overflow-y", 'auto');
        $(list).css("overflow-x", 'hidden');
        $(list).height($(window).height() - $("#search_box").height()-140);
    }
}

function resizeIframe() {
    //next step is to retrieve element spaces with jquery
    topmenuSpace = 30;
    usenetSpace = 90;
    bannerSpace = 10;
    bottomMargin = 10;
    rightColSpace = 40;
    shadowSpace = -25;
    //make the banner section appear fixed in position
    if ($("#torrent_window").length) {
        $('#banner-section').css('padding-left', '');
        $('#usenet-container').css('margin-left', '0px');
        $("#leftcol").css("height",'auto');

        $("#torrent_window").width($(window).width() - $("#leftcol").width() + shadowSpace);
        if ($('#rightcol').length) {
            $("#torrent_window").width($("#torrent_window").width() - $("#rightcol").width() - rightColSpace);
        }
        elementHeights = $("#topmenu").height()  + topmenuSpace + bottomMargin;
        if ($("#banner-section").length) {
            elementHeights += $("#banner-section").height() + bannerSpace;
        }
        if ($("#usenet-container").length) {
            elementHeights += $("#usenet-container").height() + usenetSpace;
        }
        $("#torrent_window").height($(window).height() - elementHeights -50);
    }
	
}

function collapseSidebar() {
    shadowSpace = 15;
    collapserSpace = 15;
    paddings = 22;
    elementsWidth = shadowSpace + collapserSpace + paddings;
    sidebarSpace = $('#sidebar').width() + shadowSpace;
    if ($('#main-container').css('margin-left') == "227px") {
        $('#sidebar').hide();
        $('#collapse2').show();
 $('#arrow-resize').show();
 $('#a_id_show').show();
  $('#searchbox_replacer').show();
        $('#banner-section').css('padding-left', '205px');
        $('#main-container').css('margin-left', '22px');
        //in search page
        if ($("#torrent_window").length) {
            $("#torrent_window").width($("#torrent_window").width() + sidebarSpace - elementsWidth);
            $("#usenet-container").width($("#usenet-container").width() + sidebarSpace - elementsWidth);
        }
    } else {
        //in search page
        if ($("#torrent_window").length) {
            $("#torrent_window").width($("#torrent_window").width() - sidebarSpace + elementsWidth);
            $("#usenet-container").width($("#usenet-container").width() - sidebarSpace + elementsWidth);
        }
        $('#banner-section').css('padding-left', '');
        $('#main-container').css('margin-left', '227px');
        $('#collapse2').hide();
        $('#sidebar').show();
    }
}

// removes scrollbars of the body, adds scrollbar to the main content, to the engine list,removes overflow of content if in search mode
function hideBodyScroll() {
    maincolSpace = 15;
   // $('body').css("overflow-y","hidden");
  //  $('body').css("overflow-x","hidden");

    $('#maincol').height($(window).height() - $('#topmenu').height());
    $('#maincol').css("overflow-y","hidden");
    $('#maincol').css("overflow-x","hidden");
    //ie
    //$('#maincol').width($(window).width() - maincolSpace - $('#leftcol').width());
//    $('#shadow-container').height($('#maincol').height());
//    $("#leftcol").css("min-height", ($(window).height() - $('#logo').height()-100));
    
}

function tooltip(target, element, changePosition, action, deviation) {
    if (deviation == undefined) {
        deviation = 10;
    } else {
        deviation = 10 + deviation;
    }
    if (changePosition == undefined) changePosition = false;
    var position = $(target).offset();
    var topPos = 0;
    if (changePosition == true) {
        topPos = position.top - deviation;
    } else {
        topPos = position.top;
    }
//tooltip = $('<div></div>');
    $(element).css('top',  topPos + 'px').css('left', (position.left + $(target).width()) + 'px');
    if (action != undefined) {
        if (action == 'hide') {
            $(element).hide();
        } else {
            $(element).show();
        }
    } else {
        $(element).show();
    }
}


