function select_chat_type(type){
    if(type == "jtv"){ type = "chat"; }
    if(PP.channel_default_chat_type == "jtv"){ PP.channel_default_chat_type = "chat"; }
    var default_type = PP.channel_default_chat_type || "chat";
    var types = ["chat", "twitter", "myspace", "facebook"];
    if(!types.include(type)) { type = "chat"; }
    if(type != PP.channel_default_chat_type || window.location.hash.length > 0) {
        modify_location_hash("chat", type);
    }

    types.each(function(t){ if(t != type) $(t).hide(); });
    switch(type) {
    case "chat":
        $('chat').show();
        break;
    case "twitter":
        show_twitter();
        break;
    case "myspace":
        show_myspace();
        break;
    case "facebook":
        show_facebook();
        break;
    }
    $$('#chat_selector li').each(function(b) {
      $(b).removeClassName('selected');
    });
    $('chat_switch_' + type).addClassName('selected');
}

function toggle_chat() {
    if($('chat_holder').style.display == '') {
        log_stat("toggle_chat_click", ["action=hide"]);
        $('chat_redisplay_holder').show();
        Effect.Fade('chat_holder', {duration:0.25, from: 1.0, to: 0.0, afterFinish: LateLoader.reposition_all_ads});
    } else {
        log_stat("toggle_chat_click", ["action=show"]);
        $('chat_redisplay_holder').hide();
        Effect.Appear('chat_holder', {duration:0.25, from: 0.0, to: 1.0, afterFinish: LateLoader.reposition_all_ads});
    }
}

function show_twitter() { show_ajax_chat("twitter"); }
function show_myspace() { show_ajax_chat("myspace"); }
function show_facebook() { show_ajax_chat("facebook"); }

// AJAXily get the login and chat panels for the system in question so as not to increase
// page weight. When you first call show_twitter, it over writes itself and is replaced 
// upon a successful return
function show_ajax_chat(type){
    eval("show_" + type + " = function(){ return true; };")
    
    if(IRC.debug) {
        var debug_str = "?chat_debug=true";
    } else {
        var debug_str = "";
    }
    
    new Ajax.Request('/' + type + '/html' + debug_str, {
        method: 'get', 
        onSuccess: function(request){
            var obj = request.responseText.evalJSON();
            $(type).innerHTML = obj.panel;
            obj.panel.evalScripts();
            if(obj.login) $(type + '_login_container').innerHTML = obj.login;
            
            var cap = type.substring(0,1).toUpperCase() + type.substring(1);
            // replace show_myspace (or whichever) with a function that actually does the 
            // proper initialization checks
            eval("show_" + type + " = function() { $('" + type + "').show(); if(!" + cap + ".initialized) { " + cap + ".init(); } }");
            // now run that function
            eval("show_" + type + "();")
        } 
    });
}

/*********************************/
function show_share(){
    if($('share_forms').style.display == ''){ Element.hide('share_forms'); }
    else Element.show('share_forms');

    if(!PP.user_id){
        $('email-share-form').hide();
    } else {
        if($('share_friends').innerHTML.strip() == ""){
            $('share_friends').innerHTML = "<img src=\"/images/share_spinner.gif\" />";
            new Ajax.Updater("share_friends", "/community/share_friends", {
                onComplete: LateLoader.reposition_all_ads
            });
        }
        if($('share_contacts').innerHTML.strip() == ""){
            $('share_friends').innerHTML = "<img src=\"/images/share_spinner.gif\" />";
            new Ajax.Updater("share_contacts", "/community/share_contacts", {
                onComplete: LateLoader.reposition_all_ads
            });
        }
    }
    LateLoader.reposition_all_ads();
}

// categories

function show_category_chooser(categories) {
    $('category_chooser').show();
    $('channel_cover').show();
    refresh_category_options(categories);
    hide_flash_player();
    window.scrollTo(0, 0);
}

function hide_category_chooser() {
    $('category_chooser').hide();
    $('channel_cover').hide();
    show_flash_player();
}


// ad countdown

var ad_timer = 15;
Event.observe(window, 'load', function(){
    if($('ad_container')) {
        window.setInterval(function(){
            ad_timer -= 1;
            if(ad_timer >= 0) {
                $('ad_counter').innerHTML = ad_timer;
            }
            if(ad_timer == 0) {
                toggleAdElements();
            }
        }, 1000);
    }
});

function toggleAdElements() {
    $('ad_timer_txt').hide();
    ad_timer = 0;
    $$('.ad_toggle').each(function(t){
        Element.toggle(t)
    });  
}

/********************************
 * Favorite channel
 ********************************/

function display_favorite(){
    return;
    $("favorited").innerHTML = "FAV";
}

function favorite_channel(channel){
    return;
    if(PP.user_id){
        display_favorite();
        new Ajax.Request("/user/favorite/" + channel);
    } else {
        show_login_box(_("Please log in to set this channel as your favorite"));
    }
}

/********************************
 * status box
 ********************************/
function toggle_edit_status(){
    if(PP.login != PP.channel) return false;
    $('status_form').toggle();
    $('status_message').toggle()
}


// DVR functionality
window.rawFromTime = read_location_hash()['from'] || "";
window.fromTime = Date.parse(rawFromTime.replace(/\./g, ":"));
if(fromTime){
    window.fromTimeStamp = "" + fromTime.getTime();
} else {
    window.fromTimeStamp = null;
}
function run_dvr(){
    var rawFromTime = read_location_hash()['from'];
    if(rawFromTime){
        $("chat_holder").style.display = "none";
        $("dvr").style.display = "none";
        $("from_holder").style.display = "";
        if($('from_holder').innerHTML.strip() != ""){ return; }
        if(fromTime != null) {
            $("from_holder").innerHTML = "You are watching archived video, starting at " + fromTime.toString("h:mm tt on dddd, MMMM d, yyyy");
        } else {
            $("from_holder").innerHTML = "Sorry, we couldn't understand the date and time '" + rawFromTime + "'.";
            $("from_holder").style.color = "#ff0000";
        }
    }
}
function getFromTime() {
    return fromTimeStamp;
}
function fromTimeNotFound() {
    var error_text = "Sorry, there is no archived video for this channel at " + fromTime.toString("h:mm tt on dddd, MMMM d, yyyy") + ".";
    $("from_holder").innerHTML = error_text;
    if((Date.now() - fromTime.getTime()) / (1000 * 60) < 15) {
        $("from_holder").innerHTML += "<br>Video archives take approximately 15 minutes to produce.  Please try again later.";
    }
    $("from_holder").style.color = "#ff0000";
}
function show_from_url() {
    log_stat('dvr_button_click');
    $("dvr").style.fontWeight = "normal";
    $("dvr").innerHTML = "Use the link below to continue watching this broadcast when you have time, starting from what you're watching right now (to within a few minutes).<br>"
                         + "This link will continue to work for 7 days.<br>"
                         + "<form action=''><input type='text' onfocus='this.select();' style='color:blue; width:450px' value='" + window.location.toString().split("#")[0] + "#from-" + (new Date()).toString("H.mm,MMMM-d-yyyy") + "'></form>";
    return false;
}

//*********************************** REDESIGN ****************************/
// Justin (6/09): This code is all for the channel page redesign
function switch_data_tab(tab) {
    $$('#info .content').each(function(el){
        if(el.id == tab) {
            el.addClassName("selected");
        } else {
            el.removeClassName("selected");
        }
    });
    
    $$('#info .tab').each(function(el){
        if(el.id == tab + "_tab") {
            el.className = "tab selected col_bg";
        } else {
            el.className = "tab";
        }
    });
    LateLoader.refresh();
}

function switch_action_tab(tab) {
    $$('#actions .tabs .action').each(function(el){
        if(el.id == tab + "_tab") {
            el.addClassName("selected");
        } else {
            el.removeClassName("selected");
        }
    });
    
    $$('#action_contents .top').each(function(el){
        if(el.id == tab + "_contents") {
            el.addClassName("selected");
        } else {
            el.removeClassName("selected");
        }
    });
}
