var Flashbox = {

	init: function(){
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);
		this.overlay = new Element('div', {'id': 'lbOverlay'}).injectInside(document.body);
		this.center = new Element('div', {'id': 'lbCenter', 'styles': {'display': 'none'}}).injectInside(document.body);
		this.image = new Element('div', {'id': 'lbImage'}).injectInside(this.center);
		
		this.topC = new Element('div', {'id': 'lbTopContainer'}).injectInside(document.body);
		this.Panel = new Element('div', {'id': 'lbPanel'}).injectInside(this.topC);
		new Element('div', {'id': 'lbCloseLink'}).injectInside(this.Panel);
		$('lbTopContainer').onclick = this.close.bind(this);
		this.overlay.onclick = this.close.bind(this);
	
		this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.Panel);
		
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects({duration: 400}),
			image: this.image.effect('opacity', {duration: 500})
		};
	},

	open: function(str,myRoot,current_media,showtit,dom){
		var arr =str.split('|uppod|');
		this.image_pub = new Element('div', {'id': 'lbPub'}).injectInside(this.image);
		this.fv = Json.evaluate(arr[0]);
		this.fv['uid']='lbPub';
		if(dom!=''){
			this.swf=myRoot+"customers/"+dom+"/uppod.swf";
		}
		else{
			this.swf=myRoot+"player/uppod"+ver+".swf";
		}
		
		if(arr[1]!=''){
		//	this.fv_ = Json.evaluate(arr[1]);
			var fv_ =arr[1].split('|');
			fvw=500;
			fvh=375;	
			if(arr[2]=='file'){
				fvw=fv_[3];
				fvh=fv_[4];
			}
			if(arr[2]=='pl'){
				fvw=fv_[1];
				fvh=fv_[2];
			}
			if(fv_[6]>0){
				this.flashW = fvw*1;
				this.flashH = fvh*1;
			}
			else{
				this.flashW = fvw*1;
				this.flashH = fvh*1;
			}
			this.flashWs=fvw+"";
			this.flashHs=fvh+"";
			this.colorBG = fv_[5];
		}
		else{
			this.center.setProperty('class','');
			this.image.setProperty('class','');
			if(current_media=='audio'){
				this.flashW = 300;
				this.flashH = 90;
				this.flashWs="300";
				this.flashHs="90";	
			}
			else{
				this.flashW = 500;
				this.flashH = 375;
				this.flashWs="500";
				this.flashHs="375";
			}	
			
			this.colorBG = '000000';
		}
		this.title = arr[3];
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		showtit?this.caption.setHTML(this.title):'';
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.7);
		$('lbCloseLink').setStyles({'display': 'block'});
		var YO=this.getYOffset();
		var HYO=window.getHeight()/2-this.flashH/2-50+YO;
		HYO<20?HYO=30:0;
		//alert(this.flashW+' '+this.flashH);
this.center.setStyles({'width':this.flashW,'top':HYO,'height':this.flashH,'marginLeft':-this.flashW/2,'background-color': this.colorBG});
this.topC.setStyles({'display':'block','width':this.flashW,'top':window.getHeight()/2-this.flashH/2-100+YO,'left':window.getWidth() / 2 -this.flashW/2});
	this.center.setStyles({'padding':0});
	this.image.setStyles({'padding':0});
	this.topC.setStyles({'width':this.flashW});
	if(arr[1]!=''){
		if(fv_[6]>0){
			this.center.setStyles({'padding':10});
			this.image.setStyles({'padding':10});
			this.topC.setStyles({'width':this.flashW+20});
		}
	}
	
	//	this.fx.resize.start({width: this.flashW, height:this.flashH, marginLeft: -this.flashW/2});
		//if(this.fv_.tr=='1'){
		//	var params = {wmode:"transparent"};
	//	}
	//	else{
		$('lbCenter').setStyles({'backgroundColor': '#'+this.colorBG});
		var params = {bgcolor:this.colorBG, allowFullScreen:"true",allowScriptAccess:"always"};
	//	}
		var attributes = {id:'lbPub',name:'lbPub'};
		new swfobject.embedSWF(this.swf, 'lbPub', this.flashWs, this.flashHs, '10.0.0',false,this.fv, params, attributes);
	//	return this.changeImage();
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
		//	el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
	},
	getYOffset: function(){
	    var pageY;
	    if(typeof(window.pageYOffset)=='number') {
	       pageY=window.pageYOffset;
	    }
	    else {
	       pageY=document.documentElement.scrollTop;
	    }
	    return pageY;
	},
	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
		}
	},
	changeImage: function(){
		this.fx.image.hide();
		this.center.className = 'lbLoading';
		return false;
	},

	close: function(){
		for (var f in this.fx) this.fx[f].stop();
		$('lbCloseLink').setStyles({'display': 'none'});
		this.center.style.display = this.topC.style.display = 'none';
		this.center.setStyles({'width':0,'top':0,'height':0,'marginLeft':0});
		$('lbImage').innerHTML='';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};
window.addEvent('domready', Flashbox.init.bind(Flashbox));

