var player_link = "http://video.e-skills.com/frontend/videoplayer.html?keepThis=true&TB_iframe=true&height=440&width=459";
function make_video_url(id)
{
    //put our variable before anything else 
    //TODO: fix this behavior in smoothbox
    return player_link.replace('?','?videoID='+id+'&');
}

//parses query string and gets named parameter
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function isEven(x)
{
    return !(x%2);
}
var VideoRemote = {
	init: function(server_host) {
		this.server_host = server_host;
        
	},
	getVideo: function(video_id,load_to) {
		var cls = this;
        var callback_function;
        
        switch(load_to)
        {
            case 'vertical':callback_function = cls.loadVertical;break;
            case 'horizontal':callback_function = cls.loadHorizontal;break;
            case 'player':callback_function = cls.loadPlayer;break;
            case 'homepage1':callback_function = cls.loadHome1;break;
            case 'homepage2':callback_function = cls.loadHome2;break;
        }
        
		new JsonP('http://'+ this.server_host +'/videos/jsonp/'+ video_id +'/', {
    	onComplete: callback_function
		}).request();
	},
    
    getList: function(page_num) {
        var page = page_num || 1;
        var cls = this;
        new JsonP('http://'+ this.server_host +'/videos/jsonp/list/?page='+page, {
    	onComplete: cls.loadHomepage
		}).request();
        
    },
    loadHomepage: function(data)
    {
        $('videopage_pagination_active_page').setHTML(data.current_page);
        $('videopage_pagination_all_pages').setHTML(data.num_page);
        var list = $E('ol','videopage_pagination');
        list.setHTML('');
        for (var i = 1; i <= data.num_page; i++)
        {
            var item = new Element('li');
            item.setHTML('<a href="#" onclick="VideoRemote.getList('+i+');return false;">'+i+'</a>');
            if (i==data.current_page) 
            {
                item.setAttribute('class','vp_active');
                item.setHTML(i);
            }
            else
            {
                item.setAttribute('onclick','VideoRemote.getList('+i+')');
            }
            item.injectInside(list);
        }
        $('video_blocks').setHTML(data.html);
        
        TB_init();
        Site.start();
    },
    
    loadHorizontal:function(start_data)
    {
        var data = start_data.entry || start_data;
        data.related = start_data.related;
        //load thumbnail
        $('vp_horizontal_preview').setAttribute('src',data.thumbnail);
        $('video_link').setAttribute('title',data.title);
        $('video_link').setAttribute('href',make_video_url(data.id));
        //set video title and desc
        $E('h2','vp_horizontal_unit_content').setHTML(data.title);
        $E('p','vp_horizontal_unit_content').setHTML(data.description);
        $E('a','vp_horizontal_unit_play_link').setAttribute('href',make_video_url(data.id));
        //clean 'related videos' list
        var list = $E('ul','vp_horizontal_unit_related_videos_1');
        list.setHTML('');
        
        for (var i=0;i < data.related.length;i++)
        {
            var mov = data.related[i];
            var even = 'even';
            if (!isEven(i)) {even = 'odd';}
            
            var el = new Element('li', {'id':'vp_horizontal_l_'+(i+1),'class':'vp_'+even});
            var link = new Element('a',{'title':mov.title+'::Click to play',
                'href':make_video_url(mov.id),
                'class':'smoothbox video-tooltip'});
            
            link.setHTML('<img src="'+mov.thumbnail_small+'" width="64" height="42" alt="" />');
            link.injectInside(el);
            el.injectInside(list);
        }
        TB_init();
        new Tips($$('.video-tooltip'));
    },
    loadVertical:function(start_data)
    {
        var data = start_data.entry || start_data;
        data.related = start_data.related;
        //load thumbnail
        $('vp_vertical_preview').setAttribute('src',data.thumbnail);
        $('video_link').setAttribute('title',data.title);
        $('video_link').setAttribute('href',make_video_url(data.id));
        //set video title and desc
        $E('h3','vp_vertical_unit_content').setHTML(data.title);
        $E('p','vp_vertical_unit_content').setHTML(data.description);
        $E('a','vp_vertical_unit_play_link').setAttribute('href',make_video_url(data.id));
        //clean 'related videos' list
        $('vp_vertical_l').setHTML('');
        
        //loop through related videos
        for (var i=0;i < data.related.length;i++)
        {
            var mov = data.related[i];
            var even = 'even'; if (!isEven(i)) {even = 'odd';}
            
            //construct list element
            var el = new Element('li', {'id':'vp_vertical_l_'+(i+1),'class':'vp_'+even});
            //link element 
            var link = new Element('a',{'title':mov.title+'::Click to play',
                'href':make_video_url(mov.id),
                'class':'video-tooltip smoothbox'});
            
            link.setHTML('<img src="'+mov.thumbnail_small+'" width="64" height="42" alt="" />');
            link.injectInside(el);
            el.injectInside($('vp_vertical_l'));
        }
         
        new Tips($$('.video-tooltip'));
        
        TB_init();
        
    },
    loadPlayer:function(data)
    {
        $ES('p','desc')[0].setHTML(data.title);
        $ES('p','desc')[1].setHTML(data.description);
        $('vp_popup_list_l').setHTML('');
        $('vp_l').setHTML('');
        
        if (data.main_page)
        {
            $('main_page_link').setStyle('visibility','visible');
            $('main_page_link').setAttribute('href',data.main_page);
        }
        else 
        {
            $('main_page_link').setStyle('visibility','hidden');
        }
        for (var i=0;i < data.related.length;i++)
        {
            var mov = data.related[i];
            var even = 'even'; if (!isEven(i)) {even = 'odd';}
            
            //construct list element
            var el = new Element('li', {'id':'vp_popup_list_l_'+(i+1),'class':'vp_'+even});
            //link element 
			//
			if (window.ie){
				var link = new Element('a',{'title':mov.title + '::Click to play',
					'href':make_video_url(mov.id),
					'class':'vp_scr video-tooltip'});
			} else {
				var link = new Element('a',{'title':mov.title,
					//'href':'#',
					'href':make_video_url(mov.id),
					'class':'vp_scr','onclick':'VideoRemote.getVideo('+mov.id+',"player")'});
			}


            link.setHTML('<img src="'+mov.thumbnail_small+'" width="64" height="42" alt="" />');
            link.injectInside(el);
            el.injectInside($('vp_popup_list_l'));
            
            //add same video to the "related pages"
            
        }
        
        for (var i=0;i< data.related_pages.length;i++)
        {
            page = data.related_pages[i];
            
            el = new Element('li',{'id':'vp_l_'+(i+1),'class':'vp_scr'});
            link = new Element('a',{'href':page.url,'target':'_parent'});
            link.setHTML(page.name);
            link.injectInside(el);
            el.injectInside($('vp_l'));
        }
        
        //play video
        
        var fo = new SWFObject("http://74.55.29.194/media/FlowPlayerLight.swf", "FlowPlayer", "380", "254", "9", "#fff", true);
        fo.addVariable("config", "{videoFile: '"+data.video+"',showFullScreenButton:false,showMenu:false}");
		if(window.ie) fo.addParam("wmode","transparent");
        fo.write("video");
        TB_init();
        new Tips($$('.video-tooltip'));
        
    },
    loadHome1:function(start_data)
    {
        var data = start_data.entry || start_data;
        data.related = start_data.related;
        //load thumbnail
        $('vp_horizontal_preview_1').setAttribute('src',data.thumbnail);
        //set video title and desc
        $E('h2','vp_horizontal_unit_content_wide_1').setHTML(data.title);
        $E('p','vp_horizontal_unit_content_wide_1').setHTML(data.description);
        $('video_link_1').setAttribute('href',make_video_url(data.id));
        $('play_link_1').setAttribute('href',make_video_url(data.id));
        $('video_link_1').setAttribute('title',data.title);
        //clean 'related videos' list
        var list = $E('ul','vp_horizontal_unit_related_videos_wide_1');
        list.setHTML('');
        
        for (var i=0;i < data.related.slice(0,5).length;i++)
        {
            var mov = data.related[i];
            var even = 'even';
            if (!isEven(i)) {even = 'odd';}
            
            var el = new Element('li', {'id':'vp_horizontal_l_'+(i+1),'class':'vp_'+even});
            var link = new Element('a',{'title':mov.title+'::Click to play',
                'href':make_video_url(mov.id),
                'class':'video-tooltip smoothbox'});
            
            link.setHTML('<img src="'+mov.thumbnail_small+'" width="64" height="42" alt="" />');
            link.injectInside(el);
            el.injectInside(list);
        }
        TB_init();
    },
    
    loadHome2:function(start_data)
    {
        var data = start_data.entry || start_data;
        data.related = start_data.related;
        //load thumbnail
        $('vp_horizontal_preview_2').setAttribute('src',data.thumbnail);
        //set video title and desc
        $E('h2','vp_horizontal_unit_content_wide_2').setHTML(data.title);
        $E('p','vp_horizontal_unit_content_wide_2').setHTML(data.description);
        //clean 'related videos' list
        $('video_link_2').setAttribute('href',make_video_url(data.id));
        $('play_link_2').setAttribute('href',make_video_url(data.id));
        $('video_link_2').setAttribute('title',data.title);
        var list = $E('ul','vp_horizontal_unit_related_videos_wide_2');
        list.setHTML('');
        
        for (var i=0;i < data.related.slice(0,5).length;i++)
        {
            var mov = data.related[i];
            var even = 'even';
            if (!isEven(i)) {even = 'odd';}
            
            var el = new Element('li', {'id':'vp_horizontal_l_'+(i+1),'class':'vp_'+even});
            var link = new Element('a',{'title':mov.title+'::Click to play',
                'href':make_video_url(mov.id),
                'class':'smoothbox video-tooltip'});
            
            link.setHTML('<img src="'+mov.thumbnail_small+'" width="64" height="42" alt="" />');
            link.injectInside(el);
            el.injectInside(list);
        }
        TB_init();
    },
    
	
    getPageVideos: function(page_slug,load_to) {
		var cls = this;
        var callback_function;
        switch(load_to)
        {
            case 'vertical':callback_function = cls.loadVertical;break;
            case 'horizontal':callback_function = cls.loadHorizontal;break;
            case 'player':callback_function = cls.loadPlayer;break;
        }
        
		new JsonP('http://'+ this.server_host +'/videos/jsonp/page/'+ page_slug +'/', {
    	onComplete: callback_function
		}).request();
	},
	showPageVideos: function(data) {
		// add functions to show page's videos data
		console.log(data);
	}
    
}

