﻿//	Connections Scripts

Chem.GamePortal = (function(){
	return function(options){
		Chem.GamePortal.parent.constructor.call(this, options);
	}
})();

Chem.Extend(Chem.GamePortal, Chem.AjaxModal);

Chem.GamePortal.prototype.addModalData = function(data){
	Chem.GamePortal.parent.addModalData.call(this, data);
	this.modal
		.find('iframe').autoFrameHeight({ onReadyEvent: 'updateModal' }).end()
		.find('a.starter').click($.proxy(this.launchGame, this));
};

Chem.GamePortal.prototype.hide = function(){
	Chem.GamePortal.parent.hide.call(this);
	window.location.reload();
};

Chem.GamePortal.prototype.launchGame = function(e){
	var $link = $(e.target);
	this.modal
		.find('div.content').remove().end()
		.find('iframe').attr('src', $link.attr('href') + '&frame=true');
	return false;
};

//	INITIATE GAME PORTAL PLUGIN

(function($){
	$.fn.gamePortal = function(){
		return this.each(function(options){
			var element = $(this),
			opt = $.extend({
				cl: 'gportal',
				ajaxType: 'GET',
				trigger: element,
				cacheData: false,
				ajaxData: { ajaxmode: 1 },
				id: element.attr('matchid'),
				ajaxUrl: element.attr('href')
			}, options || {});
			if (element.data('gamePortal')) return;
			var gamePortal = new Chem.GamePortal(opt);
			element.data('gamePortal', gamePortal);
		});
	}

	$.chem.updateGameUI = function(){
		$('a.close-portal').click(function(){
			window.top.location.reload();
			return false;
		});
		$('a.launch-portal').gamePortal();
	}

})(jQuery);

(function($){
	try{
		$.chem.updateGameUI();
		if(typeof closePortal == 'boolean' && closePortal){
			window.top.location.href = redirecturl;
		}
	}
	catch(error){
		logc(error)
	}
})(jQuery);

