/* ===========================================================================
 *
 * JQuery URL Parser
 * Version 1.0
 * Parses URLs and provides easy access to information within them.
 *
 * Author: Mark Perkins
 * Author email: mark@allmarkedup.com
 *
 * ---------------------------------------------------------------------------
 *
 * CREDITS:
 *
 * Parser based on the Regex-based URI parser by Stephen Levithian.
 * For more information (including a detailed explaination of the differences
 * between the 'loose' and 'strict' pasing modes) visit http://blog.stevenlevithan.com/archives/parseuri
 *
 */
jQuery.url=function(){var segments={};var parsed={};var options={url:window.location,strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};var parseUri=function(){str=decodeURI(options.url);var m=options.parser[options.strictMode?"strict":"loose"].exec(str);var uri={};var i=14;while(i--){uri[options.key[i]]=m[i]||""}uri[options.q.name]={};uri[options.key[12]].replace(options.q.parser,function($0,$1,$2){if($1){uri[options.q.name][$1]=$2}});return uri};var key=function(key){if(!parsed.length){setUp()}if(key=="base"){if(parsed.port!==null&&parsed.port!==""){return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/"}else{return parsed.protocol+"://"+parsed.host+"/"}}return(parsed[key]==="")?null:parsed[key]};var param=function(item){if(!parsed.length){setUp()}return(parsed.queryKey[item]===null)?null:parsed.queryKey[item]};var setUp=function(){parsed=parseUri();getSegments()};var getSegments=function(){var p=parsed.path;segments=[];segments=parsed.path.length==1?{}:(p.charAt(p.length-1)=="/"?p.substring(1,p.length-1):path=p.substring(1)).split("/")};return{setMode:function(mode){strictMode=mode=="strict"?true:false;return this},setUrl:function(newUri){options.url=newUri===undefined?window.location:newUri;setUp();return this},segment:function(pos){if(!parsed.length){setUp()}if(pos===undefined){return segments.length}return(segments[pos]===""||segments[pos]===undefined)?null:segments[pos]},attr:key,param:param}}();


/* ===========================================================================
 * 
 * LTWS Functions
 * 
 */

// homepage slideshow

var currentSlide = 1;
var looping = true;
var totalSlides;
var theNextSlide;

jQuery(document).ready(function($) {
	
	totalSlides = $('#slideshow .slide').length;

	$.timer(6000, function (timer) {
		timer.stop();
		if (looping == true) {
			autoAdvance();
			timer.reset(6000);
		}
	});
	
	// show first image
	$('#slideshow #slide1').fadeIn(1000);
	$('#slideshow #slidemenu ul li a').click(function(e){
		
		e.preventDefault();
		looping = false; // disable auto-advance
		theNextSlide = this.id.substr(4); // get the target image to show and fade it in

		// hide all the images
		$('#slideshow div.slide').each(function() {
			$(this).hide();
		});
		
		// show the selected one
		if (this.className == 'current') {
			$('#'+theNextSlide).show();
		} else {
			$('#'+theNextSlide).fadeIn(1000);
		}

		// turn off arrows, except for one being rolledover
		$('#slidemenu ul li a').each(function() {
			$(this).removeClass('current');
		});
		$(this).addClass('current');

	});

});

function autoAdvance() {
	
	// fade out current slide
	jQuery('#slideshow #slide' + currentSlide).fadeOut(1000, fadeNext);
	// update nav
	jQuery('#slideshow #slidemenu ul li a#nav_slide' + currentSlide).removeClass('current');
	
	currentSlide++;
	if (currentSlide > totalSlides) {
		theNextSlide = 1;
	} else {
		theNextSlide = currentSlide;
	}
	
	jQuery('#slideshow #slidemenu ul li a#nav_slide' + theNextSlide).addClass('current');
	
	currentSlide = theNextSlide;
	
}

function fadeNext() {
	jQuery('#slideshow #slide' + theNextSlide).fadeIn(1000);
}

// homepage tips scroller
/* this needs to be updated so that it works by dynamically checking the
value of the next (or previous) LI width, then use that value to scroll the appropriate amount */

var currentRightmostTip = 3;
var totalScrollerTips;

jQuery(document).ready(function($) {

	totalScrollerTips = $('#tips_scroller_window li').length;
	//scrollerTipsWidth = $('#tips_scroller_window ul').width();
	
	$('#tips_scroller a.previous, #tips_scroller a.next').click(function(evtB){
		
		evtB.preventDefault();
		var direction = evtB.currentTarget.className;
		
		// check what button the user pressed and set the distance to scroll
		switch(direction) {
			
			case "previous":
				if (currentRightmostTip > 3) {
					$('#tips_scroller_window ul').animate({left: '+=285'}, 750);
					currentRightmostTip--;
				}
				break;
			case "next":
				if (currentRightmostTip < totalScrollerTips) {
					$('#tips_scroller_window ul').animate({left: '-=285'}, 750);
					currentRightmostTip++;
				}
				break;
			default:
				// do nothing
				break;
			
		}
		
		// scroll the tip questions


	});
	
});


// tips page slideshow

var currentTip;
var tipLooping = true;
var totalTips;

jQuery(document).ready(function($) {
	
	totalTips = $('#tips_qa .tip').length;
	
	// check to see if a tip value has been passed in the URL
	currentTip = jQuery.url.param("tip");
	if (currentTip == null) {
		currentTip = 1;
	}
	
	// show first image + nav title
	$('#tips_qa #tip'+currentTip).fadeIn(1000);
	$('.navtip'+currentTip).show();
	
	$('#tips_qa .tips_nav a').click(function(evt){
		
		evt.preventDefault();
		tipLooping = false; // disable auto-advance
		var direction = evt.currentTarget.className;
		
		// hide all the tips
		$('#tips_qa div.tip, .tips_nav span').each(function() {
			$(this).hide();
		});
		
		// figure out what the user pressed and set the value for the next tip
		switch(direction) {
			
			case "previous":
				currentTip--;
				if (currentTip < 1) {
					currentTip = totalTips;
				}
				break;
			case "next":
				currentTip++;
				if (currentTip > totalTips) {
					currentTip = 1;
				}
				break;
			default:
				// do nothing
				break;
			
		}
		
		// show the next tip + update the nav title as well
		$('#tip'+currentTip).fadeIn(1000);
		$('.navtip'+currentTip).show();

	});

});

