var mhp_tv = function()
{
    
}

mhp_tv.prototype =
{
    videos : [],
    callback : null,
    last_html : '',
    last_video :
    {
        show_duration : function()
        {
            var min     = Math.floor(this.duration / 60);
            var sek     = (this.duration % 60);
            sek         = sek < 10 ? "0"+sek : sek;
            return min+":"+sek;
        }
    },
    currentIndex : 0,
    container : null,
    thumbWidth : 64,
    thumbHeight : 48,
    margin : 0,
    timer : 0,
    items : [],
    links : [],
    load : function(el, callback)
    {
        this.callback = callback;
        this.container = document.getElementById(el);

        var remote = document.createElement('script');
        remote.type = 'text/javascript';

        //remote.src  = 'http://gdata.youtube.com/feeds/users/MuzHPtv/uploads?alt=json-in-script&format=5&callback='+callback+'.init';
        remote.src  = 'http://gdata.youtube.com/feeds/users/MuzHPtv/uploads?alt=json-in-script&max-results=50&callback='+callback+'.init';

        //remote.onload = function() { movies(); };

        var head = document.getElementsByTagName('head')[0];
        head.appendChild(remote);
        
        
    },
    init : function(data)
    {
        this.data = data;
        this.prepare();
    },
    prepare : function()
    {
        var feed = this.data.feed;

        var entries = feed.entry || [];
        this.videos = entries;
        var html = [''];

        this.width = entries.length * this.thumbWidth;

        this.currentIndex = Math.round(Math.random()*this.videos.length);
        var entry = entries[this.currentIndex];
        
        this.last_video.title       = entry.title.$t;
        this.last_video.desc        = entry.media$group.media$description.$t;
        this.last_video.img         = entry.media$group.media$thumbnail[3].url;
        this.last_video.duration    = entry.media$group.yt$duration.seconds;
        this.last_video.date        = entry.published.$t.substring(0, 10);
        
        this.thumbs();
        this.showVideo();
    },
    getVideo : function()
    {
        var entry = this.videos[this.currentIndex];
        with(this.last_video)
        {
            title   = entry.title.$t;
            desc    = entry.media$group.media$description.$t;
            img     = entry.media$group.media$thumbnail[3].url;
            duration= entry.media$group.yt$duration.seconds;
            date    = entry.published.$t.substring(0, 10);
        }
    },
    switchTo : function(id)
    {
        this.currentIndex = id;
        this.getVideo();

        if(this.full)
        {
            this.back();
            $("#last_video").fadeOut(400, function() { tv.showVideo(); });
        }
        else
        {
            $("#last_video").fadeOut(400, function() { tv.showVideo(); });
        }
        
                
    },
    showVideo : function()
    {

        
        document.getElementById('lv_title_span').innerHTML = this.last_video.title;
        document.getElementById('lv_desc').innerHTML = this.last_video.desc;
        document.getElementById('lv_img').src = this.last_video.img;
        document.getElementById('lv_time').innerHTML = this.last_video.show_duration();
        document.getElementById('lv_date').innerHTML = this.last_video.date;
        document.getElementById('lv_loading').style.display = 'none';
        this.container.style.display = 'block';

        $("#last_video").fadeIn(400);
    },
    thumbs : function()
    {
        for(var i = 0; i < this.videos.length; i++)
        {
            var entry = this.videos[i];
            var img = '<img src="'+entry.media$group.media$thumbnail[3].url+'" width="'+this.thumbWidth+'" height="'+this.thumbHeight+'" id="related_'+i+'" border="0">'

            $('div#other_videos div#ov_container').append('<a href="javascript:void(0);" onclick="tv.switchTo('+i+');" title="'+entry.title.$t+'">'+img+'</a>');
        }        
    },
    nextPage : function()
    {
        this.moveFirst();
        this.margin -= (this.thumbWidth + 4);
        this.anim();
    },
    prevPage : function()
    {
        this.moveLast();
        this.margin += (this.thumbWidth + 4);
        this.anim();
    },
    anim : function()
    {
        $('div#other_videos div#ov_container').animate({ marginLeft: this.margin});
        this.margin = this.newMargin;
    },
    play : function()
    {
        this.full = true;
        //this.last_html = $('#videos').html();
        $('#lv_full_desc').fadeOut(300);
        $('#lv_title').fadeOut(300, tv.scaleThumb);
        
    },
    scaleThumb : function()
    {
        $('#lv_img').animate({width: '100%', height: 298}, 600, '', tv.placePlayer);
    },
    placePlayer : function()
    {
        content = tv.videos[tv.currentIndex].media$group.media$content[0];

        tv.player = new SWFObject(content.url, "mhp_tv_player", "100%", "360", "9", "#ffffff");
        tv.player.addParam("wmode", "transparent");
        tv.player.write("videos_player");
        $('#last_video').css('display', 'none');

        $('#videos_player').append('<br><br><span class="btn_gray btn icon_arrow_back"><a href="javascript:void(0)" onclick="tv.back();">Wróć do opisu</a></span>');
    },
    back : function()
    {
        this.full = false;
        $('#videos_player').html('');
        $('#last_video').css('display', 'block');
        $('#lv_img').animate({width: 197, height: 140}, 600, '', tv.showDetails);

        //$('#videos').html(tv.last_html);
    },
    showDetails : function()
    {
        $('#lv_title').fadeIn(300);
        $('#lv_full_desc').fadeIn(300);
    },
    moveFirst : function()
    {
        this.margin += (this.thumbWidth + 4);
        thumb = $('div#other_videos div#ov_container a:first');
        $('div#other_videos div#ov_container').append(thumb);
        
    },
    moveLast : function()
    {
        this.margin -= (this.thumbWidth + 4);
        thumb = $('div#other_videos div#ov_container a:last');
        $('div#other_videos div#ov_container').prepend(thumb);
    }




}

