var __firstTime = 'true';
var __adHoc = 0;
var __videoContainer = 'PlayerHolder';
var __clickedNum = 0;
var __section = '';
var __LCKey = 'undefined';
var ONTV_FIRST_LOAD = true;

if (!Array.prototype.indexOf){ Array.prototype.indexOf = function(elt) { var len = this.length;  var from = Number(arguments[1]) || 0; from = (from < 0)  ? Math.ceil(from)  : Math.floor(from); if (from < 0) from += len;  for (; from < len; from++) { if (from in this &&   this[from] === elt) return from; } return -1; }; }
var cat = new Array();
cat[0] = '2';
cat[1] = '3';
cat[2] = '4';
cat[3] = '5';
cat[4] = '41';
var adfreq = new Array();
adfreq[0] = 3;
adfreq[1] = 3;
adfreq[2] = 3;
adfreq[3] = 3;
adfreq[4] = 2;
var catName = new Array();
catName[0] = 'broadcast';
catName[1] = 'news';
catName[2] = 'ent';
catName[3] = 'life';
catName[4] = 'commentary';

var ontv = {
	
		skin : {
			current: '',
			tracking: '',
			backdropAlpha: 0,
			forceApplySkin: '',
			isExpandable: 1,
			showYahoo: 0
		},
		
		cat : {
			broadcast : {id:'1',name:'broadcast'},
			news : {id:'2',name:'news'},
			commentary : {id:'5',name:'commentary'},
			ent : {id:'3',name:'ent'},
			life : {id:'4',name:'life'},
			download : {id:'6',name:'download'},
			macau : {id:'7',name:'macau'}
		},
		channelMenu : {
			type: 'menu',
			path: 'top_bar.swf',
			wmode: 'transparent',
			allowfullscreen: 'false',
			width: '465',
			height: '65',
			flashheight: '65',
			backgroundColor: '#000000',
			backgroundImage:''
			//backgroundImage: 'img/index_01.jpg'
		},
		mainMenu : {
			type: 'menu',
			path: 'main_menu.swf',
			wmode: 'window',
			allowfullscreen: 'false',
			width: '1000',
			height: '35',
			flashheight: '100%',
			backgroundColor: '#000000',
			backgroundImage: ''
		},
		catMenu : {
			type: 'menu',
			path: 'cat_menu.swf',
			wmode: 'transparent',
			allowfullscreen: 'false',
			width: '1000',
			height: '350',
			flashheight: '350',
			backgroundColor: '#000000',
			backgroundImage: ''
		},
		defaultPicker : {
			type: 'menu',
			path: 'default_picker.swf',
			wmode: 'transparent',
			allowfullscreen: 'false',
			width: '333',
			height: '130',
			flashheight: '130',
			backgroundColor: '#000000',
			backgroundImage: ''
		},
		playerVOD : {
			type: 'player',
			path: 'player_vod.swf',
			wmode: 'window',
			allowfullscreen: 'true',
			width: '587',
			height: '356',
			backgroundColor: '#000000',
			backgroundImage: ''
		},
		footer : {
			type: 'footer',
			backgroundImage: '',
			content: '<div>東方報業集團網站<br><br>| <a href=\"http://opg.com.hk/\" target=\"_blank\">企業網站</a> | <a href=\"http://tv.on.cc/disclaimer.html\" target=\"_blank\">免責聲明</a> | <a href=\"http://opg.com.hk/tc/product_websites.html\" target=\"_blank\">關於on.cc</a> | <a href=\"http://on.cc/cgi-bin/mem/newuser_terms.cgi?came_from=O\" target=\"_blank\">登記成為on.cc會員</a> | <a href=\"http://on.cc/edm/accept.html\" target=\"_blank\">免費會員尊訊</a> | <a href=\"http://on.cc/copyright/\" target=\"_blank\">版權收費機制</a> | <a href=\"http://opg.com.hk/tc/contactus.html\" target=\"_blank\"> 聯絡我們</a> | <a href=\"http://on.cc/ratecard/\" target=\"_blank\">廣告查詢</a> | <a href=\"javascript:ontv.footer.popupwin(\'http://on.cc/orimain/contactus/index.html\',\'501\',\'421\');\">意見查詢</a> | <a href=\"javascript:ontv.footer.popupwin(\'http://tv.on.cc/ontv/feedback.html\',\'470\',\'390\');\">改善意見</a> | <a href=\"http://on.cc/orimain/recruit/index.html\" target=\"_blank\">招聘人才</a> | <a href=\"http://on.cc/faq/index.html\" target=\"_blank\">常見問題</a> | <a href=\"javascript:ontv.footer.popupwin(\'userguide/userguide.html\',\'890\',\'700\');\">使用說明</a> | <a href=\"http://on.cc/faq/hkfonts_bottom.html\" target=\"_blank\">下載字庫</a> |<br><br></div>',
			copyright: get_bottom ('ontv'),
			popupwin: function(link, width, height) {window.open(link,'popupdetail','location=no,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);}
		},
		sectionMapping: function(section) {
			var rtnVal;
			switch(section) {
				case '3': case '15': case '10': case '12': case '13': case '95': case '69':
					rtnVal = '3';
					break;
				case '41': case '64': case '52': case '82': case '80': case '42': case '43': case '44': case '45': case '46': case '48': case '49': case '50': case '51': case '68':
					rtnVal = '41';
					break;
				case '2': case '104': case '11': case '89': case '90': case '91': case '92': case '7': case '8': case '9': case '53': case '54': case '93':
					rtnVal = '2';
					break;
				case '4': case '20': case '16': case '17': case '18': case '19': case '87':
					rtnVal = '4';
					break;
				case '5': case '21': case '22': case '23': case '24': case '25': case '26': case '105':
					rtnVal = '5';
					break;
				default:
					rtnVal = '3';
					break;
			} 
			return rtnVal;
		},
		write: function(pContainer, webObject, section) {
			switch (webObject.type) {
				case 'menu': ontv.writeMenu(pContainer, webObject, section); break;
				case 'player': ontv.writePlayer(pContainer, section); break;
				case 'ad': ontv.writeAd(pContainer, webObject, section); break;
				case 'footer': ontv.writeFooter(pContainer); break;
			}
		},
		
		writeMenu: function(pContainer, menu, section) {
			var style = document.getElementById(pContainer).style;
			style.height = menu.height;
			style.width = menu.width;
			/*if (menu.backgroundImage!='')
				style.backgroundImage = 'url('+menu.backgroundImage+')';*/
			
			var so = new SWFObject(menu.path+'?lckey='+ontv.genLCKey(), 'flashid_'+pContainer, menu.width, menu.flashheight, '9', menu.backgroundColor);
			so.addParam('wmode', menu.wmode);
			if (pContainer == "ChannelMenuHolder") {
				var qSection = ontv.getQueryVariable('s');			
				if (qSection != 'undefined') {
					section = ontv.sectionMapping(qSection);
				}
				else {
					section = "";
				}
			}
			var qMId = ontv.getQueryVariable('i');
			var qMdate = ontv.getQueryVariable('d');	
			if (qMId != 'undefined' && qMdate != 'undefined') {
				so.addVariable('mid', qMId);
				so.addVariable('mdate', qMdate);
			}
			else
			{
				so.addVariable('mid', "");
				so.addVariable('mdate', "");
			}
			var qKw = ontv.getQueryVariable('kw');
			if (qKw != 'undefined') {
				so.addVariable('searchKw', qKw);
			}
			so.addParam('allowScriptAccess', 'always');
			so.addVariable('msect', section);
			if (ontv.skin.current != '') {
				so.addVariable('ontvCurSkin', ontv.skin.current);
			}
			so.addVariable('ontvCurSkinExpandable', ontv.skin.isExpandable);
			so.addVariable('ontvCurSkinAlpha', ontv.skin.backdropAlpha);
			so.addVariable('ontvCurSkinShowYahoo', ontv.skin.showYahoo);
			so.addVariable('mrelatednews', '300x60.swf');
			so.addVariable('mrelatedvideo', 'todayplaylist.swf');
			so.addVariable('todayDate', ontv.todayDate());
			// so.addVariable('lckey', ontv.genLCKey());
			so.write(pContainer);
		},

		writeCatMenu: function(pContainer, menu, section, inOnair, inBreak, inCurServerTime, inBreakcount, inLivepath, innOnair, innBreak, innBreakcount, innLivepath, inmid, inmdate, indsect) {

			var style = document.getElementById(pContainer).style;
			style.height = menu.height;
			style.width = menu.width;
			/*if (menu.backgroundImage!='')
				style.backgroundImage = 'url('+menu.backgroundImage+')';*/
			
			var so = new SWFObject(menu.path+'?lckey='+ontv.genLCKey(), 'flashid_'+pContainer, menu.width, menu.flashheight, '9', menu.backgroundColor);
			so.addParam('wmode', menu.wmode);
			so.addParam('allowScriptAccess', 'always');
			var qSSection = ontv.getQueryVariable('ss');
			if (qSSection != 'undefined') {
				so.addVariable('ssect', qSSection);
			}
			so.addVariable('msect', section);
			so.addVariable('todayDate', ontv.todayDate());
			// so.addVariable('lckey', ontv.genLCKey());
			so.addVariable('mid', inmid);
			so.addVariable('mdate', inmdate);
			so.addVariable('onair', inOnair);
			so.addVariable('break', inBreak);
			so.addVariable('curServerTime', inCurServerTime);
			so.addVariable('breakCount', inBreakcount);

			so.addVariable('livepath', inLivepath);
			so.addVariable('nonair', innOnair);
			so.addVariable('nbreak', innBreak);
			so.addVariable('nbreakCount', innBreakcount);
			so.addVariable('nlivepath', innLivepath);
			
			so.addVariable('dsect', indsect);
			so.write(pContainer);
		},

		writePicker: function(pContainer, picker, indsect) {
			var so = new SWFObject(picker.path+'?lckey='+ontv.genLCKey(), 'flashid_'+pContainer, picker.width, picker.flashheight, '9', picker.backgroundColor);
			so.addParam('wmode', 'transparent');
			so.addParam('allowScriptAccess', 'always');
			so.addVariable('dsect', indsect);
			so.write(pContainer);
		},

		writePlayer: function(pContainer) {
			var style = document.getElementById(pContainer).style;
			style.height = ontv.playerVOD.height;
			style.width = ontv.playerVOD.width;
			__videoContainer = pContainer;
		},
		
		writeFooter: function(pContainer) {
			document.getElementById(pContainer).innerHTML = ontv.footer.content + ontv.footer.copyright;
			//document.getElementById(pContainer).innerHTML = ontv.footer.content + '版權所有 &copy; 2009 ON.CC (BVI) LTD. All rights reserved.';
		},
		
		gotoChannel: function(channel) {
			window.location = location.pathname + '?cat=' + channel;
		},
		
		refreshAds: function(section) {
			var mychannel = catName[cat.indexOf(String(section))];
			document.getElementById('TextlinkFrame').src = 'textlink_'+mychannel+'.html?lckey='+ontv.genLCKey();
			document.getElementById('LargerectFrame').src = 'largerect_'+mychannel+'.html?lckey='+ontv.genLCKey();
			if (ontv.skin.tracking != '') {
				document.getElementById('skin_control_tracking').src = ontv.skin.tracking;
			}
		},
		setLarge: function(path) {
			document.getElementById('LargerectFrame').src = path;
		},
		genVideo: function(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, p_ssect, p_livepath, initvc) {
			if(__firstTime == 'false') { }
			else
			{
			   if(ontv.getQueryVariable('mid') != 'undefined')
			   {
				   __firstTime = 'false';
			   }
			}
			if(initvc != "" && initvc != null && initvc != undefined && initvc !="undefined")
			{
				__clickedNum = 1;
				ontv.writeVideoPlayer(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, '1', p_ssect, p_livepath, initvc);
			}
			else
			{
				if (__clickedNum >= adfreq[cat.indexOf(String(__section))]){
				   	__clickedNum = 1;
					ontv.writeVideoPlayer(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, '1', p_ssect, p_livepath,"");
				}else{
					__clickedNum++;
				   ontv.writeVideoPlayer(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, '0', p_ssect, p_livepath,"");
				   __firstTime = 'false';
				}
			}

			ontv.refreshAds(p_msect);

		},
		
		//function for write player by swfoject

		writeVideoPlayer :  function(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, p_tvc, p_ssect, p_livepath, initvc)		{
			//alert(p_livepath);
			var so = new SWFObject(ontv.playerVOD.path+'?lckey='+ontv.genLCKey()+'&msect='+((p_msect == undefined || p_msect == '' ) ? __section : p_msect )+'&ssect='+p_ssect, 'player', ontv.playerVOD.width, ontv.playerVOD.height, '9', ontv.playerVOD.backgroundColor);
			so.addParam('allowFullScreen', 'true');
			// so.addVariable('lckey', ontv.genLCKey());
			//so.addVariable('msect', (p_msect == undefined || p_msect == '' ) ? __section : p_msect );
			so.addVariable('mid', p_mid);
			so.addVariable('mdate', p_mdate);
			so.addVariable('todaydate', ontv.todayDate());
			so.addVariable('tvc', p_tvc);
			so.addVariable('playmode', p_playmode);
			so.addVariable('autoplay', p_autoplay);
			so.addVariable('breakcount', p_breakcount);
			so.addVariable('livetime', p_livetime);
			so.addVariable('livepath', p_livepath);
			so.addVariable('listid', p_listid);
			so.addVariable('hightlistid', p_highlist);
			if(initvc != "" && initvc != null && initvc != undefined && initvc !="undefined")
			{
            
				so.addVariable('infixtvc', initvc);
			}
			so.write(__videoContainer);
		},

		todayDate: function() {
			return todayDate;
		},
		
		catId: function(catName) {
			if (catName != 'undefined') {
				return eval('ontv.cat.'+catName+'.id');
			} else {
				return ontv.cat.news.id;
			}
		},
		
		genLCKey: function() {
			if (__LCKey == 'undefined') {
				var thisKey = "";
				thisKey = Math.round(Math.random()*100000)+"";
				__LCKey = thisKey;
			}
			return __LCKey;
		},
		
		getQueryVariable: function (variable) {
			var query = window.location.search.substring(1);
			var vars = query.split("&");
			for (var i=0;i<vars.length;i++) {
				var pair = vars[i].split("=");
				if (pair[0] == variable) {
					return pair[1];
				}
			} 
			return('undefined');
		},
		//Add by James 2008-08-13
		resizeMain: function(pWidth, pHeight) {
			//document.getElementById('utils_container').innerHTML += pHeight + ",";
			var tgDiv = document.getElementById('MainmenuHolder');
			tgDiv.style.height = pHeight+'px';
			//WebAnimator.ResizeArea('MainmenuHolder', pWidth, pHeight, 1);
		},
		
		changeSection: function(pSection) {
			__clickedNum = 0;
			__section = String(pSection);
			__clickedNum = adfreq[cat.indexOf(__section)] - 1; //added by kit for ads Count demo
			if (ontv.skin.forceApplySkin == '') {
				var mychannel = catName[cat.indexOf(String(pSection))];
				document.getElementById('skin_control').src = 'skin_control_'+mychannel+'.html?rnd='+Math.random();
			} else {
				applySkin(ontv.skin.forceApplySkin);
			}
		}
		
		
};
function init()
{
	if (ontv.getQueryVariable('skin') != 'undefined') {
		ontv.skin.forceApplySkin = ontv.getQueryVariable('skin');
	}
	ontv.write('ChannelMenuHolder', ontv.channelMenu);
	ontv.write('FooterHolder', ontv.footer);
}

function writeBoth(insect, inOnair, inBreak, inCurServerTime, inBreakcount, inLivepath, innOnair, innBreak, innBreakcount, innLivepath, inmid, inmdate, indsect)
{
	__section = String(insect);
	__clickedNum = adfreq[cat.indexOf(__section)] - 1; //added by kit for ads Count demo
	ontv.writePicker('DefaultPicker', ontv.defaultPicker, indsect);
	ontv.write('MainmenuHolder', ontv.mainMenu, indsect);
	ontv.writeCatMenu('CatmenuHolder', ontv.catMenu, insect, inOnair, inBreak, inCurServerTime, inBreakcount, inLivepath, innOnair, innBreak, innBreakcount, innLivepath, inmid, inmdate, indsect);
}

function writeLiveClock(inBreakcount, inCurServerTime) {
	var so = new SWFObject("liveclock.swf?lckey="+ontv.genLCKey(), 'flashid_liveclock',587, 356, '9', '#000000');
	so.addVariable('breakCount', inBreakcount);
	so.addVariable('curServerTime', inCurServerTime);
	// so.addVariable('lckey', ontv.genLCKey());
	//document.getElementById('PlayerHolder').innerHTML = "<img src='img/typhoon_new.jpg' width='587' height='336'>";
	so.write("PlayerHolder");
	ontv.refreshAds("2");

}

function showpicker(){
	document.getElementById('TextlinkFrame').style.visibility = 'hidden';
	document.getElementById('DefaultPicker').style.visibility = 'visible';
}
function hidepicker(){
	document.getElementById('TextlinkFrame').style.visibility = 'visible';
	document.getElementById('DefaultPicker').style.visibility = 'hidden';
}

var ontvSearchPanel = {
	firstSearch: true,
	curKeyword:'',
	curRow: 0,
	search: function(pKeyword) {
		this.show();
		if (this.curKeyword != pKeyword) {
			this.writeResult(pKeyword);
			this.firstSearch = true;
		} else {
			this.firstSearch = false;
		}
		this.curKeyword = pKeyword;
	},
	show: function() {
		document.getElementById('yahooPanelCTN').style.display = 'block';
	},
	hide: function() {
		document.getElementById('yahooPanelCTN').style.display = 'none';
	},
	writeResult: function(pKeyword) {
		var so = new SWFObject("yahoo_result.swf?lckey="+ontv.genLCKey(), 'flashid_yahoo_result',600, 450, '9', '#000000');
		so.addParam('wmode', 'transparent');
		so.addVariable('msect', (__section != '') ? __section : '2');
		so.addVariable('keyword', pKeyword);
		so.write("yahoo_result");
	},
	refreshAds: function(pKeyword, pRow){
		document.getElementById('ads_yahoo_iframe').style.height = '20px';
		document.getElementById('ads_yahoo_iframe').src = 'ads_yahoo.html?k='+pKeyword;
		document.getElementById('yahooPanelCTN').style.height = 80+(pRow*130) + 'px';
	},
	resizeIframe: function(pHeight) {
		document.getElementById('ads_yahoo_iframe').style.height = pHeight+'px';
		if (parseInt(document.getElementById('yahooPanelCTN').style.height) < pHeight) {
			document.getElementById('yahooPanelCTN').style.height = pHeight + 'px';
		}
	},
	playVideo: function(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, p_ssect) {
		if (this.firstSearch) {
			ontv.writeVideoPlayer(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, p_ssect);
			this.firstSearch = false;
		} else {
			ontv.genVideo(p_mid, p_mdate, p_autoplay, p_msect, p_playmode, p_breakcount, p_livetime, p_listid, p_highlist, p_ssect);
		}
	}
}

function goRelatedLink(arg)
{	
	//refreshads();
	ontv.refreshAds(__section);
	window.open(remove_amp(arg));	
}
function remove_amp(arg)
{
	aarray = arg.split("&amp;")
	aresult = "";
	for(var i=0; i<aarray.length; i++)
	{
		if(aresult != "")
		{
			aresult += "&";
		}
		aresult += aarray[i];
	}
	return(aresult);
}

function popupwin(link, width, height) {
	window.open(link,'popupdetail','location=no,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);
}

function showTimeTable() {
	window.open('timetable.html','timeTable','location=no,toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no,width=500,height=270');
}
//Skin Function

var skinManager = {
	skins:[],
	attachCss: function(pSkinName) {
		if (this.skins.indexOf(pSkinName) == -1) {
			var link = document.createElement('link');
			link.rel = 'alternate stylesheet';
			link.type = 'text/css';
			link.title = 'skin_'+pSkinName;
			link.href = 'css/skin_'+pSkinName+ '.css';
			document.getElementsByTagName('head')[0].appendChild(link);
			this.skins.push(pSkinName);
		}
	},
	attachJs: function(pPath) {
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = pPath;
		document.getElementsByTagName('head')[0].appendChild(script);
	},
	clearJs: function() {
		var headTag = document.getElementsByTagName('head')[0];
		var scriptTags = headTag.getElementsByTagName('script');
		for(var i = 0;i < scriptTags.length;i++) {
			if (scriptTags[i].src.indexOf('js/skins/') != -1) {
				headTag.removeChild(scriptTags[i]);
			}
		}
	},
	load: function(pSkinName) {
		this.clearJs();
		this.attachCss(pSkinName);
		this.attachJs('js/skins/'+ pSkinName + '.js');
	},
	changeSection: function(pSkinName) {
		this.clearJs();
		this.attachJs('js/skins/'+ pSkinName + '_sect.js');
	}
}

function setStyleSheet(pCssTitle) {
	var i, csslink;
	for(i=0; (csslink=document.getElementsByTagName("link")[i]); i++) {
		if(csslink.getAttribute("rel").toLowerCase()=="alternate stylesheet" && csslink.getAttribute("title")) {
			csslink.disabled = true
			if(csslink.getAttribute("title") == pCssTitle) {
				csslink.disabled = false;
			}
		}
	}
}

function applySkin(pSkinName, pSkinTracking) {
	if (typeof pSkinName == 'undefined' || pSkinName == '') pSkinName = "default";

	//ontv.skin.tracking = pSkinTracking;
	if (ontv.skin.current != pSkinName) {
		ontv.skin.current = pSkinName;
		ontv.skin.lastSection = __section;
		skinManager.load(pSkinName);
	} else {
		if (ontv.skin.lastSection != __section) {
			skinManager.changeSection(pSkinName);
			ontv.skin.lastSection = __section;
		}
	}
}

//Skin Function: common gallery 
function skin_writeGalleryContainer(pPicIndex, pSkinName, pSubFolder) {
	if (typeof pPicIndex == 'undefined') pPicIndex = 0;
	if (typeof pSubFolder == 'undefined') pSubFolder = '';
	var so = new SWFObject('skins/'+pSkinName+'/gallery_container.swf', 'fl_gallery_container', '587', '356', '9');
	so.addParam('menu', 'false');
	so.addVariable('fvSwfFolder', 'skins/'+pSkinName);
	so.addVariable('fvSubFolder', pSubFolder);
	so.addVariable('fvPicIndex', pPicIndex);
	so.addVariable('lckey', ontv.genLCKey());
	so.write(__videoContainer);
}
