$(document).ready(function(){
	// Anti-spam email
	$('a.email').each(function(i) {
		var text = $(this).text();
		var address = text.replace(" (at) ", "@");
		$(this).attr('href', 'mailto:' + address);
		$(this).attr('title', address);
		$(this).text(address);
	});
	
	// Opacity
	$('.opacity').hover(
		function () {
			$('img', this).stop().fadeTo(200, 0.7);
		}, 
		function () {
			$('img', this).stop().fadeTo(400, 1.0);			
		}
	);
	
	// Qtip
	$("#navigation a#tip1").qtip({
		content: {
   			text: '<strong>View &amp; Download</strong> - Freestanding &amp; Built-In Appliances Brochure.',
   			title: { text: 'The Collection' }
		},
		
		position: {
   			corner: {
      			target: 'topMiddle',
      			tooltip: 'bottomMiddle'
			},
			adjust: { x: 0, y: -2 }
		},
		
		style: { 
			border: { width: 1, radius: 5 },
			title: { 'font-size': 12 },
			'font-size': 11,
			tip: { corner: 'bottomMiddle' },
			name: 'dark',
			width: 200
		},
		
		show: {
			effect: {
				type: 'fade',
				length: 300
			}
		},
		
		hide: {
			effect: { 
				type: 'fade',
				length: 500
			}
		}
	});
	
	$(".hotbox a#tip2").qtip({
		content: {
   			text: '<strong>View &amp; Download</strong> - Freestanding &amp; Built-In Appliances Brochure.',
   			title: { text: 'The Collection' }
		},
		
		position: {
   			corner: {
      			target: 'leftMiddle',
      			tooltip: 'rightMiddle'
   			},
			adjust: { x: -4, y: -17 }
		},
		
		style: { 
			border: { width: 1, radius: 5 },
			title: { 'font-size': 12 },
			'font-size': 11,
			tip: { corner: 'rightMiddle' },
			name: 'light',
			width: 200
		},
		
		show: {
			effect: {
				type: 'fade',
				length: 300
			}
		},
		
		hide: {
			effect: { 
				type: 'fade',
				length: 500
			}
		}
	});
	
	// Slider
	$('#slider').s3Slider({
		timeOut: 4000
	});
	
	// Links
	$("a[href^='http:']").not("[href*='magicfountains.co.uk']").attr('target','_blank');
	$("a[rel='external']").attr('target','_blank');
	// Additional analytics
	$("a[href^='http:']").not("[href*='magicfountains.co.uk']").click(function () {
	// $('a[href^="http"]:not([href*="://' + document.domain + '"])').click(function () {
		var url = $(this).attr("href");
		pageTracker._trackEvent('Custom', 'External', url);
		_gaq.push(['_trackEvent', 'Custom', 'External', url]);
		// pageTracker._trackPageview(url);
	});
	 
	$("a[href*='mailto:']").click(function () {
		var url = $(this).attr("href");
		pageTracker._trackEvent('Custom', 'E-mail', url);
		_gaq.push(['_trackEvent', 'Custom', 'E-mail', url]);
		// pageTracker._trackPageview(url);
	});
	 
	$("a[href*='.pdf']").click(function () {
		var url = $(this).attr("href");
		pageTracker._trackEvent('Custom', 'Downloads', url);
		_gaq.push(['_trackEvent', 'Custom', 'Downloads', url]);
		// pageTracker._trackPageview(url);
	});
	 
	$("a[rel='track']").click(function(){
		var url = $(this).attr("href");
		pageTracker._trackEvent('Custom', 'Track', url);
		_gaq.push(['_trackEvent', 'Custom', 'Track', url]);
		// pageTracker._trackPageview(url);
	});
	
	// Contact
	if($('#form')) {
		// Example
		$('#email, #name, #telephone, #enquiry').example(function() {
			$(this).val("");
			return $(this).attr('title'); 
		});
		// Status
		$('.form').prepend('<div class="status"></div>');
		// Validation
		$("#submit").click(function(){
			// Validate
			var $continue = true;
			// Name
			if($("#name").attr("value") == "");
			if($("#name").attr("value") == "Name")
		    {
		      $("#name").addClass("alert");
		      $continue = false;
		    }
		    else { $("#name").removeClass("alert"); }
			
				// Email
		    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		    
		    if($("#email").attr("value") == "")
		    {
		      $("#email").addClass("alert");
		      $continue = false;
		    }
		    else if (!filter.test($("#email").attr("value"))) { $("#email").addClass("alert"); $continue = false; }
		    else { $("#email").removeClass("alert"); }
			
			// Telephone
			if($("#telephone").attr("value") == "");
			if($("#telephone").attr("value") == "Telephone")
		    {
		      $("#telephone").addClass("alert");
		      $continue = false;
		    }
		    else{ $("#telephone").removeClass("alert"); }
		    
		    // Captcha
		    /*
			var random1 = parseInt($("#random1").attr("value"));
			var random2 = parseInt($("#random2").attr("value"));
			var total = random1 + random2;
			
			if($("#captcha").attr("value") == "")
			{
			  $("#captcha").addClass("alert");
			  $continue = false;
			}
			else if ($("#captcha").attr("value") != total) {
			  $("#captcha").addClass("alert");
			  $continue = false;
			}
			else { $("#captcha").removeClass("alert"); }
			*/
		    
			// Error
		    if ($continue == false){
				$(".status").hide().html('<div id="error"><p><strong>Whoops! An error has occurred.</strong></p><p>Please check the required fields &amp; resubmit the form.</p></div>').fadeIn("slow");
				return false;
			}
		});
	}
	
});

