/*
    __                  __            
   |  | _____.__. ____ |  | _______   
   |  |/ <   |  |/  _ \|  |/ /\__  \  
   |    < \___  (  |_| )    <  / __ \_
   |__|_ \/ ____|\____/|__|_ \(____  /
        \/\/                \/     \/ 
     
--- Copyright 2010, 2011, 2012 Gaikai Inc. ---

*/

	(function(){
	
		try {
		
			if ((false || (window.gaikai && gaikai.kyokaIframe)) && !parent.gkKyokaFrame && window.gaikai && !window.gaikai.kimonoTargetId) {
			
				var canvas = document.getElementById('gkCanvas');
			
				gaikai.isLightbox = canvas === null;
			
				var f = document.createElement('iframe');
				
				f.id = 'gkKyokaFrame';
				f.setAttribute('frameborder', 0);
				f.setAttribute('allowtransparency', true);
				
				if (gaikai.isLightbox) {
					f.style.position = 'absolute';
					f.style.left = '0px';
					f.style.top = '0px';
					f.style.width = '0%';
					f.style.height = '0%';
					f.style.zIndex = 2147483647;
					document.body.appendChild(f);
				}
				else {
					f.style.width = '100%';
					f.style.height = '100%';
					canvas.innerHTML = '';
					canvas.appendChild(f);
				}
						
				window.gkKyokaFrame = f;
					
				var kUrl = location.protocol + '//kyoka.gaikai.com/kjs?';

				var doc = f.contentWindow.document;
					
				doc.open();
				doc.write('<!DOCTYPE html>');
				doc.write('<html><body style="background-color: transparent"><div id="gk-kimono-container"></div></body></html>');
				doc.write('<scr' + 'ipt src="' + kUrl + '&HTTP_REFERER=' + document.location.href + '"></scr' + 'ipt>');
				doc.close();
				
				return;
			}
		
			var clone = function(obj) {
		
				if (obj === null) {
					return null;
				}
				
				if (typeof obj !== 'object') {
					return obj;
				}
				
				var c = (obj instanceof Array) ? [] : {};
				
				for (var p in obj) {
					if (obj.hasOwnProperty(p) === true) {
						if (typeof obj[p] === 'object') {
							if (p instanceof Array) {
								c[p] = [];
								for (var i = 0; i < p.length; i++) {
									c[p].push(p[i]);
								}
							}
							else {
								c[p] = clone(obj[p]);
							}
						}
						else {
							c[p] = obj[p];
						}
					}
				}
				return c;
			};
			
			
	window.gaikaiApi = window.gaikaiApi || new function() {
			
		var self = this;
		
		var queue;
		var callback;
		
		self.getCallback = function() {
			return callback;
		};
		
		self.getQueue = function() {
			return queue;
		};
		
		self.refreshOfferPoints = function() {
			try {
				gkCampaignPage.refresh();
			}
			catch(ex) {
				if (gkLog) {
					gkLog(ex, ex.stack);
				}
			}
		};
		
		self.refreshGameCodeBanners = function() {
			try {
				gaikai.lib.tasks.injectBanners.refresh();
			}
			catch(ex) {
				if (gkLog) {
					gkLog(ex, ex.stack);
				}
			}
		};
				
		self.doPlay = function(gameCode, f) {
			
			var log = window.gkLog || function(){};
			
			log('gaikaiApi.doPlay()', gameCode, f);
			
			if (typeof gameCode !== 'string') {
				log('!', 'Error: gameCode must be a string');
				return;
			}
			
			if (typeof f === 'function') {
				callback = f;
				log('callback =', f);
			}
			
			var r = false;
			try {
				r = gaikai.lib.tasks.waitForPlay.play(gameCode);
			}
			catch(ex) {}
			
			if (r !== true && queue === null) {
				queue = gameCode;
				callback('QUEUED ' + gameCode);
			}
		};
		
		self.init = function() {
			queue = null;
			callback = function(){};
		};
		
		(function(){
			self.init();
		})();
		
	};
				
			window.gaikai = window.gaikai || parent.gaikai || {};
			
			window.gaikaiClone = clone(window.gaikai);

			window.gkReferer = window.gkReferer || '';
			
			if (!window.gkReferer) {
				try {
					console.log('Missing HTTP_REFERER, Kyoka aborted');
				}
				catch(ex){}
				return;
			}
			
			gaikai.kyokaStarted = true;
			
			
			
			
(function() {

	window.gaikai = window.gaikai || {};
	
	if (gaikai.callbackArray instanceof Array === false) {
		gaikai.callbackArray = [];
	}
	
	gaikai.gkCallback = gaikai.gkCallback || function(obj) {
		if (typeof gaikai.messageCallback === 'function') {
			gaikai.messageCallback(obj);
		}
		for (var i = 0; i < gaikai.callbackArray.length; i++) {
			if (typeof gaikai.callbackArray[i] === 'function') {
				gaikai.callbackArray[i](obj);
			}
		}
	};

})();
			
			
			var gameCode = gaikai.gameCode || null;
		
			if (document.getElementById('gkKyokaSetup') === null) {
			
				var js = document.createElement('script');
				js.id = 'gkKyokaSetup';
				js.className = 'gkKyokaElement';
				js.src = location.protocol + '//kyoka.gaikai.com/Kyoka/php/kyokaSetup.js.php?&' + (new Date).getTime();
				js.src += '&';
				js.src += '&HTTP_REFERER=' + window.gkReferer;
				
				if (typeof gaikai.sesKey === 'string') {
					js.src += '&gkSesKey=' + gaikai.sesKey;
				}
				
				if (gaikai.refId) {
					js.src += '&refId=' + gaikai.refId;
				}
				
				if (gaikai.gkTestId) {
					js.src += '&gkTestId=' + gaikai.gkTestId;
				}
			
				if (gameCode) {
					js.src += '&gameCode=' + gameCode;
				}
				
				try {
					if (window == top && location.search.charAt(0) === '?') {
						js.src += '&' + location.search.substr(1);
					}
				}
				catch(ex){}
				
				var elem = document.getElementsByTagName('script')[0];
				elem.parentNode.insertBefore(js, elem);
				
				gaikai.gkCallback({message: 'kyokaVersion', data: {version: '9.4.2'}});
			}
			
				}
		catch(ex){
			try {
				console.log('Caught exception in kyoka.js.php', ex, ex.stack);
			}
			catch(ex){}
		}
	})();
	

