/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/

/*

Scripts written by Alex Dickson for Acura Multimedia (http://www.acura.com.au)
jQuery (http://www.jquery.com) by John Resig (http://www.ejohn.org)

*/
// add this class to specify .javascript * in css for quicker css application
$('html').addClass('javascript');


/*
hover intent
*/

																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																															(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);




(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {   
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
        		'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								setTimeout(function(){
									$.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
							removeFilter($(this)[0]);
						});
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

jQuery.fn.exists = function() { return (this.length > 0); };

// on DOM ready
$(document).ready(function(){



	$('#imageSlideshow').innerfade({
            speed: 'slow', 
            timeout: 3000, 
            type: 'sequence', 
            containerheight: '463px' 
         }); 
	
	$('html').addClass('dom-loaded');

	
	dropDownMenu.setup();
	
	newWindow.setup();
	
	$('.inputText, textarea').bind('focus', function() { $(this).css({backgroundColor: '#CDDAA0'});  });

	$('.inputText, textarea').bind('blur', function() { $(this).css({backgroundColor: '#fff'});  });
		
		
		
		$('.errorMessage .message, #no-ie6 p').after('<span class="closeMessage" title="Hide this message">X</span>');	
		
		
		$('.closeMessage').bind('click', function() { $(this).parent().slideUp(500) });
		
		$('#contact-us-form').bind('submit', function() { return validator.checkForm($(this))  });
});


//////////////////////////////////////////////////////////////////////////////////////////////////

// on window loaded
$(window).load(function(){  // window load
	$('html').addClass('window-loaded');

	//google analytics
	try {
		var pageTracker = _gat._getTracker("UA-693909-33");
		pageTracker._trackPageview();
	} catch(err) {}
	
	
});



var dropDownMenu = {
	menuActivated: null,
	hideMenuDelay: null,
	setup: function() {
		$('.menuHeading').hoverIntent(function() {
													 clearTimeout(dropDownMenu.hideMenuDelay);
													 dropDownMenu.slideMenuDown($(this).next());
												 }, function() {  
													 dropDownMenu.hideMenuDelay = setTimeout(function() {  dropDownMenu.slideMenuUp($('#' + dropDownMenu.menuActivated));  }, 500);         
												});
		
		$('.dropDownMenu').hoverIntent(function() { 
														clearTimeout(dropDownMenu.hideMenuDelay) }
													, function() { 
														dropDownMenu.slideMenuUp($(this));
													 });
		
	}, slideMenuUp: function(menuObject) {
		menuObject.animate({opacity: 0.5, top: '5px'},500);
		if (menuObject.id != dropDownMenu.menuActivated)
		{
			dropDownMenu.menuActivated = null;
		}
	},
	
	slideMenuDown: function(menuObject) {
		if (menuObject.attr('id') != dropDownMenu.menuActivated) {
								dropDownMenu.slideMenuUp($('#' + dropDownMenu.menuActivated));
								menuObject.animate({opacity: 1.0, top: '215px'});
								dropDownMenu.menuActivated = menuObject.attr('id');
					 }
	}
}



//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////




















//////////////////////////////////////////////////////////////////////////////////////////////////
// Open links marked with rel="external" in a new window.  
//////////////////////////////////////////////////////////////////////////////////////////////////

var newWindow = {
	
	setup: function() {
		
		$('a[rel=external]').attr('title','Link opens in a new window').each(function() { $(this).bind('click', function() {  newWindow.openNewWindow($(this).attr('href')); return false;}) });
	}, openNewWindow: function(URI) {
		
		var newBlankWindow = window.open(URI, '_blank');
		newBlankWindow.focus();	
		
		
	}
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

// validator class - checks form

var validator = {
	requiredFields: {}, formElements: {}, formObject: null,
	
	checkForm: function(formObject) {
		validator.formObject = formObject;
		validator.requiredFields = 'name, email, enquiry';
		
		//$(formObject).attr('action','#');
		$(formObject).children().children('input[type=text], textarea').each(function() { 
																					  validator.formElements[$(this).attr('name')] = $(this).val();
																							
																							
																								$(this).bind('keyup', function() { 
																															   if ($(this).hasClass('inputError')) {
																															   		$(this).animate({backgroundColor: '#F29D00'}).removeClass('inputError') ;
																															   }
																															   });
																							
																							
																																				});
																																				 
		if (!validator.formElements['name'].match(validator.nameRegex) && validator.requiredFields.match(/name/i)) {
			
			validator.displayError("Please enter your full name.", 'name');
			return false;
		}
		
		if (!validator.formElements['email'].match(validator.emailRegex) && validator.requiredFields.match(/email/i)) {
			
			validator.displayError("Please enter a valid email address.", 'email');
			return false;
		}
		
		if (validator.formElements['enquiry'].length < 5 && validator.requiredFields.match(/enquiry/i)) {
			
			validator.displayError("Please enter a valid enquiry.", 'enquiry');
			return false;
		}

	
	
		return true;
	},
	
	
	
	displayError: function(errorMessage, fieldName) { //css({backgroundColor: '#E4574D'}), color: '#F29D00'
		
		$('* [name=' + fieldName + ']', validator.formObject).focus().addClass('inputError');
		
		try {
			
			$('* [name=' + fieldName + ']', validator.formObject).animate({ backgroundColor: '#E4574D'},500);
			
		} 
		catch(err)
		{
			void(0);	
		}
		
		
		$(validator.formObject).children('.clientMessage').removeClass('information').addClass('error').children('span').text(errorMessage);
		
	}, 
	nameRegex: /^[a-z-'\s]{2,}$/i, phoneRegex: /[\+ 0-9-\s\(\)]{6,}/i, emailRegex: /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i, postcodeRegex: /[0-9]{4,6}/i, placeNameRegex: /^[a-z-'\s]{2,}$/i, postalAddressRegex: /[a-z-'\s0-9,]{3,}$/i


	
}





/* ' */
