/*
	jMailer jQuery plugin version 1.1
	
	Copyright (c) 2009 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", "subject", "message"],
	
	//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:"Hi, check out this great page I found: ",
			shim:(ie6 == true) ? true : false,
			forceShim:false,
			defaultClass:"mailer-container",
			additionalClass:null,
			suppressTo:false,
			animation: "hide"
		}
	};
  
	//extend jquery with the plugin
	$.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 to create the widget
	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(config.defaultClass).css({backgroundColor:"#cccccc", padding:"7px 5px 10px 0", display:"none", position:"absolute", zIndex:"99999", width:"300px"}).appendTo($("body"));
		
		//add additonal class if configured
		(config.additionalClass != null) ? $("#" + config.id).addClass(config.additonalClass) : null ;
		
		//create mailer form
		$("<fieldset>").attr({id:"mailerForm"}).css({border:0,padding:0}).appendTo("#" + config.id);
		
		//add default fields to form
		$.each(fields, function() {
			if (this == "to") {
				if (config.suppressTo != true) {
					$("<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);
					$("<input>").attr("id", this + "_field").css({width:"200px", fontFamily:"verdana", fontSize:"11px"}).appendTo($("#" + this));
				}
			} else {
				$("<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>").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() {
			
			//determine animation
			if (config.animation == "hide") {
				$("#" + config.id).hide("slow", function() { tidyUp() });
			} else if (config.animation == "slide") {
				$("#" + config.id).slideUp("slow", function() { tidyUp() });
			} else if (config.animation == "fade") {
				$("#" + config.id).fadeOut("slow", function() { tidyUp() });
			} else {
				$("#" + config.id).remove();
				tidyUp();
			}			
		});
		
		//remove plugin elements
			function tidyUp() {
				(ie6 == true) ? $("#shim").remove() : null ;
				$("#overlay").remove();
			};
		
		//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) ;
		});
	};
	
	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:"100%", width:"100%", position:"fixed", top:0, left:0}).insertBefore("#" + config.id) : null ;
		
		//create shim if enabled
		(config.shim == true || config.forceShim == 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 ;
		
		//viewport dimensions
		var vDim = {
			w:(!window.innerWidth) ? document.documentElement.offsetWidth : window.innerWidth,
			h:(!window.innerHeight) ? document.documentElement.offsetHeight : window.innerHeight
		};
		
		//has the visitor scrolled
		var sDim = {
			x:(document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft,
			y:(document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop
		};
		
		//set mailer position
		var left = sDim.x + (vDim.w - $("#" + config.id).width()) / 2;
		var top = sDim.y + (vDim.h - $("#" + 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", paddingBottom:"25px"}).insertBefore("#mailerForm");
		
		//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();
		});
	};
		
})(jQuery);