/* JQuery script for collapsible channel navigation */

document.getElementsByClassName = function(class_name) {
    var docList = this.all || this.getElementsByTagName('*');
    var matchArray = new Array();

    /*Create a regular expression object for class*/
    var re = new RegExp("(?:^|\\s)"+class_name+"(?:\\s|$)");
    for (var i = 0; i < docList.length; i++) {
        if (re.test(docList[i].className) ) {
            matchArray[matchArray.length] = docList[i];
        }
    }

	return matchArray;
}

function toggle_subchannel($s){
    var $p = $s.parent();
    if ($p.hasClass('hot')){
        $s.slideUp('fast');
        $p.removeClass('hot');
        return false;
    }
    else{
        $s.slideDown('fast');
        $p.addClass('hot');
        return true;
    }
}

function load_new_videos(section, page){
    $video_grid = $('.video_archive_grid_list', '#video_archive_bucket_channels');
    $pag_top = $('.video_list_pagination_top', '#video_archive_bucket_channels');
    $pag_bot =  $('.video_list_pagination_bottom', '#video_archive_bucket_channels');
    
    var ad_data = document.getElementsByClassName("bigbox_ad");
    var ad_save = "<li class=\"bigbox_ad\">" + ad_data[0].innerHTML + "</li>";
    
    var grid_data = document.getElementsByClassName("video_archive_grid_list")
    var grid_save = grid_data[0];
    
    $video_grid.html("<div class=\"circle_loader\">&nbsp;</div>");
   
    $.ajax({
        type: 'GET',
        url: section,
        data: ({ajax: 'yes'}),
        error: function(){
            $video_grid.html("<li>No videos were found for this section.</li><li></li>");
            grid_data[0].innerHTML += ad_save;
        },
        success: function(data){
            //grab the data section we need from the response and parse it into a jQuery object
            $data_elems = $("<div></div>");
            $data_elems.html(data);
        
            $grid_data = $('#va_ajax_section', $data_elems).children('li');
            $pag_data = $('#va_ajax_pagination', $data_elems);
            $ad_data = $('.bigbox', $data_elems);
        
            $video_grid.html("");
        
            $grid_data.each(function(i){
            
                if($(this).hasClass('bigbox_ad')){
                    grid_data[0].innerHTML += ad_save;
                }
                else{
                    $video_grid.append($(this));
                }
            });
        
            //apply them where needed
            //$video_grid.html($grid_data.html());
            $pag_top.html($pag_data.html());
            $pag_bot.html($pag_data.html());

            //reload the pagination link functionality
            init_pagination('.media_arrows', load_new_videos);

            //redo the stars for the rating radio buttons
            initContentRatings($('input[type=radio].rating-star'), $('form.rating_form'));
        }
    });
    return false;
}

function init_pagination(sel_name, link_action){
    $pagination = $(sel_name).children('li');
    $('a', $pagination).click(function(){
        //  call the link_action if we have one
        if(link_action)
            return link_action($(this).attr('href'));

        // else just naviagte to the link.
        document.location.href = $(this).attr('href');
    });
}

function init_channels(sel_name, link_action){
    $channels = $(sel_name).children('li');
    $('a', $channels).click(function(){
        // remove the active_channel class from the LI tag and add it to the clicked one
        $('a', $channels).removeClass("active_channel");
        $(this).addClass('active_channel');

        // try to get a reference to the possible subchannel node
        $subchannel = $(this).siblings('ul:first');
        
        // check if there is actually a subchannel, and toggle accordingly
        if($subchannel.length != 0){
            var channel_on = toggle_subchannel($subchannel);

	     if(link_action && channel_on)
                return link_action($(this).attr('href'));

            return false;
        }
        
        // fell through, so perform our link_action if we have one
        if(link_action)
            return link_action($(this).attr('href'));
            
        // no subchannel, no link action, so just navigate to the link
        document.location.href = $(this).attr('href');
    });
}