/**
 * @author clayliao
 */
// isStart 0->stop, 1->start
var isStart = 0;
var count = 0;
var newsNum = 1;
var t;
var speed;
var old_feedUrl;

// Error Messages
var ERROR_MSG = "連線中斷或此新聞分類連結毀損，請選擇其他分類繼續閱讀。";
var ERROR_MSG_START = "已達此分類頁首，請選擇其他分類繼續閱讀。";
var ERROR_MSG_END = "已達此分類結尾。請選擇其他分類繼續閱讀。";
var ERROR_CONNECT = "連線異常，請回首頁重新開始。";

function showYahooNews(yNews, link) {
	var newsSet = $(yNews).find('#ynwsart');
	var newsTitle = newsSet.find('h1').text();
	var newsContent = newsSet.find('#ynwsartcontent');
	var newsImgTable = newsContent.find('table');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		var i = link.indexOf("*") + 1;
		link = link.substring(i);		

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		if (newsImgTable.html() !== null) {
			var img = newsImgTable.find('img').addClass('image-left');
			newsImgTable.remove();
			$('#newsContent').html(img);
			$('#newsContent').append(newsContent.html());
			var interval_yahoo = Number(newsContent.html().length) * speed;
		} else {
			$('#newsContent').html(newsContent.html());
			var interval_yahoo = Number(newsContent.html().length) * speed;
		}
		$('#providerLogo>a').html("<img src='./img/logo/logo_yahoo.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		
		$('.title>a').attr('href', link);
		
		count++;
		t = setTimeout("getUrls()", interval_yahoo);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}

function showbnextNews(bnextNews, link) {
	var newsSet = $(bnextNews).find('.ArticleArea');
	var newsTitle = newsSet.find('h3').text();
	var newsContent = newsSet.find('#Cache_0');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		
		var interval_bnext = Number(newsContent.html().length) * speed * 0.7;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());

		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_bnext.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_bnext);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}

function showUdnNews(udnNews, link) {
	var newsTitle = $(udnNews).find('#story_title').text();
	var newsContent = $(udnNews).find('#story');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		
		// delete unuseful elements
		newsContent.find('#mvouter').remove();
		newsContent.find('a').remove();
		newsContent.find('#media_desc').remove();
		newsContent.find('#media_producer').remove();
		newsContent.find('strong').remove();
		newsContent.find('b').remove();		
		
		var interval_udn = Number(newsContent.html().length) * speed;

		// For News Imgage
		var newsImgTable = $(udnNews).find('#media_file>img');
		var newsImgTable_1 = $(udnNews).find('#media_file_1>img');

		if (newsImgTable.length !== 0) {
			var newsImgUrl = newsImgTable.eq(0).attr('src');
			var i = newsImgUrl.search(/media/i);
			var newsImgUrl = "http://udn.com/NEWS/" + newsImgUrl.substring(i);
			newsImgTable.eq(0).attr('src', newsImgUrl);
			var img = newsImgTable.addClass('image-left');
			newsContent.find('#media_file').remove();
			$('#newsContent').html(img);
			$('#newsContent').append(newsContent.html());
		} else if (newsImgTable_1.length !== 0) {
			var newsImgUrl = newsImgTable_1.eq(0).attr('src');
			var i = newsImgUrl.search(/media/i);
			var newsImgUrl = "http://udn.com/NEWS/" + newsImgUrl.substring(i);
			newsImgTable_1.eq(0).attr('src', newsImgUrl);
			var img = newsImgTable_1.addClass('image-left');
			newsContent.find('#media_file_1').remove();
			$('#newsContent').html(img);
			$('#newsContent').append(newsContent.html());
		} else {
			$('#newsContent').html(newsContent.html());
		}

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_udn.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_udn);
	} else {
		alert(ERROR_MSG);
		stop();
	}
}

function showBBCNews(bbcNews, link) {
	var newsTitle = $(bbcNews).find('.mxb>h1').text();
	var newsContent = $(bbcNews).find('.storybody');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		newsContent.find('#socialBookMarks').remove();
		newsContent.find('#bbccom_storyprintsponsorship').remove();
		newsContent.find('.mvtb').remove();
		newsContent.find('form').remove();
		
		var interval_bbc = Number(newsContent.html().length) * (speed / 5);

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());
		$('#providerLogo>a').html("<img src='./img/logo/logo_bbc.gif'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_bbc);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}
/*
function showHinetNews(hinetNews, link) {
	var newsTitle = $(hinetNews).find('#newst').text();
	var newsContent = $(hinetNews).find('#newsp');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		var newsImgTable = newsContent.find('div.img>a');
		newsImgTable.find('img').removeAttr('onload');
		newsImgTable.find('img').removeAttr('onerror');
		newsImgTable.find('.txtGray12').remove();
		newsImgTable.find('img').addClass('image-left');
		newsContent.find('div.img').remove();
		newsContent.find('.picMore').remove();
		
		var interval_hinet = Number(newsContent.html().length) * speed;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsImgTable.html());
		$('#newsContent').append(newsContent.html());
		$('#providerLogo>a').html("<img src='./img/logo/logo_hinet.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_hinet);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}
*/
function showPchomeNews(pchomeNews, link) {
	var newsTitle = $(pchomeNews).find('#iCliCK_SafeGuard').text();
	var newsContent = $(pchomeNews).find('#newsContent');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		// delete unuseful elements
		newsContent.find('span#iclickAdBody_Start').remove();
		newsContent.find('span#iclickAdBody_End').remove();
		newsContent.find('table').remove();
		newsContent.find('div.t1628').remove();
		newsContent.find('a').remove();

		// For News Image
		var newsImgTable = $(pchomeNews).find('#newsContent').find('img');

		if (newsImgTable.length !== 0) {
			newsContent.find('img').remove();
			var newsImgUrl = newsImgTable.eq(0).attr('src');
			var i = newsImgUrl.search(/newsdata/i);
			var newsImgUrl = "http://news.pchome.com.tw/"
					+ newsImgUrl.substring(i);
			newsImgTable.eq(0).attr('src', newsImgUrl);
			var img = newsImgTable.eq(0).addClass('image-left');
			$('#newsContent').html(img);
			$('#newsContent').append(newsContent.html());
		} else {
			$('#newsContent').html(newsContent.html());
		}
		
		var interval_pchome = Number(newsContent.html().length) * speed;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);

		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_pchome.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_pchome);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}

function showYamNews(yamNews, link) {
	var newsTitle = $(yamNews).find('.newstitleXL').text();
	var newsContent = $(yamNews).find('#article');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		newsContent.find('div#video').remove();

		var i = link.indexOf("?") + 1;
		link = link.substring(i);
		
		var interval_yam = Number(newsContent.html().length) * speed;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());

		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_yam.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_yam);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}

function showLibNews(libNews, link) {
	var newsTitle = $(libNews).find('#newtitle').text();
	var newsContent = $(libNews).find('#newcontent');
	newsContent.find('script').remove();
	newsContent.find('.picture').remove();

	if (newsContent.html() !== null) {
		
		var interval_libNews = Number(newsContent.html().length) * speed;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());

		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_lib.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_libNews);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}

function showMsnNews(msnNews, link) {
	var newsSet = $(msnNews);
	var newsTitle = newsSet.find('.articletitle').find('b').text();
	var newsContent = newsSet.find('#msncontent');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		
		var interval_msn = Number(newsContent.html().length) * speed;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());		

		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_msn.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_msn);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}
/*
function showCTNews(ctNews, link) {
	var newsSet = $(ctNews);
	var newsTitle = newsSet.find('#cont1').find('.highlight').text();
	var newsContent = newsSet.find('#ctkeywordcontent');
	//newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		var interval_ct;
		interval_ct = Number(newsContent.html().length) * speed;

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());		

		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_ct.png'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_ct);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}
*/
/*
function showAppleNews(appleNews, link) {
	var newsSet = $(appleNews).find('#tbl_content');
	var newsTitle = newsSet.find('#header>div>h1').text();
	var newsContent = newsSet.find('#intro');
	newsContent.find('script').remove();

	if (newsContent.html() !== null) {
		var newsContentSection = newsSet.find('.section');
		newsContent.find('.imgwrap').remove();
		newsContentSection.find('.imgwrap').remove();

		var interval_appleNews;
		if (newsContentSection.html() !== null
				&& newsContentSection.html() < 187) {
			interval_appleNews = (Number(newsContent.html().length) + Number(newsContentSection
					.html().length))
					* speed;
		} else {
			interval_appleNews = Number(newsContent.html().length) * speed;
		}		

		$('.title>a').html(newsTitle);
		$('#sourceUrl>a').html(link);
		$('#newsContent').html(newsContent.html());
		$('#newsContent').append(newsContentSection.html());
		$('.title>a').attr('href', link);
		$('#providerLogo>a').html("<img src='./img/logo/logo_apple.gif'/>");
		$('#providerLogo>a').attr('href', link);
		$('#sourceUrl>a').attr('href', link);
		$('.title>a').attr('href', link);

		count++;
		t = setTimeout("getUrls()", interval_appleNews);
	} else {
		alert(ERROR_MSG);
		stop();
	}

}
*/
function showContent(newsUrl, provider) {
	$.ajax( {
		url :'/showNews',
		data : {
			newsUrl :newsUrl
		},
		error : function(xhr) {
			alert(ERROR_CONNECT);
			stop();
		},
		success : function(response) {			
			switch (provider) {
			case 'yahoo':
				showYahooNews(response, newsUrl);
				break;
			case 'udn':
				showUdnNews(response, newsUrl);
				break;
			case 'bnext':
				showbnextNews(response, newsUrl);
				break;
			case 'bbc':
				showBBCNews(response, newsUrl);
				break;
			case 'hinet':
				showHinetNews(response, newsUrl);
				break;
			case 'pchome':
				showPchomeNews(response, newsUrl);
				break;
			case 'yam':
				showYamNews(response, newsUrl);
				break;
			case 'lib':
				showLibNews(response, newsUrl);
				break;
			case 'msn':
				showMsnNews(response, newsUrl);
				break;
			case 'ct':
				showMsnNews(response, newsUrl);
				break;
			case 'apple':
				showAppleNews(response, newsUrl);
				break;
			default:
				alert(ERROR_CONNECT);
				stop();
			}
			// alert(DEBUG: response);
	}
	});
}

function getUrls() {
	clearTimeout(t);
	var provider = $('#pro').val();
	var feedUrl = $('#cat').val();

	if (old_feedUrl !== feedUrl) {
		count = 0;
	}
	if (count < newsNum) {
		$('#status').fadeOut();
		$.ajax( {
			url :'/parseFeed',
			data : {
				cat :feedUrl
			},
			error : function(xhr) {
				alert(ERROR_CONNECT);
				stop();
			},
			success : function(response) {				
				urls = $(response).find('url')
				newsNum = urls.length;
				var url = urls.eq(count).text();
				speed = Number($('#speed').val());
				showContent(url, provider, newsNum);
				old_feedUrl = feedUrl;
			}
		});
	} else {
		alert(ERROR_MSG_END);
		stop();
	}
}

// Dynamic Category Generator- start
function createCat(sProvider) {
	$('#cat>option').remove();
	sProvider.find('attributes').each(
			function() {
				var catValue = $(this).find('url').text();
				var catName = $(this).find('name').text();
				$("#cat").append(
						"<option value=" + catValue + ">" + catName
								+ "</option>");
			});
}

function parseConfigXml(configXml) {
	var provider = $('#pro').val();
	switch (provider) {
	case 'yahoo':
		var catSet = $(configXml).find("provider[id=yahoo]");
		createCat(catSet);
		break;
	case "udn":
		var catSet = $(configXml).find("provider[id=udn]");
		createCat(catSet);
		break;
	case "bnext":
		var catSet = $(configXml).find("provider[id=bnext]");
		createCat(catSet);
		break;
	case "hinet":
		var catSet = $(configXml).find("provider[id=hinet]");
		createCat(catSet);
		break;
	case "pchome":
		var catSet = $(configXml).find("provider[id=pchome]");
		createCat(catSet);
		break;
	case "bbc":
		var catSet = $(configXml).find("provider[id=bbc]");
		createCat(catSet);
		break;
	case "yam":
		var catSet = $(configXml).find("provider[id=yam]");
		createCat(catSet);
		break;
	case "lib":
		var catSet = $(configXml).find("provider[id=lib]");
		createCat(catSet);
		break;
	case "msn":
		var catSet = $(configXml).find("provider[id=msn]");
		createCat(catSet);
		break;
	case "ct":
		var catSet = $(configXml).find("provider[id=ct]");
		createCat(catSet);
		break;
	case "apple":
		var catSet = $(configXml).find("provider[id=apple]");
		createCat(catSet);
		break;
	default:
		alert(ERROR_CONNECT);
		stop();
	}
}

function cat() {
	$.ajax( {
		type :"GET",
		url :"./cat/config.xml",
		dataType :"xml",
		success :parseConfigXml
	});
};
// Dynamic Category Generator - end

// Player actions - Start
function start() {
	$('#header').slideUp();
	$(this).attr('disabled', true);
	$('#pro').attr('disabled', true);
	$('#cat').attr('disabled', true);
	$('#btn_pause').attr('disabled', false);
	$('#btn_stop').attr('disabled', false);
	
	getUrls();
	isStart = 1;
}

function stop() {
	clearTimeout(t);
	location.href = "/";
}

function pause() {
	clearTimeout(t);
	$('#btn_start').attr('disabled', false);
	$('#btn_pause').attr('disabled', true);
	if (count > 1) {
		$('#btn_previous').attr('disabled', false);
	} else {
		$('#btn_previous').attr('disabled', true);
	}
	if (count > 0 && count < newsNum) {
		$('#btn_next').attr('disabled', false);
	} else {
		$('#btn_next').attr('disabled', true);
	}
	$('#status').fadeIn();	
	isStart = 0;
}

function next() {
	if (count >= newsNum) {
		count = newsNum - 1;
		alert(ERROR_MSG_END);
		stop();
	} else {		
		start()
		isStart = 1;
	}
}

function previous() {
	count = count - 2;
	if (count < 0) {
		count = 0;
		alert(ERROR_MSG_START)
		stop();
	} else {
		start()
		isStart = 1;
	}
}
function action() {
	if (isStart !== 0) {
		$('#btn_start').click(pause);
	} else {
		$('#btn_start').click(start);
	}
	$('#btn_start').click(start);
	$('#btn_pause').click(pause);
	$('#btn_stop').click(stop);
	$('#btn_previous').click(previous);
	$('#btn_next').click(next);

	$('#loading').ajaxStart( function() {
		$(this).show();
		$('#pro').attr('disabled', true);
		$('#cat').attr('disabled', true);
		$('#speed').attr('disabled', true);
		$('#btn_start').attr('disabled', true);
		$('#btn_next').attr('disabled', true);
		$('#btn_previous').attr('disabled', true);
		$('#btn_pause').attr('disabled', true);
		$('#btn_stop').attr('disabled', false);
	});
	$('#loading').ajaxStop( function() {
		$(this).hide();
		$('#pro').attr('disabled', false);
		$('#cat').attr('disabled', false);
		$('#speed').attr('disabled', false);
		$('#btn_pause').attr('disabled', false);
				
		if (isStart !== 0) {
			$('#btn_start').attr('disabled', true);
			if (count > 0) {
				$('#btn_pause').attr('disabled', false);
				$('#btn_stop').attr('disabled', false);
			} else {
				$('#btn_pause').attr('disabled', true);
				$('#btn_stop').attr('disabled', true);
			}
			if (count > 1) {
				$('#btn_previous').attr('disabled', false);
			}
			if (count > 0 && count < newsNum) {
				$('#btn_next').attr('disabled', false);
			}
		} else {
			$('#btn_start').attr('disabled', false);
			if (count > 0) {
				$('#btn_pause').attr('disabled', true);
				$('#btn_stop').attr('disabled', false);
			} else {
				$('#btn_pause').attr('disabled', true);
				$('#btn_stop').attr('disabled', true);
			}
			if (count > 1) {
				$('#btn_previous').attr('disabled', false);
			}
			if (count > 0 && count < newsNum) {
				$('#btn_next').attr('disabled', false);
			}
		}

	});
}
// Player actions - End

// Events - start
function checkKeycode(e) {
	var keycode;

	if (window.event) {
		keycode = window.event.keyCode; // IE
	} else if (e.which) {
		keycode = e.which; // Netscape/Firefox/Opera
	}

	if (keycode == 32 || keycode == 86 || keycode == 90) {
		if (isStart !== 0) {
			pause();
		} else {
			start();
		}
	} else {
		switch (keycode) {
		case 39:
			// next news
			next();
			break;
		case 67:
			// next news
			next();
			break;
		case 37:
			// previous news
			previous();
			break;
		case 88:
			// previous news
			previous();
			break;
		case 38:
			// text size inc
			ts('content', 1);
			break;
		case 49:
			// text size inc
			ts('content', 1);
			break;
		case 40:
			// text size dec
			ts('content', -1);
			break;
		case 50:
			// text size dec
			ts('content', -1);
			break;
		case 27:
			// stop
			stop();
			break;
		}
	}
}
//Events - end
