/**
 * neXGam 3
 */

if (typeof _nexgam == 'undefined') {
	var _nexgam = {};
};


_nexgam.setHtml = function(o, html)
{
	o.css('opacity', 0);
	o.html( html );
	o.animate({
		opacity : 1
	});
};

_nexgam.loader =
{
	show : function()
	{
		$('#left-content-loader').show();
		$('#right-content-loader').show();
	},

	hide : function()
	{
		$('#left-content-loader').hide();
		$('#right-content-loader').hide();
	}
};

_nexgam.add =
{
	reload : function()
	{
		// Werbung neu laden

	}
};

_nexgam.error =
{
	show : function(msg)
	{
		$('.message-error p').html( msg );
		$('.message-error .close').html('');

		$('.message-error .close').append(
			new _ptools.Button({
				text    : 'schließen',
				onclick : function(Btn)
				{
					_nexgam.error.hide();
				},
				width : 120
			}).create()
		);

		$('.message-error .close').css({
			'width'  : 120,
			'margin' : '0 auto'
		});

		$('.message-bg').show();
		$('.message-error').show('fast');
	},

	hide : function()
	{
		$('.message-bg').hide();
		$('.message-error').hide();
	}
};

_nexgam.info =
{
	show : function(msg)
	{
		$('.message-info p').html( msg );
		$('.message-info .close').html('');

		$('.message-info .close').append(
			new _ptools.Button({
				text    : 'schließen',
				onclick : function(Btn)
				{
					_nexgam.info.hide();
				},
				width : 120
			}).create()
		);

		$('.message-info .close').css({
			'width'  : 120,
			'margin' : '0 auto'
		});

		$('.message-bg').show();
		$('.message-info').show('fast');
	},

	hide : function()
	{
		$('.message-bg').hide();
		$('.message-info').hide();
	}
};

_nexgam.report =
{
	show : function()
	{
		$('.message-bg').show();

		var Elm = $(document.createElement('div'));

		Elm.css({
			position : 'fixed',
			width    : '600px',
			height   : '400px',
			top      : '50%',
			left     : '50%',
			marginTop  : '-250px',
			marginLeft : '-300px',
			background : '#fff',
			zIndex     : 100,
			border     : '5px solid #3738C4',
			display    : 'none'
		});

		Elm.attr('id', 'report-window');

		Elm.append(
			'<div style="margin: 20px;">' +
				'<h3>Fehler melden</h3>' +

				'<p>' +
					'<label for="report-username">Name</label>' +
					'<input name="username" id="report-username" type="text" size="60" />' +
				'</p>' +
				'<p>' +
					'<label for="report-email">E-Mail</label>' +
					'<input name="email" id="report-email" type="text" size="60" />' +
				'</p>' +
				'<p>' +
					'<label for="report-message">Message</label>' +
					'<textarea id="report-message" name="message" rows="9" cols="42"></textarea>' +
				'</p>' +

				'<div id="report-btns"></div>' +
			'</div>'
		);

		$('body').append( Elm );

		$('#report-btns').append(
			new _ptools.Button({
				name    : '',
				text    : 'schließen',
				width   : 100,
				onclick : function(Btn)
				{
					_nexgam.report.hide();
				}
			}).create()
		);

		$('#report-btns').append(
			new _ptools.Button({
				name    : '',
				text    : 'melden',
				width   : 100,
				onclick : function(Btn)
				{
					_nexgam.report.send();
				}
			}).create()
		);

		$('#report-window').show('fast');
	},

	hide : function()
	{
		$('#report-window').hide('fast', function() {
			$('#report-window').remove();
		});

		$('.message-bg').hide();
	},

	send : function()
	{
		_Ajax.asyncPost('ajax_nexgam_report', function(result)
		{
			_nexgam.report.hide();

			_nexgam.info.show(
				'Deine Nachricht wurde erfolgreich versendet.'
			);
		}, {
			project  : _pcsg.Project.name,
			lang     : _pcsg.Project.lang,
			id       : _pcsg.Site.id,
			username : $('#report-username').val(),
			email    : $('#report-email').val(),
			message  : $('#report-message').val(),
			onError  : function(Exception)
			{
				_nexgam.report.hide();

				_nexgam.error.show(
					Exception.getMessage()
				);
			}
		});
	}
};

_nexgam.socialbox =
{
	load : function()
	{
		var path = BIN_URL +'images/social/';

		$('.social-box-header').append(
			//'<img src="'+ path +'nexgam.png" class="social-icon" />' +
			'<img src="'+ path +'facebook.png" class="social-icon" type="facebook" />' +
			'<img src="'+ path +'twitter.png" class="social-icon" type="twitter" />' +
			'<img src="'+ path +'rss.png" class="social-icon" type="rss" />'
		);


	},

	open : function(Elm)
	{
		switch (Elm.attr('type'))
		{
			case 'twitter':
				_nexgam.socialbox.twitter();
			break;

		}
	},

	loadStart : function()
	{

	},

	loadStop : function()
	{

	}
};

_nexgam.galery =
{
	load : function()
	{
		// galerie nicht klickbar machen
		$('.galery .title').css({
			'cursor'     : 'default',
			'MozOutline' : 'none'
		});
		$('.galery .title').click(function() {
			return false;
		});


		// next + prev
		$('.galery-sheets').prepend(
			'<div class="prev"></div>' +
			'<div class="next"></div>'
		);

		$('.galery-sheets .prev').css({
			background : 'url("'+ BIN_URL +'images/22x22/prev.png") no-repeat center center',
			width      : '30px',
			height     : '20px',
			cursor     : 'pointer',
			'float'    : 'left'
		});

		$('.galery-sheets .prev').click(function()
		{
			_nexgam.galery.prev( this );
			return false;
		});

		$('.galery-sheets .next').css({
			background : 'url("'+ BIN_URL +'images/22x22/next.png") no-repeat center center',
			width      : '30px',
			height     : '20px',
			cursor     : 'pointer',
			'float'    : 'left'
		});

		$('.galery-sheets .next').click(function()
		{
			_nexgam.galery.next( this );
			return false;
		});

		// lupen event
		this.loadImageEvents();
	},

	loadImageEvents : function()
	{
		$('.galery li').each(function()
		{
			$(this).addClass('highslide ');
			$(this).click(function()
			{
				$(this).find('a').trigger('onclick');
			});
		});
	},

	next : function()
	{
		var sheet = parseInt($('.galery .title').attr('sheet'));

		if (!sheet) {
			sheet = 0;
		};

		sheet = sheet + 1;

		this.ajax( sheet );
	},

	prev : function(Elm)
	{
		var sheet = parseInt($('.galery .title').attr('sheet'));

		if (!sheet) {
			sheet = 0;
		};

		sheet = sheet - 1;

		this.ajax( sheet );
	},

	ajax : function(sheet)
	{
		$('.galery .galery-loader').show();

		_Ajax.asyncPost('ajax_nexgam_galery_images', function(result, Ajax)
		{
			$('.galery ul').html( result.html );
			_nexgam.galery.loadBgs();

			// next
			//$(Ajax.getAttribute('Elm')).attr('sheet', parseInt(result.next));
			$('.galery .title').attr('sheet', parseInt(result.sheet))

			var tSpan = $('.galery .title span');
			var text  = 'Galerie '+ (parseInt(result.sheet)+1) +' / '+ tSpan.attr('sheets');

			$('.galery .title span').html( text );

			// lupen event
			_nexgam.galery.loadImageEvents();

			$('.galery .galery-loader').show();
		}, {
			project  : _pcsg.Project.name,
			lang     : _pcsg.Project.lang,
			id       : _pcsg.Site.id,
			sheet    : sheet,
			//Elm      : Elm,
			onError  : function(Exception)
			{
				_nexgam.report.hide();

				_nexgam.error.show(
					Exception.getMessage()
				);
			}
		});
	},

	loadBgs : function()
	{
		$('.galery ul li').each(function()
		{
			$(this).css({
				opacity : 0
			});

			var path = $(this).css('backgroundImage').toString();
			path     = path.substring(4, (path.length-1)).replace(/"/g, '');

			var Parent = this;

			$(new Image()).attr('src', path).load(function()
			{
				$(Parent).animate({
					opacity : 1
				});
			});
		});
	}
};

$(window).load(function()
{
	if ($.cookie('nexgam-bg'))
	{
		$('html').css({
			background : $.cookie('nexgam-bg')
		});
	};
});

$(window).ready(function()
{
	// Kontakt Button
	$('.page-header .report').click(function()
	{
		_nexgam.report.show();
		return false;
	});

	// social
	_nexgam.socialbox.load();

	// background
	$('.changebg').click(function()
	{
		$('html').css({
			background : 'url('+ $(this).attr('bgimage') +') no-repeat fixed'
		});

		if ($(this).attr('bg')) {
			$('html').css('background-color', $(this).attr('bg'));
		};

		$.cookie('nexgam-bg', $('html').css('background'));

	}).css({
		'float' : 'left',
		margin : 10,
		color  : '#fff',
		cursor : 'pointer'
	});

	_pcsg.plugins.searchPlus.bind($('#searchfield'), [
		'gamereviews/entry',
		'gamereviews/hardware',
		'base/list',
		'base/listing',
		'standard'
	], {
		onsubmit : function(Elm)
		{
			window.location = '/'+ Elm.attr('url');
		}
	});

	_nexgam.galery.load();

	/**
	 * youtube player laden
	 */

	if (!$('.nexgam-youtube-videos').length) {
	 	return;
	};

    swfobject.embedSWF("http://www.youtube.com/e/"+ $('.nexgam-youtube-videos')[0].getAttribute('yid') +"?enablejsapi=1&playerapiid=ytplayer",
                       "ytapiplayer", "300", "356", "200", null, null, params, atts);

	var params = {
		allowScriptAccess: "always",
		wmode : "opaque"
	};

    var atts = { id: "myytplayer" };

    swfobject.embedSWF(
    	"http://www.youtube.com/e/"+ $('.nexgam-youtube-videos')[0].getAttribute('yid') +"?enablejsapi=1&playerapiid=ytplayer",
        "ytapiplayer", "300", "200", "8",
        null, null, params, atts
	);

	// video load
	$(".nexgam-youtube-videos").hover(
		function()
		{
			if ($(this).hasClass('active')) {
				return;
			};

			$(this).stop().animate({"opacity": "1"}, "fast");
		},
		function()
		{
			if ($(this).hasClass('active')) {
				return;
			};

			$(this).stop().animate({"opacity": "0.7"}, "fast");
		}
	).css({
		"opacity": "0.7"
	});

	$('.nexgam-youtube-videos').first().addClass('active').css({
		"opacity": "1"
	});

	$('.nexgam-youtube-videos').click(function()
	{
		ytplayer.loadVideoById(
			$(this).attr('yid'), 0
		);

		$('.nexgam-youtube-videos').each(function()
		{
			$(this).css({"opacity": "0.7"});
			$(this).removeClass('active');
		});

		$(this).addClass('active');
		$(this).css({"opacity": "1"})
	});
});

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
//  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
};

