var GameTreat;
var widget;
var launcher;
var mainView;
var gameView;
var comMgr;
var g_launchersCounter = 0;
var browserSupported;
var windowOriginalHTML;
var windowOriginalOverflow;
var windowOriginalScroll;
if(g_isJqueryEnable == undefined){
var g_isJqueryEnable = false;
}
if (!GameTreat) {
	window.GameTreat = {};
}

//disable drag and drop
GameTreat.Defines = {
		siteUrl: "http://widget.gametreat.com",
		footerLinksSiteUrl: "http://www.gametreat.com",
		urlParamsStartToken: "#GameTreatWidget?",
		widgetLoadingId:"widgetLoading",
		widgetLauncherId:"widgetLauncher",
		widgetMainViewId:"widgetMainView",
		widgetGameViewId:"widgetGameView",
		widgetContainerId:"gameTreatWidget",
		widgetMaskId:"widgetMask",
		//widgetLauncherUrl:"http://widget.gametreat.com/static/launcher.html",
		widgetLauncherUrl:"http://widget.gametreat.com/do/widgetLayout?type=launcher&width=%width%&height=%height%&wid=%wid%",
		widgetMainViewUrl:"http://widget.gametreat.com/do/widgetLayout?type=mainview&width=%width%&height=%height%&wid=%wid%",
		widgetGameViewUrl:"http://widget.gametreat.com/do/widgetLayout?type=gameview&width=%width%&height=%height%&wid=%wid%&GOTD=%GOTD%"
}
GameTreat.styles = {
		lightsOutMask:{
			position:"fixed",
			top:"0px",
			left:"0px",
			zIndex:9000,			
			backgroundColor:"#000",
			width:"100%",
			height:"100%",
			display:"none"
			
		},
		lightsOutMaskNoDocType:{
			position:"absolute"
		},
		loader:{
			color:"#FFFFFF",
			textAlign:"center",
			backgroundColor:"#343434",
			margin:"0px auto"
		},
		loaderImg:{
			display:"block",
			margin:"0px auto",
			width:"48px",
			height:"48px",
			position:"relative",
			top:"40%"
		},
		loaderRetryButton:{
			width:"123px",
			height:"33px",
			background:"url(http://widget.gametreat.com/static/img/retryButton.png) 0px 0px no-repeat",
			cursor:"pointer",
			margin:"0px auto",
			display:"none",
			position:"relative",
			top:"40%"
		},
		mainViewHover:{
			margin:"0px auto",
			fontFamily:"arial",
			fontSize:"12px",
			color:"#fff",
			textAlign:"center",
			left:"0px",
			top:"0px",
			position : "absolute",
			display:"none",
			zIndex:9001,
			overflow:"hidden"
		},
		mainViewEmbed:{
			position:"relative",
			display:"none",
			zIndex:9001,
			overflow:"hidden"
		},
		gameViewEmbed:{
			position:"relative",
			zIndex:9001,
			display:"none"
		},
		launcherGamatanium:{
			background:"url(http://widget.gametreat.com/static/img/launcherCommon/gametanium.png) no-repeat top right",
			width:"74px",
			height:"16px",
			float:"right",
			margin:"5px 12px 0 0px",
			cursor:"pointer"
		},
		footerStripeStyle425x425:{
			float:"right",
			width:"100%",
			height:"25px",
			fontFamily:"arial",			
			fontSize:"12px",
			position:"relative",
			zIndex:8999
		},
		footerLogo425x425:{
			background:"url('http://www.gametreat.com/static/img/logo.png') 0px 0px no-repeat",
			width:"90px",
			height:"22px",
			float:"left",
			margin:"0px 12px 2px 0",
			outline:"none",
			border:"none"
		},
		footerLink425x425:{
			width:"auto",
			margin:"3px 5px 0 0",
			height:"15px",
			float:"right",
			cursor:"pointer",
			textDecoration:"none",
			outline:"none",
			textAlign:"right",
			color:"#4A4A4A"
		},
		footerStripeStyle300x250:{
			float:"right",
			width:"100%",
			height:"25px",
			fontFamily:"arial",			
			fontSize:"12px",
			position:"relative",
			zIndex:8999
		},
		footerLogo300x250:{
			background:"url('http://widget.gametreat.com/static/img/logo.png') 0px 0px no-repeat",
			width:"90px",
			height:"22px",
			float:"left",
			margin:"0px 12px 2px 0",
			outline:"none",
			border:"none"
		},
		footerLink300x250:{
			width:"auto",
			margin:"3px 5px 0 0",
			height:"15px",
			float:"right",
			cursor:"pointer",
			textDecoration:"none",
			outline:"none",
			textAlign:"right",
			color:"#4A4A4A"
		},
		footerStripeStyle728x90:{
			float:"right",
			width:"129px",
			height:"40px",
			fontFamily:"arial",			
			fontSize:"12px",
			margin:"-40px 5px 0px 0px",
			position:"relative",
			zIndex:8999
		},
		footerLogo728x90:{
			background:"url('http://widget.gametreat.com/static/img/launcherCommon/gametanium.png') 0px 0px no-repeat",
			width:"74px",
			height:"16px",
			float:"right",
			margin:"0px 12px 2px 0",
			outline:"none",
			border:"none"
		},
		footerLink728x90:{
			width:"100%",
			margin:"3px 8px 0 0",
			height:"15px",
			float:"right",
			cursor:"pointer",
			textDecoration:"none",
			outline:"none",
			textAlign:"right",
			font:"11px arial"			
		},
		footerStripeStyle160x600:{
			float:"left",
			width:"129px",
			height:"40px",
			fontFamily:"arial",			
			fontSize:"12px",
			margin:"-40px 0px 0px 0px",
			position:"relative",
			zIndex:8999
		},
		footerLogo160x600:{
			background:"url('http://widget.gametreat.com/static/img/launcherCommon/gametanium.png') 0px 0px no-repeat",
			width:"74px",
			height:"16px",
			float:"left",
			margin:"0px 40px 0px",
			outline:"none",
			border:"none"
		},
		footerLink160x600:{
			width:"100%",
			margin:"3px auto 0 5px",
			height:"15px",
			float:"left",
			cursor:"pointer",
			textDecoration:"none",
			outline:"none",
			textAlign:"right",
			font:"11px arial"			
		},
		footerStripeStyle190x190:{
			float:"left",
			width:"180px",
			height:"40px",
			fontFamily:"arial",			
			fontSize:"12px",
			margin:"-25px 0px 0px 0px",
			position:"relative",
			zIndex:8999
		},
		footerLogo190x190:{
			background:"url('http://widget.gametreat.com/static/img/launcherCommon/gametanium.png') 0px 0px no-repeat",
			width:"74px",
			height:"16px",
			float:"left",
			margin:"0px 5px 0px",
			outline:"none",
			border:"none"
		},
		footerLink190x190:{
			width:"auto",
			margin:"5px 0px",
			height:"15px",
			float:"right",
			cursor:"pointer",
			textDecoration:"none",
			outline:"none",
			textAlign:"right",
			font:"11px arial"			
		}		
};
GameTreat.footerLinksMaxLength={
		//Launchers
		"190x190":17,
		"728x90":26,
		"160x600":23,
		"425x425":51,
		//Full Game View
		"800x770":51,
		"960x730":51,
		//Single Game Layout
		"652x596":51
};
GameTreat.footerLinks={
		1:'Games for websites',
		2:'Embed games',
		3:'Games for your website',
		4:'Free games for your website'
};

ObjectAddToZIndex={
		"lightsOutMask":2,
		"mainViewHover":3,
		"mainViewEmbed":3,
		"gameViewEmbed":3,
		"footerStripeStyle425x425":1,
		"footerStripeStyle300x250":1,
		"footerStripeStyle728x90":1,
		"footerStripeStyle160x600":1,
		"footerStripeStyle190x190":1		
};
	
	

GameTreat.getWidget = function(params) {		
	if (params.maxZIndex){
		for(var obj in ObjectAddToZIndex){
			GameTreat.styles[obj].zIndex=params.maxZIndex+ObjectAddToZIndex[obj];
		}
	}
	//allow overriding the widget container id (only for the main container)
	//&& params.embed
	if(params.widgetContainerId){
		GameTreat.Defines.widgetContainerId = params.widgetContainerId;
	}
	//create new object
	widget = new GameTreat.Widget(params);
	return widget;
};

function setWidgetInfoCallback(data){
	if (widget){
		widget.setWidgetInfo(data);
	}
}
		

// constructor

GameTreat.Widget = function(params) {
	// members
	this.m_throwErrors = false;
	this.m_isOpen = false;
	this.m_wid = params.wid;
	this.m_type = params.type;
	this.m_width = params.width;
	this.m_height = params.height;
	this.m_style = params.style;
	this.m_embed  = params.embed;
	this.m_textFooter = params.footerLinkText;
	this.m_params  = params;
		
	this.widgetInfo;
	var flagWidgetLoaded=false;
	
	function isValidObject(obj) {
		return ((typeof obj != "undefined" ) && (null != obj));
	}
	
	this.setWidgetInfo=function (widgetInfoData){
		this.widgetInfo=widgetInfoData;
		//if the widget Iframe was loaded and the response from widgetInfoAction was not returned yet. 
		//this will do the job
		if (flagWidgetLoaded && !Bitwise.isFlagSet(this.widgetInfo.options,Bitwise.NO_FOOTER_LINKS)){
			$(".gameTreatFooter").show();		
		}
	};
	
	this.init = function() {
		comMgr = new GameTreat.Widget.CommunicationManager();
		// determine what object to import
		switch (this.m_type)
		{
		case "launcher":
		case "LAUNCHER":
			launcher = new GameTreat.Widget.Launcher(this.m_params);
			break;
		case "mainview":
		case "FULL_SIZED_WIDGET":
			mainview = new GameTreat.Widget.MainView(this.m_params);
			break;
		case "gameview":
		case "SINGLE_GAME_PLAYER":
			gameview = new GameTreat.Widget.GameView(this.m_params);
			break;
		default:
			alert("The type "+this.m_type+" in the script is not legal");
		break;
		}
		if(!g_isJqueryEnable){
			this.handleImports();
		}
		//retrieve widgetInfo with JSONP
		this.getWidgetInfo();
	};

	// loop until jquery has finished loading and than procced with jqueryReady
	this.checkJquery = function() {
		try {
			$(window).load(function(){
				// jquery is ready
				clearInterval(widget.checkJqueryInterval);
				widget.jqueryReady();
			});
		} catch (e) {
		}
	}

	// ****======================= ACTIONS =======================****//
	// ===============================================================//

	// call actions dynamically
	this.doActions = function(params){
		// parse parameters to action and parameters arguments
		var action = params.action;
		if(action){
			var args = new Array();
			var argsCount = 0;
			for(each in params){
				if(each != "action"){
					args[each] = params[each];
					argsCount++;
				}
			}
			if(argsCount == 0){
				eval("this." + action)();
			}else{
				eval("this." + action)(args);
			}
		}
	}
	
	// create new widget ()
	this.createWidget = function(params){
		new GameTreat.getWidget(params);
	}

	// show widget layout
	this.displayWidget = function(params) {	
		if(params.status){
			//hide scroll bars in ie
			
			//Remove the comment will enable scroll
			//widget.displayWindowScrollBars(true);
			
			// fade in mask
			// Get the screen height and width
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
			// Set height and width to mask to fill up the whole screen
			$('#' + GameTreat.Defines.widgetMaskId).css({
				'width' : maskWidth,
				'height' : maskHeight
			});
			$('#' + GameTreat.Defines.widgetMaskId).fadeTo("fast", 0.8,function(){
			});
			// fade in main view
			if(!params.maskOnly){
				$("#" + params.objId).show();//(dont use fade beacuse of bug in ie8 iframe transparency)
			}
		}else{
			//show scroll bars in ie
			//widget.displayWindowScrollBars(false);
			
			// fade out mask
			$('#' + GameTreat.Defines.widgetMaskId).fadeTo("fast", 0, function(){
				$('#' + GameTreat.Defines.widgetMaskId).hide();
			});
			// fade out main view
			if(!params.maskOnly){
				$("#" + params.objId).hide();				
			}
		}
	};

	this.displayWindowScrollBars = function(show){
		var browserName = navigator.appName;
		if(browserName=="Microsoft Internet Explorer"){
			if(show){
				//store window overflow and scroll
				windowOriginalOverflow = document.body.style.overflow;
				windowOriginalScroll = document.body.scroll;
				windowOriginalHTML = document.getElementsByTagName("html")[0].style.overflow;//ie7
				//hide window scroll
				//document.body.style.overflow = "hidden";//ff
				document.body.scroll = "no";//ie
				document.getElementsByTagName("html")[0].style.overflow = "hidden";//ie7
			}else{
				//restore window overflow and scroll
				//document.body.style.overflow = windowOriginalOverflow;//ff
				document.getElementsByTagName("html")[0].style.overflow = windowOriginalHTML;//ie7
				document.body.scroll = windowOriginalScroll;//ie
			}
		}
	}
	
	this.WidgetReady = function(){	
		try{			
			if(widget){					
				clearInterval(widget.checkJqueryInterval);
				widget.jqueryReady();
				var params = {
						'action' : 'displayWidget',
						'maskOnly' : false,
						'status' : true,
						'objId': GameTreat.Defines.widgetMainViewId
				}
				
				//incase we are on hovering mainview display widget and mask once we are ready 
				if((!widget.m_params.embed && widget.m_params.type == "mainview") || (!widget.m_params.embed && widget.m_params.type == "FULL_SIZED_WIDGET")){
					widget.displayWidget(params);
				}
				//alert("iframeLoaded");
			}
		}catch(e){
			//alert(e);
		}
	}
	// ===============================================================//
	// ===============================================================//

	this.jqueryReady = function()
	{
		g_isJqueryEnable = true;
		//reveal all launchers and hide all loaders
		for (var i = 1; i <= g_launchersCounter; i++){
			$("#" + GameTreat.Defines.widgetLauncherId + i).show();			
			//$(".gameTreatFooter").show();
			$("#" + GameTreat.Defines.widgetLoadingId + i).hide();
		}
		$("#" + GameTreat.Defines.widgetGameViewId).show();
		$("#" + GameTreat.Defines.widgetMainViewId).show();		
				
		// setup mask and center window
		this.initWindow();
		
		if(this.widgetInfo!=null && this.widgetInfo.options!=null){	
			if (!Bitwise.isFlagSet(this.widgetInfo.options,Bitwise.NO_FOOTER_LINKS)){
				$(".gameTreatFooter").show();		
			}
		}
		flagWidgetLoaded=true;
	};
	
	//Check widget info with jsonp
	this.getWidgetInfo = function (){
			$.ajax({
				url:'http://widget.gametreat.com/do/widgetInfo?wId='+this.m_wid,
				timeout: 10000,
				dataType:"jsonp",
				jsonp: "callback",
     			jsonpCallback: "setWidgetInfoCallback"
			});
	};
		
	this.checkBrowserSupport = function () {

		var browserName=navigator.appName;
		var browserVer=parseInt(navigator.appVersion);
		browserSupported=true;
		if ((browserName=="Netscape" && browserVer>=5) || (browserName=="Microsoft Internet Explorer" && browserVer>=4)){
		  /*if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1){
			browserSupported=false;
		  }*/
		 }
		else{
		 //alert("You need an upgrade, I think.");
		 browserSupported=false;
		 }
		return browserSupported;
	}

	// import relevant js files and check when the document has finished
	// loading.
	this.handleImports = function() {
		this.checkBrowserSupport();
		if (!browserSupported) {
			for (var i = 1; i <= g_launchersCounter; i++){
						//$("#" + GameTreat.Defines.widgetLauncherId + i).show();
						$("#" + GameTreat.Defines.widgetLoadingId + i).html(function() {
							var html = '<div class="gameTreatNoBrowserSupport">Sorry!<br/>Your browser is not supported.<br/>Please try a higher version of Internet Explorer or Firefox.</div>';
  							return html;
							});
						$("#" + GameTreat.Defines.widgetLoadingId + i + " .loadingImg").hide();

					}
//			document.write('<link rel="stylesheet" type="text/css" href="' + GameTreat.Defines.siteUrl + '/static/css/GameTreatWidget.css" />');
		}else{
			if(!g_isJqueryEnable){
				if(this.m_embed){
					if (typeof jQuery == 'undefined'){
					  document.write('<scr' + 'ipt type="text/javascript" src="' + GameTreat.Defines.siteUrl + '/static/js/jquery.js"></scr' + 'ipt>');
					}
					document.write('<scr' + 'ipt type="text/javascript" src="' + GameTreat.Defines.siteUrl + '/static/js/jqueryUI.js"></scr' + 'ipt>');
					document.write('<scr' + 'ipt type="text/javascript" src="' + GameTreat.Defines.siteUrl + '/static/js/jquery.windowResizeFix.js"></scr' + 'ipt>');
//					document.write('<link rel="stylesheet" type="text/css" href="' + GameTreat.Defines.siteUrl + '/static/css/GameTreatWidget.css" />');
					if(this.m_type == "mainview"){
						document.write('<link rel="stylesheet" type="text/css" href="/static/css/GameTreatWidgetGameViewEmbed.css"/>');
					}
				}
				//if widget didn't load in after x seconds
				setTimeout(function(){
					if(!g_isJqueryEnable){
						//show buttons for all launchers
						for (var i = 1; i <= g_launchersCounter; i++){
							//$("#" + GameTreat.Defines.widgetLauncherId + i).show();
							//$("#" + GameTreat.Defines.widgetLoadingId + i + " .retryButton").show();
							if(document.getElementById("gameTreatRetryButton")){
								document.getElementById("gameTreatRetryButton").style.display = "block";
							}
							$("#" + GameTreat.Defines.widgetLoadingId + i + " .loadingImg").hide();
							$("#" + GameTreat.Defines.widgetLoadingId + i + " .retryButton").click(function(){widget.jqueryReady()});
						}
					}
				},20000);
				
				this.checkJqueryInterval = setInterval(this.checkJquery, 1000);
			}
		}
	};

	this.initWindow = function() {
		// Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		// Set height and width to mask to fill up the whole screen
		$('#' + GameTreat.Defines.widgetMaskId).css({
			'width' : maskWidth,
			'height' : maskHeight
		});
		// reposition overlay every time the window resizes
		$(window).resize(function() {
				widget.repositionWidget('#' + GameTreat.Defines.widgetMainViewId, false);
		});
		// reposition overlay every time the window is scrolled
		$(window).scroll(function(){
				widget.repositionWidget('#' + GameTreat.Defines.widgetMainViewId, false);
		});
		
				
	};

	this.repositionWidget = function(widgetId, bCenter) {
		if(!widget.m_embed){
			//var id = $("#" + GameTreat.Defines.widgetMainViewId );
			// Get the window height and width and scrolling value
			var winH = $(window).height();
			var winW = $(window).width();
			var scrollTop = $(window).scrollTop();//or use 0 to stick to top
			var scrollLeft = $(window).scrollLeft();

			if (!isValidObject(bCenter)) {
				bCenter = false;
			}
			
			if (bCenter) {
				$(widgetId).css('top', ((winH - this.m_height) / 2) + scrollTop);
			}
			
			// Set the popup window to center
			//$(widgetId).css('top', scrollTop);//stick to top
			$(widgetId).css('left', ((winW - this.m_width) / 2) + scrollLeft);
			
			if(GameTreat.Widget.checkDocType(this.m_params.noDocTypeSupport)){
				$('#' + GameTreat.Defines.widgetMaskId).css('top', scrollTop);
			}
		}
	}
	this.init();
};

GameTreat.Widget.getFooterTextLinks=function(width, height, key){
	var maxLength=GameTreat.footerLinksMaxLength[width+"x"+height];
	//if i arrive with a key text and this key is valid
	if (key>0 && key<5 && GameTreat.footerLinks[key].length<=maxLength){
		return GameTreat.footerLinks[key];
	}
	//return the defualt text
	else {
		if (GameTreat.footerLinks[1].length<=maxLength){
			return GameTreat.footerLinks[1];
		}
		else
		{	
			return GameTreat.footerLinks[2];
		}
	}	
};
GameTreat.Widget.isElementExist = function(elementId){
	if(document.getElementById(elementId)){
		return true;
	}else{
		return false;
	}
}
// widget end





// polls over the url and decide which action to take, than resets the url (but
// leaves # to prevent page refresh)
GameTreat.Widget.CommunicationManager = function() {
	// constructor
	this.m_originalUrl;
	this.m_lastUrl;
	this.m_urlListener;

	this.init = function(){				
		var url = window.location.protocol + "//" + window.location.hostname + window.location.pathname;
		//alert(url);

		this.setLastUrl(url);
		this.startUrlListener();
	}
	
	this.convertTitle = function (title){
		var titleText = (title.length>36)? unescape(title).substring(0,36)+"...":title;
		return unescape(titleText);	
	}
	
	this.startUrlListener = function(){
		this.m_urlListener = setInterval(this.checkUrlAction, 10);// create
																	// the url
																	// listener
	};
	this.stopUrlListener = function(){
		clearInterval(this.m_urlListener);
	};

	this.getLastUrl = function(){
		return this.m_lastUrl;
	};

	this.setLastUrl = function(url){
		this.m_lastUrl = url;
	};

	this.getOriginalUrl = function(){
		return this.m_originalUrl;
	};

	this.setOriginalUrl = function(url){
		this.m_originalUrl = url;
	};

	// converts url params into actions
	this.checkUrlAction = function(){
		
		var url = window.location.href;
		if (url.indexOf(GameTreat.Defines.urlParamsStartToken) != -1){

			try{
				var params = comMgr.convertUrlToParams(url);
				// check if url has changed reset url
				if(url != comMgr.getLastUrl() && widget){					
					widget.doActions(params);
					comMgr.resetUrl(url);
					comMgr.setLastUrl(url);
				}
			}catch(e){}

		}

	};

	this.addParamsToObject = function(targetObj,additionalParams){
		for(each in additionalParams){
			var name = each;
			var value = additionalParams[each].toString();
			targetObj[name] = value;
		}
		return targetObj;
	};
	this.escapeObject = function(obj){
		for(each in obj){
			obj[each] = escape(obj[each]);
		}
	};
	this.unescapeObject = function(obj){
		for(each in obj){
			obj[each] = unescape(obj[each]);
		}
	};

	this.startLoadingBox = function (){
		$.blockUI.defaults.message = "<img src='/static/img/loading.gif'/>";
	    $.blockUI({ css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#fff',
            background:'none'
        } });
	};

	this.stopLoadingBox = function(){
		$.unblockUI();
	};

	//this method should be called in each launcher/mainview/gameview onload event 
	//in order to display the widget when it's ready and not wait for page
	this.iframeLoaded = function(){
		try{		
			//activate widget ready -  as long as we aren't in game view not embeded (not inside the main view);			
			if(!(mainParams.type == "gameview" && !mainParams.embed) && !(mainParams.type == "SINGLE_GAME_PLAYER"&& !mainParams.embed)){				
				var params = {
					'action' : 'WidgetReady'
				};
				comMgr.sendEventToParent(mainParams.parentUrl, params);
				if(mainParams.type == "mainview" || mainParams.type == "FULL_SIZED_WIDGET" || (mainParams.type == "mainview" || mainParams.type == "FULL_SIZED_WIDGET")){
					
					//since the iframe is ready - we can now load the ad,
					//the ad loading is async because of the geo code retrival 					
					comMgr.getCountryCode(comMgr.displayAdContent);
					
					//we display the ad any way after 5 seconds as a fail safe mechanism
					setTimeout(function(){comMgr.displayAdContent();},5000);
				}			
			}			
		}catch(e){
			//alert(e);
		}		
	}
	
	
	//retrives country code from geo location, then:
	//either return the country or calls the onComplete method with the countryCode parameter - onComplete(countryCode)
	this.getCountryCode = function(onComplete){		
		//get country code
		var countryCode = "";		
		try{
			$.ajax({
				   url: "/do/getGeoCode",
				   dataType: 'json',				   
				   success: function(json){
					  if(json != ""){
						  json = $.parseJSON(json);
						  if(null != json.countryCode && "null" != json.countryCode && "undefined" != json.countryCode){
							  countryCode = json.countryCode;							  
						  }
					  }
					  return (onComplete)? onComplete(countryCode): countryCode;
				   },
				   error:function(xhr, textStatus, errorThrown){
					  return (onComplete)? onComplete(countryCode): countryCode;
				   }
			});
		}catch(e){
			   return (onComplete)? onComplete(countryCode): countryCode;
		}		
	}
	
	//loads the ad content into the iframe and displays it
	this.displayAdContent = function(countryCode){		
		var adsFrame = document.getElementById('adsIframe');

		//we check the ad's display to prevent duplicate initial loading
		if(adsFrame.style.display == "none"){
			//display ad iframe content
			if(adsFrame){
				var url = "/do/showAd/top728x90";
				//add params to url
				if(mainParams && mainParams.wid){
					url += "?wid=" + mainParams.wid;								
					url += (countryCode)? ("&countryCode=" + countryCode) : ("&countryCode=default");				
				}
				adsFrame.src = url;
				adsFrame.style.display = "block";
			}
		}
	}

	// converts only the url query added by the widget (parameters the appear after our start token)
	// and returns an object with them (each paramValue can be accessed by using paramsObject.paramName)
	this.convertUrlToParams = function(url){
		//take relevant part of string
		var startString = GameTreat.Defines.urlParamsStartToken;
		//var parentUrlToken = "parentUrl";
		var urlQuery = url.substring(url.indexOf(startString)+startString.length,url.length);

		var params = urlQuery.split("$");
		// split to params
		var paramsObject = new Object();
		//contain temp parent url to prevent parsing errors
		
		var nSize = params.length;
		var tempParentUrl = null;
		
		for (i = 0; i < nSize; i++) {
			var temp = params[i].split("=");
			var name = temp[0];
			var value = temp[1];
			
			if (name.indexOf("parentUrl") != -1){
				tempParentUrl = params[i].replace("parentUrl=","");	
			} else {
				// convert strings to booleans
				if(value == "true") {
					value = true;
				}
				
				if(value == "false") {
					value = false;
				}
				
				paramsObject[name] = value;
			}
		}

		//reinsert parent url
		paramsObject.parentUrl = tempParentUrl;
		return paramsObject;
	}
	// converts the objects parameters to url and returns it as a string.
	// the url returned will be only the parameters without '?' at the beginning)
	this.convertParamsToUrl = function(params){
		//add the default token ("#GameTreatWidget?") at the beginning of the url
		var urlQuery = GameTreat.Defines.urlParamsStartToken;
		for (each in params) {
			var name = each;
			var value = params[each].toString();
			urlQuery += name + "=" + value;
			urlQuery += "$";
		}
		// remove extra & at the end of the url
		if(urlQuery.substring(urlQuery.length-1) == "$"){
			urlQuery = urlQuery.substring(0,urlQuery.length-1);
		}
		return urlQuery;
	};

	// removes any parameters after the start token and changes the url so it would be originalUrlAdress+#
	this.resetUrl = function(url) {
		if (url.indexOf(GameTreat.Defines.urlParamsStartToken) != -1) {
			var newUrl = url.substring(0, url.indexOf(GameTreat.Defines.urlParamsStartToken) + 1);
			// "_" is added to prevent the page to jump to top
			newUrl = newUrl.replace(/#_#/g,"#");
			window.location.replace(newUrl += "_");			
		}		
	};

	// pass a variable to the url of the widgetContainer iframe which is used to
	// notify the iframe on actions
	this.sendEventToChild = function(params) {
		if(isSameDomain){
		}else{
			var elem = $('#' + GameTreat.Defines.widgetMainViewId );
			var paramsString = this.convertParamsToUrl(params);
			var newUrl = GameTreat.Defines.widgetMainViewUrl  + "#?"	+ paramsString;
			elem.attr("src",newUrl);// change iframe src
		}
	};

	//send events from child iframe to parent by change the top url
	this.sendEventToParent = function(parentUrl,params,immidateParent){	
			try{
				var url = parentUrl;
				var startString = GameTreat.Defines.urlParamsStartToken;
				if (url.indexOf("#") == (url.length-1)){
					url = url.substring(0,url.length-1);
				}
				if(url.indexOf(startString) != -1){
					url = url.substring(0,url.indexOf(startString));
				}
				url += this.convertParamsToUrl(params);
					
				
				if(immidateParent){
					parent.location.replace(url);
				}else{
					top.location.replace(url);
				}
			}catch(e){}
	};

	//create the progress window and add content to it
	this.createProgressInfoWindow = function(windowContent){
		var windowName = "gameTreat_progressWindow";

		//if window exist than remove it
		if($("#" + windowName).length != 0){
			$("#" + windowName).remove();
		}

		//create a new window
		$("<div>", {
			//properties
			"id": windowName,
			"class":"progressInfoWindow"
		}).appendTo($("body"));
		
		$("<div>", {
			"class":"msgWindowTop"
		  }).appendTo($("#"+windowName));
		
		$("<div>", {
			"class":"msgWindowMiddle"
		  }).appendTo($("#"+windowName));
		
		$("<div>", {
			"class":"msgWindowBottom"
		  }).appendTo($("#"+windowName));

		//update window content
		//$("#" + windowName).html(windowContent);
		$("#"+windowName+" .msgWindowMiddle").html(windowContent);		
	};

	this.removeProgressInfoWindow = function(){
		var windowName = "gameTreat_progressWindow";
		$("#" + windowName).remove();
	};
	
	//create the basic message window and add content to it
	this.createMsgWindow = function(name,windowContent){
		if ($(".msgWindow").length>0) {
				$(".msgWindow").remove();
		}
		var windowName = (name)? "gameTreat_" + name : "gameTreat_msgWindow";
		
		//if object doesn't exist than create it
		if($("#" + windowName).length == 0){
			$("<div>", {
			  //properties
			  "id": windowName,
			  "class":"msgWindow"
			}).appendTo($("body"));
				
		$("<div>", {
			"class":"msgWindowTop"
		  }).appendTo($("#"+windowName));
		
		$("<div>", {
			"class":"msgWindowMiddle"
		  }).appendTo($("#"+windowName));
		
		$("<div>", {
			"class":"msgWindowBottom"
		  }).appendTo($("#"+windowName));
		
		//update window content
		$("#"+windowName+" .msgWindowMiddle").html(windowContent);		
		}
		
		$("#" + windowName + " .closeButton").click(function(){
			$("#" + windowName).remove();
			g_suspendExitPostRoll=false;
		});
		
		$("#" + windowName + " .closeWindowButton").click(function(){
			$("#" + windowName).remove();
			g_suspendExitPostRoll=false;
		});
		
		$("#" + windowName).show();
	};

	//create the large message window and add content to it
	this.createLargeMsgWindow = function(name,windowContent){
		var windowName = (name)? "gameTreat_" + name : "gameTreat_msgWindow";
		//if object doesn't exist than create it
		if($("#" + windowName).length == 0){
			$("<div>", {
			  //properties
			  "id": windowName,
			  "class":"largeMSGWindowContainer"
			}).appendTo($("body"));
		}
		//update window content
		$("#" + windowName).html(windowContent);
		$("#" + windowName + " .closeButton").click(function(){
			$("#" + windowName).remove();
		});
		$("#" + windowName + " .closeWindowButton").click(function(){
			$("#" + windowName).remove();
		});
	};

	//create the large message window and add content to it
	this.createLeaderBoardMsgWindow = function(name,windowContent){
		var windowName = (name)? "gameTreat_" + name : "gameTreat_msgWindow";
		//if object doesn't exist than create it
		if($("#" + windowName).length == 0){
			$("<div>", {
			  //properties
			  "id": windowName,
			  "class":"leaderBoardMsgWindowContainer"
			}).appendTo($("body"));
		}

		//update window content
		$("#" + windowName).html(windowContent);
		$("#" + windowName + " .closeButton").click(function(){
			$("#" + windowName).remove();
		});
		$("#" + windowName + " .closeWindowButton").click(function(){
			$("#" + windowName).remove();
		});
	};

	//remove msg window
	this.removeMsgWindow = function(name){
		var windowName = "gameTreat_" + name;
		$("#" + windowName).remove();
	}
	//creates the html content for the error message window
	this.getErrorMsgContent = function(description,hideCloseButton,retryButton){
		//var errorDescription = 'An error occured while trying to load<br/>your game. Please try again or<br/>Click here for more information.';
		var closeButtonString = (hideCloseButton)? '':'<div class="closeButton"></div>';
		var retryButtonDiv='';		
		if (retryButton){ 
			var retryButtonDiv = '<div id="PopUpErrorRetryButton" class="GameloaderRetryButton"></div>';
		}
		htmlString = closeButtonString +
					'<div class="errorIconContainer"><img src="/static/img/errorIcon.png"></div>' +
					'<div class="container containerError">' +
						'<div class="field">' +
							'<div class="text wideDescription">' + description + retryButtonDiv +'</div>' +
						'</div>' +
					'</div>';
		
		return htmlString;
	};
	
	//create footer
	this.getFooterContent = function(showLogo,showLink1,showLink2){
		
		//don't use relative paths!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		var footerStripeStyle='style="width:100%;height:25px;font-family:arial;font-size:12px;" ';
		var linkStyle =	'style="width:auto;margin-top:3px;margin-right:5px;height:15px;float:right;color:#4a4a4a;cursor:pointer;text-decoration:none;outline:none;" ';
		var linkHover = 'onmouseover="this.style.color=\'#fab400\'" onmouseout="this.style.color=\'#4a4a4a\'"';
		var logoStyle = 'style="width:90px;height:22px;background:url(' + GameTreat.Defines.footerLinksSiteUrl + '/static/img/logo.png) 0px 0px no-repeat;float:left;margin-left:2px;outline:none;border:none;" ';
		var seperatorStyle = 'style="width:1px;margin:5px 5px 0px 5px;height:12px;float:right;background-color:#9a9a9a;" ';
		var html =  '<div class="footerStripe" ' + footerStripeStyle + '>'+
							((showLogo)? '<a href="' +  GameTreat.Defines.footerLinksSiteUrl + '/publishers/built-with-gametanium?wid='+params.wid+'" title="Built with GameTanium" target="_blank" ' + logoStyle + '></a>' : '') +
							((showLink1)? '<a class="footerLink" href="' +  GameTreat.Defines.footerLinksSiteUrl + '?wid='+params.wid+'" title="Embed games on your website!" target="_blank" ' + linkStyle + linkHover +' >'+GameTreat.Widget.getFooterTextLinks(params.width,params.height,params.fl)+'</a>': '')+
							((showLink1 && showLink2)? '<div class="seperator" ' + seperatorStyle + '></div>' : '')+
							((showLink2) ?  '<a class="footerLink" href="' +  GameTreat.Defines.footerLinksSiteUrl + '/publishers/user-legal-information?wid='+params.wid+'"  target="_blank" ' + linkStyle + linkHover + '>Terms of Use</a>' : '')+
					'</div>';
		return html;
	};
		
	this.getFooterContentForLauncher = function(params){
		var linkColor = params.style=="basic" ? "white":"black";
		var linkHover = 'onmouseover="this.style.color=\'#fab400\'" onmouseout="this.style.color=\''+getMouseOutLinkColor(params)+'\'"';
		
		var html =  '<div id="footerStripe">'+
							'<a id="logo" href="' + GameTreat.Defines.footerLinksSiteUrl + '/about/built-with-gametanium?wid='+params.wid+'" title="Built with GameTanium" target="_blank"></a>'+
							'<a id="footerLink" href="' +  GameTreat.Defines.footerLinksSiteUrl + '?wid='+params.wid+'" title="Embed games on your website!" target="_blank"' + linkHover + 'style=color:'+linkColor+'; >'+GameTreat.Widget.getFooterTextLinks(params.width,params.height,params.fl)+'</a>'+
					'</div>';
		return html;
	};

	this.init();
};
// end CommunicationManager

function getMouseOutLinkColor(params){
	if (GameTreat.styles['footerLink'+params.width+'x'+params.height].color){
		return GameTreat.styles['footerLink'+params.width+'x'+params.height].color;
	}
	else if (params.style=='basic'||params.style=='') {
		return '#FFFFFF';
	}else if (params.style=='white'){
		return '#000000';
	}
}

//apply styles hardcoded
GameTreat.Widget.applyElementsSize = function(width,height,elementsArr){
			for (var i = 0; i < elementsArr.length;i++){
				var elementObj = document.getElementById(elementsArr[i]);
				if(elementObj){
					elementObj.style.width = width + 'px';
					elementObj.style.height = height + 'px';
				}
			}
};

GameTreat.Widget.applyStyle = function(elementId,styleDataName){
	
	var elementObj = document.getElementById(elementId);
	if(elementObj){
		$.each(GameTreat.styles[styleDataName], function(key, value){
			$('#'+elementId).css(key,value);
		});
	}
};

//create launcher
GameTreat.Widget.Launcher = function(params) {
			
	params.parentUrl = window.location.href;
	this.m_urlParams = comMgr.convertParamsToUrl(params);
	
	//add parameters to url
	GameTreat.Defines.widgetLauncherUrl = GameTreat.Defines.widgetLauncherUrl.replace("%width%",params.width).replace("%height%",params.height).replace("%wid%",params.wid);

	g_launchersCounter++;
	this.init = function() {
		
			footerHTML = '<div class="gameTreatFooter" style="width:' + params.width + 'px;top:-0px;display:none;z-index:8999;">' +  comMgr.getFooterContentForLauncher(params) + '</div><div id="mainViewIframeContainer"></div>';
		
			// create new launcherview
			var myWidget = document.getElementById(GameTreat.Defines.widgetContainerId);
			myWidget.innerHTML  +='<div id="' + GameTreat.Defines.widgetLoadingId + g_launchersCounter + '" class="widgetLoading"><img id="gameTreatLoadingImage" class="loadingImg" src="' + GameTreat.Defines.siteUrl + '/static/img/loading.gif" /><a id="gameTreatRetryButton" class="retryButton"></a></div>'
								+ '<iframe id="' + GameTreat.Defines.widgetLauncherId + g_launchersCounter + '" width="'
								+ params.width
								+ '" height="'
								+ params.height
								+ '" frameborder="0" src="' + GameTreat.Defines.widgetLauncherUrl + this.m_urlParams + '"class="launcherContainerIframe" scrolling="no" allowtransparency="true" style="display:none;"></iframe>' + footerHTML;
			//apply styles in embed mode
			var elementsArr = [GameTreat.Defines.widgetContainerId,GameTreat.Defines.widgetLoadingId + g_launchersCounter,GameTreat.Defines.widgetLauncherId + g_launchersCounter];
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetLoadingId + g_launchersCounter,"loader");
			GameTreat.Widget.applyStyle("gameTreatLoadingImage","loaderImg");
			GameTreat.Widget.applyStyle("gameTreatRetryButton","loaderRetryButton");
			
			//Apply Footer Style
			GameTreat.Widget.applyStyle("footerStripe","footerStripeStyle"+params.width+'x'+params.height);
			GameTreat.Widget.applyStyle("logo","footerLogo"+params.width+'x'+params.height);
			GameTreat.Widget.applyStyle("footerLink","footerLink"+params.width+'x'+params.height);
						
			GameTreat.Widget.applyElementsSize(params.width,params.height,elementsArr);
	};

	this.init();
	
};

//create main view iframe
GameTreat.Widget.MainView = function(params) {
	params.parentUrl = window.location.href;
	if(screen.height>=768){
		params.displayTopAd = true;
	}else{
		params.displayTopAd = false;
	}

	g_launchersCounter++;
	this.init = function() {
			//support mask in case doctype is missing
			var myWidget = document.getElementById(GameTreat.Defines.widgetContainerId);
			//remove previous create mainview
			var mainviewIframe = document.getElementById(GameTreat.Defines.mainViewContainerId);
			if(mainviewIframe){
				mainviewIframe.parentNode.removeChild(mainviewIframe);
			}
			var dummyContainer = document.getElementById("dummyContainer");
			if(dummyContainer){
				dummyContainer.parentNode.removeChild(dummyContainer);
			}
			
			if(params.embed){

				//main view is embed
				var iframeClass  = "mainViewContainerIframe mainViewContainerEmbed";

				var maskHTML = (GameTreat.Widget.checkDocType(params.noDocTypeSupport))? "":'<div id="' + GameTreat.Defines.widgetMaskId + '" class="widgetMask"></div>';
				var footerHTML = '<div class="gameTreatFooter" style="width:' + params.width + 'px;top:0px;height:25px;position:relative;z-index:9001;margin:0px auto;display:none;">' + comMgr.getFooterContent(true,true,true) + '</div>';
				var preloaderHTML = '<div id="' + GameTreat.Defines.widgetLoadingId + g_launchersCounter + '" class="widgetLoading"><img id="gameTreatLoadingImage" class="loadingImg" src="' + GameTreat.Defines.siteUrl + '/static/img/loading.gif" /><a id="gameTreatRetryButton" class="retryButton"></a></div>';

				var element = document.getElementById(GameTreat.Defines.widgetContainerId);
				//add parameters to url
				GameTreat.Defines.widgetMainViewUrl = GameTreat.Defines.widgetMainViewUrl.replace("%width%",params.width).replace("%height%",params.height).replace("%wid%",params.wid);
				//insert to existing mainview container
				element.innerHTML = preloaderHTML + '<iframe id="' + GameTreat.Defines.widgetMainViewId + '" class="' + iframeClass + '" src="' +  GameTreat.Defines.widgetMainViewUrl +  comMgr.convertParamsToUrl(params) + '" width="' + params.width  + '" height="' + 	params.height + '" frameborder="0"  onload=widget.jqueryReady(); scrolling="no" allowtransparency="true"></iframe>' + maskHTML + footerHTML;
				var elementsArr = [GameTreat.Defines.widgetLoadingId + g_launchersCounter,GameTreat.Defines.widgetMainViewId,GameTreat.Defines.widgetContainerId];
				GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMainViewId,"mainViewEmbed");
			}else{
				//main view is hovering
				var iframeClass  = "mainViewContainerIframe";
				var maskHTML = '<div id="' + GameTreat.Defines.widgetMaskId + '" class="widgetMask"></div>';
				var footerHTML = '';
				//var element = document.getElementById('mainViewIframeContainer');
				//add parameters to url
				GameTreat.Defines.widgetMainViewUrl = GameTreat.Defines.widgetMainViewUrl.replace("%width%",params.width).replace("%height%",params.height).replace("%wid%",params.wid);
				//this div is created to prevent html flickering when using innerHTML
				newChild = document.createElement("div");
				newChild.id = "dummyContainer";
				document.body.appendChild(newChild);
				//insert to existing mainview container
				newChild.innerHTML += '<iframe id="' + GameTreat.Defines.widgetMainViewId + '" class="' + iframeClass + '" src="' +  GameTreat.Defines.widgetMainViewUrl +  comMgr.convertParamsToUrl(params) + '" width="' + 	params.width  + '" height="' + 	params.height + '" frameborder="0"  scrolling="no" allowtransparency="true" style="display:none;"></iframe>' + maskHTML + footerHTML;
				//center iframe on creation (only for hovering)
				setTimeout(function(){widget.repositionWidget('#' + GameTreat.Defines.widgetMainViewId, true)},100);
				var elementsArr = [GameTreat.Defines.widgetMainViewId];
				//default main view iframe class
				GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMainViewId,"mainViewHover");
			}
			//common styles for main view
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMaskId,"lightsOutMask");

			if(GameTreat.Widget.checkDocType(params.noDocTypeSupport)){
				GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMaskId,"lightsOutMaskNoDocType");//handle no doc type mode
			}
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetLoadingId + g_launchersCounter,"loader");
			GameTreat.Widget.applyStyle("gameTreatLoadingImage","loaderImg");
			GameTreat.Widget.applyStyle("gameTreatRetryButton","loaderRetryButton");
			GameTreat.Widget.applyElementsSize(params.width,params.height,elementsArr);
			if(params.embed && document.getElementById(GameTreat.Defines.widgetMainViewId).style.height == "730px"){
				document.getElementById(GameTreat.Defines.widgetMainViewId).style.height = "700px";
			}else if (params.embed && document.getElementById(GameTreat.Defines.widgetMainViewId).style.height == "770px"){
				document.getElementById(GameTreat.Defines.widgetMainViewId).style.height = "750px";
			}
	}
	this.init();
}

GameTreat.Widget.checkDocType = function(doctype){
	if(doctype && (doctype != "undefined")  && (doctype+"" != "false")){
		return true;
	}
	return false;
}

//create game view iframe
GameTreat.Widget.GameView = function(params) {
	var dummyContainer = document.getElementById("dummyContainer");
	if(dummyContainer){
		dummyContainer.parentNode.removeChild(dummyContainer);
	}

	g_launchersCounter++;
	this.init = function() {
		if(params.embed){
			params.parentUrl = window.location.href;
			if(GameTreat.Widget.checkDocType(params.noDocTypeSupport)){
				params.noDocTypeSupport=true;
			}
			//game view is embed - display it
			var iframeClass  = "gameViewContainerIframe";
			var maskHTML = (GameTreat.Widget.checkDocType(params.noDocTypeSupport))? "":'<div id="' + GameTreat.Defines.widgetMaskId + '" class="widgetMask"></div>';
			var preloaderHTML = '<div id="' + GameTreat.Defines.widgetLoadingId + g_launchersCounter + '" class="widgetLoading"><img id="gameTreatLoadingImage" class="loadingImg" src="' + GameTreat.Defines.siteUrl + '/static/img/loading.gif" /><a id="gameTreatRetryButton" class="retryButton"></a></div>';
			
			var footerHTML ='<div class="gameTreatFooter" style="width:' + params.width + 'px;top:0px;height:25px;position:relative;z-index:9001;margin:0px auto;display:none;">' + comMgr.getFooterContent(true,true,true) + '</div>';
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMaskId,"lightsOutMask");
		}else{
			//game view is child of gameview - hide it
			params.parentUrl = "";
			var iframeClass  = "gameViewContainer";
			var maskHTML = '';
			var footerHTML = '';
		}

		//add parameters to url
		GameTreat.Defines.widgetGameViewUrl = GameTreat.Defines.widgetGameViewUrl.replace("%width%",params.width).replace("%height%",params.height).replace("%wid%",params.wid).replace("%GOTD%",params.GOTD);
		GameTreat.Defines.widgetGameViewUrl = GameTreat.Defines.widgetGameViewUrl.replace("&GOTD=undefined","");
		GameTreat.Defines.widgetGameViewUrl += "&embed=" + params.embed;

		// if doesn't exist already - create new game view other wise show the existing game view
		if(!GameTreat.Widget.isElementExist(GameTreat.Defines.widgetGameViewId)){
			var myWidget = document.getElementById(GameTreat.Defines.widgetContainerId);
			if(params.embed){
				myWidget.innerHTML +=  preloaderHTML +  '<iframe id="' + GameTreat.Defines.widgetGameViewId  + '" class="' + iframeClass + '" src="about:blank" width="' + params.width  + '" height="' + params.height + '" frameborder="0"  scrolling="no" allowtransparency="true" style="display:none;"></iframe>' + footerHTML;
			}else{
				myWidget.innerHTML += '<iframe id="' + GameTreat.Defines.widgetGameViewId  + '" class="' + iframeClass + '" src="about:blank" width="' + params.width  + '" height="' + params.height + '" frameborder="0"  scrolling="no" allowtransparency="true" onload="widget.jqueryReady();"></iframe>' + footerHTML;
			}
			var iframeId = GameTreat.Defines.widgetGameViewId;
			document.getElementById(iframeId).src = GameTreat.Defines.widgetGameViewUrl + comMgr.convertParamsToUrl(params);
		}
		//add mask in embed mode at end of body
		if(params.embed){
			document.getElementById(GameTreat.Defines.widgetContainerId).innerHTML += maskHTML;
			//apply styles in embed mode
			var elementsArr = [GameTreat.Defines.widgetGameViewId,GameTreat.Defines.widgetContainerId,GameTreat.Defines.widgetLoadingId + g_launchersCounter];
			GameTreat.Widget.applyElementsSize(params.width,params.height,elementsArr);
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetGameViewId,"gameViewEmbed");
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMaskId,"lightsOutMask");

			if(GameTreat.Widget.checkDocType(params.noDocTypeSupport)){
				GameTreat.Widget.applyStyle(GameTreat.Defines.widgetMaskId,"lightsOutMaskNoDocType");//handle no doc type mode
			}
			GameTreat.Widget.applyStyle(GameTreat.Defines.widgetLoadingId + g_launchersCounter,"loader");
			GameTreat.Widget.applyStyle("gameTreatLoadingImage","loaderImg");
			GameTreat.Widget.applyStyle("gameTreatRetryButton","loaderRetryButton");
		}
	};
	
	this.init();
};

var Bitwise = new Object({
	
	NO_FOOTER_LINKS:4,	
	
	getDefaultFlagValue:function(){
		return 0;
	},
	/**
	 * checks a flags value in the options
	 * @param options the options to check from which to check the flag
	 * @param flag the flag to check inside the options
	 * @returns {Boolean} value of flag
	 */
	isFlagSet : function(options, flag) {
		return ((options & flag) == flag);
	},
	/**
	 * converts regular numbers to bitwise values (2^0,2^1,2^2,2^3...)
	 * @param n the number of flag you want
	 * @returns the bitwise value that will be required for bitwise operations
	 */
	getFlagValue : function (n) {
		return (Math.pow(2, n));
	}	
});	

// Error Handling ==========================================================
GameTreat.Widget.prototype.errorsList = {
	100 : "Widget: Please enter a valid widget id",
	101 : "Widget: Please enter a valid type( launcher / mainview / gameview )",
	102 : "Widget: Please enter a valid resolution",
	200 : "launcher: Please enter a valid resolution",
	300 : "main view: Please enter a valid resolution",
	400 : "game view: Please enter a valid resolution"
}

GameTreat.Widget.prototype.getError = function(errorNumber) {
	return this.errorsList[errorNumber];
}

GameTreat.Widget.prototype.error = function(errorNumber) {
	var errorString = "ERROR( " + errorNumber + " )::: " + this.getError(errorNumber);
	if (this.m_throwErrors) {
		alert(errorString);
		throw (errorString);
	}
	return;
}

