var filmquiz = {

		VERSION: 1,
		NAME: "filmquiz",
		cloudBasePath: "http://npcloud.tdc.dk/filmquiz/",
		trackingParam: "vod-filmquiz",
		divFlashId: "",
		divFlashContainerId: "",
		flashId: "",
		flashWidth: "900px",
		flashHeight: "600px",
		
		jQueryURL: "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",
		swfObjectURL: "http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js",
		startOverlay: false,
		parameters: {},
		
		init: function(){
			this.runOverlay();
		},
		runOverlay: function(force){
			//check for parameter in url:
			this.parseParameters();
			this.startOverlay = this.parameters["start_overlay"] == "true";
			if (this.startOverlay || force) this.checkjQuery();
		},
		parseParameters: function(){
			var query = window.location.href;
			this.parameters = new Object ();
			if ( ! query ) return;
			if (query.indexOf("?") == -1) return;
			query = query.split("?").pop();
			var pairs = query.split(/[;&]/);
			for ( var i = 0; i < pairs.length; i++ ) {
				var keyVal = pairs[i].split('=');
				if ( ! keyVal || keyVal.length != 2 ) continue;
				var key = unescape( keyVal[0] );
				var val = unescape( keyVal[1] );
				val = val.replace(/\+/g, ' ');
				this.parameters[key] = val;
			}
		},

		checkjQuery: function(){
			if (typeof(jQuery) == "function" && typeof(jQuery.getScript) == "function") {
				this.checkSwfObject();
			} else {
				this.getjQuery();
			}
		},
		getjQuery: function(){
			var jquery = document.createElement('script');
			jquery.setAttribute('id', 'jquery_script');
			jquery.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js');
			document.getElementsByTagName('head')[0].appendChild(jquery);
			var scope = this;
			(function(){
				if(typeof(window.jQuery) == "function"){
					scope.jquery_added = true;
					scope.checkSwfObject();
				} else {
					setTimeout(arguments.callee);
				}
			})();
		},
		checkSwfObject: function(){
			if (typeof(swfobject) == "function") {
				this.createOverlay();
			} else {
				this.loadSwfObject();
			}
		},
		loadSwfObject: function(){
			var scope = this;
			jQuery(document).ready(function(){
				jQuery.getScript(scope.swfObjectURL, function(){
					scope.createOverlay();
				});
			});
		},
		createOverlay: function(){
			jQuery('body').append('<div id="'+this.NAME+'_overlay" />');
			jQuery('#'+this.NAME+'_overlay').append('<div id="'+this.NAME+'_overlay_background" />');
			jQuery('#'+this.NAME+'_overlay').append('<div id="'+this.NAME+'_overlay_flashcontainer" />');
			jQuery('#'+this.NAME+'_overlay_flashcontainer').append('<div id="'+this.NAME+'_overlay_flashcontainer_flashelement" />');

			jQuery('#'+this.NAME+'_overlay')
				.css({
					display: 'none'
				});
			
			jQuery('#'+this.NAME+'_overlay_flashcontainer')
				.css({
					position: 'absolute',
					top:0,
					left: 0,
					width: this.flashWidth,
					height: this.flashHeight,
					overflow : 'hidden',
			   		zIndex: 1001
				});
			
			jQuery('#'+this.NAME+'_overlay_background')
				.css({
					display: 'none',
					position: 'absolute',
					top:0,
					left: 0,
					width: '100%',
					height: jQuery(document).height() + "px",
			   		zIndex: 1000,
					overflow : 'hidden',
					background: 'white'
			})
			
			this.tweenInOverlay();
		},
		tweenInOverlay: function(){
			var scope = this;
			jQuery('#'+this.NAME+'_overlay')
			.css({
				display: 'block'		
			});
			jQuery('#'+this.NAME+'_overlay_background').fadeIn(1000,function(){
				scope.onTweenInOverlay();
			});
		},
		onTweenInOverlay: function(){
			this.startFlash();
		},
		startFlash: function(){
		
			this.hideIframes()
			
			var flashvars = {
				"cloudBasePath": this.cloudBasePath,
				"confirmPage": (this.parameters["confirmPage"] == "true"),
				"trackingParam": this.trackingParam
			};

			var params = {};
			params.allowscriptaccess = "always";
			params.allowfullscreen = "true";
//			params.wmode = "transparent";
			var attributes = {};
			
			this.flashId = "flash_filmquiz";
			attributes.id = this.flashId;
			attributes.name = attributes.id;
			
			swfobject.embedSWF(
					this.cloudBasePath+"main.swf?v="+this.VERSION, 
					this.NAME+'_overlay_flashcontainer_flashelement', 
					this.flashWidth, 
					this.flashHeight, 
					"10.0.0", 
					this.cloudBasePath+"swfobject/expressInstall.swf", 
					flashvars, 
					params, 
					attributes
			)
		},
		hideIframes: function(){
			jQuery("iframe").css({"display": "none"});
		},
		showIframes: function(){
			jQuery("iframe").css({"display": "block"});
		},
		closeOverlay: function(){
			//immediately removing SWF, crashes browser
			var scope = this;
			setTimeout(function(){swfobject.removeSWF(scope.flashId);}, 100)
			
			this.tweenOutOverlay();
		},
		tweenOutOverlay: function(){
			this.showIframes();
			var scope = this;
			jQuery('#'+this.NAME+'_overlay_background').fadeOut(1000,function(){
				scope.onTweenOutOverlay();
			});
		},
		onTweenOutOverlay: function(){
			jQuery('#'+this.NAME+'_overlay')
			.css({
				display: 'block'		
			});
		
			jQuery('#'+this.NAME+'_overlay_flashcontainer_flashelement').remove();
			jQuery('#'+this.NAME+'_overlay_flashcontainer').remove();
			jQuery('#'+this.NAME+'_overlay_background').remove();
			jQuery('#'+this.NAME+'_overlay').remove();
		
			filmquiz = null;
		},
		toString: function(){
			return "FilmQuiz";
		},
		log: function(text){
			if (typeof(console) == "object" && typeof(console.log) == "function") {
				console.log(text);
			}
		}
}
filmquiz.init();