/**
 * Common Javascript File
 *
 * Include any site wide javascript methods or functionality within
 * this file. This includes things such as menu's , tell a friend links,
 * bookmark page & google map stuff.
 *
 * @copyright  2010 Graphics Online
 * @author     $Author: gockwell $$
 * @version    $Id: common.js 42 2010-09-02 01:56:47Z gockwell $
 * @since      $Date: 2010-09-02 11:56:47 +1000 (Thu, 02 Sep 2010) $
 */

 
/**
 * Global Onload Method
 * currently this sets up the tell a friend popup in a lightbox as well as sets up the bookmark site link.
 */
$(function() {

	
	
	
	/** Add custom site wide onload functionality here */
	
	
	// Slide menu
	$('#menu').slideMenu(); 
	
	// flyout menu (superfish & supersubs) '
	// i've applyed this menu to the top-menu
	if ($('ul.sf-menu').length) {
		$('ul.sf-menu').supersubs({ 
            	minWidth:    8,   // minimum width of sub-menus in em units 
            	maxWidth:    27,   // maximum width of sub-menus in em units 
            	extraWidth:  1  
        	}).superfish({
        		delay:       200,                            // one second delay on mouseout
        		speed: 		'fast', 						 // speed up the menu eh :)
            	animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
				autoArrows: false
		});
	}
	
	
	
	// im going to auto fancybox any links with rel=fancybox for ease of use.
	if ($('a[rel=fancybox]')) {
		$('a[rel=fancybox]').fancybox({
			centerOnScroll		: true,
			transitionIn		: 'elastic',
			transitionOut		: 'elastic',
			titlePosition		: 'over'
		})
	}
	
	// attach fancybox to the send to friend link in the footer if both exist
	if ($.fancybox && $('#tellAFriend').length >= 1) {
		$('#tellAFriend').fancybox({
			'autoScale'			: false,
			'scrolling'			: 'no',
			'centerOnScroll'	: true,
			'overlayOpacity'	: 0.5,
			'overlayColor'		: '#000', 
			'showNavArrows'		: false,
			'width'				: 420,
			'height'			: 310,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'type'				: 'iframe'
		});
	}
	
	// attach a fancybox to our google map link , also apply a tipsy to the link itself.
	if ($.fancybox && $('#gmap').length >= 1) {
		$('#gmap').fancybox({
                'autoScale'			: false,
                'scrolling'				: 'no',
                'centerOnScroll'		: true,
                'overlayOpacity'		: 0.5,
                'overlayColor'		: '#000',
                'showNavArrows'	: false,
                'width'					: 980,
                'height'					: 500,
                'transitionIn'			: 'elastic',
                'transitionOut'		: 'elastic',
                'type'					: 'iframe'
        });

		$('#gmap-div').tipsy({
			gravity: 'e', fade: true
		});
	}
	
	
	// attach jquery bookmark plugin to the bookmark site link.
	if ($('#bookmarkSite').length >= 1) {
		$('#bookmarkSite').jFav();
	}
	
	
	// attach any form validation here
	
	// validate the contact us form.
	if ($('FORM#contact-form').length === 1) {
		$('FORM#contact-form').validate({
			rules: {
				Name: { required: true, minlength: 2 },
				Email: { required: true, email: true },
				Enquiry: { required: true },
				_captcha_code: { required: true, minlength: 5 }
			}
		});
	}
	

	
	if ($('FORM#search-form INPUT[name=search_text]').length) {
		$('FORM#search-form INPUT[name=search_text]').focus(function() { if ($(this).val() == 'SEARCH') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('SEARCH'); } });
	}
	
	// set our subscribe form on blur and focus events here - also attach validateSubscribeForm method to on submit..
	// @todo Fix this up so its alot nicers.
	if ($('FORM#subscribe-form INPUT[name=email]').length && $('FORM#subscribe-form INPUT[name=name]').length) {
		$('FORM#subscribe-form INPUT[name=email]').focus(function() { if ($(this).val() == 'Email Address') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('Email Address'); } });
		$('FORM#subscribe-form INPUT[name=name]').focus(function() { if ($(this).val() == 'Full Name') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('Full Name'); } });
		$('FORM#subscribe-form input[name=email]').tipsy({  gravity: 'e', trigger: 'manual', fade: true });
		$('FORM#subscribe-form input[name=name]').tipsy({  gravity: 'e', trigger: 'manual', fade: true });
		$('FORM#subscribe-form INPUT[name=phone]').focus(function() { if ($(this).val() == 'Phone') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('Phone'); } });
		$('FORM#subscribe-form INPUT[name=location]').focus(function() { if ($(this).val() == 'Location') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('Location'); } });
		$('FORM#subscribe-form input[name=phone]').tipsy({  gravity: 'e', trigger: 'manual', fade: true });
		$('FORM#subscribe-form input[name=location]').tipsy({  gravity: 'e', trigger: 'manual', fade: true });
		$('FORM#subscribe-form').submit(validateSubscribeForm);
	}
	
		if ($('FORM#subscribe-form2 INPUT[name=email]').length && $('FORM#subscribe-form2 INPUT[name=name]').length) {
		$('FORM#subscribe-form2 INPUT[name=email]').focus(function() { if ($(this).val() == 'Email Address') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('Email Address'); } });
		$('FORM#subscribe-form2 INPUT[name=name]').focus(function() { if ($(this).val() == 'Full Name') { $(this).val(''); } }).blur(function() { if ($(this).val() == '') { $(this).val('Full Name'); } });
		$('FORM#subscribe-form2 input[name=email]').tipsy({  gravity: 'e', trigger: 'manual', fade: true });
		$('FORM#subscribe-form2 input[name=name]').tipsy({  gravity: 'e', trigger: 'manual', fade: true });
		$('FORM#subscribe-form2').submit(validateSubscribeForm2);
	}
	
	
	
	
});




/**
 * Shonky validate the subscribe form, needs to be cleaned up.
 * 
 * @todo , integrate floating validation errors into jquery.validate
 * @return bool
 */
function validateSubscribeForm() {

	var email = $('FORM#subscribe-form input[name=email]');
	var name = $('FORM#subscribe-form input[name=name]');
	var focus = null;

	if (name.val() == 'Full Name') {
		name.attr('title', 'Name is a required field');
		name.tipsy('show');
		setTimeout("$('FORM#subscribe-form input[name=name]').tipsy('hide');", 2000);
		focus = (focus != null) ? focus : name;
	}
	
	if (email.val() == 'Email Address') {
		email.attr('title', 'Email Address is a required field');
		$('FORM#subscribe-form input[name=email]').tipsy('show');
		setTimeout("$('FORM#subscribe-form input[name=email]').tipsy('hide');", 2100);
		focus = (focus != null) ? focus : email;
	} else if (email.val().indexOf('@') === -1 || email.val().indexOf('.') === -1) {
		email.attr('title', 'Email Address must be valid.');
		$('FORM#subscribe-form input[name=email]').tipsy('show');
		setTimeout("$('FORM#subscribe-form input[name=email]').tipsy('hide');", 2100);
		focus = (focus != null) ? focus : email;
	}

	if (focus !== null) {
		focus.select();
		focus.focus();
		return false;
	}

	return true;
}


function validateSubscribeForm2() {

	var email = $('FORM#subscribe-form2 input[name=email]');
	var name = $('FORM#subscribe-form2 input[name=name]');
	var focus = null;

	if (name.val() == 'Full Name') {
		name.attr('title', 'Name is a required field');
		name.tipsy('show');
		setTimeout("$('FORM#subscribe-form2 input[name=name]').tipsy('hide');", 2000);
		focus = (focus != null) ? focus : name;
	}
	
	if (email.val() == 'Email Address') {
		email.attr('title', 'Email Address is a required field');
		$('FORM#subscribe-form2 input[name=email]').tipsy('show');
		setTimeout("$('FORM#subscribe-form2 input[name=email]').tipsy('hide');", 2100);
		focus = (focus != null) ? focus : email;
	} else if (email.val().indexOf('@') === -1 || email.val().indexOf('.') === -1) {
		email.attr('title', 'Email Address must be valid.');
		$('FORM#subscribe-form2 input[name=email]').tipsy('show');
		setTimeout("$('FORM#subscribe-form2 input[name=email]').tipsy('hide');", 2100);
		focus = (focus != null) ? focus : email;
	}

	if (focus !== null) {
		focus.select();
		focus.focus();
		return false;
	}

	return true;
}



/**
 * This is a jquery plugin that I use to preload a bunch of images, its easy to use, just call 
 * 
 * $.preLoadImages("image1.jpg", "image2.jpg")
 * 
 */
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);	
		}
	};
})(jQuery);
