/**
 * @author Antonio
 * v 1.2.5
 */
function wdbox(className)
{
	if(!className) className = "";
	var parent = this;
	this.dimensions = {
		w : false,
		h : false,
		x : false,
		y : false,
		originals : {
			w : false,
			h : false,
			x : false,
			y : false
		}
	}
	this.appereance = {
		overlayered : true,
		status : 'close',
		appendTo : "body",
		makeDimensions : function(){
			var dim = parent.dimensions;
			var view = parent.view;
			if(dim.w) view.box.css("width", dim.w);
			if(dim.h) view.box.css("height", dim.h);
			if(dim.x) view.box.css("left", dim.x);
			if(dim.y) view.box.css("top", dim.y);
		}
	};
	this.data = {
		
	};
	this.view = {
		className : className,
		markup : "<div class = 'wdbox "+className+"'><div class = 'head'><a class = 'closeLink'>x</a></div><div class = 'content'><div class = 'title'></div><div class = 'body'></div><div class = 'prompt'></div></div></div>",
		faderMarkup : "<div class = 'fader'></div>",
		box : $("."+className+".wdbox"),
		title : $("."+className+".wdbox .title"),
		body : $("."+className+".wdbox .body"),
		content : $("."+className+".wdbox .content"),
		prompt :  $("."+className+".wdbox .prompt"),
		all : $("."+className+".wdbox, body .fader"),
		clear : function()
		{
			this.body.html("");
			//this.title.html(parent.model.title);
			this.prompt.html("");
		},
		refresh : function()
		{
			this.body.html(parent.model.body);
			this.title.html(parent.model.title);
			this.prompt.html(parent.model.prompt);
		},
		showFunction : function(){
			
		},
		show : function()
		{
			var main = $(parent.appereance.appendTo);
			if(parent.appereance.overlayered) main.append(this.faderMarkup);
			main.append(this.markup);
			//controllare
			this.box = $("."+className+".wdbox");
			this.fader = $("body .fader");
			this.title = $("."+className+".wdbox .title");
			this.body = $("."+className+".wdbox .body");
			this.content = $("."+className+".wdbox .content");
			this.prompt = $("."+className+".wdbox .prompt");
			$(".fader").height($(document).height()).css({opacity : 0.5});
			this.body.html(parent.model.body);
			this.title.html(parent.model.title);
			this.prompt.html(parent.model.prompt);
			parent.appereance.makeDimensions();
			var marginLeft = -(this.box.width()/2);
			if(this.box.width() == 0){
				marginLeft = -(parseInt(this.box.css("width"))/2);
			}
			if(parent.dimensions.x){
				marginLeft+= parent.dimensions.x;
			}
			var cssMod = {
				top: $(window).scrollTop() + parent.dimensions.y,
				left : "50%",
				marginLeft : marginLeft
			}
			this.box.css(cssMod);
			parent.appereance.status = 'open';
			parent.controller.setup();
			parent.view.all = $("."+className+".wdbox, body .fader");
			parent.view.showFunction();
			
		}
	}
	this.model = {
		title : false,
		body : false,
		prompt : false,
		defaultWait : "loading...",
		setup : function (title, body, prompt)
		{
			this.title = title;
			this.body = body;
			this.prompt = prompt;
		}
	}
	this.create = function(dim){
		if(dim) this.dimensions = dim;
		
	}
	this.controller = {
		repair : function(){
			parent.controller.close = function(){
				parent.controller.closeFunction();
				parent.appereance.status = 'close';
			}
			parent.controller.cancel = function(){
				parent.controller.close();
			}
		},
		save : function(){},
		confirm : function(){},
		defaultClose : function()
		{
			parent.view.box.remove();
			parent.view.fader.remove();
		},
		closeFunction : function()
		{
			parent.controller.defaultClose();
		},
		close : function()
		{
			parent.controller.closeFunction();
			parent.appereance.status = 'close';
		},
		cancel : function()
		{
			this.close();
		},
		faderClick : function(){
			this.close();
		},
		wait : function(waitText){
			if(!waitText) waitText = parent.model.defaultWait;
			parent.view.clear();
			parent.view.body.html(waitText);
		},
		refresh : function(){
			window.location.reload();
		},
		setup :function()
		{
			$("."+className+".wdbox .prompt .save").livequery("click", function()
			{
				parent.controller.save();
			});
			$("."+className+".wdbox .prompt .confirm").livequery("click", function()
			{
				parent.controller.confirm();
			});
			$("."+className+".wdbox .prompt .cancel").livequery("click", function()
			{
				parent.controller.cancel();
			});
			$("."+className+".wdbox .closeLink").livequery("click", function()
			{
				parent.controller.close();
			});
			$(".fader").livequery("click", function()
			{
				parent.controller.faderClick();
			});
			$("."+className+".wdbox .refreshLink").livequery("click", function()
			{
				parent.controller.refresh();
			});
		}
	}
	
}