
if(!console){ var console = { log:function(){}, dir:function(){} } }

(function(){

    if(!window.VTV) window.VTV = {}

    var Template = {
        loader:'<div><img src="/assets/images/loader.gif" />Loading please wait...</div>'
    }


    VTV = {

        cache : {
            _cache:{},
            get:function(key){
                return VTV.cache._cache[key] 
            },
            set:function(key, val){
                VTV.cache._cache[key] = val
            },
            del:function(key){
                delete VTV.cache._cache[key]
            }
        },

        initSchedule:function(){

            var tools = $('.tools a');

            $('.tools a').click(function(e){
                var blocks = $(".schedule"),
                    i = tools.index(this);
                if (i > 0) {
                    blocks.hide().eq(i-1).show();
                } else {
                    blocks.show();    
                }
                tools.removeClass('on').eq(i).addClass('on');
                return false;
            });

            // Animate on now indicator 
            var el = $('.schedule_mod .on .info');
            setInterval(function(){
                el.animate({'background-position' : "440px 8px"}, 250, function(){
                    el.animate({'background-position' : "446px 8px"}, {duration:250});
                })
            }, 2000);

            // Auto select a range from a fragment 
            var matches = String(window.location.href).match(/#(.+)$/);
            if (matches && matches.length > 1) {
                $(".tools a[href*='" + matches[1] + "']").click();
            }

            /*
            var container = $("#range_container"),
                days = $('#days a'),
                day_parents = $("#days li");

            days.click(function(e){
                e.preventDefault();
                day_parents.removeClass('on');
                $(this).parent().addClass('on');
                var url = this.href;
                $.get(url, function(data){
                    container.html(data);        
                });
            });
            */
            
        },
        initVideo:function(){

            var videoElement = $('#bc_container'); 
            var videosContainer = $("#video_list_container");
            var categories = $('#video .categories a');
            var categoryName = $("#category_name");
            
            // Handle the video categories
            categories.bind('click', function(e){
                e.preventDefault();
                var me = $(this);
                if(!me.parent().hasClass('on')) {
                    me.parent().addClass('on');
                    categories.not(me).parent().removeClass('on');
                    categoryName.html(me.html());
                    var url = $(this).attr('href');
                    videosContainer.html(Template.loader);
                    $.get(url, function(data){
                        videosContainer.html(data);
                    });
                }
            });

            // handle pagination


            // Handle the video clicks
            $('#video_list_container .video_list li').live('click', function(e){

                e.preventDefault();
                e.stopPropagation();

                var me = $(e.currentTarget);
                me.addClass('on');
                var video_id = me.attr('id').split('_')[1];

                modCon.getMediaAsynch(video_id);

            });

            // Handle the featured video click
            $('#featured li').bind('click', function(e){

                e.preventDefault();
                e.stopPropagation();

                var me = $(e.currentTarget);
                me.addClass('on');
                var video_id = me.attr('id').match(/\d+/)[0]
                console.log('Load: %s', video_id);

                modCon.getMediaAsynch(video_id);

            });

        },

        initEvents:function(){

            // IE6 is too slow for .live
            if ($.browser.msie && $.browser.version < 7) { 
                // - 
            }
            else {
                var w = $("#events_container").width();
                var h = $("#events_container").height();

                $("#events_container").before('<div id="events_loader"> </div>');
                var loader = $("#events_loader");
                loader.css({'width' : w, 'height' : h}).hide();
                $(".paginator a").live('click', function(e){

                    if ('href' in e.target) {
                        e.preventDefault();

                        var href = e.target.href;
                        var results = VTV.cache.get(href);

                        if (results) {
                            $("#events_container").html(results);
                        } else {
                            loader.show();
                            $.ajax({
                                url:href,
                                success:function(data, status){
                                    VTV.cache.set(href, data);
                                    $("#events_container").html(data);
                                    loader.fadeOut(function(){ loader.hide();});
                                }
                            });
                        }
                    }

                });
            }

        },

        initFeatures:function(){
            var events = $("#feature_container .featured_item li"),
                speed = 4000,
                n = events.length,
                p = 0,
                container = $('#feature_container .featured_item ul').css('width', n*505),
                dots = $("#feature_container .dot");
                interval = null;
                move = function(){
                    //console.log('args %s', arguments.length);
                    //console.dir(arguments);
                    p = arguments[0] == null ? p+1 : arguments[0];
                    p = p < 0 ? n-1 : p > n-1 ? 0 : p;
                    console.log(p);
                    container.stop().animate({left : -(p*505)}, 500); 
                    dots.removeClass('on').eq(p).addClass('on');
                };
            if(n > 1) {
                $('#feature_container .next').click(function(){
                    clearInterval(interval);
                    move(p+1);
                    interval = setInterval(move, speed, null);
                    return false;
                });
                
                $('#feature_container .prev').click(function(){
                    clearInterval(interval);
                    move(p-1);
                    interval = setInterval(move, speed, null);
                    return false;
                });
                dots.click(function(){
                    clearInterval(interval);
                    move(dots.index(this));
                    interval = setInterval(move, speed, null);
                    return false;
                })
                interval = setInterval(move, speed, null); 
            } else {
                $("#feature_container .pagination").hide();
            }
        },

        initSignupForm:function(){
            // Handle signup form
            var container = $("#newsletter_form_container");
            $("#newsletter_form").live('submit', function(e){
                e.preventDefault();
                var data = $(this).serialize();
                container.html(Template.loader);
                $.post('/signup/', data, function(data){
                    container.html(data).hide().fadeIn();            
                });
            });
        },

        initMessages:function(){
            var messages = $(".messages");
            if (messages.length > 0) {
                setTimeout(function(){
                    messages.slideUp("slow");
                }, 3000);
            }
        },

        initHomepage:function(){
           $(".feature_link_box").css('cursor', 'pointer').click(function(){
                window.location.href = $("a", this).attr('href');
           });
        }

    }

})()


$(document).ready(function(){
    VTV.initSignupForm();
    VTV.initMessages();
});


