/*
 * Dialog plugin for jQuery
 * version: 1.1 (17/03/2009)
 * @requires jQuery v1.2 or later
 * @requires dialog.css
 *
 * Created by Igor karasyov [ ikarasyo@ryerson.ca ]
 * 
 * 
 * Open:
 * 		$.popUp(url,options);
 *
 * 
 * Close:
 * 		$.popDown();
 *
 */

(function($){
	
	$.extend({
		popUp : function(url, options) {
			settings = jQuery.extend({
				"opacity":0.7,
				"width":200
			}, options);
			
			$("body").append("<div id='overlay'></div>");
			$("#overlay").hide()
				.css({"opacity":settings.opacity})
				.click(function(){$.popDown();})
				.fadeIn(200);
				
			$("body").append("\
				<div id='dialog'>\
					<h2>Untitled</h2>\
					<div class='error'></div>\
					<div class='body'>\
						<p>Empty dialog box...</p>\
					</div>\
					<div class='footer'>\
						<input id='btnClose' type='button' value='Close' />\
					</div>\
				</div>");
			
			$("#dialog").hide().load(url,null,centerPopup);
			
			function centerPopup(){
				if(settings.height){
					$("#dialog .body").css({"height":settings.height,"overflow-y":"auto"});
				}
				$("#dialog").css({
					"width":settings.width,
					"top":getWindowHeight()/5,
					"left":($(window).width() - settings.width)/2})
				.fadeIn(200);
				$("#dialog #btnClose").click(function(){
					$.popDown();
				})
			}
			
			
			$(document).bind("keydown", function(e) {
				if (e.keyCode == 27) $.popDown();
			});
		},
		popDown : function() {
			$("#dialog").fadeOut(200,function(){
				$(this).remove();
			});
			$("#overlay").fadeOut(200, function(){
				$(this).remove();
			});
			$(document).unbind("keydown");
    	}
	});
	
	function getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) { // all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		return new Array(xScroll,yScroll) 
	};
	
	function getWindowHeight() {
		var windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowHeight = document.body.clientHeight;
		}	
		return windowHeight;
	};
	
	function clearForm(form) {
		$(":input", form).each(function() {
			var type = this.type;
			var tag = this.tagName.toLowerCase();
			if (type == "text" || type == "password" || tag == "textarea")
				this.value = "";
			else if (type == "checkbox" || type == "radio")
				this.checked = false;
			else if (tag == "select")
				this.selectedIndex = -1;
		});
	};
	
})(jQuery);