var PGATeaser = Class.create();
PGATeaser.prototype = {
	container:  null,
	type:		'teaser',
	clientWnd:  null,
	currentSystem: 'PC-CDROM',

	initialize: function()
	{
		this.type = 'charts';
	},

	updateTeaserItems: function(data)
	{
		var html = "";
		for (var i=0;i<data.length;i++)
		{
			var spielname = data[i]["spielname"];
			var spielnamekurz = '';
			if (spielname.length > 15)
			{
				spielnamekurz = spielname.substring(0,7) + " ... " + spielname.substring(spielname.length-7,spielname.length);
			} else {
				spielnamekurz = spielname;
			}
			html += '		<div class="PGATeaserImage">';
			//html += '			<div class="PGATeaserImageContainer" onmouseover=\'new Effect.Scale(this,150,{scaleFromCenter:true, duration: 0.15});\' onmouseout=\'new Effect.Scale(this,66.6,{scaleFromCenter:true, duration: 0.15, scaleMode: {orginalHeight:80, originalWidth:60}});\'>';
			html += '			<div class="PGATeaserImageContainer">';
			html += '			<a href="' + data[i]['url'] + '" title="' + spielname +'"><img id="PGATeaserImage_' + data[i]['spielid'] + '" src="' + data[i]['imageurl'] + '" alt="' + spielname + '"><br>';
			html += '			<div class="PGATeaserText">' + spielnamekurz + '</div></a>';
			html += '			</div>';
			html += '		</div>';
		}

//		pga.teaserWnd.clientWnd.replaceContent(html);
		pga.teaserWnd.clientWnd.container.innerHTML = '';
		pga.teaserWnd.clientWnd.nextLevel(html);
	},

	showKonsolenCharts: function()
	{
		pga.teaserWnd.clientWnd.setHeight(130);
		var html = "";
		html += "<div class='PGAToolbar'><div class='PGAToolbarButtonBack' onclick='pga.teaserWnd.clientWnd.prevLevel()'></div><div class='PGACaption'>Konsolen-Verkaufscharts</div></div>";
		html += "<div class='PGACharts'><img src='" + PGAImageURL + "charts.png'></div>";
		pga.teaserWnd.clientWnd.nextLevel(html);
	},

	hideMediaControlCharts: function()
	{
		//new Effect.Scale('PGATeaserClient',100,{scaleFrom: 140, scaleContent: false, scaleX: false, duration: 0.5, scaleMode: {originalHeight: 130}});
		pga.teaserWnd.clientWnd.setHeight(130);
		pga.teaserWnd.clientWnd.prevLevel();
	},

	loadCharts: function(system)
	{
		if ($('PGAChartSelectButton_' + system).hasClassName('PGAActive'))
		{
			//Bereits aktiv;
			return;
		}

		$('PGAChartSelectButton_' + this.currentSystem).removeClassName('PGAActive');
		this.currentSystem = system;

		//Loader anzeigen
		var oldcontent = $('PGAChartSelectButton_' + this.currentSystem).innerHTML;
		$('PGAChartSelectButton_' + this.currentSystem).innerHTML = "<img class='PGALoader' src='" + PGAImageURL + "ajaxloader_msg.gif'>";

		var self = this;
		pga.callFunction("loadcharts",
						 "SYSTEM=" + system,
						 function(result)
						 {
						 	var html = "<ol>";
						 	for (var i=0;i<result.length;i++)
						 	{
							 	html += '<li>(' + result[i]['lwpos'] + ') <a href="' + result[i]['url'] + '" title="' + result[i]['name'] + '">' + pga.makeShort(result[i]['name'],20) + '</a></li>';
						 	}
						 	html += "</ol>";

						 	$('PGAChartsCharts').update(html);
						 	$('PGAChartSelectButton_' + self.currentSystem).update(oldcontent);
						 }
						 );

		$('PGAChartSelectButton_' + this.currentSystem).addClassName('PGAActive');
	},

	showMediaControlCharts: function() {
		if (Prototype.Browser.IE || Prototype.Browser.Opera) {
			pga.teaserWnd.clientWnd.setHeight(166);
		} else {
			pga.teaserWnd.clientWnd.setHeight(160);
		}

		var html = '';
//		html += '<div class="PGAToolbar"><div class="PGAToolbarButtonBack" onclick="pga.teaserWnd.hideMediaControlCharts()"></div><div class="PGACaption">4P-Store Charts</div></div>';
		html += '<div id="PGAChartsPlattformList">';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'PC-CDROM\')" id="PGAChartSelectButton_PC-CDROM">PC</div>';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'PlayStation2\')" id="PGAChartSelectButton_PlayStation2">PS2</div>';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'PlayStation3\')" id="PGAChartSelectButton_PlayStation3">PS3</div>';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'360\')" id="PGAChartSelectButton_360">360</div>';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'Wii\')" id="PGAChartSelectButton_Wii">Wii</div>';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'PSP\')" id="PGAChartSelectButton_PSP">PSP</div>';
		html += '	<div class="PGAListItem" onclick="pga.teaserWnd.loadCharts(\'NDS\')" id="PGAChartSelectButton_NDS">NDS</div>';
		html += '</div>';
		html += '<div id="PGAChartsCharts">';
		html += '</div>';

		pga.teaserWnd.clientWnd.container.innerHTML = '';
		pga.teaserWnd.clientWnd.nextLevel(html);

		$('PGATeaserHeadline').update('4P-Store Charts');
		this.loadCharts(this.currentSystem);
	},

	updateCharts: function() {
		var html = "";
		html += "<div id='PGAChartsList'>";
//		html += "	<div class='PGAListItem' onclick='pga.teaserWnd.showKonsolenCharts();'>Konsolen-Charts</div>";
		html += "	<div class='PGAListItem' onclick='pga.teaserWnd.showMediaControlCharts();'>4P-Store Charts</div>";
		html += "	<div style='clear:both'></div>";
		html += "</div>";
		this.clientWnd.replaceContent(html);
		$('PGATeaserHeadline').innerHTML = "Charts";
	},

	showWatchlist: function(data) {
		var html = '';
		var gameName = 'Diesen Namen gibt es nicht!!!';
		var height = 0;
		for (i = 0, j = data.length; i < j; i++) {
			if ((gameName != data[i]['nameshort']) && (gameName != data[i]['name'])) {
				gameName = data[i]['nameshort'] ? data[i]['nameshort'] : data[i]['name'];
				html += '<div class="WatchlistGame">' +
						'	<a href="http://www.4players.de/4players.php/spielinfo/Allgemein/' + data[i]['spielid'] + '/' + gameName + '.html">' + gameName + '</a>' +
						'</div>';
				height += 20;
			}
			var headline = data[i]['teaser'] ? data[i]['teaser'] : data[i]['headline'];
			var href = (data[i]['weiterlesen'] != '') ? data[i]['weiterlesen'] : 'http://www.4players.de/4players.php/spielinfonews/Allgemein/' + data[i]['spielid'] + '/' + data[i]['newsid'] + '/' + gameName + '.html';
			html += '<div id="WatchlistItem' + data[i]['newsid'] + '" class="WatchlistItem">' +
					'	<a href="' + href + '" title="' + headline + '">' +	headline.truncate(30, '...') + '</a>' +
					'	<div id="WatchListClose' + data[i]['newsid'] + '" class="PGAWatchlistButtonClose WatchlistSpielid' + data[i]['spielid'] + '"></div>' +
					'</div>';
			height += 18;
		}
		if (data.length == 0) {
			html += '<div class="WatchlistNoItem">Es gibt keine Neuigkeiten auf deiner Watchlist.</div>';
			height += 36;
		}
		html += '<div class="WatchlistManage">( <a href="https://login.4players.de/go/watchlist.php">Watchlist verwalten</a> )</div>';
		//this.clientWnd.replaceContent(html);
		pga.teaserWnd.clientWnd.setHeight(height + 20);
		pga.teaserWnd.clientWnd.nextLevel(html);
		$$('.PGAWatchlistButtonClose').each(function(e) {
			var newsid = e.id.replace(/WatchListClose/, '');
			var spielid = e.className.replace(/PGAWatchlistButtonClose WatchlistSpielid/, '');
			e.observe('click', function() {
				Effect.SwitchOff('WatchlistItem' + newsid);
				new Ajax.Request('/common/fusion/ajax/fusionproxy.php', {
					method: 'post',
		            parameters: {
		                COMMAND: 'getstatus',
		                SPIELID: spielid,
		                NEWSID: newsid,
						SCRIPTNAME: 'watchlistinfo'
		            }
				});
			});
		});
	},

	update: function() {
		if (this.type == 'teaser') {
			$('PGATeaserHeadline').update('Die aktuellen 4Players-Tests');
			this.showLoader();
			if (pga.teaserWnd.clientWnd.currentLevel > 1) {
				pga.teaserWnd.clientWnd.setHeight(130);
			}
			pga.callFunction('loadcontentteaser', '', this.updateTeaserItems);
		} else if (this.type == 'watchlist') {
			$('PGATeaserHeadline').update('Neues auf der Watchlist');
			this.showLoader();
			pga.callFunction('loadwatchlist', '', this.showWatchlist);
		} else if (this.type == 'charts') {
			this.showMediaControlCharts();
		}
	},

	showLoader: function() {
		pga.teaserWnd.clientWnd.replaceContent('<img style="margin:10px;" class="PGALoader" src="' + PGAImageURL + 'ajaxloader_msg.gif">', 0);
	},

	createWnd: function(container)
	{
		this.container = $(container);
		this.container.innerHTML = this.createHTML();

		this.clientWnd = new HorizontalNaviCtrl('PGATeaserClient','PGATeaserClientWnd','');
	},

	createHTML: function()
	{
		var html = "";

		html += "	<div class='PGAHeadline'><div class='PGACaption' id='PGATeaserHeadline'>Heute im 4Players-Magazin</div><div class='PGAHeadlineButtons'></div></div>";
		html += "	<div id='PGATeaserClient' class='PGAScrollContainer'>";
		html += "	</div>";

		return html;
	}
}
