/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		buildPopup();
		centerPopup();
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
		//CLOSING POPUP
		//Click the x event!
		$("#popupContactClose").click(function(){
			disablePopup();
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
			disablePopup();
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#addthis").click(function(){
		//load popup
		loadPopup();
		$("#tellafriendform-submit").click(function(){
			processTellAFriend();
		});
		return false;
	});				
});

function processTellAFriend(){
	var your_name = $("#tellafriendform-name").val();
	var friends_name = $("#tellafriendform-friendsname").val();
	var friends_email = $("#tellafriendform-friendsemail").val();
	var your_message = $("#tellafriendform-message").val();
	
	var validated = true;
	if(your_name == ''){
		validated = false;
		$("#tellafriendform-name").addClass("tellafriendform-error");
	} else {
		$("#tellafriendform-name").removeClass("tellafriendform-error");
	}
	if(friends_name == ''){
		validated = false;
		$("#tellafriendform-friendsname").addClass("tellafriendform-error");
	} else {
		$("#tellafriendform-friendsname").removeClass("tellafriendform-error");
	}
	if(friends_email == ''){
		validated = false;
		$("#tellafriendform-friendsemail").addClass("tellafriendform-error");
	} else {
		if((friends_email.indexOf(".") > 2) && (friends_email.indexOf("@") > 0)){
			$("#tellafriendform-friendsemail").removeClass("tellafriendform-error");
		} else {
			validated = false;
			$("#tellafriendform-friendsemail").addClass("tellafriendform-error");
		}		
	}
	if(your_message == ''){
		validated = false;
		$("#tellafriendform-message").addClass("tellafriendform-error");
	} else {
		$("#tellafriendform-message").removeClass("tellafriendform-error");
	}	
	
	if(validated) {
		$.post("/public/ajax/tell-a-friend/", { name: your_name, friendsname: friends_name, friendsemail: friends_email, message: your_message, page_url: document.location.href } );
		$("#tellafriendform").empty();
		$("#tellafriendform").append("Thanks. We've forwarded your message to your friend.");
	}
}

function buildPopup(){
	$("#popupContact").remove();
	$("#backgroundPopup").remove();
	var popHTML = '';
	popHTML += '<div id="popupContact">';
	popHTML += '<a id="popupContactClose">x</a>';
	popHTML += '<h1>Bookmark This Page...</h1>';
	popHTML += '<p>Click a bookmarking service to submit this URL.</p>';
	popHTML += '<div class="social-icons">';
	popHTML += '<ul>';
	popHTML += '<li><a target="_blank" href="http://www.facebook.com/share.php?u='+document.location.href+'"><img src="/public/images/social-icons/facebook.gif" alt="Facebook" /></a></li>';
	popHTML += '<li><a target="_blank" href="http://del.icio.us/post?url='+document.location.href+'"><img src="/public/images/social-icons/delicious.png" alt="Delicious" /></a></li>';
	popHTML += '<li><a target="_blank" href="http://www.digg.com/submit?phase=2&url='+document.location.href+'"><img src="/public/images/social-icons/digg.png" alt="Digg" /></a></li>';
	popHTML += '<li><a target="_blank" href="http://www.furl.net/storeIt.jsp?u='+document.location.href+'"><img src="/public/images/social-icons/furl.png" alt="Furl" /></a></li>';
	popHTML += '<li><a target="_blank" href="http://www.newsvine.com/_tools/seed&save?u='+document.location.href+'"><img src="/public/images/social-icons/newsvine.gif" alt="Newsvine" /></a></li>';
	popHTML += '<li><a target="_blank" href="http://www.stumbleupon.com/submit?url='+document.location.href+'"><img src="/public/images/social-icons/stumbleupon.gif" alt="Stumble Upon" /></a></li>';
	popHTML += '<li><a target="_blank" href="http://reddit.com/submit?url='+document.location.href+'"><img src="/public/images/social-icons/reddit.gif" alt="Reddit" /></a></li>';
	popHTML += '</ul>';
	popHTML += '</div>';
	popHTML += '<h1>OR, TELL A FRIEND ABOUT IT...</h1>';
	popHTML += '<p>Enter your name, your friends name & email address and a short message and we\'ll do the rest!</p>';
	popHTML += '<div id="tellafriendform">';
	popHTML += '<div class="tellafriendform-row">';
	popHTML += '<div class="tellafriendform-label">Your Name:</div>';
	popHTML += '<div class="tellafriendform-field"><input class="tellafriend-text" type="text" id="tellafriendform-name" name="tellafriendform-name" value=""></div>';
	popHTML += '</div>';
	popHTML += '<div class="tellafriendform-row">';
	popHTML += '<div class="tellafriendform-label">Friends Name:</div>';
	popHTML += '<div class="tellafriendform-field"><input class="tellafriend-text" type="text" id="tellafriendform-friendsname" name="tellafriendform-friendsname" value=""></div>';
	popHTML += '</div>';
	popHTML += '<div class="tellafriendform-row">';
	popHTML += '<div class="tellafriendform-label">Friends Email:</div>';
	popHTML += '<div class="tellafriendform-field"><input class="tellafriend-text" type="text" id="tellafriendform-friendsemail" name="tellafriendform-friendsemail" value=""></div>';
	popHTML += '</div>';
	popHTML += '<div class="tellafriendform-row">';
	popHTML += '<div class="tellafriendform-label">Message:</div>';
	popHTML += '<div class="tellafriendform-field"><textarea id="tellafriendform-message" name="tellafriendform-message"></textarea></div>';
	popHTML += '</div>';
	popHTML += '<div class="tellafriendform-row">';
	popHTML += '<div class="tellafriendform-label">&nbsp;</div>';
	popHTML += '<div class="tellafriendform-field"><img id="tellafriendform-submit" src="/public/images/btn_send.gif" alt="Sumbit" /></div>';
	popHTML += '</div>';
	popHTML += '</div>';
	popHTML += '</div>';
	popHTML += '<div id="backgroundPopup"></div>';
	$("body").append(popHTML);
}