/*
	jMailer jQuery plugin version 1
	
	Copyright (c) 2008 Dan Wellman
  
	Dual licensed under the MIT and GPL licenses:
  http://www.opensource.org/licenses/mit-license.php
  http://www.gnu.org/licenses/gpl.html
	
*/

(function($) {
	  
	//url of current page
	var pageURL = window.location.href,
	
	//fields
	fields = ["To", "From", "Your_Name", "message"],
	 
	//viewport dimensions
	vDim = {
		width:(!window.innerWidth) ? document.documentElement.offsetWidth : window.innerWidth ,
		height:(!window.innerHeight) ? document.documentElement.offsetHeight : window.innerHeight
	},
	
	//detect ie6
	ie6 = ($.browser.msie == true && $.browser.version == "6.0") ? true : false;
	
	//define jMailer object with some default properties
	$.jMailer = {
		defaults: {
			modal:true,
			id:"mailer",
			message:"I thought you might be interested in this.  I found this page on Loan Rescue Programs.  They seem to be able to help a lot of people with foreclosure.  ",
			buttonImages:false,
			shim:(ie6 == true) ? true : false
		}
	};
  
	$.fn.extend({
		jMailer:function(config) {
																
			//use defaults or properties supplied by user
			config = $.extend({}, $.jMailer.defaults, config);
			
			//get href of link to post data to
			url = this.attr("href");
			
			//create mailer and add to dom
			createMailer(config, url);
						
			//add click handler
			this.click(function(e){
				
				//stop link following href
				e.preventDefault();
				
				//show mailer
				show(config);
			});
			
			//return the jquery object for chaining
			return this;
		}
  });		
		
	function createMailer(config, url) {
		
		//normalize body if ie6
		(ie6 == true) ? $("body").css({padding:"0", margin:"0"}).width("100%").height("100%") : null ;
		
		//create mailer div
	  $("<div>").attr("id", config.id).addClass("mailerDiv").css({backgroundColor:"#cccccc", padding:"10px 5px 30px 0", display:"none", position:"absolute", zIndex:"99999", width:"300px"}).appendTo($("body"));
			
		//create mailer form
		$("<fieldset>").attr({id:"mailerForm"}).css({border:0,padding:0}).appendTo("#" + config.id);
		
		//add default fields to form
		$.each(fields, function() {
			$("<div>").attr("id", this).css({padding:"3px 0"}).appendTo("#mailerForm");
			$("<div>").addClass("label").text(this + ":").css({float:"left", width:"80px", textAlign:"right", marginRight:"10px", fontFamily:"Verdana", fontSize:"12px", paddingTop:"2px"}).appendTo("#" + this);
			(this != "message") ? $("<input>").attr("id", this + "_field").css({width:"200px", fontFamily:"verdana", fontSize:"11px"}).appendTo($("#" + this)) : $("<textarea rows=7>").attr("id", this + "_field").text(config.message).css({width:"196px", fontFamily:"Verdana", fontSize:"11px"}).appendTo("#" + this) ;			
		});
		
		(ie6 == true) ? $(".label").css({marginRight:"5px"}) : null ; 
		
		//add cancel button
		$("<button>").attr({id:"cancel", title:"Cancel"}).text("Cancel").css({height:"32px", textAlign:"center", width:"60px", color:"#000000", fontWeight:"bold", fontFamily:"verdana", fontSize:"12px", padding:"0 5px", float:"right", curosr:"pointer", position:"relative", left:"-5px"}).appendTo("#" + config.id).click(function() {
						
			//remove plugin elements
			$("#" + config.id).hide("slow");
			(ie6 == true) ? $("#shim").remove() : null ;
			$("#overlay").remove();
		});
		
		//are images included
		(config.buttonImages != false) ? $("#cancel").css({background:config.buttonImages.cancel, border:"1px solid #990000", color:"#ffffff"}).hover(function() {
		  $(this).css({background:config.buttonImages.cancelOver}) 
		 },function() {
			$(this).css({background:config.buttonImages.cancel})
		}) : null ;
		
		//add send button
		$("<button>").attr({id:"send", title:"Send"}).text("Send!").css({height:"32px", textAlign:"center", width:"60px", color:"#000000", fontWeight:"bold", fontFamily:"verdana", fontSize:"12px", cursor:"pointer", float:"right", position:"relative", left:"-10px"}).appendTo("#" + config.id).click(function() {
			
			//remove error message and styling if present
			(!$(".errorMsg")) ? null : $(".errorMsg").remove();
			$("#" + config.id + " input").each(function(){
				$(this).css({border:"1px solid #abadb3"});
				$(this).prev().css({color:"#000000"});
			});
			
			//get value of fields into data object
			var formData = {};
			formData.pageURL = pageURL;
			var errors = 0;
			$("#" + config.id + " input").each(function(){
				
				//check fields not empty
				if ($(this).val() == "") {
					
					//show failed validation
				  $(this).css({border:"1px solid #ff0000"});
					$(this).prev().css({color:"#ff0000"});
					errors += 1;
				} else {
					
					//populate data object
					formData[$(this).parent().attr("id")] = $(this).val();
				}
			});
			
			$("#" + config.id + " textarea").each(function(){
				 
				//check textarea not empty
				if ($(this).val() == "") {
					
					//show failed validation
				  $(this).css({border:"1px solid #ff0000"});
					$(this).prev().css({color:"#ff0000"});
					errors += 1;
				} else {
					
					//populate data object
					formData[$(this).parent().attr("id")] = $(this).val();
				}
			});
			
			//make request if no errors
			(errors == 0) ? $.post(url, formData, processResponse) : $("<div>").addClass("errorMsg").text("Please complete all fields").css({height:"17px", padding:"3px 0 0 20px", fontSize:"10px", fontWeight:"bold", float:"right", fontFamily:"verdana", position:"relative", left:"-14px", top:"6px", color:"#ff0000"}).appendTo("#" + config.id) ;
		});
		
		//are images included
		(config.buttonImages != false) ? $("#send").css({background:config.buttonImages.send, border:"1px solid #060f8a", color:"#ffffff"}).hover(function() {
		  $(this).css({background:config.buttonImages.sendOver}) 
		 },function() {
			$(this).css({background:config.buttonImages.send})
		}) : null ;
	};
	
	function show(config) {
		
		//create modal if set to true
		(config.modal == true) ? $("<div>").attr("id", "overlay").css({opacity:0.7, backgroundColor:"#000000", zIndex:99990, height:$("body").height(), width:"100%", position:"fixed", top:0, left:0}).insertBefore("#" + config.id) : null ;
		
		//create shim if enabled
		(config.shim == true) ? $("<iframe>").attr({src:"", frameBorder:"0", scrolling:"no", id:"shim"}).css({display:"block", zIndex:99989, opacity:0, height:$("body").height(), width:"100%", position:"absolute", top:0, left:0}).insertBefore("#overlay") : null ;
		
		//set mailer position
		var left = vDim.width / 2 - $("#" + config.id).width() / 2;
		var top = vDim.height / 2 - $("#" + config.id).height() / 2;
		
		//remove error message and styling if present
		(!$(".errorMsg")) ? null : $(".errorMsg").remove();
			$("#" + config.id + " input").each(function(){
				$(this).css({border:"1px solid #abadb3"});
				$(this).prev().css({color:"#000000"});
			});
		
		//show mailer
		$("#" + config.id).css({left:left, top:top}).show("slow");
	};
	
	function processResponse(data) {
	  
	  //hide form elements
		$("#mailerForm").hide("fast");
		$("#send").hide("fast");

    //change cancel button text
		$("#cancel").text("Close").attr("title", "Close");
		
		//add message
		$("<div>").attr("id", "confirm").text(data).css({width:"100%", fontSize:"15px", textAlign:"center", position:"relative", top:"16px", fontWeight:"bold", fontFamily:"Verdana", marginTop:"32px", paddingBottom:"25px"}).appendTo(".mailerDiv");
		
		//add new button
		$("<button>").attr({id:"back", title:"Go Back"}).text("Back to Form").css({height:"32px", textAlign:"center", width:"112px", color:"#000000", fontWeight:"bold", fontFamily:"verdana", fontSize:"12px", cursor:"pointer", float:"right", position:"relative", left:"-10px"}).insertAfter("#cancel").click(function() {
			
			//show form elements again
			$("#confirm").remove();
			$("#mailerForm").show();
			$("#send").show();
			$(this).remove();
		});
		
		//add background image for button if configured
		var over = ($("#send").css("background") == "") ? null : $("#send").css("background").split(".")[0] + "_over." + $("#send").css("background").split(".")[1] ;
		($("#send").css("background") == "") ? null : $("#back").css({background:$("#send").css("background"), color:"#ffffff", border:"1px solid #060f8a"}).hover(function() {
			$(this).css({background:over}); 
		},function() {
			$(this).css({background:$("#send").css("background")});
		});
	};
		
})(jQuery);