jQuery(document).ready(function(){ 
	
	//Find all link elements and add an onfocus attribute and value
	function hideFocusBorders(){
		if (!document.getElementsByTagName) return false;
		var theahrefs = document.getElementsByTagName("a");
		if (!theahrefs){return;}
		for(var x=0;x!=theahrefs.length;x++){
		theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
		}
	}

	// Newsletter signup input
	jQuery(".newsletterInput").click(function(){
		if (jQuery(this).val() == 'YOUR EMAIL HERE...') jQuery('.newsletterInput').val('');
	});
	jQuery("#s").click(function(){
		if (jQuery(this).val() == 'SEARCH') jQuery('#s').val('');
	});
	

	jQuery("#buttonsGuide").hover(function(){
	    	jQuery('#buttonsGuide ul').show();
	    }, 
	    function () {
	    	jQuery('#buttonsGuide ul').hide();
	    });
		
	function toggleSidebar(){
		//toggle classes and hide the sidebar    
		jQuery("#top #signupBar").toggle();
	}
	
	//if the 'PINQ_SIGNUPBAR_CLOSED' is set to '1', then hide the sidebar on page load...
	if (jQuery.cookie("PINQ_SIGNUPBAR_CLOSED") != 1) toggleSidebar();

	// Validation and error handling for Subscribe header
	jQuery("#signupForm").validate({
		success: function(html){
			/* Toggle the signup bar on a successful submit */
			if (jQuery.cookie("PINQ_SIGNUPBAR_CLOSED") != 1) { 
				jQuery.cookie("PINQ_SIGNUPBAR_CLOSED", 1, { path: '/', expires: 9999 }); //cookie set to expire in 9999 days...
			}
		}
	});
		
	// Validation and error handling for Email Sharing sidebar 	
	jQuery("#shareForm").validate({
	   submitHandler: function(form) {
			var shareURI = document.getElementById("shareURI").value;
			var shareTitle = document.getElementById("shareTitle").value;
			var webPermalink = document.getElementById("webPermalink").value;
			var yourEmail = document.getElementById("yourEmail").value;
	 		var yourFriendsName = document.getElementById("yourFriendsName").value;
			var yourFriendsEmail = document.getElementById("yourFriendsEmail").value;
		
			jQuery.ajax({
				type: "POST",
				url: "http://pinq.ca/wordpress/wp-content/themes/pinq/mailer.php",
				data: 		
						"shareURI=" + shareURI +
						"&shareTitle=" + shareTitle +
						"&webPermalink=" + webPermalink +
						"&sendersEmail=" + yourEmail +
				 		"&recipientName=" + yourFriendsName +
						"&recipientEmail=" + yourFriendsEmail,
				success: function(html){
					jQuery("#shareForm").html("<a href='#' class='successMessage'>Your message has been sent! <br />Click to send again.</a>");
					jQuery(document).ready(function() {
						jQuery(".successMessage").click(function(){	 
								jQuery('#shareForm').html('<input id="webPermalink" type="hidden" value="' + webPermalink + '" name="webPermalink"/><input id="shareURI" type="hidden" value="' + shareURI + '" name="shareURI"/><input id="shareTitle" type="hidden" value="' + shareTitle + '" name="shareTitle"/><label for="yourEmail">Your email:</label><input id="yourEmail" class="shareBarInput required email" type="text" value="' + yourEmail + '" name="yourEmail"/><label for="yourFriendsName">Your friend\'s name:</label><input id="yourFriendsName" class="shareBarInput required" type="text" value="" name="yourFriendsName" minlength="2"/><label for="yourFriendsEmail">Your friend\'s email:</label><input id="yourFriendsEmail" class="shareBarInput required email" type="text" value="" name="yourFriendsEmail"/><input class="shareBarSubmit" type="submit" value="SEND"/>');									
						});
					});
				}
			});
			jQuery("#shareForm").html('<span class="loadingMessage">Sending...</span>');
		}
	});

		
	// Validation and error handling for Multi Share Page 	
	jQuery("#multiShareForm").validate({
	  submitHandler: function(form) {					
			var webPermalink = document.getElementById("webPermalink").value;
			var shareURI = document.getElementById("shareURI").value;
			var sharePost = document.getElementById("sharePost").value;
			var shareTitle = document.getElementById("shareTitle").value;
			var yourEmail = document.getElementById("multiShareYourEmail").value;
			var yourName = document.getElementById("multiShareYourName").value;
			var recipientEmail1 = document.getElementById("recipientEmail1").value;
			var recipientEmail2 = document.getElementById("recipientEmail2").value;
			var recipientEmail3 = document.getElementById("recipientEmail3").value;			
			var recipientEmail4 = document.getElementById("recipientEmail4").value;			
			var recipientEmail5 = document.getElementById("recipientEmail5").value;
			var shareMessage = document.getElementById("shareMessage").value;

			jQuery.ajax({
				type: "POST",
				url: "/wordpress/wp-content/themes/pinq/multiMailer.php",
				data: 		
					"shareURI=" + shareURI +
					"&webPermalink=" + webPermalink +
					"&shareTitle=" + shareTitle +
					"&sendersEmail=" + yourEmail +
					"&sendersName=" + yourName +
					"&recipientEmail1=" + recipientEmail1 +
					"&recipientEmail2=" + recipientEmail2 +
					"&recipientEmail3=" + recipientEmail3 +
					"&recipientEmail4=" + recipientEmail4 +
					"&recipientEmail5=" + recipientEmail5 +
					"&shareMessage=" + shareMessage,
				success: function(html){
						jQuery("#multiShareForm").html("<a class='successMessage' href='/share/?sharePost="+ sharePost +"&shareURI="+ shareURI +"&webPermalink="+ webPermalink +"&yourName="+ yourName +"&yourEmail="+ yourEmail +"'>Your message has been sent! <br />Click to send to more friends.</a>");	
						//		ajaxReload();
						// Reload the share form upon request
						/*jQuery(document).ready(function() {
							jQuery(".successMessage").click(function(){	 
									jQuery('#multiShareForm').html('<input type="hidden" value="' + shareURI + '" name="shareURI" id="shareURI" /> <input id="shareTitle" type="hidden" value="' + shareTitle + '" name="shareTitle"/> <ul> <li class="yourInfo"> <label class="yourNameLabel" for="yourName">Your name:</label> <input type="text" value="' + yourName + '" name="yourName" minlength="2" class="multiShareInput required" id="yourName" /> </li> <li class="yourInfo rightSide"> <label class="yourEmailLabel" for="yourEmail">Your email:</label> <input type="text" value="' + yourEmail + '" name="yourEmail" class="multiShareInput required email" id="yourEmail" /> </li> <li class="rightSide"> <label for="shareMessage">Message (optional)</label> <textarea name="shareMessage" class="multiShareInput" id="shareMessage"></textarea> </li> <li> <label for="recipientEmail1">Friend&rsquo;s email:</label> <input type="text" value="" name="recipientEmail1" class="multiShareInput required email" id="recipientEmail1" /> </li> <li> <label for="recipientEmail2">Friend&rsquo;s email:</label> <input type="text" value="" name="recipientEmail2" class="multiShareInput email" id="recipientEmail2" /> </li> <li> <label for="recipientEmail3">Friend&rsquo;s email:</label> <input type="text" value="" name="recipientEmail3" class="multiShareInput email" id="recipientEmail3" /> </li> <li> <label for="recipientEmail4">Friend&rsquo;s email:</label> <input type="text" value="" name="recipientEmail4" class="multiShareInput email" id="recipientEmail4" /> </li> <li> <label for="recipientEmail5">Friend&rsquo;s email:</label> <input type="text" value="" name="recipientEmail5" class="multiShareInput email" id="recipientEmail5" /> </li> <li class="rightSide submitRow"> <input type="submit" value="SEND" class="multiShareSubmit" /> <a class="multiShareCancel" href="' + webPermalink + '">CANCEL</a> </li> <li class="reset">&nbsp;</li> </ul> <div class="reset"></div>');									
							});
						});*/
					}
				});
				jQuery("#multiShareForm").html('<span class="loadingMessage">Sending...</span>');
			}
		});
	
	
});

// No Spam
function nospam(user,domain) {
	locationstring = 'mailto:' + user + '@' + domain;
	window.location = locationstring;
	}
	
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 * @name jQuery.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
