// games icons hover
$(function() {
	$("#gameblock ul li").hover(function() {
		$(this).addClass("hover");
	},function() {
		$(this).removeClass("hover");
	})
})


// change game
$(function() {
	var flashBlock = $('#flashblock');

	var swfObj = function(params) {
		return [
			'<object classid="clsid:', params.clsid,
			'" width="', params.width,
			'" height="', params.height,
			'"><param name="', params.content,
			'" value="', params.href,
			'" />',
			params.additionalParams,
			'<!--[if !IE]>--><object type="', params.type,
			'" data="', params.href,
			'" width="', params.width,
			'" height="', params.height,
			'"><!--<![endif]-->',
			params.alterText,
			'<!--[if !IE]>--></object><!--<![endif]--></object>'
		].join('')
	}

	var embedGame = function(href, w, h, type, title) {
		var html = '';

		switch(type) {
			case 'dcr':
				html = '<h3>' + title + '</h3>';
				html += swfObj({
					clsid: '166B1BCA-3F9C-11CF-8075-444553540000',
					type: 'application/x-director',
					additionalParams: "<param name=\"swRemote\" value=\"swSaveEnabled='true' swVolume='true' swRestart='true' swPausePlay='true' swFastForward='true' swContextMenu='true'\" /><param name=\"swStretchStyle\" value=\"none\" />",
					alterText: '<p>Для игры требуется Shockwave-плагин. Чтобы загрузить плагин, нажмите <a href="http://get.adobe.com/shockwave/" target="_blank">сюда</a>.</p>',
					content: 'src',
					width: w,
					height: h,
					href: href
				});
				break;

			case 'swf':
				html = '<h3>' + title + '</h3>';
				html += swfObj({
					clsid: 'D27CDB6E-AE6D-11CF-96B8-444553540000',
					type: 'application/x-shockwave-flash',
					additionalParams: '<param name="wmode" value="opaque" />',
					alterText: '<p>Для игры требуется Flash-плагин. Чтобы загрузить плагин, нажмите <a href="http://get.adobe.com/flashplayer/" target="_blank">сюда</a>.</p>',
					content: 'movie',
					width: w,
					height: h,
					href: href
				});
				break;

			case 'html':
			default:
				html = '<h3>' + title + '</h3><iframe src="' + href +'" frameborder="0" width="'+ w +'" height="'+ h +'" scrolling="no"></iframe>';
				break;
		}

		flashBlock[0].innerHTML = html;
	}

	$('#gameblock li a').click(function() {
		var width  = $(this).attr('games:width');
		var height = $(this).attr('games:height');
		var type   = $(this).attr('games:type');
		var title  = $(this).attr('games:title');
		var href   = this.href;

		flashBlock[0].innerHTML = '';

		flashBlock.addClass('slide-loading');

		flashBlock.animate(
			{'width': width }, 'fast'

		).animate(
			{'height': height }, 'slow'

		).queue(function(){
			// FIXME flashBgs.css('width', bgsWidth / 2 + 'px');
			flashBlock.css({'float': 'none', 'margin-right': 'auto'});
			$(this).dequeue();

		}).queue(function() {
			embedGame(href, width, height, type, title);

			$(this).removeClass('slide-loading').addClass('slide-loaded');

			$(this).dequeue();
		});
		var top = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		$("body").addClass("veil");
		$(".popupwrapper").css({"top": top+100, "margin-left": -(parseInt(width)+66)/2});

		if (window.navigator.userAgent.search('MSIE 7.0') != -1) {
			$(".popupwrapper").css("width", parseInt(width)+59);
		}

		return false;
	});
});

$(function() {
	$("a.closeVeil").click(function(){
		$("body").removeClass("veil");
		$(this).closest(".popupwrapper").hide(500);
		$("#flashblock").css({"width": "0", "height": "0"}).empty();
		return false;
	});
})
