var $j = jQuery.noConflict();

/**
 * Email Defuscator - jQuery plugin 1.0 alpha
 * Copyright (c) 2007 Joakim Stai
 * Dual licensed under the MIT and GPL licenses:
 * @http://www.opensource.org/licenses/mit-license.php
 * @http://www.gnu.org/licenses/gpl.html
 * Converts obfuscated email addresses into normal, working email addresses.
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @descr Converts obfuscated email addresses into normal email addresses
 */
jQuery.fn.defuscate=function(settings){settings=jQuery.extend({link:true},settings);var regex=/\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;return this.each(function(){if($j(this).is('a[@href]')){$j(this).attr('href',$j(this).attr('href').replace(regex,'$1@$2'));var is_link=true;}
$j(this).html($j(this).html().replace(regex,(settings.link&&!is_link?'<a href="mailto:$1@$2">$1@$2</a>':'$1@$2')));});}

//Functions
$j(document).ready(function(){

	//Cycle Slideshows
	if($j('.slideshow').length) {
		$j('.slideshow .slides').each(function(){
			$j(this).cycle({ 
				fx: 'fade',
				pause: 0,
				random: 1,
				timeout: 4000
			});
		});
	}
	if($j('.facilities-slideshow').length) {
		$j('.facilities-slideshow .slides').each(function(){
			$j(this).cycle({ 
				fx: 'fade',
				pause: 0,
				timeout: 0,
				pager: '#menu-facilities ul',
				activePagerClass: 'current-menu-item',
				pagerAnchorBuilder: function(idx, slide) {
					return '#menu-facilities li:eq(' + (idx) + ') a';
				},
				cleartype: true,
				cleartypeNoBg: true
			});
		});
	}

	if($j('form#savings-calculator').length) {
		$j('form#savings-calculator input#cargo-value').keyup(function() {
			var v=0;
			if($j(this).val().length > 0) {
				v = ($j(this).val()/10000)*12.5;
			}
			if(!isNaN(v)){
				$j('#savings-calculation').html('$' + v.toFixed(2));
			}else{
				alert("We're sorry, there was a calculation error. Please try again, entering only numbers.");
			}
		});
		$j('form#savings-calculator').submit(function() {
			return false;
		});
	}

	//Video link
	if($j('a.video-overview-link').length) {
		$j('a.video-overview-link').click(function() {
			$j('#sidebar-media').fadeToggle();
			$j('#sidebar-media video,#sidebar-media audio').mediaelementplayer({
				enableAutosize: true,
				features: ['playpause','progress','current','duration','volume'],
				startVolume: 0.5
			});
			return false;
		});
		$j('#sidebar-media video,#sidebar-media audio').bind('ended', function() {
			$j('#sidebar-media').fadeOut();
		});
		$j(document).click(function() {
			$j('#sidebar-media').fadeOut();
		});
		$j('#sidebar-media').click(function(e) {
			e.stopPropagation();
		});
	}

	//HTML5 media
	if($j('video,audio').length) {
		$j('#content video,#content audio').mediaelementplayer({
			enableAutosize: true,
			features: ['playpause','progress','current','duration','volume'],
			startVolume: 0.5
		});
	}

	//Add class to current menu item based on URL
	$j('.menu a').filter(function() {
		var currentURL = window.location.toString().split('/');
		return $j(this).attr('href') == currentURL[currentURL.length-1];
	}).parents('li').addClass('current-menu-item');
	if($j('.menu a').parents('li').hasClass('current-menu-item') == false) {
      $j('.menu a[href=index.php]').parents('li').addClass('current-menu-item');
    }

	//Open external websites in a new window
	$j('a').filter(function() {
	   return this.hostname && this.hostname !== location.hostname;
	}).addClass('external');
	if($j('a[href*=.pdf]').length){
		$j('a[href*=.pdf]').addClass('pdf');
	}
	if($j('a.external,a.pdf').length){
		$j('a.external,a.pdf').click(function(){
			window.open(this.href);
			return false;
		});
	}

	//Defuscate
	if($j('.email').length){
		$j('.email').defuscate()
	}

});
