/*

Scripts written by Alex Dickson for Acura Multimedia (http://www.acura.com.au)

All scripts leverage the jQuery framework (http://www.jquery.com) by John Resig (http://www.ejohn.org)

*/



// on DOM ready

$(document).ready(function(){

	$('html').addClass('dom-loaded');


       
    $('a[rel*=external],#content a[href^=http://]:not([href*=' + location.hostname + '])"').newWindow();
    
	// Contact Form
    if ($('body').hasClass('contact')) {
	    
	    
	    $('#input-enquiry').inputLabel({ defaultLabel: 'your message' });
	    $('#contact-form').validate({
	              rules: {
	                  name: {
	                      required: true
	                  },
	                  email: {
	                      required: true,
	                      email: true
	                  },
	                  enquiry: {
	                      required: true
	                  }
	              },
	
	              errorPlacement: function(error, element) {
	                 element.attr('title', error.text() ).addClass('error');
	               }
	          });          
	
	          
	
	    
    };
    
});


// on window loaded



$(window).load(function(){  // window load

	$('html').addClass('window-loaded');

	//google analytics

	try {

		var pageTracker = _gat._getTracker('UA-693909-46');

		pageTracker._trackPageview();

	} catch(err) {}

});


var slidingMenu = function($) {

	var $menu, $slidingPanel;
	
	

	var init = function() {
		
		$menu =  $('#header ul');
		
		$menu.after('<div id="sliding-panel"><a href="" style="display: none;" /></div>');
	
		$slidingPanel = $('#sliding-panel');
		
		
		// Set default size + position
		
		var menuClass = $menu.attr('class');
		
		var $targetElement = $('#menu-' + menuClass);
		
		slideTo($targetElement);
		
		// Set sliding or on click
		
		$menu.find('li a').click(function(event) {
			event.preventDefault();
			
			slideTo($(this).parent(), true, true);
		
		
		});


		// Add back and next buttons if required
		
		var $secondaryCont = $('#secondary-container');
		
		var $prev = $menu.find('li.active').prev();
		
		if ($prev.length) {
			$secondaryCont.append('<a id="prev" href="' + $prev.find('a').attr('href') + '" title="Go to \'' + $prev.find('a').text() + '\'">Previous</a>');
			$('#prev').click( function(event) { event.preventDefault(); slideTo($prev, true, true) } );
		}
		
		var $next = $menu.find('li.active').next();
		
		if ($next.length) {
			$secondaryCont.append('<a id="next" href="' + $next.find('a').attr('href') + '" title="Go to \'' + $next.find('a').text() + '\'">Next</a>');
			$('#next').click( function(event) { event.preventDefault(); slideTo($next, true, true) } );
		}
		
	};
	
	var slideTo = function($element, animate, link) {
		
		// Remove class of exisitng one
		
		$menu.find('li.active').removeClass('active');
		
		var defaultWidth = $element.outerWidth();
		var defaultLeft = $element.position().left;
		
		if (animate) {
			
			$slidingPanel.animate({ width: defaultWidth, left: defaultLeft }, 500);

			
		} else {
		
			$slidingPanel.width(defaultWidth).css({ left: defaultLeft });
		
		}
		
		$element.addClass('active');
		
		// Custom extra menus in thing... added later
		
		var $panelLink = $slidingPanel.find('a');
		
		if ($element.find('a.sub-item').length) {
		
		  
		  $panelLink
		      .attr( { href: $element.find('a.sub-item').attr('href') } )
		      .html('&bull;&nbsp;' + $element.find('a.sub-item').html())
		      .fadeIn(200);
		
		} else {
		   $panelLink.fadeOut(200);
		};
		
				
		if (link) {
			var href = $element.find('a:first').attr('href');
			window.location = href;
		};
		
	};
	
	var slideNext = function() {
		
		slideTo($menu.find('li.active').next(), true, true);
		
	};
	
	var slidePrev = function() {
		
		slideTo($menu.find('li.active').prev(), true, true);
		
	};
	
	$(document).ready(function() {
		init();
	});
	

}(jQuery);


var specials = function($) {
	
	var $specials, $bubble;
	
	var canShow = true;
	
	var init = function() {
	
		$specials = $('#specials');
		
		$specials.append('<div id="bubble" />');
	
		$bubble = $('#bubble');
		
		$bubble.hide();
		
		$specials.find('ul li').hoverIntent(function() {
			
			if ( ! canShow) {
				return;
			}
			canShow = false;
			var left = $(this).position().left + 20;
			var top = $(this).position().top - $bubble.outerHeight() + 10;
			
			var infoHtml = $(this).find('.info').html();
			
			$bubble
			.html('<div>' + infoHtml + '</div>')
			.css({ left: left, top: top })
			.fadeIn(200);
		
		
		},
		function() {
		

		
			$bubble.fadeOut(100, function () { canShow = true; });
		
		});
	
	};
	
	$(document).ready(function() {
		init();
	});


}(jQuery);


// Open links in new window

 (function($){  
  $.fn.newWindow = function(options) {   
    var defaults = {
		titleText: 'Link opens in a new window'
	};	
	var options = $.extend(defaults, options);   
     return this.each(function() {  
	   var obj = $(this);
       if ( ! obj.is('a')) {
           return;
       };
	   if (options.titleText != '') {   
		   if (obj.attr('title')) {
				var newTitle = obj.attr('title') + ' (' + options.titleText + ')';
		   } else {
				var newTitle = options.titleText;   
		   };		   
		   obj.attr('title', newTitle);   
	   };
	   obj.click(function(event) {
	   	  event.preventDefault();  
		  var newBlankWindow = window.open(obj.attr('href'), '_blank');
		  newBlankWindow.focus();
		}); 
	   });
  };  
 })(jQuery); 
 
 // inputlabel
 
 eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(6($){$.m.2=6(1){4 c={8:\'\',7:\'n\',d:\'l-k\'};4 1=$.h(c,1);f e.j(6(){4 0=$(e);5(!0.p(\':g, :v, w\')){f};5(1.8==\'\'&&0.a(\'b\')){4 2=0.a(\'b\');4 2=$(\'r[s=\'+2+\']\').9(1.d).g()}q{4 2=1.8};5(!0.3()||0.3()===2){0.9(1.7);0.3(2)};0.t(6(){5(0.3()==2){0.u(1.7);0.3(\'\')}});0.o(6(){5($.i(0.3())==\'\'){0.9(1.7);0.3(2)}})})}})(x);',34,34,'obj|options|inputLabel|val|var|if|function|dullClass|defaultLabel|addClass|attr|id|defaults|labelClass|this|return|text|extend|trim|each|use|in|fn|dulled|blur|is|else|label|for|focus|removeClass|password|textarea|jQuery'.split('|'),0,{}));

// 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);