
if(typeof ($.AWA) == "undefined") {
	jQuery.AWA = Object;
}

//Create the hompage carousel
jQuery.AWA.buildCarousel = {
	init: function(){
		jQuery('.showcase').append('<ul class="controls"><li class="previous"><a href="#">Previous</a></li><li class="pause">Pause</li><li class="resume">Play</li><li class="next"><a href="#">Next</a></li></ul>')

		jQuery('.carousel').cycle({ 
		    fx:     'scrollHorz', 
		    next:   '.showcase .next a', 
		    pause:   1,
		    prev:   '.showcase .previous a',
		    random: 1,
		    speed:  '2000', 
		    timeout: 8000
		});
		
		jQuery('.showcase .resume').hide();
		
		jQuery('.carousel .pause').click(function() { 
		    jQuery('.carousel').cycle('pause'); 
		    jQuery('.showcase .pause').hide();
		    jQuery('.showcase .resume').show();
		});
		
		jQuery('.carousel .resume').click(function() { 
		    jQuery('.carousel').cycle('resume', true);
		    jQuery('.showcase .resume').hide();
		    jQuery('.showcase .pause').show();
		});
		
	} // End init
}

jQuery.AWA.buildLogoSlideshow = {
	init: function(){
		jQuery('.clients-slideshow .logos').append('<a href="#" class="previous">Previous</a><a href="#" class="next">Next</a>')

		jQuery('.clients-slideshow .slideshow').cycle({ 
		    fx:     'scrollHorz', 
		    next:   '.clients-slideshow .logos .next', 
		    prev:   '.clients-slideshow .logos .previous',
			//random: 1,
		    speed:  '3000', 
		    timeout: 0
		});
		
	} // End init
}

//Inject HTML into each dropdown nav for curved corners
jQuery.AWA.addNavFooter = {
	init: function(){
		jQuery('.primary ul li').children('div').append('<div class="nav-bottom"><span class="l"></span><span class="r"></span></div>');
		
	} // End init
}

//Remove the margin from the 4th item of every thumbnail row to stop it wrapping onto the next line
jQuery.AWA.removeNthThumbnailMargin = {
	init: function(){
		jQuery('.landing-page .thumbnails ul li:nth-child(4n)').addClass('no-margin');
		
	} // End init
}

jQuery.AWA.buildEqmsTour = {
	init: function(){
		jQuery('.screens').cycle({ 
		    fx:     'scrollHorz', 
		    speed:  '2000', 
		    timeout: 0, 
		    pager:  '.pages div ul', 
		    pagerAnchorBuilder: function(idx, slide) { 
		        // return selector string for existing anchor 
		        return '.pages li:eq(' + idx + ') a'; 
		    } 
		});
	} // End init
}


// OVERLABEL: Position the label over the form element and hide it when given focus or if there is content
jQuery.fn.overlabel = function( options ) {
	// build main options before element iteration
	var opts = $.extend( {}, $.fn.overlabel.defaults, options );
	var selection = this.filter( 'label[for]' ).map( function() {
		var label = $( this );
		var id = label.attr( 'for' );
		var field = document.getElementById( id );
		if ( !field ) return;
		// build element specific options
		var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
		label.addClass( o.label_class );
		var hide_label = function() { label.css( o.hide_css ) };
		var show_label = function() { this.value || label.css( o.show_css ) };
		$( field )
			 .parent().addClass( o.wrapper_class ).end()
			 .focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
		return this;
	} );
	return opts.filter ? selection : selection.end();
};
// publicly accessible defaults
jQuery.fn.overlabel.defaults = {
	label_class:   'overlabel-apply',
	wrapper_class: 'overlabel-wrapper',
//	hide_css:      { 'text-indent': '-10000px' },
//	show_css:      { 'text-indent': '0px', 'cursor': 'text' },
	hide_css:      { 'display': 'none' },
	show_css:      { 'display': 'block', 'cursor': 'text' },
	filter:        false
};



jQuery(document).ready(function(){
	jQuery.AWA.buildCarousel.init();
	jQuery.AWA.buildEqmsTour.init();
	jQuery.AWA.buildLogoSlideshow.init();
	jQuery('#cse-search-box label[for=query]').overlabel();
	jQuery.AWA.addNavFooter.init();  
	jQuery.AWA.removeNthThumbnailMargin.init();
	jQuery.gaTracker('UA-10792175-1'); //Initialise Google Analytics tracking
});	
