var current_page = 0;
var bio_page = 1;
var pager_q = null; //the search term for the pager for google lists
var pager_type = null; //search listings or tab listings
var kind = ''; //todaynow, etc as url param to /list
var cookie_name = "onn_tabs";
var current_video = null;
var share_url, share_title;
var page_increment = 8;

function video_tabs_init(current_id) {
    current_video = "video_" + current_id;
    video_read_cookie();
    if(kind !== "") {
        $("#video_tabs li").removeClass("current");
        var label = $("#video_tabs #"+kind+" a").attr('rel');
        $("#video_search .label").html(label);
        $("#video_tabs #"+kind).addClass("current");
        var url = "/ajax/onn_playlist/"+ kind +"/"+ current_page +"/";
    }
    else {
        $("#video_search .label").html('Most Recent');
        kind = "mostrecent";
        var url = "/ajax/onn_playlist/"+kind+"/1/";
    }
    video_load(url);
    video_change_category();
}

function video_load(url) {
    display_loading("fade"); //start fade out
    $("#video_tab_content .content").load(url, function(){
        display_loading(); //fade back on completion
        pager_display(current_page);
        $("."+current_video).addClass("nowplaying");
        // split href of li>p,.current_video on comma, iterate results into data container. 
    });
}

function video_update_cookie(category,page) {
    //keep track of the cateory and page number theyre on
    var value = category+","+page; //comma sep category,page be explicit
    var days = 7;
    afns_createCookie(cookie_name,value,days);
}

function video_read_cookie() {
    try {
       //should return the category kind and page
       var str = afns_readCookie(cookie_name);
        if(str) {
            var data = str.split(",");
            kind = data[0];
            current_page = data[1];
        }
    }
    catch(err) {
    }
}

function video_change_category() {
    $("#video_tabs li").click(function () {
        if (!$(this).hasClass("category")){
            //change the label spot
            var label = $('a', this).attr('rel');
            $("#video_search .label").html(label);
            //what kind of list is this
            kind = $(this).attr('id');
            var url = "/ajax/onn_playlist/"+kind+"/1/";
            current_page = 1;
            //will init the cookie w the kind and num 1
            video_update_cookie(kind, current_page);
            video_load(url);
            //if a tab is set to current, set its BG to default, then set this BG
            $("#video_tabs li.current").removeClass("current");
            $(this).addClass("current");
            pager_type = null;
            $(".pager").show(); 
        }
   });
}

function pager(page) {
    if(page == "previous") {
        var page = parseInt(current_page) - 1;
    }
    else if(page == "next") {
        var page = parseInt(current_page) + 1;
    }
    var num = page;
    if(pager_type == undefined || pager_type == null) {
        var url = "/ajax/onn_playlist/"+kind+"/"+num+"/";
    }
    else {
        var url = "/ajax/onn_search/"+encodeURIComponent(pager_q)+"/"+num+"/";
    }
    current_page = page;
    video_update_cookie(kind, current_page);
    video_load(url);
}

function bioPage(page){
    if(page == "previous") {
        var page = (bio_page - 1);
    }
    else if(page == "next") {
        var page = (bio_page + 1);
        
    }
    bio_page = page;
    if (bio_page > 2) $(".pager a.next").hide();
    else if (bio_page < 2) $(".pager a.previous").hide();
    else {
        $(".pager a.next").show();
        $(".pager a.previous").show();
    }
    for (i = 0; i < 3; i++) $(".section" + (i + 1)).hide();
    $(".section" + bio_page).show();
    $(".pager a.current").removeClass("current");
    $(".pager a.page"+bio_page).addClass("current");
    return false;
}

function display_loading(state) {
    if(state !== undefined) {
        $("#video_tab_content .content ul").fadeOut('fast');
        $("#video_tab_content").addClass("list_loading");
    }
    else {
        $("#video_tab_content .content ul").show();
        $("#video_tab_content").removeClass("list_loading");
    }
}

function inline_search(theForm) {
    pager_q = $("#inline_search_val").val();
    pager_type = 'search';
    pager(1);
    return false;
}

function pager_display(page) {
    var m = $("#video_tab_content .content ul").children().length;
    var total = 500;
    if(total == "" || total == undefined) {
        total = 0;
    }
    $(".pager").show();
    if(page == 1) {
        $(".pager .previous").hide();
    }
    else {
        $(".pager .previous").show(); 
    }
    if(m < page_increment && page == 1) {
        $(".pager").hide();
    }
    if(page > 1 && m < page_increment) {
        $(".pager .next").hide();
    }
    if(page > 1 && m == page_increment) {
        $(".pager .next").show();
    }
    if(is_numeric(kind)) {
        $("#video_tab_content .pager").hide();
    }
}

function is_numeric(mixed_var) {
    if (mixed_var === '') {
        return false;
    } 
    return !isNaN(mixed_var * 1);
}


// callback to handle playlist state updates from the player
function playlist_update_handler(player_state){
    switch(player_state){
        case 1:
            // preroll
            break;
        case 2:
            // main video
            var preroll_assets = ["#wallpaper", "#preroll_ad"]
            $.each(preroll_assets, function(index, value){
                $(value).fadeOut(800);
            });
            $("#show_banner").fadeIn(400);
            break;
        case 3:
            // postroll/midroll
            break;
        case "bookend":
            // bookend
            break;
    }
}

// initalize page
$(document).ready(function() {
    // tabbed playlist area
    video_tabs_init(current);
});

