var Background = Class.create({
	initialize: function() {
		if(window.flbg != undefined){
			this.createBackground();
			this.mouseEvent();
		}
	},
	
	createBackground: function() {
		if($('top') == null)
			$$('body')[0].writeAttribute("id","top");

		var ie = parseFloat(navigator.appVersion.split('MSIE')[1]);
		var dimensions = $(window.flbg.pageWrap).getDimensions();
		var fullHeight = dimensions.height;
		
		if (Number(ie)){
			var browserHeight = document.body.clientHeight;
		}else{
			var browserHeight = window.innerHeight;
		}
	
		if (browserHeight > fullHeight) {
			fullHeight = "100%";
			$('top').style.height = "100%";
		}else {
			if (Number(ie)) {
				fullHeight = fullHeight + 15 + "px";
			}else{
				fullHeight = fullHeight + "px";
			}
		}
					
		var flashvars = {};
			flashvars.cpWhere = window.flbg.cpWhere;
			flashvars.cpMade = window.flbg.cpMade;
			flashvars.cpXMLPath = window.flbg.cpXMLPath;
			flashvars.soundCat = window.flbg.soundCat;
			flashvars.soundPath = window.flbg.soundPath;
			//flashvars.cpComponents = Object.toJSON(window.flbg.cpComponents);

		var params = {};
			params.base = "typo3conf/ext/flbg/res/";
			params.allowfullscreen = "true";
			params.quality = "best";
			params.wmode = "transparent";
			params.scale = "noScale";
			params.menu = "true";		
			
		var attributes = {};
			attributes.id = window.flbg.id;
			attributes.name = window.flbg.id;		

		swfobject.embedSWF("typo3conf/ext/flbg/res/flbg.swf", window.flbg.id, "100%", fullHeight.toString(), "8", "", flashvars, params, attributes);
		
		//$(window.flbg.id).wrap("div", { "class": window.flbg.id, "id": window.flbg.id+"ID"});	
		//$(window.flbg.id).style.height = fullHeight;
		//$$("."+window.flbg.id)[0].style.height = fullHeight;		
		
		$(window.flbg.pageWrap).wrap("div", {"id": "cpPageT3"});			
	},
	
	mouseEvent: function(){
		this.mouseX = 0;
		this.mouseY = 0;
		
		document.observe('mousemove', this.updateMousePosition);
	},
	
	updateMousePosition: function(event){
		if(typeof(event.pageX) == 'number' ){
			var xcoord = event.pageX;
			var ycoord = event.pageY;
		}else if(typeof(event.clientX)=='number'){
			var xcoord = event.clientX;
			var ycoord = event.clientY;
			var badOldBrowser = (window.navigator.userAgent.indexOf('Opera') + 1) || (window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) || (navigator.vendor == 'KDE');
			
			if(!badOldBrowser){
				if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
					xcoord += document.body.scrollLeft;
					ycoord += document.body.scrollTop;
				}else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
					xcoord += document.documentElement.scrollLeft;
					ycoord += document.documentElement.scrollTop;
				}
			}
		}else{}

		window.cpBackgroundMouseEvents = {}
		window.cpBackgroundMouseEvents._xmouse = xcoord;
		window.cpBackgroundMouseEvents._ymouse = ycoord;
	}
});

UClass.background = Background;

function sendMouseEventToFlash(){
	return window.cpBackgroundMouseEvents._xmouse+"::"+window.cpBackgroundMouseEvents._ymouse;
}
