/* 
	Title: Wave Tracks GPS - Global JavaScript document
	Description: Contains any custom JavaScript functions
	Version: 1.0
	Author: Ben Ellis
	Author URI: http://www.hungrybrowser.co.uk
	Created: 26/11/2009
	Updated: 26/11/2009
*/

$(document).ready(function(){
	/* PRETTY PHOTO */
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	/* Attach attributes for Flickr */
	$('div ul#flickr li a').each(function() {
		// $(this).attr('rel','prettyPhoto[flickr]');
		var imgSrc = $(this).children().attr('src');
		$(this).attr('href',imgSrc.replace('_s.jpg','.jpg'));
	});
	
	/* Attach attributes for Vimeo 
	$('div#vimeo ul li a').each(function() {
		$(this).attr('rel','prettyPhoto[vimeo]');
		var videoUrl = $(this).attr('href');
		$(this).attr('href',videoUrl.replace('http://vimeo.com/','http://vimeo.com/moogaloop.swf?flashvars=clip_id='))
	});
	*/
	/* Attach attributes for YouTube links 
	$('div#youtube ul li a').each(function() {
		$(this).attr('rel','prettyPhoto[youtube]');
	});
	*/
	
	/* PNG FIX FOR IE */
	$(document).pngFix()
})

// Function to toggle each textarea code field
function toggleHTMLCode(id){
	$(document).ready(function(){
		$(id).toggle();
	});
}

// Function to hide the textareas and attach the toggle function to each link
function attacHTMLCode(id){
	// Hide the texarea containers
	$(id).hide();
	
	// Loop through each link
	$('div#buttons a').each(function(){
		// Attach the onclick events
		$(this).click(function(){
		
			// Get the target id for each container from the <a> rel attribute
			var target = 'div#code_'+$(this).attr('rel');
		
			// Run the toggle function
			toggleHTMLCode(target);
		});
	});
}

// Function to control the main image slideshow on the homepage
function slideShow(){
	$(document).ready(function(){
		// Thumbnail 1
	    $("li.switch1").click(function(){
			$(".scroll").animate( { left: "0px" }, 350);
			$("li.switch1").css('opacity',1.0);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 2
	    $("li.switch2").click(function(){
			$(".scroll").animate( { left: "-201px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',1.0);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 3
	    $("li.switch3").click(function(){
			$(".scroll").animate( { left: "-402px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',1.0);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 4
	    $("li.switch4").click(function(){
			$(".scroll").animate( { left: "-603px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',1.0);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 5
	    $("li.switch5").click(function(){
			$(".scroll").animate( { left: "-804px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',1.0);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 6
	    $("li.switch6").click(function(){
			$(".scroll").animate( { left: "-1005px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',1.0);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 7
	    $("li.switch7").click(function(){
			$(".scroll").animate( { left: "-1206px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',1.0);
			$("li.switch8").css('opacity',0.5);
		});
		// Thumbnail 8
	    $("li.switch8").click(function(){
			$(".scroll").animate( { left: "-1407px" }, 350);
			$("li.switch1").css('opacity',0.5);
			$("li.switch2").css('opacity',0.5);
			$("li.switch3").css('opacity',0.5);
			$("li.switch4").css('opacity',0.5);
			$("li.switch5").css('opacity',0.5);
			$("li.switch6").css('opacity',0.5);
			$("li.switch7").css('opacity',0.5);
			$("li.switch8").css('opacity',1.0);
		});
		return false;
	})
}

// Funciton to control the reviews scroller on the homepage
function reviewsScroller (){
	$('#review_content').cycle({ 
		fx:     'scrollHorz',
		timeout: 0, 
		next:   '#next',
		prev:   '#prev'
	})
}

// Funciton to create a show/hide toggling list
function toggleList(id,hide){
	$(document).ready(function(){
		// If set hide the all of the content elements
		if(hide == 1) {
			$(""+id+" div").hide();
		}
		else $(""+id+" h5").addClass("active");
		
		//Switch the "Open" and "Close" state per click
		$(""+id+" h5").toggle(function(){
			$(this).addClass("active");
			}, function () {
			$(this).removeClass("active");
		});
	
		//Slide up and down on click
		$(""+id+" h5").click(function(){
			$(this).next(""+id+" div").slideToggle(200);
		})
	})
}

// Function to validate form
function validateForm(){
	// Remove the inner HTML from the default msg & add error msg
	$('#message').html('<p>You missed out some required fields, please correct them before proceeding:</p>');
	// Add a new class name
	$('#message').addClass("error"); 
	
	// Validate the form
	$("#commentform").validate({
		errorLabelContainer: $("#message"),
		rules: {
			author: { required: true },
			email: { required: true, email: true },
			comment: { required: true }
		},
		messages: {
			author: "- Name is required<br />",
			email: "- A valid email address is required<br />",
			comment: "- A comment is required<br />"
		}
	})
}

// Function to hide email addresses from spammers
function spamFree(id){
	$(function(){
		var target = "."+id;
		var at = / at /;
		var dot = / dot /g;
		var addr = $(target).text().replace(at,"@").replace(dot,".");
		$(target).after('<a href="mailto:'+addr+'" title="Send an email to '+ addr +'">'+ addr +'</a>')
		.hover(function(){window.status="Send an email";}, function(){window.status="";});
		$(target).remove();
	});
}
