
/* Based off of:
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 */
var mbWidth = 700;
var mbHeight = 700;
var mbScroll = 'no';

ha.util.modalbox = {
	init: function(){
		$('a.modalbox').click(function(){
			var t = this.title || this.name || null;
			if(t==null) t = "";
			var u = this.href;
			var a = $(this).attr("args");
			ha.util.modalbox.show(t,u,a);
			this.blur();
			return false;
		});
	},

	show: function(title, url, args) {
		var sOverlay = ""; var sBody = "";
		var sLoad = '<div id="mb-load"><img src="/guarantee/resources/429/images/modal-loader.gif" /></div>';
	
		try {
			
			if (typeof document.body.style.maxHeight == "undefined") {
				$("body","html").css({height: "100%", width: "100%"});
				$("html").css("overflow","hidden");
				if (document.getElementById("mb-hide") == null) {
					sOverlay += '<iframe id="mb-hide"></iframe>';
					sOverlay += '<div id="mb-overlay"></div>';
					sOverlay += '<div id="mb-window"></div>';
				}
			}
			else if(document.getElementById("mb-overlay") == null){
				sOverlay += '<div id="mb-overlay"></div>';
				sOverlay += '<div id="mb-window"></div>';
			}
		
			$("body").append(sOverlay);
			//$("#mb-overlay").click(ha.util.modalbox.remove);
	
			$("body").append(sLoad);
			$('#mb-load').show();

			if (args.length > 0){
				args = args.split(":");
				for (var i=0; i<args.length; i++){
					if (i == 0) mbWidth = args[0];
					else if (i == 1) mbHeight = args[1];
					else if (i == 2) mbScroll = args[2];
				}
			}
			var sBody = '<div id="mb-frame-box">';
				if (url.charAt(0) != '#') {
					sBody += '<iframe frameborder="0" hspace="0" src="'+ url +'" scrolling="'+ mbScroll +'" title="'+ title +'"id="mb-iframe" name="mb-iframe" style="width:'+ mbWidth +'px; height:'+ mbHeight +'px;" allowtransparency="true" onload="ha.util.modalbox.showIframe()"> </iframe>';
				}
				sBody += '</div>';
			$("#mb-window").append(sBody);
			
			if (url.charAt(0) == '#') {
				$(url).appendTo($('#mb-frame-box'));
			}
			ha.util.modalbox.position();

			$(window).resize(function(){
				ha.util.modalbox.position();
			});
			
			if(frames['mb-iframe'] === undefined){ // safari
				$("#mb-load").remove();
				$("#mb-window").css({display:"block"});
				$(document).keyup(function(e){ 
					var key = e.keyCode; 
					if(key == 27){ ha.util.modalbox.remove(); }
				});
			}

			var fr = document.getElementById("mb-iframe"); 
			if (document.all)
    			fr.document.body.focus(); 
			else 
				fr.contentDocument.body.focus();
		
		} catch(e) {
			//do nothing
		}
	},
	
	showIframe: function(){
		$("#mb-load").remove();
		$("#mb-window").css({display:"block"});
	},

	remove: function(){
		$("#mb-overlay").unbind("click");
		$("#mb-window").fadeOut("fast",function(){$('#mb-window,#mb-overlay,#mb-hide').remove();});
		$("#mb-load").remove();
		if (typeof document.body.style.maxHeight == "undefined") {
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
		}
		document.onkeydown = "";
		return false;
	},
	
	position: function(){
		var h = $(window).height();
		
		if (mbHeight > h) {
			$('#mb-frame-box').css({height: (h-30) +'px', overflow: 'scroll'});
			if (!$.browser.msie6) $("#mb-window").css({marginTop:"0px", top: "0"});
		} else {
			$('#mb-frame-box').css({height: mbHeight +'px', overflow: 'visible'});
			$("#mb-window").css({marginTop: '-'+ parseInt((mbHeight / 2),10) +'px', top: "50%"});
		}

		$("#mb-window").css({marginLeft: '-'+ parseInt((mbWidth / 2),10) +'px', width: mbWidth +'px'});
	}
	
};

