/**
 * SqueezeViewer opens content in Joomla's lightbox.
 */
var SqueezeViewer=MGViewer.extend({
	initialize:function(mediaMap, templateManager, options)
	{
	//mm_debug("SqueezeViewer Initializing");
	var me=this;
	me.setOptions($merge({prefix:""},options));
	me.mediaMap=mediaMap;
	me.templateManager=templateManager;
	var ModalViewer=new Class(SqueezeBox);
	me.SqueezeBox=new ModalViewer({
		onClose:function(){me.fireEvent('onClose');},
		onShow:function(){me.fireEvent('onOpen');}
	});
	//me.SqueezeBox=SqueezeBox;

	me.initState();
	mm_debug(["Viewer - SqueezeViewer. Loaded",{me:me}]);

	},
	display:function(module)
	{
		//alert('display');
		var me=this;
		var hieght=370;
		var width=460;
		var prefix=null;
		var showClose=true;
		if(me.opt){
			if(me.opt.prefix)prefix=me.opt.prefix;
			if(me.opt.hieght)hieght=me.opt.hieght;
			if(me.opt.width)width=me.opt.width;
			if($defined(me.opt.showClose)&&me.opt.showClose==false)showClose=me.opt.showClose;
		}
		
		var squeezeWindow=new Element('div',{'class':'geoliveSqueezeWindow'});
		squeezeWindow.setStyle('margin',"20px");
		squeezeWindow.setStyle('padding-top', "5px");
		var content=$(module.getContent());
		squeezeWindow.appendChild(content);
		me.SqueezeBox.presets.size = {x: width, y: hieght};


		me.SqueezeBox.rebuild=SqueezeViewer.ReBuild;
		me.SqueezeBox.fromElement=SqueezeViewer.FromElement;
		me.SqueezeBox.rebuild(prefix, showClose); //required since transition to/from fullscreen really messes Squeezeviewer up
		me.SqueezeBox.handlers['adopt']=function(el) {
			return el; //otherwise loses events.
		};
		//alert('open squeezeWindow');
		me.SqueezeBox.fromElement(squeezeWindow,{handler:"adopt"}); //add handler for IE 7
		var t=function(){setTimeout(function (){me.fireEvent('onOpened');},300);}();
		me.fireEvent('onOpen');
		me.SqueezeBox.addEvent('onClose',function(){me.eventInstance.fireEvent('onClose'); });

	},
	preload:function(){

	},
	close:function(){
		var me=this;
		me.SqueezeBox.close();
	}

});
SqueezeViewer.ReBuild=function(str, showClose){
	var name=str||'geolive';
	this.overlay.id=name+'sbox-overlay';
	
	this.overlay.setStyles({
		//display: 'none',
		zIndex: this.options.zIndex
	});


	this.content.id =name+'sbox-content';
	


		this.btnClose.id=name+'sbox-btn-close';
		this.btnClose.href= '#';

	this.window.id=showClose?name+'sbox-window':'';
	this.window.setStyles({
		//display: 'none',
		zIndex: this.options.zIndex + 2
	});
	this.window.adopt(this.btnClose, this.content);

	if (!window.ie6) {
		this.overlay.setStyles({
			position: 'fixed',
			top: 0,
			left: 0
		});
		this.window.setStyles({
			position: 'fixed',
			top: '50%',
			left: '50%'
		});
	} else {
		this.overlay.style.setExpression('marginTop', 'document.documentElement.scrollTop + "px"');
		this.window.style.setExpression('marginTop', '0 - parseInt(this.offsetHeight / 2) + document.documentElement.scrollTop + "px"');

		this.overlay.setStyles({
			position: 'absolute',
			top: '0%',
			left: '0%'
				//,marginTop: "expression(document.documentElement.scrollTop + 'px')"
		});

		this.window.setStyles({
			position: 'absolute',
			top: '0%',
			left: '0%'
				//,marginTop: "(expression(0 - parseInt(this.offsetHeight / 2) + document.documentElement.scrollTop + 'px')"
		});
	}

	$(document.body).adopt(this.overlay, this.window);

	this.fx = {
			overlay: this.overlay.effect('opacity', {
				duration: this.options.fxOverlayDuration,
				wait: false}).set(0),
				window: this.window.effects({
					duration: this.options.fxResizeDuration,
					wait: false}),
					content: this.content.effect('opacity', {
						duration: this.options.fxContentDuration,
						wait: false}).set(0)
	};
};

SqueezeViewer.FromElement= function(el, options) {
	this.initialize();
	this.element = $(el);
	if (this.element && this.element.rel) options = $merge(options || {}, Json.evaluate(this.element.rel));
	this.setOptions(this.presets, options);
	this.assignOptions();
	this.url = (this.element ? (this.options.url || this.element.href) : el) || '';

	if (this.options.handler) {
		var handler = this.options.handler;
		return this.setContent(handler, this.parsers[handler].call(this, true));
	}
	var res = false;
	for (var key in this.parsers) {
		if ((res = this.parsers[key].call(this))) return this.setContent(key, res);
	}
	return this;
};
