(function(){
    jQuery(function($){
        window['flvLinker'] = { player:{} };
        swfobject.embedSWF("VTD_FLVPlayer.swf", "videoWrapper", "640", "480", "10.0.0", "expressInstall.swf", {}, {
            allowScriptAccess:'always'
        }, {
            id:'flvPlayer'
        });

		function linkClick(target, event){
            var meta = $(target).attr('rel').split(':');
            $('#video_top *').show();
            $('#video_title').text($(target).text());
			setTimeout(function(){
				
				$('.prev-button').unbind('click').click(function(event){
		            if ($('#link-'+(parseInt(meta[0])-1)).length) {
						linkClick($('#link-'+(parseInt(meta[0])-1)), event);
					}
					else {
						event.preventDefault();
					}
				});
				$('.next-button').unbind('click').click(function(event){
		            if ($('#link-'+(parseInt(meta[0])+1)).length) {
						linkClick($('#link-'+(parseInt(meta[0])+1)), event);
					}
					else {
						event.preventDefault();
					}
				});
			}, 100);
            flvLinker.player.newSource('rtmp://fms.bafis.com/vod/mp4:vtd/'+meta[1]);
			event.preventDefault();
		}

        $.fn.extend({
            videoSelect:function(){
                return $(this).each(function(){
					var index = $(this).attr('rel').split(':')[0];
                    $(this).attr('id', 'link-'+ index);
                    $(this).click(function(e){
						linkClick(this, e);
					});
                });
            }
        });
        $('#video_top *').hide();
        $(window).load(function(){
            flvLinker.player = $('#flvPlayer object')[0]||$('#flvPlayer')[0];
            $('a.video-selector').videoSelect();
        });
    });
})();
