salmon.namespace.addNamespace("boots.DialogManager");
boots.DialogManager = (function(){
	var _dialogs = {};
	
	var _markup = [
		'<div class="dialog">',
		'	<div class="actions">',
		'		<ul>',
		'			<li class="close"><a href="#">Close</a></li>',
		'		</ul>',
		'	</div>',
		'	<div class="content">',
		'		<div class="t"></div>',
		'		<div class="wrapper"></div>',
		'	</div>',
		'	<div class="b"><div></div></div>',
		'</div>'	
	].join('');
	
	var Dialog = function(options){
		var MIN_HEIGHT = 100,
			OFFSET_X = 10,
			OFFSET_Y = 10;
		this.id = options.id;
					
		_init = function(){
			if($(".dialog")[0] == null){				
				$("body").append(_markup);
				$(".dialog").wrap('<div class="overlay"></div>');
				$(".dialog").css("display", "block");
			}
		};
		_init();
		
		_panel = null;
		_iframe = null;
		_handlers = {
			close: function(e){
				$(".current").trigger("click");
				return false;
			}
		};
		
		_setStyles = function(id){
			$(".overlay").attr("id", id);
		};
		
		_setContent = function(content, scrolling){
			$(".dialog .wrapper").html($(content).html());
		};
		
		_setPosition = function(e){
			var divW = $(".overlay").width(),
				divH = $(".overlay").height(),
				winW = $(window).width();
				winH = $(window).height();
				scrollX = $(document).scrollLeft(),
				scrollY = $(document).scrollTop(),
				offsetX = OFFSET_X,
				offsetY = OFFSET_Y,
				x = e.pageX,
				y = e.pageY,
				dx = 0,
				dy = 0;
				
			if(x + offsetX < 0) {
				dx = scrollX; // position left-justified
			} else if((x + divW + offsetX) > winW) {
				dx = winW + scrollX - divW; // position right-justified
			} else {
				dx = x + offsetX + scrollX; // position default
			}
			
			if((y - scrollY + offsetY + divH) > winH){
				dy = y - divH; // position over cursor
			} else {
				dy = y + offsetY; // position default
			}
			
			$(".overlay").css("left", dx+"px");
			$(".overlay").css("top", dy+"px");
			
			_iFrameShim(divW, divH, dx, dy);			
		};
		
		_iFrameShim = function(w, h, x, y){
			// prevents windowed controls poking through the dialog divs
			if($.browser.msie && $.browser.version=="6.0"){
				if(_iframe==null){
					$("body").append('<iframe id="iframe" frameborder="0" src="javascript:false;"></iframe>');
					_iframe = $("#iframe");
				}
				_iframe.css("position","absolute");
				_iframe.css("z-index", 5);
				_iframe.width(w);
				_iframe.height(h);
				_iframe.css("left", x+"px");
				_iframe.css("top", y+"px");
			}
		}
		
		_attachHandlers = function(){
			$(".dialog .actions .close a").bind("click", _handlers.close);
			if($(".dialog .pd_sendProductEmail").length>0){
				$(".dialog .pd_sendProductEmail").bind("submit",boots.product.details.validateProductEmailForm);
			}
		};
		
		_removeHandlers = function(){
			$(".dialog .actions .close").unbind("click", _handlers.close);
		};
	}
	Dialog.prototype = {
		id: null,
		show: function(args){
			_setContent(args.content);
			_setStyles(this.id);
			_setPosition(args.event);
			$(".overlay").show();
			if(_iframe){
				_iframe.show();
			}
			_attachHandlers();
		},
		hide: function(){
			_removeHandlers();
			$(".overlay").hide();
			if(_iframe){
				_iframe.hide();
			}
		}
	};
	
	return {
		addDialog: function(options){
			
			var d = this.getDialog(options);
			
			function show(e){
				$(".current").trigger("click");
				var args = {
					content: $(this).next(),
					event: e
				}
				
				d.show(args);
				$(this).addClass("current");
			};
			function hide(e){
				$(this).removeClass("current");
				d.hide();
			};
			
			$("." + options.opener).each(function(){
				$(this).toggle(show,hide);
			});
		},
		getDialog: function(options){
			if(_dialogs[options.id] == null){
				_dialogs[options.id] = new Dialog(options);
			}
			return _dialogs[options.id];
		}
	};
})();

