/**
 * 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) $
 */

 
 
 function over(e) {
	obj = $(e.target);

	if (!obj.parent().attr('class').match(/^.*?(\w+Selected)\s?/)) {
		var src = obj.attr('src');
		var filename = src.substring(src.lastIndexOf('/')+1);

		// first lets check to see if its IE6 and if the image in a png file
		if(filename == 'blank.gif' && $.browser.msie && $.browser.version == '6.0') {
				var newFilter = obj.css('filter').replace('.png', '-over.png')
				obj.css('filter', newFilter);
		} else {
				obj.attr('src', obj.attr('src').replace(/.png$/, '-over.png'));
		}
	}
}


function out(e) {
	obj = $(e.target);
	if (!obj.parent().attr('class').match(/^.*?(\w+Selected)\s?/)) {	
		var src = obj.attr('src');
		var filename = src.substring(src.lastIndexOf('/')+1);
		
		if(filename == 'blank.gif' && $.browser.msie && $.browser.version == '6.0') {
			var newFilter = obj.css('filter').replace('-over', '');
			obj.css('filter', newFilter);
		} else {
			obj.attr('src', obj.attr('src').replace('-over', ''));
		}
	}
} 
 
 
 
 
 
 
 
 
 
 
 
/**
 * 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() {
	
	
	if($('#intro_wrapper').length >= 1){
		
		$('#intro1').fadeIn(2000,function(){
		
			$('#intro1').fadeOut(2000,function(){
		
				$('#intro2').fadeIn(2000);
				
				$('#intro_tinamba').fadeIn(5000);
				$('#intro_clifton').fadeIn(5000);
			
			
			});

		
		});
		
		
	}
	
	
	
	
	
	
	
	
	
	$('#topnav img').hover(over,out);
	
	if($('.topnavSelected img').length >= 1){
		$('.topnavSelected img').attr('src', $('.topnavSelected img').attr('src').replace(/.png$/, '-over.png'));
	}
	
	
	/** Add custom site wide onload functionality here */
	
	// 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 
		});
	}
	
	
	// 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'                 : 640,
                'height'                : 480,
                'transitionIn'          : 'elastic',
                'transitionOut'         : 'elastic',
                'type'                  : 'iframe'
        });

		$('#gmap-div').tipsy({
			gravity: 'n', 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 },
				Phone: { required: true }
			}
		});
	}
	
	
	// 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').submit(validateSubscribeForm);
	}
	
	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'); } });
	}
	
	
});




/**
 * 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;
}




/**
 * 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);
