/* 
	Title: MKK - Global JavaScript document
	Description: Contains any custom JavaScript functions
	Version: 1.0
	Author: Ben Ellis
	Author URI: http://www.hungrybrowser.co.uk
	Created: 30/11/2011
	Updated: 30/11/2011
*/

/* ------------------------------------------------------------
	Declare global functions
---------------------------------------------------------------- */

/*
	Function to show an element
	@param: triggerId = the id/class of the navigation items
	@param: targetId = the id/class of the div items
*/
function showElement(triggerId,targetId){
	$(document).ready(function(){
		// Attach onclick events to the nav item
		$(triggerId).click(function(e){
			// Toggle the opacity of the drop down
			$(targetId).show();
		});
	});
}

/*
	Function to hide an element
	@param: triggerId = the id/class of the navigation items
	@param: targetId = the id/class of the div items
*/
function hideElement(triggerId,targetId){
	$(document).ready(function(){
		// Attach onclick events to the nav item
		$(triggerId).click(function(e){
			// Toggle the opacity of the drop down
			$(targetId).hide();
		});
	});
}

/* 
	Function to scroll the page
	@param: triggerId = id of the link to attach the event to
	@param: targetId = id of the object to scroll to
*/
function backToTop(triggerId,targetId){
	$(document).ready(function(){
		// Attach the onclick event
		$(triggerId).click(function(){
			// Scroll the page
			$('html, body').animate({
				scrollTop: $(targetId).offset().top
			}, 250);	
			// Ensure that the anchors don't make the page jump 
			return false;
		 });
	})
}

/*
	Function to show/hide the contact drop down menu
	@param: triggerId = the id/class of the navigation items
	@param: targetId = the id/class of the div items
*/
function contactNav(triggerId,targetId){
	$(document).ready(function(){
		// Attach onclick events to the nav item
		$(triggerId).click(function(e){
			// Prevent the default
			e.preventDefault();
			// Toggle the opacity of the drop down
			$(targetId).toggle();
			// Set the current class for the active nav item
			//$(this).addClass('selected');
		});
		// Disable the mouseup function for the form
		$(targetId).mouseup(function(){
        	return false;
        });
		// If the user clicks outside the menu
		$(document).mouseup(function(e) {
		    if($(e.target).parent(triggerId).length==0){
		        $(triggerId).removeClass('selected');
		        $(targetId).hide();
		    }
		}); 			
	});
}

/* 
	Function to fade launch an iframe lightbox
	@param: id = the element to attach the event to
	@param: width = the width of the lightbox
	@param: height = the height of the lightbox
*/
function lightBox(id,width,height){
	$(document).ready(function(){
		// Not for internet explorer (fancybox bug)
		if ($.browser.msie){
			$(id).attr('target', '_blank');
		}
		// For bigger screen sizes
		else if($(window).width() > 750){
			// Attach the onclick event
			$(id).fancybox({
				'width'			:  width,
				'height'		:  height,
				'fitToView'     : true,
				'type'			: 'iframe'
			});
		}
		// Else launch the item in a new window
		else $(id).attr('target', '_blank');
	});
}

// Function to hide email addresses from spammers
function spamFree(id){
	$(function(){
		var target = "."+id;
		var at = / at /;
		var dot = / dot /g;
		var addr = $(target).text().replace(at,"@").replace(dot,".");
		$(target).after('<a href="mailto:'+addr+'" title="Send an email to '+ addr +'">'+ addr +'</a>')
		.hover(function(){window.status="Send an email";}, function(){window.status="";});
		$(target).remove();
	});
}

/*
	Function to add custom event tracking to an object using Google Analytics
	@param: id = id of the object to target
	@param: category for the event
	@param: action for the event
	@param: title of the event
*/
function trackEvent(id,category, action, title){
	$(document).ready(function(){
		// Attach the event
		$(id).click(function(){
			_gaq.push(['_trackEvent', category, action, title]);
		});
	});
}
