var jswin = new Class({
	Implements: Options,
	options: {content:'jswin',overlay:true,width:250,height:250,htmlWidth:700,htmlHeight:550},
	initialize: function(options){
		this.setOptions(options);
		this.timer = 0;
		this.index = 0;
		this.opened = false;
		this.contentToLoad = {};
		this.contentObj = {};
		this.openClosePos = {};
		switch($type(this.options.content)){
			case 'string': 
				this.content = $$('.'+this.options.content); 
				this.content.each(function(el,i){el.addEvent('click', function(e){ e.stop(); this.open(el); }.bind(this));}, this);
			break;
			case 'element': 
				var el = this.options.content;
				this.content = [el]; 
				el.addEvent('click', function(e){ e.stop(); this.open(el); }.bind(this));
			break;
		}
		var closeButton = new Element('div',{'class':'jsbox-close'}).addEvent('click', this.close.bind(this));
		var clearControls = new Element('div',{'class':'clear'});
		this.previousButton = new Element('div',{'class':'jsbox-previous'}).addEvent('click', this.previous.bind(this));
		this.nextButton = new Element('div',{'class':'jsbox-next'}).addEvent('click', this.next.bind(this));
		this.number = new Element('div',{'class':'jsbox-number'});
		this.title = new Element('div',{'class':'jsbox-title'});
		this.controls = new Element('div',{'class':'jsbox-controls'}).adopt(this.previousButton,this.nextButton,this.number,this.title,clearControls);
		this.controlsContainer = new Element('div',{'class':'jsbox-controls-container'}).adopt(this.controls);
		this.box = new Element('div', {'styles': {'height': '100%','width':'100%','opacity':0}});
		this.boxContainer = new Element('div', {'class': 'jsbox-content'}).adopt(this.box,this.controlsContainer);
		var b1 = new Element('div', {'class': 'jsbox-top'}).adopt(new Element('div'));
		var b2 = new Element('div', {'class': 'jsbox-left'}).adopt( new Element('div', {'class': 'jsbox-right'}).adopt(this.boxContainer) );
		var b3 = new Element('div', {'class': 'jsbox-bottom'}).adopt(new Element('div'));
		this.container = new Element('div',{'class':'jsbox','styles': {'opacity':0}}).adopt(closeButton,b1,b2,b3).inject(document.body,'top');
		this.containerEffects = new Fx.Elements([this.container,this.box],{duration: 400, transition: Fx.Transitions.Sine.easeInOut});
		this.controlEffects = new Fx.Morph(this.controlsContainer, {duration: 300, transition: Fx.Transitions.Sine.easeInOut});
		if(Browser.Engine.trident4){
			closeButton.setStyles({'background-image': 'none','filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/icons/mb_close.png',sizingMethod='image')"});
			this.previousButton.setStyles({'background-image': 'none','filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/icons/mb_left.png',sizingMethod='image')"});
			this.nextButton.setStyles({'background-image': 'none','filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/icons/mb_right.png',sizingMethod='image')"});
		}
		new Drag(this.container,{handle:b1});
		this.reset();
	},
	getOpenClosePos: function(el){
		if(el.getFirst()){
			var w = el.getFirst().getCoordinates().width; if(w < 0){w = 0}
			var h = el.getFirst().getCoordinates().height; if(h < 0){h = 0}
			this.openClosePos = { 'width': w, 'height': h, 'top': el.getFirst().getCoordinates().top, 'left': el.getFirst().getCoordinates().left };
		}
		else{
			var w = el.getCoordinates().width; if(w < 0){w = 0}
			var h = el.getCoordinates().height; if(h < 0){h = 0}
			this.openClosePos = { 'width': w, 'height': h, 'top': el.getCoordinates().top, 'left': el.getCoordinates().left };
		}
		return this.openClosePos;
	},
	open: function(el){
		if(this.options.overlay){ this.overlay = new Overlay({onClick:this.close.bind(this)}); }
		ie_fix_overlay(true);
		this.index = this.content.indexOf(el);
		if(!this.opened){
			this.opened = true;
			if(this.options.overlay){ this.overlay.show(); }
			this.container.setStyles(this.getOpenClosePos(el));
			this.container.setStyle('display', 'block');
			this.containerEffects.start({
				0: {'width': this.options.width,'height': this.options.height,'top': (window.getHeight()/2)+(window.getScrollTop())-(this.options.height/2),'left': (window.getWidth()/2)-(this.options.width/2),'opacity': 1},
				1: {'height': this.options.height}
			});
			this.load(this.index);
		}
		else{
			this.hideControls();
			this.getOpenClosePos(this.content[this.index]);
			this.timer = this.hideContent.delay(500,this);
			this.timer = this.load.delay(1100,this,this.index);
		}
		return false;
	},
	load: function(index){
		this.boxContainer.addClass('jsbox-loading');
		var link = this.content[index];
		var contentOptions = {};
		if($chk(link.rel)){var optArr = link.rel.split(',');optArr.each(function(el){ var ta = el.split(':'); contentOptions[ta[0]] = ta[1]; });}
		this.contentObj.width = contentOptions.width ? contentOptions.width : this.options.width;
		this.contentObj.height = contentOptions.height ? contentOptions.height : this.options.height;
		var str = link.href.substr(link.href.lastIndexOf('.')+1).toLowerCase();
		switch(str){
			case 'jpg': case 'gif': case 'png': this.type = 'image'; break;
			default: this.type = 'html'; break;
		}
		this.contentToLoad = { title: link.title ? link.title : null, number: index+1 };
		if(this.type == 'image'){ this.contentObj = new Asset.image(link.href, {onload: this.resize.bind(this)}); }
		else{ 
			var bc = new Element('div', {'styles': {'padding':'20px'} }).load(link.href);
			this.contentObj = new Element('div', {'styles': {'overflow':'auto', 'height': this.options.htmlHeight } }).adopt(bc);
			this.contentObj.width = contentOptions.width ? contentOptions.width : this.options.htmlWidth;
			this.contentObj.height = contentOptions.height ? contentOptions.height : this.options.htmlHeight;
			this.resize(); 
		}
	},
	resize: function(){
		var top = (window.getHeight()/2)-((this.contentObj.height.toInt()/2)+46)+window.getScrollTop();
		var left = (window.getWidth()/2)-(this.contentObj.width/2);
		if(top < 0){top = 0;}
		if(left < 0){left = 0;}
		this.containerEffects.cancel();
		this.containerEffects.start({
			0: {'width': this.contentObj.width.toInt()+42,'height': this.contentObj.height,'top': top,'left': left,'opacity': 1},
			1: {'height': this.contentObj.height}
		});
		this.timer = this.showContent.delay(500,this);
	},
	showContent: function(){
		this.boxContainer.removeClass('jsbox-loading');
		this.removeContent();
		this.box.adopt(this.contentObj).fade('in');
		this.title.set('html',this.contentToLoad.title);
		this.number.set('html',this.contentToLoad.number+' of '+this.content.length);
		this.timer = this.showControls.delay(800,this);
	},
	showControls: function(){
		if(this.content.length > 1){
			this.contentToLoad.number == 1 ? this.previousButton.addClass('jsbox-previous-disabled') : this.previousButton.removeClass('jsbox-previous-disabled');
			this.contentToLoad.number == this.content.length ? this.nextButton.addClass('jsbox-next-disabled') : this.nextButton.removeClass('jsbox-next-disabled');
			this.controlEffects.start({'height': this.controls.getStyle('height')});
		}
	},
	hideContent: function(){
		this.boxContainer.addClass('jsbox-loading');
		this.box.fade('out');
		this.hideControls();
		this.removeContent.delay(500,this);
	},
	removeContent: function(){ this.box.empty(); },
	hideControls: function(){this.controlEffects.start({'height': 0});},
	zoomOut: function(){
		this.containerEffects.start({
			0:{'width': this.openClosePos.width,'height': this.openClosePos.height,'top': this.openClosePos.top,'left': this.openClosePos.left,'opacity': 0},
			1: {'height': this.openClosePos.height,'opacity': 0}
		});
		this.reset.delay(500,this);
	},
	next: function(){if(this.index < this.content.length-1){this.index++;this.hideControls();this.getOpenClosePos(this.content[this.index]);this.timer = this.hideContent.delay(500,this);this.timer = this.load.delay(1100,this,this.index);}},
	previous: function(){if(this.index > 0){this.index--;this.hideControls();this.getOpenClosePos(this.content[this.index]);this.timer = this.hideContent.delay(500,this);this.timer = this.load.delay(1100,this,this.index);}},
	reset: function(){this.container.setStyles({'opacity':0,'display': 'none'});this.controlsContainer.setStyle('height', 0);this.removeContent();this.opened = false;},
	close: function(){if(this.options.overlay){ this.overlay.hide(); }this.hideContent();this.containerEffects.cancel();this.zoomOut.delay(500,this);ie_fix_overlay.delay(1000);}
});
var Overlay = new Class({
	Implements: Options,
	options: { colour: '#515151', opacity: 0.8, zIndex: 1, onClick: $empty },
	initialize: function(options){
		this.setOptions(options);
		this.overlay = new Element('div', {
			'id': 'overlay',
			'styles': { 'position': 'absolute', 'left': '0px', 'top': '0px', 'width': '100%', 'zIndex': this.options.zIndex, 'background-color': this.options.colour }
		}).inject(document.body);
		this.overlay.addEvent('click', function(){ this.options.onClick(); }.bind(this));
		this.overlay.fade('hide');
		this.position.delay(10,this);
		window.addEvent('resize', this.position.bind(this));
	},
	position: function(){ 
		var h = window.getScrollSize().y; 
		this.overlay.setStyles({'top': 0, 'height': h}); 
	},
	show: function(){ $('overlay').fade(this.options.opacity); },
	hide: function(){ $('overlay').fade('out'); }
});
function ie_fix_overlay(open){
	if (Browser.Engine.trident4){
		var elements = $$(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(Browser.Engine.trident4 ? 'select' : 'embed'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
	}
}