﻿var TSN = {};
TSN.data = {};
TSN.data.articleList = {};
TSN.go = {
	KEY_pubDate: 'pd',
	KEY_sect: 's',
	sect_L1: '',
	sect: '',
	pubDate: '',
	getURLParams: function(key) {
		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] == key) {
				return pair[1];
			}
		} 
		return null;
	}﻿,
	init: function() {
		this.sect = this.getURLParams(this.KEY_sect);
		this.pubDate = this.getURLParams(this.KEY_pubDate);
		if (this.pubDate == null) this.pubDate = TSN_PUBDATE;
		if(this.sect != null && this.pubDate != null) {
			if (this.sect == '00478' || this.sect == '00486' || this.sect == '00492' || this.sect == '00496') { //時尚潮流 科技新知 星相運程 生活專欄
				this.sect_L1 = 'lifestyle';
			} else if (this.sect == '00466' || this.sect == '00432') { //產評
				this.sect_L1 = 'finance';
			} else if (this.sect == '00506') { //SUN樂園
				this.sect_L1 = 'adult';
			} else if (this.sect == '00661') {
				this.sect_L1 = 'news';
			}
			document.write('<script type="text/javascript" src="/cnt/'+this.sect_L1+'/'+this.pubDate+'/js/articleList-'+this.sect_L1+'.js"></script>');
		}
	},
	getArticleURL : function(sect) {
		var d = this.getFirstData(sect);
		var targetURL = '/cnt/'+this.sect_L1+'/tsn_notice.html?'+this.KEY_pubDate+'='+this.pubDate;
		if (d != null) {
			if (d.pubdate) {
				targetURL = '/cnt/'+this.sect_L1+'/'+d.pubdate+'/'+d.sect_L3+'_'+d.priority+'.html';
			} if (this.sect_L1 == 'finance' && this.sect == '00432') {
				targetURL = '/cnt/'+this.sect_L1+'/'+this.pubDate+'/index.html';
			} if (this.sect_L1 == 'news' && this.sect == '00661' && !d.pubdate) {
				targetURL = '/cnt/event/showall.html?sid=228342&s=00661';
			}
		} else {
			 if (this.sect_L1 == 'news' && this.sect == '00661' ) {
				//var TSN_PUBDATE = '20110808';
				
				var dateStr = '';
				var dateObj =  TSN.go.strToDate(TSN_PUBDATE);
				if (dateObj.getDay() == 0 ) {
					dateObj.setDate(dateObj.getDate()-2);					
				} else if (  dateObj.getDay() == 6 ) {
					dateObj.setDate(dateObj.getDate()-1);								
				}				
				
				var year1 = dateObj.getFullYear();
				var month1 = ( dateObj.getMonth()+1 <=9  ? '0' + (dateObj.getMonth()+1)  : (dateObj.getMonth()+1)  );
				var date1 = ( dateObj.getDate() <=9  ? '0' + dateObj.getDate()  : dateObj.getDate()  );					 
				
				dateStr = year1 + '' + month1 + '' + date1;						
				 
				targetURL = '/cnt/'+this.sect_L1+'/'+dateStr+'/00661_001.html';
			}
		}
		return targetURL;
	},
	getFirstData: function(sect) {
		if (typeof TSN.data.articleList[this.sect_L1] != 'undefined') {
			if (this.sect_L1 == 'finance' && this.sect == '00432') {
				return {};
			} else {
				var ds = TSN.data.articleList[this.sect_L1];
				for(var i = 0; i < ds.length;i++) {
					if (ds[i].sect_L2 == sect || ds[i].sect_L3 == sect) {
						
						return ds[i];
					}
				}
			}
		}
		return null;
	},
	redirect: function() {
		if ( TSN.go.getURLParams('stop') == null) {
			window.location = this.getArticleURL(this.sect);
		} else {
			//alert(this.getArticleURL(this.sect));
			document.write( this.getArticleURL(this.sect));
		}
	}, 
	strToDate: function(str) {
		if (str.length >= 8) {
			var yyyy = str.substring(0,4);
			var mm = parseInt(str.substring(4,6), 10) - 1;
			var dd = str.substring(6,8);
			var HH = (str.length >= 10) ? str.substring(8,10) : 0;
			var MM = (str.length >= 12) ? str.substring(10,12) : 0;
			var ss = (str.length >= 14) ? str.substring(12,14) : 0;
			return new Date(yyyy, mm, dd, HH, MM, ss);
		}
		return new Date();	
	}
	
}
TSN.go.init();
window.onload = function() {

	TSN.go.redirect();
}

