/* Base Path */
document.base_path = '/';


/* Set up defaults */
$('#form_email').val('enter email here');


/* Validate string as an email address */
function valid_email( email )
{
	at_pos = email.indexOf( '@' );
	stop_pos = email.lastIndexOf( '.' );
	
	if( '' == email || -1 == at_pos || -1 == stop_pos || stop_pos < at_pos || 1 == ( stop_pos - at_pos ) ) return false;
	else return true;
}

/* jQuery On Document Ready */
$(document).ready(
	function()
	{
		/* Email Collection */
		if( $('#form-email-collection').length )
		{
			// Clear value on focus
			$('#form_email').focus(
				function()
				{
					if( 'enter email here' == $(this).val() ) $(this).val('');
				}
			);

			// Return value on blur
			$('#form_email').blur(
				function()
				{
					if( '' == $(this).val() ) $(this).val('enter email here');
				}
			);

			// Validate and Submit data on form submit
			$('#form-email-collection').submit(
				function()
				{
					var email = $('#form_email').val();

					if( '' == email || 'enter email here' == email || ! valid_email( email ) )
					{
						
						jQuery.facebox({ ajax: document.base_path + 'newsletter/ajax_error' });
					}
					else
					{
						$.ajax(
							{
								type: "POST",
								url: document.base_path + 'newsletter/ajax',
								data: 'email=' + email,
								success: function( msg )
								{
									$('#form_email').val('enter email here');
									jQuery.facebox({ ajax: document.base_path + 'newsletter/ajax_thankyou' });
								}
							}
						);
						pageTracker._trackPageview('/cta/email');
					}

					return false;
				}
			);
		}	
		
		
		/* Tour Category Hovers */
		if( $('#tour-categories').length )
		{
			$('#tour-categories li a').hover(
				function()
				{
					$(this).children('span.toggle').css('top', '0px').css('display', 'none').fadeIn(1000);
				},
				function()
				{
					$(this).children('span.toggle').fadeOut('fast');
				}
			);
		}
		
		
		/* Home Page Slideshow 
		if( $('#slideshow').length )
		{
			$('#slideshow').cycle(
				{
					timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
				    speed:         1000,  // speed of the transition (any valid fx speed value) 
				    next:          null,  // id of element to use as click trigger for next slide 
				    prev:          null,  // id of element to use as click trigger for previous slide 
				    before:        null,  // transition callback (scope set to element to be shown) 
				    after:         null,  // transition callback (scope set to element that was shown) 
				    height:       'auto', // container height 
				    sync:          1,     // true if in/out transitions should occur simultaneously 
				    fit:           0,     // force slides to fit container 
				    pause:         0,     // true to enable "pause on hover" 
				    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
				    slideExpr:     null   // expression for selecting slides (if something other than all children is required)
				}
			);
		}
		*/
		
		if( $('#home-slideshow').length )
		{
			$('#home-slideshow').media(
				{
					width : 931, 
					height : 406, 
					autoplay : true,
					src : '/_assets/flash/slideshow.swf'
				}
			);
		}
		
		/* Photo Galleries for the Tours */
		if( $('ul.gallery').length )
		{
			$('ul.gallery').galleria(
				{
					history   : false, // activates the history object for bookmarking, back-button etc.
					clickNext : true, // helper for making the image clickable
					insert    : '#main_image', // the containing selector for our main image
					onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

						// fade in the image & caption
						if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
							image.css('display','none').fadeIn(1000);
						}
						caption.css('display','none').fadeIn(1000);

						// fetch the thumbnail container
						var _li = thumb.parents('li');

						// fade out inactive thumbnail
						_li.siblings().children('img.selected').fadeTo(500,0.3);

						// fade in active thumbnail
						thumb.fadeTo('fast',1).addClass('selected');

						// add a title for the clickable image
						image.attr('title','Next image >>');
					},
					onThumb : function(thumb) { // thumbnail effects goes here

						// fetch the thumbnail container
						var _li = thumb.parents('li');

						// if thumbnail is active, fade all the way.
						var _fadeTo = _li.is('.active') ? '1' : '0.3';

						// fade in the thumbnail when finnished loading
						thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

						// hover effects
						thumb.hover(
							function() { thumb.fadeTo('fast',1); },
							function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
						)
					}
				}
			);
		}
		
		/* Travelers Tool Box */
		
		if( $('#currency-convert-tool').length )
		{
			$('#currency-convert-tool').submit(
				function()
				{
					var v = $('#form_value').val();
					var r = $('#form_conversion').val();
					var r2 = $('#form_conversion_2').val();
					
					if( '' == v )
					{
						alert( 'You must include a value to calculate.' );
					}
					else
					{	
						//alert( document.base_path + 'travelers-toolbox/ajax-currency/' + r + '/' + r2 + '/' + v );					
						$.ajax(
							{
								type: "POST",
								url: document.base_path + 'travelers-toolbox/ajax-currency/' + r + '/' + r2 + '/' + v,
								success: function( msg )
								{
									$('#form_result').text(msg);
									//alert( msg );
								}
							}
						);
					}
					
					return false;
				}
			);
		}
		
		if( $('#metric-tool').length )
		{
			$('#metric-tool').submit(
				function()
				{
					var v = $('#form_metric_value').val();
					var r = $('#form_metric_type').val();
					
					if( '' == v )
					{
						alert( 'You must include a value to calculate.' );
					}
					else
					{	
						//alert( document.base_path + 'travelers-toolbox/ajax-currency/' + r + '/' + r2 + '/' + v );					
						$.ajax(
							{
								type: "POST",
								url: document.base_path + 'travelers-toolbox/ajax-metric/',
								data: 'form_metric_type=' + r + '&form_metric_value=' + v,
								success: function( msg )
								{									
									$('#currency_total').text(msg);
									
									//alert( msg );
								}
							}
						);
					}
					
					return false;
				}
			);
		}
		
	}
);
