var cambio = {
    remote: { }
}

cambio.remote.settings = {    
    skinsDirectory: '/stylesheets/skins/',
    height: {
        normal: 157,
        expanded: 307
    },
    scrollable: {
        offset: -35
    },
    scroller: {
        showArrows:true,
        scrollbarWidth:12, 
        scrollbarMargin: 0,
        dragMinHeight: 50,
        animateTo:true
    },
    server: "services.cambio.com",
    domain: "cambio.com"
}

jQuery.extend(cambio.remote, cambio.remote.settings);


cambio.remote.init = function () {
        
    var $add_faves          = $("#add-to-favorites").find('a'),
        $add_alert          = $("#add-to-alerts").find('a'),
        $alerts_faves       = $('#add-to-alerts a, #add-to-favorites a'),      
        $content            = $('#remote-content'),
        $create             = $('#create-button'),
        $forget_password    = $('#forget-password'),
        $login              = $('#login-button, #init-logo'),
        $panels             = $('#remote-content').find('div.panel'),
        $password_submit    = $("#reset-password-submit"),
        $close_remote       = $('#close-remote'),
        $open_remote        = $('#open-remote'),
        $loader             = $('#remote-loader'),
        $remote             = $('#remote'),
        $remote_list        = $('#remote-channel-list'),
        $txt                = $('#remote').find('.text'),
        $user_name          = $('#current-user-name'),
        $log_out            = $('#logout'),
        $avatar_select      = $('#avatar-select'),
        currentChannel      = $.cookie('channel'),
        loggedin            = $.cookie('loggedIn'), 
        controller          = $.cookie('controller'),   
        userName            = $.cookie('username'),
        avatar              = $.cookie('avatar'),
        skin                = $.cookie('skin');

    // Some cookie checks
    cambio.remote.checkCookies();
    
    $loader.hide();
    
    $log_out.click(cambio.remote.logout);
    
    $login.click(function(e) {        
        var $this = $(this);        
        $this.parent().find('a').removeClass('active');
        $this.addClass('active');     
        $('#login-panel').find('.error').hide();
        cambio.remote.filterPanel('login-panel');
        return false;        
    });

    
    $txt.click(function() {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    }).blur(function() {
        if (this.value == '') {
            this.value = this.defaultValue;
        }
    });

    $create.click(function(e) {
        $(this).parent().find('a').removeClass('active');
        $(this).addClass('active');
        $panels.filter(':visible').fadeOut(300, function () {
            $content.animate({height: 205}, 400, 'easeOutQuart');
            $remote.animate({height: 260}, 400, 'easeOutQuart', cambio.remote.register);
        });
        return false;   
    });

    
    $forget_password.click(function(e) {
        $panels.filter(':visible').fadeOut(300, function () {
            $('#password-panel-one').fadeIn(400);
        });
        return false;
    }); 


    $close_remote.click(function() {
        if (!$(this).hasClass('disabled')) { 
            cambio.remote.loadController('close');
            $.cookie('channel', '',{domain: cambio.remote.domain});  
            $loader.hide();     
            $(this).addClass('disabled');
        }
        return false;
    });

    $open_remote.click(function() {
        if (!$(this).hasClass('disabled')) { 
            cambio.remote.loadController('open');   
            $.cookie('channel', '',{domain: cambio.remote.domain});  
            $loader.hide(); 
            scrollable.resetMenu();
            $close_remote.removeClass('disabled');
        }
        return false;
    });
    
    var channels = new Array();
    
    $remote_list.find('li').each(function(i){
        
        channels.push($.trim($(this).find('a').text()));        
        var index = i+1;   
        
        $(this).delegate('a', 'click', function(i) {

            if($(this).parent().hasClass('disabled')) { return };            
            if (typeof(controller_state) == 'undefined') { controller_state == 'open' }
            
            if (controller_state != 'open') cambio.remote.loadController('open');  

            var $this     = $(this),  
                classes   = $(this).parent('li').attr("class").split(" "),
                $channel  = classes[0];  

            $this.parent().siblings().fadeOut(100);            
            cambio.remote.gotoChannel($channel, index);
            $.trace('Loading '+$channel+', '+index);

            return false;
        });
        
    });
    
    $add_faves.click(function(e) {
        var id = $(this).attr('rel');
        cambio.remote.addFavorite(id);
        return false;
    });
    

    $add_alert.click(function(e) {
        var id = $(this).attr('rel');
        cambio.remote.addAlert(id);
        return false;
    });  
    
    $avatar_select.find('img').live("click", cambio.remote.updateAvatar); 
    $("#skin-select").find('img').live("click", cambio.remote.updateSkin);
                            
} 
                                 
cambio.remote.checkCookies = function () {     
    var currentChannel      = $.cookie('channel'),
        loggedin            = $.cookie('loggedIn'), 
        controller          = $.cookie('controller'),   
        userName            = $.cookie('username'),
        avatar              = $.cookie('avatar'),
        skin                = $.cookie('skin');
    
    if (loggedin) {
        if (currentChannel) {
            cambio.remote.loadChannel(currentChannel);
        } else {
            cambio.remote.loadController('open');
        } 
        if (avatar) {
            $('#avatar').css('background-image', 'url(' + avatar + ')');
        }   
        if (skin) {
            $('#remote-skin').attr('href', cambio.remote.skinsDirectory + skin + '.css'); 
        } else {
            $('#remote-skin').attr('href', cambio.remote.skinsDirectory + 'default.css');
        }
        $('#current-user-name').html(userName);
    };
}

cambio.remote.register = function () {

    var $register_panel       = $('#register-panel'), 
        $register_password    = $('#register-password'), 
        $confirm_password     = $('#confirm-password'),
        $submit               = $('#register-submit');

    $register_panel.fadeIn(400, function() {   
        validate_age();  
        $register_panel.find('.scroll-pane').jScrollPane(cambio.remote.scroller);         
    });

    $("#select-birthday #user_birthday_3i, #select-birthday #user_birthday_2i, #select-birthday #user_birthday_1i").change(validate_age);

    function validate_age () {

        var day = $("#user_birthday_3i").val(),
            month   = $("#user_birthday_2i").val(),
            year    = $("#user_birthday_1i").val(),
            $scroll = $('#register-panel').find('.scroll-pane'),
            $fields = $('#register-firstname, #register-lastname, #my-contact-information'),  
            age     = 13;       

        // Last validating for Age
        var mydate = new Date();
        mydate.setFullYear(year, month-1, day);
        var currdate = new Date();
        currdate.setFullYear(currdate.getFullYear() - age);       
        
        if ((currdate - mydate) < 0) {
            $fields.hide(); 
            $('#register-email').val('');
            $scroll.jScrollPane(cambio.remote.scroller); 
        } else {
            $fields.show();
            $scroll.jScrollPane(cambio.remote.scroller);
        } 
        
    } 

    $submit.click(cambio.remote.validate_registration);  

}

cambio.remote.validate_registration = function() { 
    
    var $register           = $('#register-form'),
        username            = $('#register-username').val(),
        email               = $('#register-email').val(),
        password            = $('#register-password').val(),
        confirm_password    = $('#confirm-password').val(), 
        secret_question     = $('#secret-question').val(),
        secret_answer       = $("#secret-answer").val();

    if (password != confirm_password) { alert("Passwords do not match"); return false }
    
    if (username == '' || password == '' || confirm_password == '' || secret_question == '' || secret_answer == '') {
        alert("Please fill out all required fields"); return false;
    }     

}


cambio.remote.logout = function() {
	$("#jsprox").attr("src", "http://services.cambio.com/users/sign_out?jsprox=1");
    return false;
}

cambio.remote.filterPanel = function(panel, state) {
    
    var $remote             = $('#remote'),
        $content            = $('#remote-content'),
        $panels             = $('#remote-content').find('div.panel');
        
    $panels.filter(':visible').fadeOut(300, function () {
        $content.animate({height: (state == 'expanded') ? 184 : 100}, 400, 'easeOutQuart');
        $remote.animate({height: (state == 'expanded') ? 250 : cambio.remote.height.normal}, 400, 'easeOutQuart', function() {
            $("#" + panel).fadeIn(400);
        });            
    });
}
cambio.remote.layout = function (state, callback) {
    
    var $remote             = $('#remote'),
        $content            = $('#remote-content');    
        
    $content.animate({height: (state == 'expand') ? 184 : 100}, 400, 'easeOutQuart');
    $remote.animate({height: (state == 'expand') ? 258 : cambio.remote.height.normal}, 400, 'easeOutQuart', function() {
        if (typeof(callback) == 'function') { callback(); }
    });
    
}
cambio.remote.gotoChannel = function (channel, index) {
    
    var $up_arrow        = $('#up'),
        $down_arrow      = $('#down'),
        $remote_content  = $('#remote-content'),         
        $loader          = $('#remote-loader'),
        $content         = $('<div/>').attr('id','channel-content').appendTo($remote_content),
        $current_channel = $('#remote-channel-list li.'+channel),
        $current_class   = $current_channel.attr('class'),
        $prev_channel    = $current_channel.prev('li').attr('class'),
        $next_channel    = $current_channel.next('li').attr('class'),
        $last_channel    = $current_channel.parent().find('li:last').attr('class'),
        $first_channel   = $current_channel.parent().find('li:first').attr('class');
        
    $.cookie('channel', channel,{domain: cambio.remote.domain});             
    
    $loader.fadeIn(100);
    
    switch (channel) {
      case 'faves':
        cambio.remote.fetchFavorites();
        break;
      
      case 'alerts':
        cambio.remote.fetchAlerts();
        break;
      
      case 'details':
        cambio.remote.fetchDetails();
        break;
      
      case 'points':
        cambio.remote.fetchPoints();
        break;
      
      case 'friends':
        $content.addClass('friends-channel');
        cambio.remote.fetchFriends();
        break;
    }
    
    $current_channel.addClass('disabled');

    $up_arrow.unbind('mousedown click').bind('click', function(e){
        
        $current_channel.removeClass('disabled');
       
        if (channel == 'faves') {
            cambio.remote.switchChannel($last_channel);
        } else {
            cambio.remote.switchChannel($prev_channel);
        }
        return false;
        
    });

    $down_arrow.unbind('mousedown click').bind('click', function(e){
        
        $current_channel.removeClass('disabled');
        
        if (channel == 'details') {
            cambio.remote.switchChannel($first_channel);
        } else {
            cambio.remote.switchChannel($next_channel);
        }
        return false;

    });    
        
            
}


cambio.remote.switchChannel = function (channel) {
    var $channel_item    = $('#remote-channel-list li.'+channel),
        $channel_content = $('#channel-content');
    
    $channel_item.show();
    $channel_item.siblings().hide();
    $channel_content.fadeOut(200, function(){
        $(this).empty().remove();
    });
	if ($('.prompt').length) {
		$('.prompt').remove();
	}
    setTimeout(function () {   
        cambio.remote.gotoChannel(channel); 
    }, 500);

}
cambio.remote.loadChannel = function (channel) {
    
    var $remote         = $('#remote'), 
        $panels         = $remote.find('div.panel'),
        $channel_item   = $('#remote-channel-list li.'+channel+' a');
        
    $panels.fadeOut(200);
    cambio.remote.loadController('open');
    cambio.remote.toggle('on');
    $.cookie('channel', channel,{domain: cambio.remote.domain}); 
    $('#remote-channel-list').find('li').removeClass('active');
    $channel_item.find('a').trigger('click');
    $('div.prompt').remove();
    
    setTimeout(function () {    
        cambio.remote.switchChannel(channel);
    }, 500);
    
}

cambio.remote.toggle = function (toggle) {
    
    var $close = $('#close-remote'),
        $open = $('#open-remote');
    
    if (toggle == 'on') {
        $close.removeClass('disabled');
        $open.removeClass('disabled');
    } else {
        $close.addClass('disabled');
        $open.addClass('disabled');
    }    
}

cambio.remote.flash = function(callback) {
    var $remote = $("#remote");
    $remote.toggleClass('regular alert').delay(1200).queue(function(next){
        $(this).toggleClass('regular alert');
        next();         
        if (typeof(callback) == 'function') { callback(); }      
    });
} 
 
cambio.remote.updateAvatar = function () {
    var id = $(this).attr('id'),
        src = $(this).attr('rel');
        

    $('#user_avatar_id').val(id);
    $('#avatar').css('background-image', 'url(' + src + ')');
    
    $(this).parent().parent().find('.selected').removeClass('selected');    
    $(this).addClass('selected'); 
    
    $.cookie('avatar', src, {domain: cambio.remote.domain});    
} 

cambio.remote.updateSkin = function () {
    var $remote = $("#remote"),
        $stylesheet = $("#remote-skin"),
        skin = $(this).attr('id'),
        src = $(this).attr('src');
    
    $remote.addClass(skin); 
    $('#user_css_theme').val(skin); 
    $(this).parent().parent().find('.selected').removeClass('selected');
    $(this).addClass('selected');                                        
    $stylesheet.attr('href', cambio.remote.skinsDirectory + skin + '.css');
    
    $.cookie('skin', skin, {domain: cambio.remote.domain});    
}

 
cambio.remote.loadController = function (state) {
    
    var $remote             = $('#remote')
        $user_info          = $('#user-info'),
        $controller         = $('#controller'),
        $channel_content    = $('#channel-content'),
        $channel_list       = $('#remote-channel-list'),
        $close_button       = $('#close-remote').find('a'),
        $open_button        = $('#open-remote').find('a'),
        $up_arrow           = $('#up'),
        $down_arrow         = $('#down'),
        ChannelCookie       = $.cookie('channel'),    
        controller_state    = '';
                                   
    $channel_list.find('li').removeClass('disabled');          
        
    if ($('#login').filter(':visible').size() > 0) {
        $('#login').hide();
    }
    if ($remote.find('div.panel').filter(':visible').size() > 0) {
        $remote.find('div.panel').hide();
    }
    if (!$user_info.filter(':visible').size() > 0) {
        $user_info.fadeIn(400);
    }
    cambio.remote.toggle('on');
    if (typeof(state) == 'undefined') {
        state == 'open'
    }
    if (state == 'open') {
        $open_button.addClass('active');
        $close_button.removeClass('active');
        $remote.addClass('expand');
        $('.prompt').remove();
        $channel_content.fadeOut(200, function() {
            $channel_content.remove();            
            $channel_list.find('li')
                .removeClass('disabled active')
                .fadeIn(100);
        });
        $controller.fadeOut(200, function() {
            $channel_list.attr('style','');     
            scrollable.resetMenu();
            $channel_list.find('.active').removeClass('active');
            $(this).removeClass('close');
        });
        cambio.remote.layout('expand',function() {
             $controller
                .addClass('open')
                .animate({"marginTop": "8px"}, 100, 'easeOutQuart')
                .fadeIn('400');
        });
        controller_state = 'open';
        $.cookie('controller', 'open',{domain: cambio.remote.domain});
    } else {
        $('.prompt').remove();
        $open_button.removeClass('active');
        $close_button.addClass('active');
        $remote.removeClass('expand');
        $channel_content.remove();
        $channel_list.find('li').fadeIn(100);

        $controller.fadeOut(200, function() {
            $(this).removeClass('open');
        });
        cambio.remote.layout('close',function() { 
            $controller
                .addClass('close')
                .animate({"marginTop": "0px"}, 100, 'easeOutQuart')
                .fadeIn('400', function(){
                     scrollable.init();
                });
        });
        controller_state = 'close';
        $.cookie('controller', 'close',{domain: cambio.remote.domain});
    }
    
}


cambio.remote.fetchFavorites = function () {    
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/favorites');
}
cambio.remote.fetchAlerts = function () {    
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/alerts');
}
cambio.remote.fetchDetails = function () {
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/remote/details');
}
cambio.remote.fetchPoints = function () {
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/points');
}
cambio.remote.fetchFriends = function () {
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/aim');
}
cambio.remote.addFavorite = function (id) {
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/add_favorite?id=' + id);
    
    // If the user is logged in to AIM, set their status
    if (aim.settings.token != "" && aim.settings.aimsid != "") {
      aim.update_status("I am currently watching " + $("head title").html().replace(" - Cambio", "") + " on Cambio");
    }
}
cambio.remote.removeFavorite = function (id, $this) {   
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/remove_favorites?id=' + id);
}
cambio.remote.addAlert = function (id) {
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/add_alert?id=' + id);
}
cambio.remote.removeAlert = function (id, $this) {   
    $('#jsprox').attr('src', 'http://' + cambio.remote.server + '/jsproxy/remove_alerts?id=' + id);
}
cambio.remote.animateIcon = function (type) {
    
    var img_url = (type == "alert") ? "/images/remote/alerts-icon.png" : "/images/remote/faves-icon.png",
        $header = $("#main .cap"),    
        $icon   = $("<img/>", { id: "animated-icon", src: img_url}).appendTo($header);
    
    $icon.show().animate({
        width:  'toggle',
        height: 'toggle',
        opacity: '0.4',
        top:    '-215px',
        right:  '35px'
    }, {
        duration: 1400, 
        specialEasing: {
            width:  'easeInOutCirc',
            height: 'easeInOutCirc',
            opacity:'easeOutQuad',
            top:    'easeInOutCirc',
            right:  'easeInOutCirc'
        }, 
        complete: function() {
            $(this).fadeOut(10, cambio.remote.flash);                                                                             
        }
    });    
        
}

cambio.remote.renderFavorites = function (faves, el) {
    
    var html     = "",
        $ul      = $('<ul />').attr('id','my-favorites-list'),
        $scroll  = $('<div />').attr('class','scroll-pane'),
        $content = $("#channel-content");

    $.each(faves, function(i, fave) {
        var even = (i % 2 === 0) ? 'even' : '';
        html += '<li id="fave-'+fave.content_node_id+'" class="'+ even +'"><a href="/'+fave.content_node_slug+'">'+fave.content_node_title.substring(0,27)+'<span class="remove"></span></a></li>\r\n';
    });
    
    $ul.html(html).appendTo($scroll);
    $scroll.appendTo($content);
    
    $content.fadeIn(400, function() {
        $content.find('.scroll-pane').jScrollPane(cambio.remote.scroller);  
    });
    
    $ul.delegate('.remove', 'click', function() {
        var $this = $(this).parent().parent(),
            id = $this.attr('id').replace('fave-','');        
        if (confirm("Are you sure you want to remove this favorite?")) {
            cambio.remote.removeFavorite(id, $this);
            return false;
        }        
        return false;
    });
    
}
cambio.remote.renderAlerts = function (alerts, el) {
    
    var html     = "",
        $ul      = $('<ul />').attr('id','my-alerts-list'),
        $scroll  = $('<div />').attr('class','scroll-pane'),
        $content = $("#channel-content");

    $.each(alerts, function(i, alert) {
        var even = (i % 2 === 0) ? 'even' : '';
        html += '<li id="alert-'+alert.content_node_id+'" class="'+ even +'"><a href="/'+alert.content_node_slug+'">'+alert.content_node_title.substring(0,27)+'<span class="remove"></span></a></li>\r\n';
    });
    
    $ul.html(html).appendTo($scroll);
    $scroll.appendTo($content);
    
    $content.fadeIn(400, function() {
        $content.find('.scroll-pane').jScrollPane(cambio.remote.scroller);  
    });
    
    $ul.delegate('.remove', 'click', function() {
        var $this = $(this).parent().parent(),
            id = $this.attr('id').replace('alert-','');        
        if (confirm("Are you sure you want to remove this alert?")) {
            cambio.remote.removeAlert(id, $this);
            return false;
        }        
        return false;
    });
    
}

cambio.remote.noFavesPrompt = function () {
    
    var $remote     = $('#remote-content'),
        html        = "<strong>You have no favorites yet!</strong> Click  the<img src='/images/remote/faves.png' /> button just above the video to add one.",
        $no_faves   = $('<div/>').attr('id','no-favorites').addClass('prompt').appendTo($remote);
    
    $no_faves.html(html).fadeIn(400);
    
}
cambio.remote.noAlertsPrompt = function () {
    
    var $remote     = $('#remote-content'),
        html        = "<strong>You have no alerts yet!</strong> Click the <img src='/images/remote/alerts.png' /> button just above the video to add one.",
        $no_alerts  = $('<div/>').attr('id','no-alerts').addClass('prompt').appendTo($remote);
    
    $no_alerts.html(html).fadeIn(400);
    
}
cambio.remote.checkEmail = function(email) {
    if (/.*@.*\..*/.test(email)) {
        return true;
    } else {
        return false;
    }
}
                      
// Inspired by the King of the Kurbs! 
$.extend({
    trace: function(msg){
    	if (window.console) {
    		console.log(msg);
    	};
    }
});

var scrollable = {    
    
    init : function () {
        
        var $mask       = $('#controller').find('.mask'),
            $menu       = $('#remote-channel-list'),
            $up_arrow   = $('#up'),
            $down_arrow = $('#down');

        $.trace('scrolling init');   
        
        controller_state = 'close';
        $.cookie('controller', 'close',{domain: cambio.remote.domain});  
        
        scrollable.scrollToChannel(ChannelCookie);  
        
        $up_arrow.unbind('mousedown click').bind('click', scrollable.scrollUp);  
        $down_arrow.unbind('mousedown click').bind('click', scrollable.scrollDown);        
        $menu.find('a').unbind('click').bind('click', scrollable.assignMenuHandler);

    },    
    
    assignMenuHandler : function () {      
                      
        // old index code, hacked to get this working, shall be refactored post-beta luanch
        var index = 1;
        
        if($(this).parent().hasClass('disabled')) { return };            
        if (typeof(controller_state) == 'undefined') { controller_state == 'open' }

        if (controller_state != 'open') cambio.remote.loadController('open');

        var $this     = $(this),   
            $menu     = $('#remote-channel-list'),
            classes   = $(this).parent('li').attr("class").split(" "),
            $channel  = classes[0];

        $this.parent().siblings().fadeOut(100);            
        cambio.remote.gotoChannel($channel, index);
        $.trace('Loading via assignMenuhandler '+$channel+', '+index);

        return false;
        
        
    },
    
    scrollToChannel : function (channel) {  
        
        var $mask    = $('#controller').find('.mask'),
            $menu    = $('#remote-channel-list'),
            $channel = (channel) ? $menu.find('li.'+channel) : $menu.find('li.friends'),
            index    = $channel.index() + 1;
            
        $menu.find('li').removeClass('active'); 
        $menu.find('a.active').removeClass('active');  
        $menu.find('.old-menu-item').remove();
        
        scrollable.reBuildMenu(index, function () {  
            $mask.stop().scrollTo($channel, 400, {offset: -35, easing: 'easeInOutCirc', onAfter:function(){
                $channel.addClass('active');
                $channel.find('a').addClass('active');
                $.trace('scrolling...');                    
             }}); 
             $menu.find('a').unbind('click').bind('click', scrollable.assignMenuHandler);
        }); 
                
    },
    
    scrollUp : function () {     
        
        var   $menu    = $('#remote-channel-list'),
              $channel = $menu.find('li.active'),
              $next    = $channel.next('li'),
              classes  = $next.attr("class").split(" "),
              channel  = classes[0];    
        
        scrollable.scrollToChannel(channel); 
        
    }, 

    scrollDown : function () {
        
        var $menu    = $('#remote-channel-list'),
            $channel = $menu.find('li.active'),
            $next    = $channel.prev('li'),
            classes  = $next.attr("class").split(" "),
            channel  = classes[0];

        scrollable.scrollToChannel(channel);
        
    },
    
    appendMenuItem : function (channel) {
        
        var $item = $('#remote-channel-list').find('li.'+channel),
            $clone = $item.clone().addClass('cloned').appendTo('#remote-channel-list');
            
        $item.addClass('old-menu-item');

    },
    
    prependMenuItem : function (channel) {
        
        var $item = $('#remote-channel-list').find('li.'+channel),
            $clone = $item.clone().addClass('cloned').prependTo('#remote-channel-list');
            
        $item.addClass('old-menu-item');    
        
    },
    
    reBuildMenu : function (index, callback) {
        
        if (index < 3) {    
            
            var $last    = $('#remote-channel-list').find('li:last-child'),  
                classes  = $last.attr("class").split(" "),
                channel  = classes[0];    
                
            scrollable.prependMenuItem(channel);        
                      
        } else if (index > 3) {
            
            var $first   = $('#remote-channel-list').find('li:first-child'), 
                classes  = $first.attr("class").split(" "),
                channel  = classes[0];    
                
            scrollable.appendMenuItem(channel);  

        }
                    
        if (typeof(callback) == 'function') { callback(); }    
        
    }, 
    resetMenu : function (hide) {
  
        var $menu    = $('#remote-channel-list'),
            channels = {'faves': 'My Favorites','alerts': 'My Alerts','friends':'My Friends','points':'My Points','details':'My Details'}, 
            html     = ''; 

       $.each(channels, function(key, value) {  
          html += '<li class="'+ key +'"><a href="#">' + value + '</a></li>\r\n';     
       }); 
       
       $menu.empty().html(html); 
       
       $menu.find('a').unbind('click').bind('click', scrollable.assignMenuHandler);            
    
    }
    
}
