// JavaScript Document
Choquin.Window = Base.extend({
	constructor:function(options){
		this.options = Object.extend({},options);
		this.element = $(this.createElement());
		if(this.options.content) this.setContent(this.options.content);
		if(this.options.title) this.setTitle(this.options.title);
		if(this.options.wrap) this.wrap(this.options.wrap);
	
		this.getTitleElement = this._virtual;
	},
	
	_virtual: function(){
		throw ('Choquin.Window is a virtual class');	
	},
	
	setContent: function(content){
		this.getContentElement().update(content);
	},
	
	setTitle: function(title){
		this.getTitleElement().update(title);
	},
	
	getContentElement: function(){
		return this.contentElement;
	},
	
	wrap:function(element){
		var element = $(element);
		if(!element) return;
		var title = element.select('[class~="tableTitle"]')[0];
		//element.replace fails in opera...
		Element.replace(element,this.element);
		
		this.setContent(element);
		this.setTitle(title);
		this.element.addClassName(element.identify()+'_container');
	}

});


Choquin.Window.Modal = Choquin.Window.extend({
	constructor: function(options){
		//this.contentElement = options.content;
		this.base(options);
		//this.createOverlay();	
		document.body.appendChild(this.element);
		this.close();
		Event.observe(window,'resize',this._onResize.bindAsEventListener(this));		
	},
	
	_onResize:function(event){
		this.positionWindow();
		this.getOverlay();
	},
	
	getOverlay: function(){
		if(!Choquin.Window.Modal.overlay) {
			Choquin.Window.Modal.overlay = new Element('div');
			Choquin.Window.Modal.overlay.setStyle({
				'zIndex':1000,
				position:'absolute',
				width:'100%',
				height:'100%',
				backgroundColor:'#000',
				top:0,
				left:0,
				opacity:this.options.opacity || .6
			});
	        var objBody = $$('body')[0];
			objBody.appendChild(Choquin.Window.Modal.overlay);
		};
		
		Choquin.Window.Modal.overlay.setStyle({
			width:Window.Document.getWidth()+'px',
			height:Window.Document.getHeight()+'px'
		});
		return Choquin.Window.Modal.overlay;
		
	},
	
	createElement: function(){
		var element = new Element('div');
		this.contentElement = new Element('div');
		element.insert(this.contentElement);
		return element;
	},
	
	positionWindow: function(){
		var arrayPageScroll = document.viewport.getScrollOffsets();
		this.element.setStyle({position:'absolute',zIndex:1001});
		var lightboxLeft = [0,arrayPageScroll[0] + ((document.viewport.getWidth()-this.element.getWidth())/2)].max();
        var lightboxTop = [0,arrayPageScroll[1] + ((document.viewport.getHeight()-this.element.getHeight())/2)].max();
		this.element.setStyle({top:lightboxTop+'px',left:lightboxLeft+'px'});
	},
	
	open: function(){
		if(this._opened===true) return;
		this.positionWindow();
	
		this.getOverlay().observe('click',function(event){
			this.close();
		}.bindAsEventListener(this)).show();
		
		this.element.setStyle({'zIndex':1001});
		this.element.appear ? this.element.appear() : this.element.show();
		
		this._opened = true;
	},
	
	close:function(){
		if(this._opened===false) return;
		
		this.getOverlay().stopObserving('click').hide();
		this.element.hide();
		this._opened = false;
	},
	
	toString: function(){
		return '[object Choquin.Window.Modal]';
	}
	
	
});



Choquin.Window.Fixed = Choquin.Window.extend({
	constructor:function(options){
		this.className = 'window fixed';
		this.base(options);
	},
	
	createElement: function(){
		var div = new Element('div');
		div.update(this.structure());
		var centralTD= $(div.select('td')[4]);
		this.contentElement = new Element('div',{className:'content'});
		centralTD.update(this.contentElement);
		return div.firstChild;
	},


	structure: function(){	
		var table = ["<table class=\""+this.className+"\">"];
		
		var imgNumber = 1;
		for (var row=0;row<3;row++){
			table.push("<tr>");
			for (var col=0;col<3;col++) {
				table.push(this.getTD(imgNumber));
				imgNumber++;
			}
			table.push("</tr>");
		}
		table.push("</table>");
		return table.join("");
	},
	
	getTD: function(i){
		var src = this.getImage(i);
		var className="";
		var tdNames = ['top left','top center','top right','middle left','middle center','middle right','bottom left','bottom center','bottom right'];
		if(i==2) className= "title";
		className+=' '+tdNames[i-1];
		return "<td class=\""+className+"\"></td>";
	},
	
	getImage: function(i){
		if(this.options.getImage) return this.options.getImage(i);
		else return "/images/table_0"+i+".gif";
	},
	
	getTitleElement: function(){
		return $(this.element.select('td')[1]);
	},

	toString:function(){
		return "Choquin.Window.Fixed";
	}
});




Choquin.Window.Fixed.Legacy = Choquin.Window.extend({
	constructor:function(options){
		this.base(options);
	},
	
	createElement: function(){
		var div = new Element('div');
		div.update(this.structure());
		return div.firstChild;
	},


	structure: function(){
		var table = ["<table class=\"window fixed\">"];
		
		var imgNumber = 1;
		for (var row=0;row<3;row++){
			table.push("<tr>");
			for (var col=0;col<3;col++) {
				table.push(this.getTD(imgNumber));
				imgNumber++;
			}
			table.push("</tr>");
		}
		table.push("</table>");
		return table.join("");
	},
	
	getTD: function(i){
		var src = this.getImage(i);
		//if(i==5) return "<td style=\"background-image:url("+src+")\"></td>";
			var width = [1,3,7,9].include(i) ? "1px":"auto";
			var height = [1,3,7,9].include(i) ? "1px":"auto";
			var className = "";
			if(i==2) className= "title";
			var content = [1,3,7,9].include(i) ? "<img src=\""+src+"\"/>" : "";
			var background = "url("+src+")";
			switch(i){
				case 2: background+="repeat-x top"; break;
				case 8: background+="repeat-x bottom"; break;
				case 4: background+="repeat-y left";break;
				case 6: background+="repeat-y right";break;
			}
			return "<td class=\""+className+"\" style=\"background:"+background+";width:"+width+";height:"+height+"\">"+content+"</td>";
	},
	
	getImage: function(i){
		if(this.options.getImage) return this.options.getImage(i);
		else return "/images/table_0"+i+".gif";
	},
	
	getContentElement: function(){
		return $(this.element.select('td')[4]);
	},

	getTitleElement: function(){
		return $(this.element.select('td')[1]);
	},

	toString:function(){
		return "Choquin.Window.Fixed";
	}
});

