$(document).ready(function(){
	
	/*
	Client logos rotator.
	*/
	$("#client-logos-slider").easySlider({
		auto: true, 
		continuous: true,
		controlsShow: false,
		pause: 6000
	});
	
	/*
	Removes input field default text on focus, replaces it on blur
	*/
	$('#newsletter-subscription input[type="text"]').focus(function() {
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('#newsletter-subscription input[type="text"]').blur(function() {
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	}); 

	/*
	Any links with rel="external" attribute will open in new window.
	*/
	$('A[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});
	
	/*
	Hides newsletter sticky footer.
	*/
	$('#newsletter-hide-link').click(function(){
		$('#newsletter-subscription').slideUp();
		return false;	
	});
	
	/*
	FAQs
	Scrolls to an FAQ answer, and back to top of page.
	*/
	$('#faqs a').click(function(){
		var faqid = $(this).attr('href');
		var faqnum = faqid.substring(7);
		var targetOffset = $('#faq-'+faqnum).offset().top - 10;
	 	$('html,body').animate({scrollTop: targetOffset}, "slow");
		return false;
	});
	$('dl.faq a.back-to-top').click(function(){
		$('html,body').animate({scrollTop: 0}, "slow");
		return false;
	});
	
	/*
	Home feature slider
	*/
	
	//Paging  and Slider Function
	rotate = function(){
	    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
	    var feature_reelPosition = triggerID * 940; //Determines the distance the image reel needs to slide
	
	    $(".paging a").removeClass('active'); //Remove all active class
	    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
	    //Slider Animation
	    $(".image_reel").animate({
	        left: -feature_reelPosition
	    }, 500 );
	
	}; 
	
	//On Click
	$(".paging a").click(function() {
		
		//$('#home-feature-flash-1').stopVideo();
		// Stop any Flash videos playing.
		// Relies on Flash's ExternalInterface having registered a Flash method stopVideo as a JS callback.
		var myFlash = swfobject.getObjectById("home_feature_flash_"+$(".paging a.active").attr("rel"));
		// Proceed only if Flash has created a JS callback for its stopVideo method.
		if (myFlash && $.isFunction(myFlash.stopVideo)){
			myFlash.stopVideo();
		}
		
	    $active = $(this); //Activate the clicked paging
	    rotate(); //Trigger rotation
	    return false; //Prevent browser jump to link anchor
	});
	
	// Fix for the sticky footer, since mobile safari doesn't support positon:fixed how you expect it to.
	/*
	var deviceAgent = navigator.userAgent.toLowerCase();
	var isMobileSafari = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (isMobileSafari) {
		var win_y = $(window).height(); 
		var scroll_y = $(window).scrollTop();
		$("#newsletter-subscription").css({ 
			bottom: 'auto',
			position: 'absolute',
			top: ((win_y - 49) + scroll_y) + "px"
		}); 
		function showNavBar() { 
			var win_y = $(window).height(); 
			var scroll_y = $(window).scrollTop(); 
			$("#newsletter-subscription").css({ 
				top: ((win_y - 47) + scroll_y) + "px"
			}); 
		} 
		var showTimer = false; 
		function maybeShowNavBar(evt) { 
			if ( showTimer ) { 
				clearTimeout( showTimer ); 
			} 
			showTimer = setTimeout( showNavBar, 175 ); 
		} 
		$(window).bind( "scroll", maybeShowNavBar ); 
	}
	*/
	var deviceAgent = navigator.userAgent.toLowerCase();
	var isMobileSafari = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (isMobileSafari) {
		var win_y = $(window).height(); 
		var scroll_y = $(window).scrollTop();
		$("#newsletter-subscription").css({ 
			position: 'static'
		}); 
	}
	

});
