var weamplify = 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, options) {

			// Math for putting the dialog in the middle vertically
			//Math.round(weamplify.viewport.o().innerWidth/2) + weamplify.viewport.o().pageXOffset - Math.round($(elem).width()/2)
			
			if (options == undefined) options = {};
			pin_to_top = (options.pin_to_top != undefined) ? options.pin_to_top : false;

			var distance_from_top = 140;
			$(elem).css("position", "absolute");
			$(elem).css("top", Math.round(weamplify.viewport.o().pageYOffset)+distance_from_top);
			$(elem).css("left", Math.round(weamplify.viewport.o().innerWidth/2) + weamplify.viewport.o().pageXOffset - Math.round($(elem).width()/2));
			
			$(elem).show("normal");
			$(window).resize(function(){
				$(elem).css("top", Math.round(weamplify.viewport.o().pageYOffset)+distance_from_top);
				$(elem).css("left", Math.round(weamplify.viewport.o().innerWidth/2) + weamplify.viewport.o().pageXOffset - Math.round($(elem).width()/2));
			});
			
			$(window).scroll(function(){
				if (pin_to_top != true) {
					$(elem).css("top", Math.round(weamplify.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;
					weamplify.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  = "weamplify.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 pin_to_top     = (options.pin_to_top != undefined) ? options.pin_to_top : false;
			
			function display_message() {		

				weamplify.hide_anti_popup_elems("#message-dialog");
				weamplify.position_dialog("#message-dialog", { pin_to_top: pin_to_top });
				
			}
		
			if ($("#message-dialog").length == 0) {
				
				win_height = $(document).height();
				win_height = win_height+200;
				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" 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=60);-moz-opacity:0.6;opacity:0.6;"></div>';
				$("body").prepend(dialog_background);
				
				this.position_dialog("#message-dialog", { pin_to_top: true });			
				
				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(); weamplify.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() {
				
				weamplify.hide_anti_popup_elems("#confim-dialog");
				weamplify.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=60);-moz-opacity:0.60;opacity:0.60;"></div>';
				$("body").prepend(dialog_background);
				display_dialog();
			}
			return false;

		},
		
		update_status: function(status, status_target, message_id, timeout) {
			
			var status_target = (!status_target || typeof status_target === 'undefined') ? "#content-container" : status_target;
			var message_id    = (!message_id || typeof message_id === 'undefined') ? "status-update" : message_id;
			var timeout       = (!timeout || typeof timeout === 'undefined') ? 3000 : timeout;

			var status_message = ''+
			'<div id="'+message_id+'" style="display: none;">'+"\n"+
			"\t"+status+"\n"+
			'</div>'+"\n";
			var timeout_action = function() {
				$("#"+message_id).fadeOut("slow", function() { $("#"+message_id).remove(); });
			}
			
			if ($("#"+message_id).length == 0) {
				$(status_target).prepend(status_message);
			} else {
				$("#"+message_id).html(status_message);
			}
			scroll(0,0);
			$("#"+message_id).show("normal", function() {
				setTimeout(timeout_action, timeout);
			});
		
		},
		
		display_error: function(error, error_target) {
			var status_target = (!error_target || typeof error_target === 'undefined') ? "#content-container" : error_target;
			weamplify.update_status(message, error_target, 'status-error', 5000);
		},
		
		accept_friend_request: function(url, friend, request_container, status_target) {
		
			var status_target = (status_target == undefined) ? "#one-col-container" : status_target;
			
			$.getJSON(url, function() {
				$(request_container).remove();
				weamplify.update_status(friend+" was added to your friends list.", status_target);
				if ($("div.friend").length == 0) {
					$("#accept-all-friends").remove();
				}
			});
			
			return false;
		
		},
		
		add_friend: function(link_elem, add_url, first) {
			
			weamplify.add_friend.link_elem = link_elem;
			weamplify.confirm([ '$.get(\''+add_url+'\', function() { weamplify.add_friend_callback(\''+ first +'\'); } ); return false;', '', 'Add '+ first +' as a friend?', '<p>You are about to add '+ first +' as a friend? We will then notify '+ first +', who will have to confirm that you are friends.</p>', 'Add Friend']); return false;
		
		},
		
		add_friend_callback: function(first_name) {
			
			var link_elem = weamplify.add_friend.link_elem;
			
			$("#confim-dialog").fadeOut("slow", function() {
				if (link_elem != undefined) {
					$(link_elem).replaceWith('<div>Friend Request Sent</div>');
				}
				$("#confim-dialog").remove();
				$('#dialog-background').remove();
				weamplify.update_status("A friend request has been sent to "+first_name+" for approval.", "#one-col-container");
			});
		},
		
		pf: function(add_url, first, gender, user_id) {
			
			var him_her = (gender == "m") ? 'him' : 'her';
			var his_her = (gender == "m") ? 'his' : 'her';
			var he_she  = (gender == "m") ? 'he' : 'she';
			weamplify.confirm([ '$.get(\''+add_url+'\', function() { weamplify.pf_callback(\''+first+'\', \''+ gender +'\'); } ); return false;', '', 'Pray for '+ first, '<p>Say a quick prayer for '+ first +'.  Pray that God would use '+ him_her +' and provide opportunities to serve Him.  Also pray for any struggles that you know of in '+ his_her +' life.</p><p>After you\'re done praying, click the "I Prayed For '+first+'" button, and we will send '+ him_her +'  an email notification that you prayed, so that '+ he_she +' can be encouraged.</p>', 'I Prayed for '+first]); return false;
		
		},
		
		pf_callback: function(data, text_status, first, gender, status_target) {
		
			var him_her = (gender == "m") ? 'him' : 'her';
			
			switch(data.pf_notification) {
				case "error_sending_email":
					error   = true;
					message = "There was an error sending your prayer notification. Please try again later.";
				break;
				case "unable_to_retrieve_notification":
					error   = true;
					message = "There was an error retrieving your prayer notification and your notification was not sent.";
				break;
				case "user_notification_turned_off":
					error   = true;
					message = first+" has prayer notifications turned off and will not receive a notification.";
				break;
				default:
					error   = false;
					message = "We sent a notification to "+first+" letting "+ him_her +" know you prayed for "+ him_her +".";
				break;
			}
			
			
			$("#message-dialog").fadeOut("slow", function() {
				$("#message-dialog").remove();
				$('#dialog-background').remove();
				if (error == false) {
					weamplify.update_status(message, status_target);
				} else {
					weamplify.display_error(message, status_target);
				}
			});

		}
		
	
	}
	
}();