var defaultVideo = 0;
var ord = Math.random() * 10000000000000000;

jQuery(
	function(){
		jQuery('#tab-' + here).addClass("here");
		jQuery('.videotable:first').show();
		if (jQuery.cookie('selected-video')){
			jQuery('.videodesc-full:eq(' + jQuery.cookie('selected-video') + ')').show();
			jQuery('#dmm_playlist tr:eq(' + jQuery.cookie('selected-video') + ')').children('td').addClass("selected-row");
		} else {
//			jQuery('.videodesc-full:first').show();
			jQuery('.videodesc-full:eq(' + defaultVideo + ')').show();	
			jQuery('#dmm_playlist tr:first').children('td').addClass("selected-row");
		}
		jQuery.cookie('selected-video', null);
		jQuery('.desctitle > a').each(
			function(){
				jQuery(this).click(
					function(){
						jQuery.cookie('selected-video', jQuery('.desctitle > a').index(this));
					}
				);
			}
		)
		jQuery('.videothumb > a').each(
			function(){
				jQuery(this).click(
					function(){
						jQuery('.videodesc-full').hide();
						jQuery('.videodesc-full:eq(' + jQuery('.videothumb > a').index(this) + ')').show();
						jQuery.cookie('selected-video', jQuery('.videothumb > a').index(this));
					}
				);
			}
		)
		var randomCandidate = Math.floor( Math.random() * jQuery('.candidate-preview').length );
		jQuery('.candidate-preview:eq(' + randomCandidate + ')').children('.candidate-preview-large').show();
		jQuery('.candidate-preview:eq(' + randomCandidate + ')').children('.candidate-preview-small').hide();
		jQuery('.candidate-preview').each(
			function(){
				jQuery(this).mouseover(
					function(){
						jQuery('.candidate-preview').children('.candidate-preview-small').show();
						jQuery('.candidate-preview').children('.candidate-preview-large').hide();
						jQuery(this).children('.candidate-preview-small').hide();
						jQuery(this).children('.candidate-preview-large').show();
					}
				);
			}
		);
		var video = getURLParam("video");
		var image = getURLParam("image");
		var movieWidth = 352;
		var movieHeight = 225;
		if (video == "" || image == ""){
			location = window.location.href + jQuery('.videothumb > a:eq(' + defaultVideo + ')').attr('href');
		}
		var dcstring = 'http://ad.hk.doubleclick.net/pfadx/misshk.tvb.com/flashvideo;sect1=_2008;sz=576x324;ord=' + ord;
		
		jQuery('#placeholder').flash(
			{
				src: 'http://img.tvb.com/p/web/swf/instream_mediaplayer.swf',
				width: movieWidth,
				height: movieHeight,
				id: 'mpl',
				name: 'mpl',
				allowscriptaccess: 'always',
				allowfullscreen: 'true',
				wmode: 'transparent',
				flashvars: {
					width: movieWidth,
					height: movieHeight,
					file: video,
					image: image,
					shuffle: false,
					autostart: true,
					preRollAdTag: dcstring,
					searchbar: false,
					enablejs: true,
					preRollAdTag: 'http://ad.hk.doubleclick.net/pfadx/misshk.tvb.com/flashvideo;sect1=_2008;sz=576x324;ord=' + ord,
					searchbar: false
				}
			},
			{ version: 8 }
		);
	}
);

function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}