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() {

	//Slideshows
	if($j('.slideshow .slides').length) {
		$j('.slides').each(function(i) {
			$j(this).cycle({
				fx: 'fade',
				timeout: 4500,
				speed: 800,
				pause: true,
				pager: '.slide-pager' + i,
				pagerAnchorBuilder: function(idx, slide) {
					return '.slide-pager:eq(' + (i) + ') li:eq(' + (idx) + ') a';
				},
				activePagerClass: 'active',
				cleartype: true,
				cleartypeNoBg: true
			});
		});
	}
	//Feature Slideshow
	if($j('#header-panel .features').length) {
		$j('#header-panel .features').cycle({
			fx: 'fade',
			timeout: 0,
			speed: 800,
			pager: '#feature-pager .slide-pager',
			pagerEvent: 'mouseover',
			activePagerClass: 'active',
			allowPagerClickBubble: true,
			pagerAnchorBuilder: function(idx, slide) {
				return '#feature-pager li:eq(' + (idx) + ') a';
			},
			cleartype: true,
			cleartypeNoBg: true
		});
		function jcarousel_links() {
			$j('.jcarousel-item').click(function() {
				$href = $j(this).children('a').attr('href');
				window.location = $href;
			});
		};
		$j('#feature-pager .slide-pager').jcarousel({
			buttonNextHTML: '<span></span>',
			buttonPrevHTML: '<span></span>',
			scroll: 1,
			vertical: true,
			visible: 4,
			initCallback: jcarousel_links
		});
	}
	//Testimonial Slideshow
	if($j('#testimonials .slides').length) {
		$j('#testimonials .slides').before('<ul id="testimonial-pager" />').cycle({
			fx: 'fade',
			timeout: 0,
			speed: 800,
			pause: true,
			pager: '#testimonial-pager',
			pagerAnchorBuilder: function(idx, slide) {
				return '<li><a href="#testimonial-' + (idx + 1) + '">' + (idx + 1) + '</a></li>';
			},
			activePagerClass: 'active',
			cleartype: true,
			cleartypeNoBg: true
		});
	}

	//SimpleModal
	if($j('a.modal-image,a.modal-media').length) {
		$j('a.modal-image').click(function(e){
			e.preventDefault();
			$href = $j(this).attr('href');
			//alert($href);
			var img = new Image();
			img.onload = function () {
				$j.modal('<img src="'+$href+'" />',{
					autoResize: true,
					closeHTML: '<span class="simplemodal-close" title="Close">Close</span>',
					maxHeight: '90%',
					maxWidth: '90%',
					overlayClose: true,
					zIndex: 1080
				});
			};
			img.src = $href;
		});
		$j('a.modal-media').click(function(e){
			e.preventDefault();
			$href = $j(this).attr('href');
			//alert($href);
			$j.modal('<iframe id="simplemodal-iframe-content" src="' + $href + '" frameborder="0" style="border:0">',{
				autoResize: true,
				closeHTML: '<span class="simplemodal-close" title="Close">Close</span>',
				maxHeight: '90%',
				maxWidth: '90%',
				onShow: function(d){
					var sm = this;
					$j('#simplemodal-iframe-content').load(function(){
						$h = $j(this).contents().height()+8;
						$w = $j(this).contents().width()+8;
						$j(this).height($h).width($w);
						d.container.animate({height:$h,width:$w},0,function(){
							sm.setPosition();
						});
					});
					d.container.fadeIn('normal');
				},
				overlayClose: true,
				zIndex: 1080
			});
		});
	}

	//HTML5 media
	if($j('video,audio').length) {
		$j('video,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');
	$j('.build-your-own a').filter(function() {
		var currentURL = window.location.toString().split('/');
		return $j(this).attr('href') == currentURL[currentURL.length-1];
	}).parents('li').addClass('current-step');

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

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

	//Validate
	if($j('form').length){
		$j.validator.addMethod('phone-us', function(phone_number, element) {
			phone_number = phone_number.replace(/\s+/g, ''); 
			return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, 'Please enter a valid phone number.');
		$j('form .phone-us').mask('(999) 999-9999');
		$j('form').validate();
		$j('form .reset').click(function(){
			$j('label.error').remove();
			$j('.error').removeClass('error');
		});
	}

	//Form submit
	$j('#form-tour-request').submit(function () {
		var str = $j(this).serialize();
		$j.ajax({
			type: 'POST',
			url: $j(this).attr('action'),
			data: str,
			success: function (msg) {
				$j('#form-tour-request .form-message').ajaxComplete(function (event, request, settings) {
					if (msg == 'OK') {
						// Message Sent? Show the 'Thank You' message and hide the form
						result = '<div class="success">Your message has been sent. Thank you!</div>';
						$j('#form-tour-request').contents().filter('label,input,textarea,br').hide();
					} else {
						result = '<div class="success">' + msg + '</div>';
					}
					$j(this).html(result);
				});
			}
		});
		return false;
	});

});

