// 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);
	},
	
	setContent: function(content){
		this.getContentElement().update(content);
	},
	
	setTitle: function(title){
		this.getTitleElement().update(title);
	},
	
	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.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";
	},
	
	getContentElement: function(){
		return this.contentElement;
		return $(this.element.select('td')[4]);
	},

	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";
	}
});

