(function($) {
	$.fn.EmailFriend = function(p_options) {
				
		drawWindow();
	}
	  
 	function drawWindow(){
		
		var browserWidth = ( jQuery.browser.msie ) ? document.documentElement.clientWidth : window.innerWidth;
		var browserHeight = ( jQuery.browser.msie ) ? document.documentElement.clientHeight : window.innerHeight;
		
		var windowOverlay = document.createElement("div");
			windowOverlay.id = "windowOverlay";
			windowOverlay.style.height = $(document).height() + "px";
			
		var tellFriendContainer = document.createElement("div");
			tellFriendContainer.id = "tellFriendContainer";
			
		var tmpTable = document.createElement("table");
			
		var tmpTableBody = document.createElement("tbody");
		
		// Create Title TR
		var titleRow = document.createElement("tr");
		var titleCell = document.createElement("td");
			titleCell.colSpan = "2";
			titleCell.className = "txtTitle";
			titleCell.appendChild(document.createTextNode("Email this article to my friend"));
			
			titleRow.appendChild(titleCell);
			tmpTableBody.appendChild(titleRow);

		// Create Close TR
		var closeRow = document.createElement("tr");
		var closeCell = document.createElement("td");
			closeCell.colSpan = "2";
		
		var closeLink = document.createElement("a");
			closeLink.id = "closeTellFriendWindow";
		
		var closeImg = document.createElement("img");
			closeImg.src = "/img/magazine/emailFriend/x.png";
			closeImg.width = 39;
			closeImg.height = 39;
			closeImg.className = "imgClose";
			
			closeLink.appendChild(closeImg);
			closeCell.appendChild(closeLink);
		
			closeRow.appendChild(closeCell);
			tmpTableBody.appendChild(closeRow);

		// Create Your Name TR
		var uNameRow = document.createElement("tr");
		
		var uNameLabelCell = document.createElement("td");
			uNameLabelCell.className = "inputLabel yourNameRow";
			uNameLabelCell.appendChild(document.createTextNode("Your Name"));
			
			uNameRow.appendChild(uNameLabelCell);
		
		var uNameInputCell = document.createElement("td");
			uNameInputCell.className = "inputCell yourNameRow";
			
		var uNameInput = document.createElement("input");
			uNameInput.id = "userName";
			
			uNameInputCell.appendChild(uNameInput);
			uNameRow.appendChild(uNameInputCell);
			
			tmpTableBody.appendChild(uNameRow);
		
		// Create Your Email Address TR
		var uEmailRow = document.createElement("tr");
		
		var uEmailLabelCell = document.createElement("td");
			uEmailLabelCell.className = "inputLabel";
			uEmailLabelCell.appendChild(document.createTextNode("Your Email Address"));
			
			uEmailRow.appendChild(uEmailLabelCell);
		
		var uEmailInputCell = document.createElement("td");
			uEmailInputCell.className = "inputCell";
			
		var uEmailInput = document.createElement("input");
			uEmailInput.id = "userEmail";
			
			uEmailInputCell.appendChild(uEmailInput);
			uEmailRow.appendChild(uEmailInputCell);
			
			tmpTableBody.appendChild(uEmailRow);
			
		// Create Friends Email Address
		var fEmailRow = document.createElement("tr");
		
		var fEmailLabelCell = document.createElement("td");
			fEmailLabelCell.className = "inputLabel";
			fEmailLabelCell.appendChild(document.createTextNode("Friends Email Address"));
			
			fEmailRow.appendChild(fEmailLabelCell);
		
		var fEmailInputCell = document.createElement("td");
			fEmailInputCell.className = "inputCell";
			
		var fEmailInput = document.createElement("input");
			fEmailInput.id = "friendEmail";
			
			fEmailInputCell.appendChild(fEmailInput);
			fEmailRow.appendChild(fEmailInputCell);
			
			tmpTableBody.appendChild(fEmailRow);
			
		// Create Message Text Area
		var msgRow = document.createElement("tr");
		
		var msgLabelCell = document.createElement("td");
			msgLabelCell.className = "inputLabel";
			msgLabelCell.appendChild(document.createTextNode("Message"));
			
			msgRow.appendChild(msgLabelCell);
		
		var msgInputCell = document.createElement("td");
			msgInputCell.className = "inputCell";
		var msgInput = document.createElement("textarea");
			msgInput.id = "userMsg";
			
			msgInputCell.appendChild(msgInput);
			msgRow.appendChild(msgInputCell);
			
			tmpTableBody.appendChild(msgRow);
		
		// Include in Email
		var includeEmailRow = document.createElement("tr");
				
		var includeEmailCell = document.createElement("td");
			includeEmailCell.style.textAlign = "right";
			includeEmailCell.style.paddingTop = "5px";
			includeEmailCell.style.paddingRight = "25px";
			includeEmailCell.colSpan = "2";
			
		var checkBox = document.createElement("input");
			checkBox.className = "checkbox";
			checkBox.type = "checkbox";
			checkBox.id = "includeEmail";
				
			includeEmailCell.appendChild(document.createTextNode("Include me in email"));
			includeEmailCell.appendChild(checkBox);
			
			includeEmailRow.appendChild(includeEmailCell);
			
			tmpTableBody.appendChild(includeEmailRow);
		
		// Include Submit Button
		var submitRow = document.createElement("tr");
		var submitCell = document.createElement("td")
			submitCell.colSpan = "2";
			submitCell.style.paddingTop = "10px";
		
			submitRow.appendChild(submitCell);
		
		var submitImg = document.createElement("img");
			submitImg.src = "../../img/magazine/emailFriend/sendArticle.png";
			submitImg.width = 149;
			submitImg.height = 34;
			
			submitImg.className = "btnSubmit";
		
		var submitLink = document.createElement("a");
			submitLink.id = "sendMail";
			//submitLink.href = "#";
			
			submitLink.appendChild(submitImg);
			submitCell.appendChild(submitLink);
		
			submitRow.appendChild(submitCell);
		
			tmpTableBody.appendChild(submitRow);				
				
		tmpTable.appendChild(tmpTableBody);
		tellFriendContainer.appendChild(tmpTable);
		
		var bkgImg = document.createElement("img");
			bkgImg.src = "/img/magazine/emailFriend/tellFriendBkg.png";
			
		tellFriendContainer.appendChild(bkgImg);
			
		document.body.appendChild(windowOverlay);
		document.body.appendChild(tellFriendContainer);


		// Position Container Element : Center According To Browser Width/Height	
		document.getElementById("tellFriendContainer").style.left = parseInt((browserWidth / 2) - (473/2)) + "px";
		document.getElementById("tellFriendContainer").style.top = parseInt((browserHeight / 2) - (320 / 2)) + "px";
		
		$("#closeTellFriendWindow").click(function(event){
			closeWindow();
		});
		
		$("#sendMail").click(function(event){
			sendEmail();
		});
		
		// Check For IE6 To Scroll
		if(jQuery.browser.msie && jQuery.browser.version == "6.0"){
			window.scroll(0, 0);
			$.getScript("/js/pngfix.js");
		}
		
		document.getElementById("userName").focus();
	}
	
	function sendEmail(){
				
		if($("#userName").val().length <= 1){
			$("#userName").css("background-color", "red");
			alert("Please enter your name");
			return false;
		}else{
			$("#userName").css("background-color", "#FFF");
		}
		
		if(!$.isEmail($("#userEmail").val())){
			$("#userEmail").css("background-color", "red");
			return false;
		}else{
			$("#userEmail").css("background-color", "#FFF");
		}
		
		if(!$.isEmail($("#friendEmail").val())){
			$("#friendEmail").css("background-color", "red");
			return false;
		}else{
			$("#friendEmail").css("background-color", "#FFF");
		}
		
		initRequest($("#userName").val(), $("#userEmail").val(), $("#friendEmail").val(), $("#userMsg").val(), $("#includeEmail").attr("checked"));	
	}
	
	function initRequest(p_userName, p_userEmail, p_friendsEmail, p_msg, p_include){
		
		var urlArray = document.location.href.split("/");
				
		var fileName = urlArray[urlArray.length - 1];
		var articleType = urlArray[urlArray.length - 2];
		
		var postData = "userName=" + p_userName;
			postData += "&userEmail=" + p_userEmail;
			postData += "&friendEmail=" + p_friendsEmail;
			postData += "&userMsg=" + p_msg;
			postData += "&include=" + p_include;
			postData += "&fileName=" + fileName;
			postData += "&articleType=" + articleType;
	
		$.ajax({
			type: "POST",
			url: "/magazine/emailFriend/send.aspx",
			dataType:"xml",
			data: postData,
			success: function(xml){
				$("#tellFriendContainer").slideUp("normal", showComplete);
			},
			   
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert("Error -> " + textStatus + " : " + errorThrown)
			}
		});

		

	}
	
	function showComplete(){
	
		$("#tellFriendContainer").empty();
		$("#tellFriendContainer").css("background", "url('/img/magazine/emailFriend/messageSent.png') no-repeat");
	
		var tmpTable = document.createElement("table");
		var tmpTableBody = document.createElement("tbody");
		
		// Create Close TR
		var closeRow = document.createElement("tr");
		var closeCell = document.createElement("td");
			closeCell.colSpan = "2";
		
		var closeLink = document.createElement("a");
			closeLink.id = "closeTellFriendWindow";
		
		var closeImg = document.createElement("img");
			closeImg.src = "/img/magazine/emailFriend/x.png";
			closeImg.width = 39;
			closeImg.height = 39;
			closeImg.className = "imgClose";
			
			closeLink.appendChild(closeImg);
			closeCell.appendChild(closeLink);
		
			closeRow.appendChild(closeCell);
			tmpTableBody.appendChild(closeRow);
		
		// Create Title TR
		var titleRow = document.createElement("tr");
		var titleCell = document.createElement("td");
			titleCell.className = "txtTitleResponse";
			titleCell.appendChild(document.createTextNode("Your message has been sent"));
			
			titleRow.appendChild(titleCell);
			tmpTableBody.appendChild(titleRow);
				
			//tmpTableBody.appendChild(submitRow);				
				
		tmpTable.appendChild(tmpTableBody);
		document.getElementById("tellFriendContainer").appendChild(tmpTable);
	
		$("#closeTellFriendWindow").click(function(event){
			closeWindow();
		});
	
		$("#tellFriendContainer").slideDown();
	
	}
		
	function getScrollXY() {
		var scrOfX = 0, scrOfY = 0;
		
		if(jQuery.browser.msie){
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		else if(jQuery.browser.mozilla){
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		}
		else{
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		}		
		return [ scrOfX, scrOfY ];
	}
		
	function closeWindow(){

		m_startWidth = 10;		// Default For Loading Status
		m_startHeight = 10;		// Default For Loading Status

		m_endWidth = 400;		// Default For Loading Status
		m_endHeight = 150;		// Default For Loading Status
		
		m_imgLoad = null;
		m_interval = null;
		
		m_mediaType = null;
		m_options = null;
		
		document.body.removeChild(document.getElementById("tellFriendContainer"))
		document.body.removeChild(document.getElementById("windowOverlay"))
	}
})(jQuery);