/* any further use of orn_playlist_ids will refer to this global */
/* also need the current track, as well as the current page */
var orn_playlist_ids = new Array();
var orn_playlist_current = null;
var orn_current_page = 1;
var orn_pager_q = null; //the search term for the pager
var orn_pager_type = null;
var orn_page_increment = 10;

/* debugging only */
var debug = false;
var quiet = false;
if (quiet) {
    var initialvolume = 0;
}
else {
    var initialvolume = 50;
}

/* This utility function resolves the string movieName
to a Flash object reference based on browser type. */
function getMovieName(name) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
      return window[name];
  }
  else {
      return document[name];
  }
}


function orn_pager(page) {
    //give the pager link an active class for style
    $("#orn_recent_pager .active").removeClass("active");
    if (orn_pager_type == undefined || orn_pager_type == null) {
        var url = "/ajax/orn_playlist/" + page + "/";
    } else {
        var url = "/ajax/orn_search/" + page + "/" + encodeURIComponent(orn_pager_q) + "/";
    }
    $("a.elem_" + page).addClass("active");
    display_loading("fade");
    $("#orn_recent .container").load(url, function(){
        orn_playlist_init();
        check_playlist_buttons();
        display_loading();
        orn_pager_display(page);
    });
}

function display_loading(state) {
    if(state !== undefined) {
        $("#orn_recent .clips").css({opacity:"0.5"});
        $("#orn_recent .clips").addClass("loading");
    }
    else {
        $("#orn_recent .clips").css({opacity:"1"});
        $("#orn_recent .clips").removeClass("loading");
    }
}

function orn_pager_display(page) {
    var m = $("#orn_recent ul").children().length;
    var total = $("#orn_recent .total_pages:first").text();
    if(total == "" || total == undefined) {
        total = 0;
    }
    if (page == 1) {
        $("#orn_recent .previous").hide();
    }
    else {
        $("#orn_recent .previous").show();
    }
    if (m < orn_page_increment && page == 1) {
        $("#orn_recent_pager").hide();
    }
    if (total == page) {
        $("#orn_recent .next").hide();
    }
    orn_current_page = page;
}

function orn_playlist_init(initfill) {
    $("#orn_recent li .action_button").click(function () {
        var item = $(this).parent().parent().parent().parent(); //shrug
        var file = $(".file", item).text();
        var title = $(".title", item).text();
        var date = $(".date", item).text();
        var slug = $(".slug", item).text();
        var id = $(".id", item).text();
        if ($.inArray(id, orn_playlist_ids) < 0)  //returns -1 on no match, otherwise index num
        {
            //clone the li into the active playlist area
            $(item).clone().appendTo("#orn_playlist_active");
            orn_playlist_ids.push(id);
            //make sure the remove action is ready to go
            playlist_set_remove();
            //playlist length display
            playlist_count_display();
            //we've added something to the playlist, turn on the next button?
            has_next_previous();
            //fade the playlist button?
            playlist_button(this, "off");
        }
    });

    $("#orn_playlist .clear").click(function () {
        $("#orn_playlist_active").empty();
        orn_playlist_ids = new Array();
        playlist_count_display();
        has_next_previous();
        $(".notify").hide();
        //fade back the add to list button
        playlist_button($("#orn_recent .action_button"), "on");
    });

    if (initfill) {
        for(i = 0; i <= initfill.length; i++) {
            var id = initfill[i];
            if (id !== undefined) {
                if (id !== "" && jQuery.inArray(id, orn_playlist_ids) == -1) {
                    orn_playlist_ids.push(id);
                    $("."+id).clone().appendTo("#orn_playlist_active");
                    playlist_button($("."+id+" .action_button"));
                    //make sure the remove action is ready to go
                    playlist_set_remove();
                }
            }
        }
        setTimeout('has_next_previous()', 500);
        playlist_count_display();
    }
}

function playlist_button(obj, state) {
    var opac = 0.5;
    if(state == "on") {
        opac = 1;
    }
    $(obj).css({opacity:opac});
}

function check_playlist_buttons() {
    //which do we when an ajax page has been loaded
    var childs = $("#orn_recent ul").children();
    for(i=0;i<=childs.length;i++) {
        var id = $(".id",childs[i]).text();
        var index = jQuery.inArray(id, orn_playlist_ids); //returns -1 on no match, otherwise index num
        if(index > -1) {
            playlist_button(childs[i], "off");
        }
    }
}

//this is a direct play of an item, not through next/prev
//and not through sequncetial playlist
function playthis(index) {
    //confirm which list we're dealing w
    var id = $("#orn_playlist_active ."+index+" .id").text();
    if(id) {
        var file = $("#orn_playlist_active ."+index+" .file").text();
        var date = $("#orn_playlist_active ."+index+" .date").text();
        var title = $("#orn_playlist_active ."+index+" .title").text();
        var slug = $("#orn_playlist_active ."+index+" .slug").text();
    }
    else {
        var id = $("#orn_recent ."+index+" .id").text();
        var file = $("#orn_recent ."+index+" .file").text();
        var date = $("#orn_recent ."+index+" .date").text();
        var title = $("#orn_recent ."+index+" .title").text();
        var slug = $("#orn_recent ."+index+" .slug").text();
    }
    var item = jsonify_forplayer(file,title,id,date,slug);
    if(id) {
        orn_playlist_currenttrack(id);
        google_orn_track(title);
        getMovieName("orn_player").manualPlay(item);
    }
    return false;
}

function playlist_set_remove() {
    $("#orn_playlist_active li .action_button").click(function () {
        var item = $(this).parent().parent().parent().parent(); //shrug
        var id = $('.id',item).text();
        $(item).remove();
        playlist_button($("."+id+" .action_button"), "on")
        //reset the playlist ids arr
        var items = $("#orn_playlist_active").children();
        orn_playlist_ids = new Array();
        for(i = 0; i < items.length; i++) {
            orn_playlist_ids.push( $('.id',items[i]).text() );
        }
        playlist_count_display();
        has_next_previous();
    });
}

function playlist_count_display() {
    $("#playlist_count").html("("+orn_playlist_ids.length+")");
}

function orn_playlist_currenttrack(id) {
    orn_playlist_current = id;
    $("#orn_recent table").removeClass('active');
    $("#orn_recent ."+id+" table").addClass('active');
    $("#orn_playlist_active table").removeClass('active');
    $("#orn_playlist_active ."+id+" table").addClass('active');
    has_next_previous();
}

function playlist_item(index) {
    if(index) {
        var file = $("#orn_playlist_active ."+index+" .file").text();
        var date = $("#orn_playlist_active ."+index+" .date").text();
        var title = $("#orn_playlist_active ."+index+" .title").text();
        var slug = $("#orn_playlist_active ."+index+" .slug").text();
        var id = $("#orn_playlist_active ."+index+" .id").text();
        var item = jsonify_forplayer(file,title,id,date,slug);
        orn_playlist_currenttrack(id);
        google_orn_track(title);
        return item;
    }
    else {
        return null;
    }
}

function google_orn_track(title) {
    _gaq.push(['_trackEvent', 'radionews', "Play", title]);
}

function has_next_previous() {
    //this is for display of next/prev buttons, turn on, turn off
    //the buttons themselves use the _player.next/prev as funcs to look for what to play
    //none of the values is stored in flash, it will always look to the page
    var index = jQuery.inArray(orn_playlist_current, orn_playlist_ids); //returns -1 on no match, otherwise index num    
    if (orn_playlist_ids.length == 0) {
        getMovieName("orn_player").backButton("off");
        getMovieName("orn_player").nextButton("off");
        return;
    }
    //case: playing a piece, removed all from list, then re-added to list, should bring up next button
    //except when the next piece in the list equals the piece currently playing or paused
    //alert("current: "+orn_playlist_current+" orn_playlist_ids: "+orn_playlist_ids+" index: "+index);
    if (orn_playlist_ids.length == 1 && orn_playlist_current !== null && orn_playlist_ids[0] !== orn_playlist_current) {
        getMovieName("orn_player").nextButton("on");
        return;
    }
    if (index > -1) {
        //how many ahead? how many before?
        var after = orn_playlist_ids.slice(index+1, orn_playlist_ids.length+1);
        var before = orn_playlist_ids.slice(1, index+1);
        if(debug) {
            alert( "after: "+after.length+" before: "+before.length+" index: "+index);
        }
	//alert( "current: "+orn_playlist_current+" orn_playlist_ids: "+orn_playlist_ids+" index: "+index);
        if(after.length > 0) {
            getMovieName("orn_player").nextButton("on");
        }
        else {
            getMovieName("orn_player").nextButton("off");
        }
        if(before.length > 0) {
            getMovieName("orn_player").backButton("on");
        }
        else {
            getMovieName("orn_player").backButton("off");
        }
    }
    else {
        if(orn_playlist_ids.length > 0) {
            getMovieName("orn_player").nextButton("on");
            getMovieName("orn_player").backButton("off");
        }
        else {
            getMovieName("orn_player").nextButton("off");
            getMovieName("orn_player").backButton("off");
        }
    }
}

function playlist_nexttrack() {
    if(orn_playlist_current == null) {
        var index = orn_playlist_ids[0];
    }
    else {
        var index = orn_playlist_ids[jQuery.inArray(orn_playlist_current, orn_playlist_ids) + 1];
    }
    return playlist_item(index);
}

function playlist_previoustrack() {
    if(orn_playlist_current == null) {
        return null;
    }
    else {
        var index = orn_playlist_ids[jQuery.inArray(orn_playlist_current, orn_playlist_ids) - 1];
    }
    if(index) {
        return playlist_item(index);
    }
}

function playlist_custom_add_items(list_id) {
    //add an editors custom playlist to the existing playlist
    var items = $("#"+list_id).children();
    var add_ids = new Array();
    for(i=0;i<=items.length;i++) {
        var item = items[i];
        if(item !== undefined) {
            var id = $(".id",item).text();
            if(id !== "" && jQuery.inArray(id,orn_playlist_ids) == -1) {
                add_ids.push(id);
                orn_playlist_ids.push(id);
                $(item).clone().appendTo("#orn_playlist_active");
                //make sure the remove action is ready to go
                playlist_set_remove();
            }
        }
    }
    $(".element_"+list_id+" .notify").html('This collection has been added to your playlist');
    has_next_previous()
    $(".element_"+list_id+" .notify").show();
    playlist_count_display();
}

function rand(min, max) {
  if (max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  } else {
    return Math.floor(Math.random() * (min + 1));
  }
}

function orn_inline_search(theForm) {
    orn_pager_q = $("#inline_search_val").val();
    orn_pager_type = 'search';
    orn_pager(1);
    return false;
}

function reset_recent() {
    orn_pager_type = null;
    orn_pager(1);
}

