//if (typeof $ == 'undefined' || typeof Effect == 'undefined') {
//	document.write('<sc'+'ript type="text/javascript" src="/www/prototype.js"></scr'+'ipt>');
//	document.write('<sc'+'ript type="text/javascript" src="/www/scriptaculous/scriptaculous.js?load=effects"></scr'+'ipt>');
//}
document.write('<sc'+'ript type="text/javascript" src="/plugins_js/templates.js"></scr'+'ipt>');

/**
 * @version 2.0
 */
var wnlive = new function() {

	this.tpl					= '';
	this.lastId					= 0;
	this.maxResults				= 0;
	this.tsLastItem				= 0;
	this.jsonData				= '';
	this.paused					= true;
	this.fetching				= false;
	this.filterNames			= ['wnFilterVote', 'wnFilterStory', 'wnFilterComm'];
	this.wnlive_title_length	= 75;

	this.run = function(lastId, tpl) {
console.info('run');
		this.lastId 	= lastId;
		this.tpl		= $.template(tpl);
		this.maxResults	= lastId;

		if (typeof wnlive_startup != 'undefined') {
			this.jsonData 		= wnlive_startup.data;
			this.tsLastItem 	= wnlive_startup.tsLastItem;
		}
		if (typeof wnlive_title_length != 'undefined') {
			this.wnlive_title_length = wnlive_title_length;
		}

		if(!this.checkPauseCookieExists()) {
			this.paused = false;

			if (this.jsonData.length == 0) {
				setTimeout('wnlive.fetchData()', 1000);
			} else {
				setTimeout('wnlive.updateHTML()', 1000);
			}
		}
	}

	this.fetchData = function(limit) {
console.info('fetchData');
		if(wnlive.paused || wnlive.fetching || wnlive.jsonData.length > 0) {
			return;
		}
		wnlive.fetching = true;

		$.get('/wnlive', { ts: wnlive.tsLastItem }, wnlive.storeResults);
	}

	this.storeResults = function(data) {
		wnlive.fetching = false;

		wnlive.jsonData 	= data.data;
		wnlive.tsLastItem	= data.tsLastItem;

		if(wnlive.jsonData.length == 0) {
			setTimeout('wnlive.fetchData(10);', 7500);
		} else {
			wnlive.tsLastItem	= data.responseText.evalJSON().tsLastItem;
			wnlive.updateHTML();
		}
	}

	this.updateHTML = function() {
console.log('updateHTML');
		if(wnlive.paused) {
			return;
		}

		json = wnlive.jsonData;

		if(json.length == 0) {
			wnlive.jsonData = new Array();
			wnlive.fetchData(10);
			return;
		}

		new_jsonData = new Array();
		for(i=1; i<json.length; i++) {
			new_jsonData.push(json[i]);
		}
		wnlive.jsonData = new_jsonData;

		newItemId = 'wnlive_'+(wnlive.lastId+1);
		insertBefore 	= 'wnlive_'+wnlive.lastId;
		removeLastElem	= 'wnlive_'+(wnlive.lastId-wnlive.maxResults+1);

		action_user = '<a href="/mitglied/'+json[0].actionUser+'">'+json[0].actionUser+'</a>';
		startpage_action_user = '<a href="/mitglied/'+json[0].author+'">'+json[0].author+'</a>\'s Nachricht';

		action = '';
		if (json[0].actionType == 16 && json[0].actionUser != '') {
			action_pic  = '/pics/wnlive_vote.gif';
			action_name = 'bewertet von';
			action_name2 = 'bewertet: ';
		} else if (json[0].actionType == 16 && json[0].actionUser == '') {
			action_pic  = '/pics/wnlive_vote.gif';
			action_user = '';
			action_name = 'anonym bewertet';
			action_name2 = 'bewertet: ';
		} else if (json[0].actionType == 32) {
			action_pic  = '/pics/wnlive_submit.gif';
			action_name = 'verlinkt von';
			action_name2 = 'eingestellt: ';
		} else if (json[0].actionType == 64) {
			action_pic  = '/pics/wnlive_comment.gif';
			action_name = 'kommentiert von';
			action_name2 = 'kommentiert:  ';
		}

		title = json[0].title;
		if (title.length > this.wnlive_title_length) {
			title = title.truncate(60);
		}
		points = (parseInt(json[0].ranking_sum) == 1 ? '_1' : '');
		data = { 'newItemId': newItemId, 'story_link': json[0].story_link, 'votes': parseInt(json[0].ranking_sum), 'points': points, 'title': title, 'author': json[0].author, 'timeago': json[0].timeago, 'host': json[0].host, 'action_pic': action_pic, 'action_user': action_user, 'action_name': action_name, 'action_name2': action_name2, 'category_link': json[0].category_link, 'category_name': json[0].category_name };

		$('#'+insertBefore).before(this.tpl, data);

		$('#'+newItemId).fadeIn();
		$('#'+removeLastElem).remove();

		wnlive.lastId++;
		setTimeout('wnlive.updateHTML()', 2000);
	}

	this.pause = function() {

		this.paused = true;
		this.setPauseCookie();
	}

	this.reset = function() {

		this.paused = true;
		this.setPauseCookie();
		this.jsonData = '';
	}

	this.resume = function() {

		if(this.paused == true) {
			this.paused = false;
			this.setPauseCookie();
			setTimeout('wnlive.updateHTML()', 500);
		}
	}

	this.playOrPause = function() {
		imgSrcAdd = '';
		if (typeof wnlive_smallswichpic != 'undefined' && wnlive_smallswichpic == true) {
			imgSrcAdd = '2';
		}
		if (this.paused == true) {
			imgSrc = '/pics/wnlive_switch_pause'+imgSrcAdd+'.gif';
			this.resume();
		} else {
			imgSrc = '/pics/wnlive_switch_play'+imgSrcAdd+'.gif';
			this.pause();
		}
		$('#wnlive_PlayOrPause').attr('src', imgSrc);
	}

	this.setPauseCookie = function() {
		_expires = new Date();
		if (this.paused == true) {
			_expires.setDate(_expires.getDate()+7);
		} else {
			_expires.setDate(_expires.getDate()-1);
		}

		_cookie = 'wnlive_pause=1; expires='+_expires.toGMTString()+'; path=/';
		document.cookie = _cookie;
	}

	this.checkPauseCookieExists = function() {
		return (document.cookie.indexOf('wnlive_pause') != -1);
	}

//	this.getFilter = function() {
//		bitVal = 0;
//		this.filterNames.each(function(s){
//			if ($(s).checked) {
//				bitVal += parseInt($(s).value);
//			}
//		});
//
//		if (bitVal == 0) {
//			this.filterNames.each(function(s){
//				$(s).checked = true;
//				bitVal += parseInt($(s).value);
//			});
//		}
//
//		return bitVal;
//	}
//
//	this.filterChange = function() {
//
//		this.setFilterCookie(this.getFilter());
//	}
//
//	this.setFilterCookie = function(bitVal) {
//
//		_expires = new Date();
//		_expires.setDate(_expires.getDate()+31);
//
//		_cookie = 'wnlive_filter='+bitVal+'; expires='+_expires.toGMTString()+'; path=/';
//		document.cookie = _cookie;
//	}

	this.clickAction = function(url) {
		document.location.assign(url);
		return;
	}
}

$(document).ready(function() {
	wnlive.run(wnlive_max, wnlive_tpl);
});