jType.registerNamespace("MdLightbox.UI");
MdLightbox.UI.Point = function(x, y){
	this._x = x;
	this._y = y;
	return {x: this._x, y: this._y}
}
MdLightbox.UI.Point.prototype = {
}

//Manager de los Popups para agruparlos
MdLightbox.UI.PopupManager = function(options){
	
	//internalProperties
	this._popupCollection = {};
	this._count = 0;
	this._currentPopup = null;

	this.setOptions(options);
	this._initialize();
}

MdLightbox.UI.PopupManager.prototype = {
	_initialize : function (){
	},
	
	addPopup : function(name, popup){		
		//if(typeof(this._popupCollection[name]) != "undefined")
			//throw new Error("The name is used by another popup");
		popup._parent = this;
		popup._name = name;
		this._popupCollection[name] = popup;
		this._count ++;
	},
	createAddPopup : function (name, target, options){
		
	},
	closeCurrentPopup : function(){
		if(this._currentPopup != null){
			this._currentPopup.close();
		}
	},
	notifyOpened : function(popup){
		this._currentPopup = this._popupCollection[popup._name];
	},
	getPopup : function(id){
		return this._popupCollection[id];
	}
}
jType.registerClass(MdLightbox.UI.PopupManager, MdLightbox.Base);

MdLightbox.UI.Popup = function(target, options){ //TODO mouseover para tooltip
	this._target = target || null;
	this._opener = null;
	this._relativeTo = null;
	this._overlay = null;
	this._mode = MdLightbox.UI.Popup.Mode.NORMAL;
	this._type = MdLightbox.UI.Popup.Type.NORMAL;
	this._position = MdLightbox.UI.Popup.Position.ABSOLUTE;
	this._halign = MdLightbox.UI.Popup.HAlign.CENTER;
	this._valign = MdLightbox.UI.Popup.VAlign.MIDDLE;
	this._autoPos = true;
	this._animate = false;
	this._fixTarget = true;
	this._overlayTemplate = MdLightbox.UI.Popup.Templates.OVERLAY;
	this._showOverlay = false;
	this._overlayOpacity = 0;
	this._delay = 500;
	this._draggable = true;
	this._automaticEscClose = true;
		
	this._minimized = false;
	this._minWidth = 300;
	this._minHeight = 300;
	this._minHAlign = MdLightbox.UI.Popup.HAlign.RIGHT;
	this._minVAlign = MdLightbox.UI.Popup.VAlign.BOTTOM;
	this._maxWidth = 0;
	this._maxHeight = 0;
	this._maxHAlign = MdLightbox.UI.Popup.HAlign.CENTER;
	this._maxVAlign = MdLightbox.UI.Popup.VAlign.MIDDLE;
	
	this._onOpeningHandler = null;
	this._onOpenedHandler = null;
	this._onClosingHandler = null;
	this._onClosedHandler = null;
	//absolute properties
	
	//relative properties
	this._overOpener = false;
	
	this._handler_opener_OnClick = Function.delegate(this, this._opener_OnClick);
	this._handler_opener_OnMouseOver = Function.delegate(this, this._opener_OnMouseOver);
	this._handler_opener_OnMouseOut = Function.delegate(this, this._opener_OnMouseOut);
	
	//internalProperties
	this._name = "noname";
	this._parent = null;
	this._currentOpener = null;
	this._odlOpener = null;
	this._opened = false;
	this._timerPosition = null;
	this._target_clickHandler = Function.delegate(this, this._target_OnClick);
	this._window_resizeHandler = Function.delegate(this, this._window_OnResize);
	this._window_scrollHandler = Function.delegate(this, this._window_OnScroll);
	this._window_closeHandler = Function.delegate(this, this._window_OnClose);	
	this._body_clickHandler = Function.delegate(this, this._body_OnClick);
	this.setOptions(options);
	this._initialize();
}
MdLightbox.UI.Popup.Names = {
	CLOSE : "popup_close_button",
	MINIMIZE : "popup_minimize_button",
	MAXIMIZE : "popup_maximize_button",
	DRAG : "popup_drag_button"
}
MdLightbox.UI.Popup.Mode = {
	NORMAL : "normal",
	TOOLTIP : "tooltip",
	NONE : "none"
}
MdLightbox.UI.Popup.Type = {
	MODAL : "modal",
	SEMI_MODAL : "semi_modal",
	NORMAL : "normal"
}
MdLightbox.UI.Popup.Position = {
	ABSOLUTE : "absolute",
	RELATIVE : "relative"
}
MdLightbox.UI.Popup.HAlign = {
	LEFT : "left",
	CENTER : "center",
	RIGHT: "right"
}
MdLightbox.UI.Popup.VAlign = {
	TOP : "top",
	MIDDLE : "middle",
	BOTTOM : "bottom"
}
MdLightbox.UI.Popup.Templates = {
	OVERLAY : "<div id='modal_overlay' class='spc_overlay'></div>"
}
MdLightbox.UI.Popup.Events = {
	OPENING : "opening",
	OPENED : "opened",
	CLOSING : "closing",
	CLOSED : "closed",
	MINIMIZING : "minimizing",
	MINIMIZED : "minimized",
	MAXIMIZING : "maximizing",
	MAXIMIZED : "maximized"
}
MdLightbox.UI.Popup.Status = {
	INACTIVE : "inactive",
	ACTIVE : "active",
	MINIMIZED : "minimized",
	MAXIMIZED : "maximized"
}

MdLightbox.UI.Popup.prototype = {
	//private methods
	_initialize : function (){
		if(this._fixTarget){
			this._target.css({
			});
		}
		this._target.hide();
		this.setOpener(this._opener);
		if(!this._overlay){
			this._overlay = $(this._overlayTemplate.toElement());
			this.rePositionateOverlay();
			this._overlay.attr("id", this._target.attr("id") + "_overlay");
			this._overlay.css({
				//"opacity": this._overlayOpacity,
				"position" : "absolute"
			});
			if($.browser.msie){
				this._overlay.css({
					"opacity": 0,
					"background-color": "#000000"
				});
				/*this._target.css({
					"opacity": 0,
					"background-image": url("pics/spotlight_loader.gif")
				});*/
				var div = $(document.createElement("div"));
				div.css({
					position: "absolute",
					left: "0px",
					top: "0px",
					right: "0px",
					bottom: "0px",
					background: "#000000",
					opacity: 0,
					zIndex:-1
				});
				this._target.prepend(div);
				
			}
			this._overlay.hide();
			$(document.body).append(this._overlay);
		}
		if(this._type == MdLightbox.UI.Popup.Type.MODAL){
			this._showOverlay = true;
		}
		this._addPopupHandlers();
		if(this._position == MdLightbox.UI.Popup.Position.ABSOLUTE){
			if(this._draggable && $("." + MdLightbox.UI.Popup.Names.DRAG).length > 0){
				$("." + MdLightbox.UI.Popup.Names.DRAG).css("cursor", "move");
				this._target.draggable({
					handle: "." + MdLightbox.UI.Popup.Names.DRAG,
					helper: "original"
				});
			}
		}
		this._target.find("." + MdLightbox.UI.Popup.Names.CLOSE).bind("click", Function.delegate(this, this._closeButton_OnClick));
		this._target.find("." + MdLightbox.UI.Popup.Names.MINIMIZE).bind("click", Function.delegate(this, this._minimizeButton_OnClick));
		this._target.find("." + MdLightbox.UI.Popup.Names.MAXIMIZE).bind("click", Function.delegate(this, this._maximizeButton_OnClick)).hide();
	},
	_getXY : function(){
		var x, y;
		if(this._relativeTo != null){
				var opener = this._relativeTo;
				var parent_location = this._relativeTo.offsetParent().offset();
				var opener_location = this._relativeTo.offset();
				opener_location.left = opener_location.left - parent_location.left;
				opener_location.top = opener_location.top - parent_location.top;
		}else{
			if(this._currentOpener != null){
				var opener = $(this._currentOpener);
                //console.log(opener.offsetParent().offset());
				var parent_location = opener.offsetParent().offset();
				var opener_location = opener.offset();
				opener_location.left = opener_location.left - parent_location.left;
				opener_location.top = opener_location.top - parent_location.top;
			}
		}
				
		switch(this._halign){
			case MdLightbox.UI.Popup.HAlign.LEFT:
				x = (this._position == MdLightbox.UI.Popup.Position.ABSOLUTE) ? 0 + $(window).scrollLeft() : opener_location.left + opener.outerWidth() - MdLightbox.Util.getTotalWidth(this._target);
				break;
			case MdLightbox.UI.Popup.HAlign.RIGHT:
				x = (this._position == MdLightbox.UI.Popup.Position.ABSOLUTE) ? $(window).width() - MdLightbox.Util.getTotalWidth(this._target) + $(window).scrollLeft() : opener_location.left;
				break;
			case MdLightbox.UI.Popup.HAlign.CENTER:
			default:
				x = (this._position == MdLightbox.UI.Popup.Position.ABSOLUTE) ? ($(window).width() - MdLightbox.Util.getTotalWidth(this._target))/2 + $(window).scrollLeft() : opener_location.left + ((opener.outerWidth() -  MdLightbox.Util.getTotalWidth(this._target))/2);
		}
		switch(this._valign){
			case MdLightbox.UI.Popup.VAlign.TOP:
				y = (this._position == MdLightbox.UI.Popup.Position.ABSOLUTE) ? 0 + $(window).scrollTop() : opener_location.top - MdLightbox.Util.getTotalHeight(this._target) + ((this._overOpener) ? opener.outerHeight() : 0 );
				break;
			case MdLightbox.UI.Popup.VAlign.BOTTOM:
				y = (this._position == MdLightbox.UI.Popup.Position.ABSOLUTE) ? $(window).height() - MdLightbox.Util.getTotalHeight(this._target) + $(window).scrollTop() : opener_location.top + opener.outerHeight() - ((this._overOpener) ? + opener.outerHeight() : 0 );
				break;
			case MdLightbox.UI.Popup.VAlign.MIDDLE:
			default:
				y = (this._position == MdLightbox.UI.Popup.Position.ABSOLUTE) ? ($(window).height() - MdLightbox.Util.getTotalHeight(this._target))/2 + $(window).scrollTop() : opener_location.top + ((opener.outerHeight() -  MdLightbox.Util.getTotalHeight(this._target))/2);
                //console.log('height: ' + $(window).height() + ' totalheight: '+ MdLightbox.Util.getTotalHeight(this._target));
        }
		return new MdLightbox.UI.Point(x, y);
	},
	setOpener : function(opener){
		if(opener != null){
			this._opener.unbind("click", this._handler_opener_OnClick);
			this._opener.unbind("mouseover", this._handler_opener_OnMouseOver);
			this._opener.unbind("mouseout", this._handler_opener_OnMouseOut);
			this._opener = opener;
			if(this._mode == MdLightbox.UI.Popup.Mode.NORMAL){
				this._opener.bind("click", this._handler_opener_OnClick);
			}else if(this._mode == MdLightbox.UI.Popup.Mode.TOOLTIP){
				this._opener.bind("mouseover", this._handler_opener_OnMouseOver);
				this._opener.bind("mouseout", this._handler_opener_OnMouseOut);
			}		
		}
	},
	_clearPosition : function(){
		this._target.css({
			left: "auto",
			top: "auto"		
		});
	},
	_addHandlers : function(){
		if(this._automaticEscClose){
			$(window).bind("keyup", this._window_closeHandler);
		}
		$(window).bind("resize", this._window_resizeHandler);		
		if(this._position == MdLightbox.UI.Popup.Position.ABSOLUTE)
			$(window).bind("scroll", this._window_scrollHandler);	
		if(this._type == MdLightbox.UI.Popup.Type.SEMI_MODAL){
			$(document.body).bind("click", this._body_clickHandler);
		}		
		this._target.bind("click", this._target_clickHandler);
	},
	_removeHandlers : function(){
		if (this._automaticEscClose) {
			$(window).unbind("keyup", this._window_resizeHandler);
		}
		$(window).unbind("resize", this._window_resizeHandler);
		if(this._position == MdLightbox.UI.Popup.Position.ABSOLUTE)
			$(window).unbind("scroll", this._window_scrollHandler);	
		if(this._type == MdLightbox.UI.Popup.Type.SEMI_MODAL)
			$(document.body).unbind("click", this._body_clickHandler);	
		this._target.unbind("click", this._target_clickHandler);
	},
	_addPopupHandlers : function(){
		if(this._onOpeningHandler != null)
			this.addOnOpening(this._onOpeningHandler);
		if(this._onOpenedHandler != null)
			this.addOnOpened(this._onOpenedHandler);
		if(this._onClosingHandler != null)
			this.addOnClosing(this._onClosingHandler);
		if(this._onClosedHandler != null)
			this.addOnClosed(this._onClosedHandler);
	},
	//handler methods
	_window_OnResize : function(event){
		//this._
		this.rePositionate();
		if(this._showOverlay)
			this.rePositionateOverlay();
	},
	_window_OnScroll : function(event){
		this.rePositionate();
		if(this._showOverlay)
			this.rePositionateOverlay();
	},
	_body_OnClick : function(event){
		if(this._type == MdLightbox.UI.Popup.Type.SEMI_MODAL){
			if(this.isOpened())
				this.close();
		}		
	},
	_window_OnClose : function(event){
		var evento = event || window.event;
		if(evento.keyCode == 27){
			if (this._automaticEscClose) {
				if (evento.preventDefault) {
					evento.preventDefault();
				}else {
					evento.returnValue = false;
				}
				this.close();
			}
		}
	},
	_opener_OnClick : function(event){
		this._oldOpener = this._currentOpener;
		this._currentOpener = event.target;
		if(this.isOpened() && this._currentOpener !== null && this._opener.index(this._currentOpener) != this._opener.index(this._oldOpener)){
			this.close(Function.delegate(this, function(){
				this.open();
			}));
		}
		if(!this.isOpened()){
			this.open();
		}else{
			this.close();
		}	
		
		event.stopPropagation();
		return false;
	},
	_opener_OnMouseOver : function(event){
		this._oldOpener = this._currentOpener;
		this._currentOpener = event.target;
		if(!this.isOpened()){
			this._timerOpen = setTimeout(Function.delegate(this, this.open), this._delay);
		}		
	},
	_opener_OnMouseOut : function(event){
		window.clearInterval(this._timerOpen);
	},
	_target_OnClick : function(event){
		event.stopPropagation();
	},
	_closeButton_OnClick : function(event){
		this.close();
	},
	_minimizeButton_OnClick : function(event){
		this.minimize();
	},
	_maximizeButton_OnClick : function(event){
		this.maximize();
	},	
	//public methods
	rePositionate : function (animate){
		var pos = this._getXY();
		
		if(typeof(animate) != "undefined"){
			this._target.stop().animate({
				left: pos.x + "px",
				top: pos.y + "px"
			}, animate, "swing");
		}else{
			this._target.css({
				left: pos.x + "px",
				top: pos.y + "px"
			});
		}
	},
	rePositionateOverlay : function(){
		this._overlay.css({
			top: 0 + $(window).scrollTop() + "px",
			left: 0 + $(window).scrollLeft() + "px",
			bottom : 0 - $(window).scrollTop() + "px",
			right : 0 - $(window).scrollLeft() + "px"
		});
	},
	open : function (){
		$(this).trigger(MdLightbox.UI.Popup.Events.OPENING, [this._currentOpener]);
		if(this._parent != null) this._parent.closeCurrentPopup();
		this._addHandlers();
		if(this._autoPos)
			this.rePositionate();
		if(this._type == MdLightbox.UI.Popup.Type.MODAL || (this._type == MdLightbox.UI.Popup.Type.SEMI_MODAL && this._showOverlay)){
			this.rePositionateOverlay();
			this._overlay.show();		
		}
		if(this._animate){
			this._target.show("fast", Function.delegate(this, function (){
				this._opened = true;
				if(this._parent != null) this._parent.notifyOpened(this);
				$(this).trigger(MdLightbox.UI.Popup.Events.OPENED, [this._currentOpener]);
			}));
		}else{
			this._target.show();
			this._opened = true;
			$(this).trigger(MdLightbox.UI.Popup.Events.OPENED, [this._currentOpener]);
		}
	},
	close : function (callback){
		$(this).trigger(MdLightbox.UI.Popup.Events.CLOSING);
		this._removeHandlers();
		if(this._type == MdLightbox.UI.Popup.Type.MODAL || (this._type == MdLightbox.UI.Popup.Type.SEMI_MODAL && this._showOverlay))
			this._overlay.hide();
		if(this._animate){		
			this._target.hide("fast", Function.delegate(this, function (){
				this._opened = false;
				if(callback)
					callback.call(this);
				$(this).trigger(MdLightbox.UI.Popup.Events.CLOSED);
			}));
		}else{
			this._target.hide();	
			this._opened = false;	
				if(callback)
					callback.call(this);
			$(this).trigger(MdLightbox.UI.Popup.Events.CLOSED);
		}
	},
	minimize : function (){
		this._maxWidth = this._target.width();
		this._maxHeight = this._target.height();
		$(this).trigger(MdLightbox.UI.Popup.Events.MINIMIZING);
		this._maxHAlign = this._halign;
		this._maxVAlign = this._valign;
		this._halign = this._minHAlign;
		this._valign = this._minVAlign;
		this._target.width(this._minWidth);
		this._target.height(this._minHeight);
		this.rePositionate("normal");
		this._target.find("." + MdLightbox.UI.Popup.Names.MINIMIZE).hide();
		this._target.find("." + MdLightbox.UI.Popup.Names.MAXIMIZE).show();
		this._minimized = true;
		if(this._showOverlay)
			this._overlay.hide();
		$(this).trigger(MdLightbox.UI.Popup.Events.MINIMIZED);
	},
	maximize : function (){
		this._minWidth = this._target.width();
		this._minHeight = this._target.height();
		$(this).trigger(MdLightbox.UI.Popup.Events.MAXIMIZING);
		this._minHAlign = this._halign;
		this._minVAlign = this._valign;
		this._halign = this._maxHAlign;
		this._valign = this._maxVAlign;
		this._target.width(this._maxWidth);
		this._target.height(this._maxHeight);
		this.rePositionate("normal");
		this._target.find("." + MdLightbox.UI.Popup.Names.MAXIMIZE).hide();
		this._target.find("." + MdLightbox.UI.Popup.Names.MINIMIZE).show();
		this._minimized = false;
		if(this._showOverlay)
			this._overlay.show();
		$(this).trigger(MdLightbox.UI.Popup.Events.MAXIMIZED);
	},
	isOpened : function(){
		return this._opened;
	},
	removeOpener : function(opener){
		
	},
	addOnOpening : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.OPENING, handler);
	},
	removeOnOpening : function(handler){
		$(this).unbind(MdLightbox.UI.Popup.Events.OPENING, handler);
	},
	addOnOpened : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.OPENED, handler);
	},
	removeOnOpened : function(handler){
		$(this).unbind(MdLightbox.UI.Popup.Events.OPENED, handler);
	},	
	addOnClosing : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.CLOSING, handler);
	},
	removeOnClosing : function(handler){
		$(this).unbind(MdLightbox.UI.Popup.Events.CLOSING, handler);
	},
	addOnClosed : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.CLOSED, handler);
	},
	removeOnClosed : function(handler){
		$(this).unbind(MdLightbox.UI.Popup.Events.CLOSED, handler);
	},
	addOnMinimizing : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.MINIMIZING, handler);
	},
	addOnMinimized : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.MINIMIZED, handler);
	},
	addOnMaximizing : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.MAXIMIZING, handler);
	},
	addOnMaximized : function(handler){
		$(this).bind(MdLightbox.UI.Popup.Events.MAXIMIZED, handler);
	}
}
jType.registerClass(MdLightbox.UI.Popup, MdLightbox.Base);




