/*-----------------------------------------------------------------------------
* Copyright 2011 Make. Sydney :: www.makeagency.com.au :: All Rights Reserved *
            ______  __  __   ____      
    /'\_/`\/\  _  \/\ \/\ \ /\  _`\    
   /\      \ \ \L\ \ \ \/'/'\ \ \L\_\  
   \ \ \__\ \ \  __ \ \ , <  \ \  _\L  
    \ \ \_/\ \ \ \/\ \ \ \\`\ \ \ \L\ \
     \ \_\\ \_\ \_\ \_\ \_\ \_\\ \____/
      \/_/ \/_/\/_/\/_/\/_/\/_/ \/___/ 
                                     
* Author		: Dwight Mowbray
* Date			: 15/08/2011
* Project		: PERFECT
* Description	: Main JS actions file
-----------------------------------------------------------------------------*/

(function(window, $) {
	var PERFECT, UTIL, log, LOGS_DISPLAY = false;
	
	PERFECT = {
		common : {
			init: function() {
				
				log("Page: common -> init() loaded.");
				
				// Newsletter signup form
				$("#signupForm").validate({
					success: function(label) {
						label.addClass("valid").text("Ok!")
					},
					errorPlacement: function(error, element) {
						// do nothing
					},
					rules: {
						"hear_about[]": {
							required: "input[name='hear_about[]']",
							minlength: 1
						},
						"confirm_email": {
							equalTo: '#email'
						}
					}
				});
				
				// Submit buttons - rollover code for CTA.
				// Needs to be JS so that it works in old IE
				$(".btn_submit").each(function() {
					var btn = $(this);
					
					$("<img>").attr("src", btn.attr("src").replace("submit.gif", "submit_over.gif"));
					
					btn.hover(function(e) {
						btn.attr("src", btn.attr("src").replace("submit.gif", "submit_over.gif"));
					},
					function(e) {
						btn.attr("src", btn.attr("src").replace("submit_over.gif", "submit.gif"));
					});
				});
				
				// Popup link for emailing to a friend
				$(".share-email").click(function(e) {
					e.preventDefault();
					
					var href = $(this).attr("href");
					var win = window.open(href, "piemail", "width=500,height=600");
				});
				
				// Search form at the top of the page
				$("#search_button").click(function (e) {
					e.preventDefault();
					$("#searchform").submit();
				});
				
				// Set our social.js options
				social.setOptions({
					url: window.location.href,
					twitterMessage: "Check out this link on Perfect Italiano! " + window.location.href
				});
				
				$('.share-social').click(function (e) {
					
					// Only interrupt if this is a share link
					if ($(this).attr("class").indexOf("share-") !== -1) {
						e.preventDefault();
						
						var network = this.hash.substr(1),
							func = social[network];
						
						if (!(network in social) || !$.isFunction(func)) {
							//alert('Invalid social network.');
							return;
						}
						func();
					}
				});
			},
			
			finalize : function() {
				
				log("Page: common -> finalize() loaded.");
			}
		}
	};
	
	// Paul Irish Util jumpstart:
	// http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
	UTIL = {
		fire : function(func, funcname, args) {
		    var namespace = PERFECT;  // indicate your obj literal namespace here
	
		    funcname = (funcname === undefined) ? 'init' : funcname;
		    if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
		    	namespace[func][funcname](args);
		    }
		},
	  	loadEvents : function() {
	    	var bodyId = document.body.id;
			
		    UTIL.fire('common', 'init');
		    UTIL.fire('common', 'addSlideEvent');
			
		    // do all the classes too.
		    $.each(document.body.className.split(/\s+/),function(i,classnm){
		    	UTIL.fire(classnm);
		    	UTIL.fire(classnm,bodyId);
		    });
			
		    UTIL.fire('common', 'finalize');
	 	}
	};
	
	// usage: log('inside coolFunc',this,arguments);
	// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
	// @modified Nathan Winch.
	log = function() {
		if (!LOGS_DISPLAY) {
			return false;
		}
		log.history = log.history || [];   // store logs to an array for reference
	  	log.history.push(arguments);
	  	if (this.console) {
	    	console.log( Array.prototype.slice.call(arguments) );
	  	}
	};
	
	window.PERFECT = PERFECT;
	window.UTIL = UTIL;
	window.log = log;
	
})(window, jQuery);

