var superstart = function() {
	
	
	return {
	
		hide_anti_popup_elems: function(popup_id) {
				
			// hide elements that interfer with the popup
			$("select").css("visibility", "hidden");
			$("textarea").css("overflow", "hidden");
			
			// unhide elements within the popup
			$(popup_id+" select").css("visibility", "visible");
			$(popup_id+" textarea").css("overflow", "auto");
		
		},

		show_anti_popup_elems: function() {
		
			// hide elements that interfer with the popup
			$("select").css("visibility", "visible");
			$("textarea").css("overflow", "auto");
			
		},
		
		viewport: {
			// this function came from, http://www.reindel.com/five_javascript_tricks_jquery/
			o: function() {
				if (self.innerHeight) {
					this.pageYOffset = self.pageYOffset;
					this.pageXOffset = self.pageXOffset;
					this.innerHeight = self.innerHeight;
					this.innerWidth = self.innerWidth;
				} else if (document.documentElement && document.documentElement.clientHeight) {
					this.pageYOffset = document.documentElement.scrollTop;
					this.pageXOffset = document.documentElement.scrollLeft;
					this.innerHeight = document.documentElement.clientHeight;
					this.innerWidth = document.documentElement.clientWidth;
				} else if (document.body) {
					this.pageYOffset = document.body.scrollTop;
					this.pageXOffset = document.body.scrollLeft;
					this.innerHeight = document.body.clientHeight;
					this.innerWidth = document.body.clientWidth;
				}
				return this;
			}
		},
		
		position_dialog: function(elem) {

			// Math for putting the dialog in the middle vertically
			//Math.round(superstart.viewport.o().innerWidth/2) + superstart.viewport.o().pageXOffset - Math.round($(elem).width()/2)

			var distance_from_top = 140;
			$(elem).css("position", "absolute");
			$(elem).css("top", Math.round(superstart.viewport.o().pageYOffset)+distance_from_top);
			$(elem).css("left", Math.round(superstart.viewport.o().innerWidth/2) + superstart.viewport.o().pageXOffset - Math.round($(elem).width()/2));
			
			$(elem).show("normal");
			$(window).resize(function(){
				$(elem).css("top", Math.round(superstart.viewport.o().pageYOffset)+distance_from_top);
				$(elem).css("left", Math.round(superstart.viewport.o().innerWidth/2) + superstart.viewport.o().pageXOffset - Math.round($(elem).width()/2));
			});
			
			$(window).scroll(function(){
				$(elem).css("top", Math.round(superstart.viewport.o().pageYOffset)+distance_from_top);
			});
			
		},
		
		ajax_form_dialog: function(url, options) {
			
			if (url != undefined) {
				
				if (options == undefined) options = {};
			
				$.get(url, function(data) {
					options.html           = data;
					options.display_ok_btn = false;
					superstart.message_dialog(options);
				});
			
			}

			return false;			
		
		},
	
		message_dialog: function(options) {
		
			var html           = (options.html != undefined) ? '<div class="body">'+options.html+'</div>' : '';
			var display_ok_btn = (options.display_ok_btn != undefined) ? options.display_ok_btn : true;
			var ok_btn_action  = "superstart.hide_message_dialog();";
			var ok_btn_value   = "OK";
			var add_timer      = (options.close_time != undefined) ? true : false;
			var close_time     = (options.close_time != undefined) ? options.close_time : '';
			var msg_title      = (options.title != undefined) ? '<h2>'+options.title+'</h2>' : '';
			var btn_focus      = (options.btn_focus != undefined) ? true : false;
			var class_name     = (options.class_name != undefined) ? 'class="'+options.class_name+'"' : '';
			var callback       = (options.callback != undefined) ? options.callback : false;
			
			function display_message() {		
			
				superstart.hide_anti_popup_elems("#message-dialog");
				superstart.position_dialog("#message-dialog");
				if (callback != false) {
					callback();
				}
				
			}
		
			if ($("#message-dialog").length == 0) {
				
				win_height = $(document).height();
				button     = (display_ok_btn == true) ? '<div class="button">'+
				'<input class="btn btn1" id="message-dialog-btn" type="button" onclick="'+ok_btn_action+'" value="'+ok_btn_value+'" />'+
				'</div>' : '';
				
			
				message_div = '<div id="message-dialog"'+class_name+' style="display: none; z-index: 200;">'+msg_title+html+button+"</div>\n";
				$("body").prepend(message_div);
				dialog_background = '<div id="dialog-background" style="position: absolute; top: 0; left: 0; z-index: 100; background-color: #000000; width: 100%; height: '+win_height+'px; filter:alpha(opacity=75);-moz-opacity:0.75;opacity:0.75;"></div>';
				$("body").prepend(dialog_background);
				display_message();
				
				if (btn_focus == true) {
					$("#message-dialog-btn").focus();
				}
				
				if (add_timer == true) {
					setTimeout(function(){ $('#message-dialog').hide('normal', function() { $('#message-dialog').remove(); $('#dialog-background').remove(); }); }, close_time);
				}
				
			}
			
		},
		
		hide_message_dialog: function() {
			
			$('#message-dialog').hide('normal', function() { $('#message-dialog').remove(); $('#dialog-background').remove(); superstart.show_anti_popup_elems(); });
			
		},
		
		/**
		 * This function displays a confirm dialog for users to confirm actions
		 * 
		 * @param array options - [confirm_action, cancel_action, confirm_title, confirm_body, btn1_value, btn2_value]
		 */	
		confirm: function(options) {

			function display_dialog() {
				
				superstart.hide_anti_popup_elems("#confim-dialog");
				superstart.position_dialog("#confim-dialog");

			}
			
			//btn1_action = "window.location = '"+options[0]+"'";
			btn1_action = options[0];
			if (typeof options[0] == "function") {
				btn1_action = options[0];
			}				
			if (options[1] == undefined || options[1] == "") {
				options[1] = "$('#confim-dialog').hide('normal', function() { $('#confim-dialog').remove(); $('#dialog-background').remove(); });";
			}
			if (options[4] == undefined || options[4] == "") {
				options[4] = "Submit";
			}
			if (options[5] == undefined || options[5] == "") {
				options[5] = "Cancel";
			}
			
			confirm_dialog = ''+
			"\t<h2>"+options[2]+"</h2>\n"+
			"\t"+'<div class="body">'+"\n"+
			"\t\t"+options[3]+"\n"+
			"\t</div>\n"+
			"\t"+'<div class="buttons">'+"\n"+
			"\t\t"+'<input class="btn btn1" type="button" onclick="'+btn1_action+'" value="'+options[4]+'" />'+"\n"+
			"\t\t"+'<input class="alt-btn btn2" type="button" onclick="'+options[1]+'" value="'+options[5]+'" />'+"\n"+
			"\t</div>\n";
			
			
			if ($("#confim-dialog").length == 0) {
				confirm_dialog = '<div id="confim-dialog" style="display: none; z-index: 200;">'+"\n"+confirm_dialog+"</div>\n";
				win_height     = $(document).height();
				$("body").prepend(confirm_dialog);
				dialog_background = '<div id="dialog-background" style="position: absolute; top: 0; left: 0; z-index: 100; background-color: #000000; width: 100%; height: '+win_height+'px; filter:alpha(opacity=15);-moz-opacity:0.15;opacity:0.15;"></div>';
				$("body").prepend(dialog_background);
				display_dialog();
			}
			return false;

		}		
	
	}
	
}();