// ***************************************************************
// ***************************************************************
// Search Javascript

// api location
var api = 'http://booking.greatcanadianholidays.com/api'; 
var image_path = 'http://booking.greatcanadianholidays.com/admin/_bank/graphics/';
var pTO = null;
var print_win = null;

// menu item labels
var multi_day_name = 'Multi-Day Getaways';
var single_day_name = 'One Day Getaways';
var coach_cruise_name = 'Coach & Cruise';

// note text
var multi_note = '<p class="note"><strong>Note:</strong> All Reservations made online must be paid for by credit card (VISA or MasterCard only)'
				  +' to complete the reservation process. Great Canadian Holidays is not responsible for errors and omissions in '
				  +' the tour descriptions.</p>';
				 
var single_note = '<p class="note"><strong>Note:</strong> All Reservations made online must be paid for by credit card (VISA or MasterCard only)'
				  +' to complete the reservation process. Great Canadian Holidays is not responsible for errors and omissions in '
				  +' the tour descriptions.</p>'

// build query string
var query = get_query_string();

// ***************************************************************
// ***************************************************************
// Search Main

$(document).ready(function(){
	
	// alert('here');
	//create site navigation
	// Firt error
	make_tour_menu();

	//init search
	init_search_forms();
		
	//show the page
	// First error on print
	show_page();
});


// ***************************************************************
// ***************************************************************
// Add Content
// Add content to the main container

function add_content(html)
{
	$('#content').append(html);	
}

// ***************************************************************
// ***************************************************************
// Make Tour Menu

function make_tour_menu()
{
	var menu ='';
	
	var single_cruise = '';
	var multi_cruise = '';
	
	// ***************************************************************
	//add coach and cruise menu
	var menu = '';
	menu+='<ul class="">'
		+ '<li>'
		+	'<div>'
		+		'<a href="/tour/list/type/multi/category/cruise" class="MenuBarItemSubmenu">Multi-Day</a>'
		+	'</div>'
		+	'<ul id="mc" class=""></ul>'
		+ '</li>'
		+ '<li>'
		+	'<div>'
		+		'<a href="/tour/list/type/single/category/cruise" class="MenuBarItemSubmenu">One Day</a>'
		+	'</div>'
		+	'<ul id="sc" class=""></ul>'
		+ '</li>'
		+'</ul>';
		
	//find the menu items and 
	var $menu = $('div#main_menu ul');
	var $menu_link = $menu.find('li a:contains("'+coach_cruise_name+'")');
	
	$menu_link.attr( 'href', 'javascript:void(0);' );
	
	var $menu_li = $menu_link.parent().parent();
	$($menu_li).append(menu);
		
	// ***************************************************************
	//get multi day tour categories
	$.get( api+'/tour.menuList.json?callback=?', function(data){
		if( data && data.items ){
			if( data.items.length > 0 ) {
				
				menu= '<ul class="">';
		
				$.each( data.items, function(i, item){		 
					menu+='<li>'
						+	'<div>'
						+		'<a href="/tour/list/type/multi/category/'+item.type_url+'" class="MenuBarItemSubmenu">'+item.type_name+'</a>'
						+	'</div>'
					
					//	check if there are tour items
					if( item.tour_items && item.tour_items.length > 0 ) {
						menu+='<ul class="">';
						$.each( item.tour_items, function(i, tour_items){
							var row = '';
							if( i < 7 )
							{						  
								row+='<li>'
									+	'<div>'
									+		'<a href="/tour/details/type/multi/name/'+tour_items.tours_web_url+'">'+tour_items.tours_destination+'</a>'
									+	'</div>'
									+'</li>';
										
								//save the multi cruise	
								if( item.type_url == 'cruise' )
									multi_cruise+=row;
							}
							else if( i == 7 )
							{						  
								row+='<li>'
									+	'<div>'
									+		'<a href="/tour/list/type/multi/category/'+item.type_url+'">View more Multi-Day Getaways...</a>'
									+	'</div>'
									+'</li>';
							}
							menu+=row;	
						});
						menu+='</ul>';
					}
					
					menu+='</li>';
				});	
				
				menu += '</ul>';
				
				//add multi cruise menu
				if(multi_cruise!='')
					$('#mc').html(multi_cruise);
					
				
				//find the menu items and 
				var $menu = $('div#main_menu ul');
				var $menu_link = $menu.find('li a:contains("'+multi_day_name+'")');
				$menu_link.addClass('MenuBarItemSubmenu');
				var $menu_li = $menu_link.parent().parent();
				$($menu_li).append(menu);
			}	
		}
		
		// ***************************************************************
		// get single day categories
		$.get( api+'/trip.menuList.json?callback=?', function(data){
			if( data && data.items ){
				if( data.items.length > 0 ) {
					
					menu= '<ul class="">';
			
					$.each( data.items, function(i, item){		 
						menu+='<li>'
							+	'<div>'
							+		'<a href="/tour/list/type/single/category/'+item.type_url+'" class="MenuBarItemSubmenu">'+item.type_name+'</a>'
							+	'</div>';
						
						//	check if there are tour items
						if( item.trip_items && item.trip_items.length > 0 ) {
							menu+='<ul class="">';
							$.each( item.trip_items, function(i, trip_items){
								var row = '';
								
								if( i < 7 )
								{
									row +='<li>'
										+	'<div>'
										+		'<a href="/tour/details/type/single/name/'+trip_items.trip_web_url+'">'+trip_items.trip_destination+'</a>'
										+	'</div>'
										+'</li>';
										
									//save the single cruise menu
									if( item.type_url == 'cruise')
										single_cruise += row;
								}
								else if( i == 7 )
								{
									row +='<li>'
										+	'<div>'
										+		'<a href="/tour/list/type/single/category/'+item.type_url+'">More One Day Getaways...</a>'
										+	'</div>'
										+'</li>';
								}
								menu+=row;
								
							});
							menu+='</ul>';
						}
							
						menu+='</li>';
					});	
					
					menu += '</ul>';
					
					//add single cruise menu
					if(single_cruise!='')
						$('#sc').html(single_cruise);
					
					//find the menu items and 
					var $menu = $('div#main_menu ul');
					var $menu_link = $menu.find('li a:contains("'+single_day_name+'")');
					var $menu_li = $menu_link.parent().parent();
					$($menu_li).append(menu);
					
				}	
			}
			
			//refresh the menu
			$menu_content = $('div#main_menu').html();
			$('div#main_menu').html('').attr('id', 'main_menu_jq');
			$('div#main_menu_jq').html($menu_content);
			var main_menu_jq = Spry.Widget.MenuBar('main_menu_jq');
			
		}, 'json');	
		
	}, 'json');		
	
	
	
}


// ***************************************************************
// ***************************************************************
// Get query string variables

function get_query_string()
{
	var qs = new Object();
	var host = location.hostname;
	var url = location.href;
	var uri = url.replace('http://', '').replace('https://','').replace( host, '' );
	qs['_host'] = host;
	
	//build query string
	if( uri == '/' )
		qs['page'] = 'index';	
	else {
		var parts = uri.split('/');		
		if( parts[0].length == 0 ) parts.shift();
		
		if( parts.length == 1 )
			qs['page'] = parts[0];
		else {
			qs['page_action'] = 'index';
			qs['page'] = parts.shift();
			
			//check if anything is left for hte page
			if( parts.length > 0 && parts[0].length != 0 )
				qs.page_action = parts.shift();
			
			//if anything is left then start pairing
			if( parts.length > 0 && parts[0].length != 0 ) {
				//loop thought the segments find the request page and create the query string
				for( var i=0; i <= parts.length-1; i+=2 ) {
					var varname = parts[i];
					if( varname ) {
						var value = ( parts[( i+1 )] )? parts[( i+1 )] : '' ;
						qs[varname] = value;
					}
				}
			}
		}
		
	}
	
	return qs;
}


// ***************************************************************
// ***************************************************************
// ***************************************************************
// Pages

function show_page()
{	

	// check for the tour page
	if( query.page == 'tour' ){
		// main page switch
		switch( query.page_action ){
			// Category List Page
			case 'list': show_list_page(); break;
			// Details Page
			case 'details': show_details_page(); break;
			// Search Page
			case 'search': show_search_page(); break;
			// Departure Page 
			case 'departure': show_departure_page(); break;
		}
	} else if( query.page == 'print' ) {
		// main page switch
		switch( query.page_action ){
			// Details Page
			case 'details': show_print_details_page(); break;
		}	
	}
}


// ***************************************************************
// ***************************************************************
// Result Page

function show_departure_page()
{
	// add page title
	$('h1.page-title').html('Multi-Day Tour Departure Points');
	
	//get tour departure locations
	$.get(api+'/tour.pickupList.json?callback=?', function(data){
	
		var list = '<p>We offer a wide variety of pick-ups spanning Southern Ontario for our overnight tours. Pickup points are chosen at convenient central spots in most cities and towns.</p>'
			+ '<p>Due to high cost of fuel, we will now be charging $15.00 one way/$30.00 round trip CAD per person for pick-up service outside the main highways (401 and QEW) in our licensed area. This is in order to keep our tour prices as low as possible and to help defray the high cost of operating our shuttle vans. Below is a complete list of our pick up locations:</p>'
			+ '<img src="/templates/images/pickup-locations.jpg" width="647" height="414" />'
			+ '<br /><br />';
		
		list += '<h1>Complimentary Pick-ups</h1>'
			+ '<ul>';
		
		$.each( data.items, function(i, item){		
			if( item.pickup_price_exc == 0 )
				list += '<li>'+item.pickup_description+'<br /><hr /></li>';
		});
		
		list += '</ul>';
		
		list += '<h1>Surcharge Pick-ups</h1>'
			+ '<ul>';
		
		$.each( data.items, function(i, item){	
			if( item.pickup_price_exc > 0 )
				list += '<li>'+item.pickup_description+'<br /><hr /></li>';
		});
		
		list += '</ul>';
		
		add_content(list);

	},'json');
}


// ***************************************************************
// ***************************************************************
// Result Page

function show_list_page()
{
	// ***************************************************************
	// ***************************************************************
	// Multi Day List
	if( query.type == 'multi' ) {
		
		// add page title
		$('h1.page-title').html('Multi-Day Getaways');	
		
		// get category name record to append to the title
		$.get(api+'/tour.categoryByUrl.json?&name='+query.category+'&callback=?', function(data){
			if(data && data.entry && data.entry[0]){
				entry = data.entry[0];
				$('h1.page-title').append(' - <span class="sub-title">'+entry.type_name+'</span>');
			}
			
			// ***************************************************************
			// Get the tour list
			
			// filter
			var filter = '';
			if( query.category ) filter+='&type='+query.category;
			if( query.order ) filter+='&order='+query.order;
			if( query.direction ) filter+='&direction='+query.direction;
	
			//get tours
			$.get(api+'/tour.getList.json?start=0&records=50&'+filter+'&callback=?', function(data){
				if( data && data.items.length > 0 ) {
					
					// add note content
					add_content(multi_note);
					
					var sort_link = '/tour/list/type/multi';
					if( query.category ) sort_link+='/category/'+query.category;
					
					// make tour list
					var list='<table class="tour-list">'
							+	'<tr>'
							+		'<th style="width:250px;">'
							+			'<a href="'+sort_link+'/order/tours_destination/direction/'+(query.order=='tours_destination' && query.direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Name</a>'
							+		'</th>'
							+		'<th style="width:120px; text-align:center">'
							+			'<a href="'+sort_link+'/order/tours_number_days/direction/'+(query.order=='tours_number_days' && query.direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by # of days</a>'
							+		'</th>'
							+		'<th>'
							+			'<a href="'+sort_link+'/order/tours_date_departure/direction/'+(query.order=='tours_date_departure' && query.direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Date</a>'
							+		'</th>'
							+		'<th>&nbsp;</th>'
							+	'</tr>';
					
					$.each( data.items, function(i, item){
						list += add_multi_item(i, item ); 
					});
					
					list 	+=	'</table>';
					
					if( data.items.length == 50 ) list += '<br /><a href="#" id="more-results">View 25 More Results</a>';
					
					add_content(list);
					
					$('#more-results').click(function() {
						add_multi_more();
						return false;
					});
					
				} else if( data && data.items.length == 0 ){
					add_content('<p class="no-results">There are currently no tours '+( entry.type_name ? 'matching <strong>'+entry.type_name+'</strong>' : '')+'. Please search using the sidebar or try again later.</p>');
				}
				
			},'json');
			
		},'json');	
	
		
	// ***************************************************************
	// ***************************************************************
	// Single Day List
	} else if( query.type == 'single' ) {
		
		// add page title
		$('h1.page-title').html('One Day Getaways');	
		
		// get category name record to append to the title
		$.get(api+'/trip.categoryByUrl.json?&name='+query.category+'&callback=?', function(data){
			if(data && data.entry && data.entry[0]){
				entry = data.entry[0];
				$('h1.page-title').append(' - <span class="sub-title">'+entry.type_name+'</span>');
			}
		
			// ***************************************************************
			// Get the tour list
			
			// filter
			var filter = '';
			if( query.category ) filter+='&type='+query.category;
			if( query.order ) filter+='&order='+query.order;
			if( query.direction ) filter+='&direction='+query.direction;
	
			//get tours
			$.get(api+'/trip.getList.json?start=0&records=50'+filter+'&callback=?', function(data){
				if( data && data.items.length > 0 ) {
					
					// add note content
					add_content(single_note);
					
					var sort_link = '/tour/list/type/single';
					if( query.category ) sort_link+='/category/'+query.category;
					
					// make tour list
					var list='<table class="tour-list">'
							+	'<tr>'
							+		'<th style="width:250px;">'
							+			'<a href="'+sort_link+'/order/trip_destination/direction/'+(query.order=='trip_destination' && query.direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Name</a>'
							+		'</th>'
							+		'<th>'
							+			'<a href="'+sort_link+'/order/trip_date_depart/direction/'+(query.order=='trip_date_depart' && query.direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Date</a>'
							+		'</th>'
							+		'<th>&nbsp;</th>'
							+	'</tr>';
					
					$.each( data.items, function(i, item){
						list += add_single_item(i, item ); 
					});					
					
					list +='</table>';
					
					if( data.items.length == 50 ) list += '<br /><a href="#" id="more-results">View 25 More Results</a>';
					
					add_content(list);
					
					$('#more-results').click(function() {
						add_single_more();
						return false;
					});
					
				} else if( data && data.items.length == 0 ){
					add_content('<p class="no-results">There are currently no trips '+( entry.type_name ? 'matching <strong>'+entry.type_name+'</strong>' : '' )+'. Please search using the sidebar or try again later.</p>');
				}
			},'json');
		
		},'json');	
			
	}
}

function add_single_item(i, item){
		
	// this is the link to the tour details
	var details_link = '/tour/details/type/single/name/'+item.trip_web_url;
	
	//share url
	var share_url = 'http://'+query._host+'/tour/details/type/single/name/'+item.trip_web_url;
	share_url = $.base64.encode(share_url).replace('=','');
	
	var facebook_share = '/share/with/facebook/title/'+escape(item.trip_destination)+'/url/'+share_url;
	var twitter_share = '/share/with/twitter/title/'+escape(item.trip_destination)+'/url/'+share_url;
	
	list='<tr class="'+((i%2==1) ? 'alt' : '' )+'">'
		+	'<td><a href="'+details_link+'" class="details">'+item.trip_destination+'</a></td>'
		+	'<td>'+item.trip_date_clean+'</td>'
		+	'<td style="width:80px; text-align:right;">'
		+		'<a href="'+details_link+'" class="list-details">VIEW DETAILS</a>'
		+		'<div class="list-share">SHARE <a target="_blank" href="'+twitter_share+'" class="share-twitter">&nbsp;</a> <a target="_blank" href="'+facebook_share+'" class="share-facebook">&nbsp;</a> </div>'
		+	'</td>'
		+'</tr>';
		
	return list;
}

function add_single_more()
{
	// filter
	var filter = '';
	if( query.category ) filter+='&type='+query.category;
	if( query.order ) filter+='&order='+query.order;
	if( query.direction ) filter+='&direction='+query.direction;
	
	// set start and records
	var rows = ( $('.tour-list tr').length ) - 1;
	
	//get tours
	$.get(api+'/trip.getList.json?start='+rows+'&records=26&'+filter+'&callback=?', function(data){
		if( data && data.items.length > 0 ) {
			var list = '';
			$.each( data.items, function(i, item){
				if( i < 25 ) list += add_single_item(i, item ); 
			});
			$('.tour-list').append(list);
			if( data.items.length != 26 ) $('#more-results').css('display','none');
		}
	},'json');
}

function add_multi_item(i, item){
						
	//share url
	var share_url = 'http://'+query._host+'/tour/details/type/multi/name/'+item.tours_web_url;
	share_url = $.base64.encode(share_url).replace('=','');
	
	var facebook_share = '/share/with/facebook/title/'+escape(item.tours_destination)+'/url/'+share_url;
	var twitter_share = '/share/with/twitter/title/'+escape(item.tours_destination)+'/url/'+share_url;
	// this is the link to the tour details
	var details_link = '/tour/details/type/multi/name/'+item.tours_web_url;
	
	list='<tr class="'+((i%2==1) ? 'alt' : '' )+'">'
		+	'<td><a href="'+details_link+'" class="details">'+item.tours_destination+'</a></td>'
		+	'<td align="center">'+item.tours_number_days+'</td>'
		+	'<td>'+item.tours_date_clean+'</td>'
		+	'<td style="width:80px; text-align:right;">'
		+		'<a href="'+details_link+'" class="list-details">VIEW DETAILS</a>'
		+		'<div class="list-share">SHARE <a target="_blank" href="'+twitter_share+'" class="share-twitter">&nbsp;</a> <a target="_blank" href="'+facebook_share+'" class="share-facebook">&nbsp;</a> </div>'
		+	'</td>'
		+'</tr>';
	return list;
}

function add_multi_more()
{
	// filter
	var filter = '';
	if( query.category ) filter+='&type='+query.category;
	if( query.order ) filter+='&order='+query.order;
	if( query.direction ) filter+='&direction='+query.direction;
	
	// set start and records
	var rows = ( $('.tour-list tr').length ) - 1;
	
	//get tours
	$.get(api+'/tour.getList.json?start='+rows+'&records=26&'+filter+'&callback=?', function(data){
		if( data && data.items.length > 0 ) {
			var list = '';
			$.each( data.items, function(i, item){
				if( i < 25 ) list += add_multi_item(i, item ); 
			});
			$('.tour-list').append(list);
			if( data.items.length != 26 ) $('#more-results').css('display','none');
		}
	},'json');
}

// ***************************************************************
// ***************************************************************
// Show Details Page

function show_details_page()
{
	multi_entry = null;
	single_entry = null;
	
	// ***************************************************************
	// ***************************************************************
	// Multi Day List
	if( query.type == 'multi' ) {
		
		//load the tour details
		$.get(api+'/tour.getEntry.json?tour='+query.name+'&callback=?', function(data){
			if(data && data.entry){
				entry = data.entry;
				
				// add page title
				$('h1.page-title').html(entry.tours_destination);	
				
				// ***************************************************************
				// details content
				var content = '';
				
				//share url
				var email_url = 'http://'+query._host+'/tour/details/type/multi/name/'+entry.tours_web_url;
				var share_url = 'http://'+query._host+'/tour/details/type/multi/name/'+entry.tours_web_url;
				share_url = $.base64.encode(share_url).replace('=','');
				
				//print url
				var print_url = 'http://'+query._host+'/print/details/type/multi/name/'+entry.tours_web_url;
								
				content +='<div id="tour-select" class="left"></div>'
						+ '<div class="right"><a id="tour-print" href="javascript:void(0);" onclick="print_tour(\''+print_url+'\')" class="print-button">Print Tour</a></div>'
						+ '<div class="clear"></div>'
						+ '<div class="tour-detail">'
						+ 	( entry.tours_flyer_filename ? '<div class="tour-image"><img src="'+image_path+entry.tours_flyer_filename+'" border="0"/></div>' : '' )
						+	'<div class="left">'
						+		'<h3>Tour Details</h3><br/>'
						+		'<strong>'+entry.tours_number_days+' Days:</strong> '+entry.tours_date_clean+'<br/>'
						+		'<strong>Tour Number:</strong> '+entry.tours_id+'<br/>'
						+	'</div>'
						+	'<div class="right" style="width:200px; text-align:right;">'
						+		'<a href="'+entry.tours_buynow+'" target="_blank"><img src="/templates/images/booknow.gif" border="0"/></a>'
						+	'</div>'
						+	'<div class="clear"></div>'
						+	'<div class="tour-description">'+entry.tours_description+'</div>'
						+ ( entry.tours_flyer_itinerary ? '<div class="tour-detail">'+entry.tours_flyer_itinerary+'</div><hr/>' : '' )
						+	( entry.tours_flyer_inclusions ? '<strong>Price Includes:</strong><br/>'+entry.tours_flyer_inclusions+'<hr/>' : '' )
						+	( entry.tours_flyer_notes && ( entry.tours_flyer_notes !== '.' ) ? entry.tours_flyer_notes+'<hr/>' : '' )
						+	'<table class="tour-price-list">'
						+		'<tr>'
						+			'<th colspan="4">PRICE PER PERSON</th>'			
						+		'</tr>'
						+		'<tr>'
						+			'<td class="first">Twin: '
						+			( entry.tours_twin_exc > 0 ? '$'+entry.tours_twin_exc : 'n/a' )
						+			'</td>'
						+			'<td>Triple: '
						+			( entry.tours_triple_exc > 0 ? '$'+entry.tours_triple_exc : 'n/a' )
						+			'</td>'
						+			'<td>Quad: '
						+			( entry.tours_quad_exc > 0 ? '$'+entry.tours_quad_exc : 'n/a' ) 
						+			'</td>'
						+			'<td>Single: '
						+			( entry.tours_single_exc > 0 ? '$'+entry.tours_single_exc : 'n/a' )
						+			'</td>'
						+		'</tr>'
						+	'</table>'
						+	'<div class="cnd-price">All prices in Canadian Dollars</div>'
						+	multi_note+'<br/>'
						+	'<div class="share-box">'
						+	'<input type="hidden" id="email_name" name="email_name" value="'+entry.tours_destination+'" />'
						+	'<input type="hidden" id="email_url" name="email_url" value="'+email_url+'" />'
						+	'<div id="email-popup"></div>'
						+ 	'<span>SHARE THIS TOUR WITH OTHERS</span> '
						+		'<a target="_blank" href="/share/with/facebook/title/'+escape(entry.tours_destination)+'/url/'+share_url+'"><img src="/templates/images/facebook_share_large.gif" border="0"/></a>'
						+		'<a target="_blank" href="/share/with/twitter/title/'+escape(entry.tours_destination)+'/url/'+share_url+'"><img src="/templates/images/twitter_share_large.gif" border="0"/></a>'
						+		'<a id="email-popup-link" href="javascript:;"><img src="/templates/images/email_share_large.gif" border="0"/></a>'
						+		'<div class="clear"></div>'
						+	'</div>'
						+'</div>';
				
				// display content
				add_content(content);
				
				// ***************************************************************
				// tour group selector
				var selector = '';
				
				$.get(api+'/tour.getGroupList.json?name='+entry.tours_id+'&callback=?', function(data){
																										  
				    // $('#tour-select').html( api+'/tour.getGroupList.json?name='+entry.tours_destination+'&callback=?' );
																																 
					if( data && data.items.length > 1 ){
						
						selector+='<span>Departure Date: </span> <select onchange="window.location=\'/tour/details/type/multi/name/\'+this.value;" style="width:460px;"> <div class="clear"></div>';
						$.each( data.items, function(i, item){
							selector+='<option value="'+item.tours_web_url+'" '+( item.tours_id == entry.tours_id? 'selected="selected"' : '' )+'>'+item.tours_date_clean+'</option>';							 
						});
						selector+='</select>';
						
					}
					$('#tour-select').html(selector);
				}, 'json');	
				
			}
			
			// Set up email share link
			init_email();
		},'json');
		
		
	// ***************************************************************
	// ***************************************************************
	// Single Day List
	} else if( query.type == 'single' ) {
		
		// document.write(api+'/trip.getEntry.json?trip='+query.name+'&callback=?');
		
		//load the tour details
		$.get(api+'/trip.getEntry.json?trip='+query.name+'&callback=?', function(data){
			if(data && data.entry){
				entry = data.entry;
				
				// add page title
				$('h1.page-title').html(entry.trip_destination);		

				// ***************************************************************
				// details content
				var content = '';
				
				//share url
				var email_url = 'http://'+query._host+'/tour/details/type/single/name/'+entry.trip_web_url;
				var share_url = 'http://'+query._host+'/tour/details/type/single/name/'+entry.trip_web_url;
				share_url = $.base64.encode(share_url).replace('=','');
				
				//print url
				var print_url = 'http://'+query._host+'/print/details/type/single/name/'+entry.trip_web_url;
				
				// main tour details
				content +='<div id="trip-select" class="left"></div>'
						+ '<div class="right"><a href="javascript:void(0);" onclick="print_tour(\''+print_url+'\')" class="print-button">Print Trip</a></div>'
						+ '<div class="clear"></div>'
						+ '<div class="tour-detail">'
						+ 	( entry.trip_web_image? '<div class="tour-image"><img src="'+image_path+entry.trip_web_image+'" border="0"/></div>' : '' )
						+	'<div class="left">'
						+		'<h3>Tour Details</h3><br/>'
						+		'<strong>Tour Number:</strong> '+entry.trip_id+'<br/>'
						+		'<strong>Date:</strong> '+entry.trip_date_clean+'<br/>'
						+	'</div>'
						+	'<div class="right" style="width:200px; text-align:right;">'
						+		'<a href="'+entry.trip_buynow+'" target="_blank"><img src="/templates/images/booknow.gif" border="0"/></a>'
						+	'</div>'
						+	'<div class="clear"></div>'
						+	'<div class="tour-description">'+entry.trip_web_description+'</div>'
						+	'<hr/><br/>'
						+	( entry.trip_web_inclusions ? '<strong>Price Includes:</strong> '+entry.trip_web_inclusions+'<br/><br/>' : '' )
						+	( entry.trip_departure_clean ? '<strong>Departure Points:</strong> '+entry.trip_departure_clean+'<br/><br/>' : '' )
						+	( entry.trip_max_price ? '<strong>Maximum per person price for this tour:</strong> <span class="tour-price">$'+entry.trip_max_price+'</span><br/><br/>' : '' )
						+	'<div class="note-red"><strong>Note:</strong> Prices may vary depending on departure location. You will be given the exact price during the booking process.</div>'
						+	single_note+'<br/>'
						+	'<div class="share-box">'
						+	'<input type="hidden" id="email_name" name="email_name" value="'+entry.trip_destination+'" />'
						+	'<input type="hidden" id="email_url" name="email_url" value="'+email_url+'" />'
						+	'<div id="email-popup"></div>'
						+		'<span>SHARE THIS TOUR WITH OTHERS</span> '
						+		'<a target="_blank" href="/share/with/facebook/title/'+escape(entry.trip_destination)+'/url/'+share_url+'"><img src="/templates/images/facebook_share_large.gif" border="0"/></a>'
						+		'<a target="_blank" href="/share/with/twitter/title/'+escape(entry.trip_destination)+'/url/'+share_url+'"><img src="/templates/images/twitter_share_large.gif" border="0"/></a>'
						+		'<a id="email-popup-link" href="javascript:;"><img src="/templates/images/email_share_large.gif" border="0"/></a>'
						+		'<div class="clear"></div>'
						+	'</div>'
						+ '</div>';
				
				// display content
				add_content(content);
				
				// ***************************************************************
				// tour group selector
				var selector = '';
				$.get(api+'/trip.getGroupList.json?name='+entry.trip_id+'&callback=?', function(data){
																										 
					// $('#trip-select').html( api+'/trip.getGroupList.json?name='+entry.trip_destination+'&callback=?' );
																										 
					if( data && data.items.length > 1 ){
						
						selector+='<span>Departure Date: </span><select onchange="window.location=\'/tour/details/type/single/name/\'+this.value;" style="width:460px;"> <div class="clear"></div>';
						$.each( data.items, function(i, item){
							selector+='<option value="'+item.trip_web_url+'" '+( item.trip_id == entry.trip_id ? 'selected="selected"' : '' )+'>'+item.trip_date_clean+' ('+item.trip_departure_clean+')</option>';							
						});
						selector+='</select>';
						
					}
					$('#trip-select').html(selector);
				}, 'json');	
				
			}
			// Set up email share link
			init_email();
		},'json');
		
	}
}

// ***************************************************************
// ***************************************************************
// Search Result Page

function show_search_page()
{
	// add page title
	$('h1.page-title').html('Search Results');		
	
	// ***************************************************************
	// Add main content
	var content = '';
	content +='<div class="search-content" id="search-single">'
			+	'<div id="single-result-msg" class="result-message"></div>'	
			+	'<div class="result-box">'
			+		'<a id="single-btn" href="javascript:void(0)" onclick="search_toggle(this, \'single-results\');" class="result-button">'+( query.single_order ? 'CLOSE' : 'VIEW RESULTS' )+'</a>'
			+	'</div>'
			+	'<div id="single-results" style="display:'+( query.single_order ? 'block' : 'none' )+';"></div>'
			+ '</div>'
			+ '<div class="search-content" id="search-multi">'
			+	'<div id="multi-result-msg" class="result-message"></div>'	
			+	'<div class="result-box">'
			+		'<a id="multi-btn" href="javascript:void(0)" onclick="search_toggle(this, \'multi-results\');" class="result-button">'+( query.multi_order ? 'CLOSE' : 'VIEW RESULTS' )+'</a>'
			+	'</div>'
			+	'<div id="multi-results" style="display:'+( query.multi_order ? 'block' : 'none' )+';"></div>'
			+ '</div>'
	add_content(content);
	
	// ***************************************************************
	//do search for single day 
	var filter = '';
	if( query.month ) filter+='&month='+query.month;
	if( query.year ) filter+='&year='+query.year;
	if( query.term ) filter+='&keyterm='+query.term;
	if( query.single_order ) filter+='&order='+query.single_order;
	if( query.single_direction ) filter+='&direction='+query.single_direction
	if( query.departure ) filter+='&departure='+query.departure;
	
	$.get(api+'/trip.getList.json?'+filter+'&callback=?',function(data){
		if( data && data.items.length > 0 ){
			
			var sort_link = '/tour/search';
			if( query.month ) sort_link+='/month/'+query.month;
			if( query.year ) sort_link+='/year/'+query.year;
			if( query.term ) sort_link+='/term/'+query.term;
			// make tour list
			var list='<table class="tour-list">'
					+	'<tr>'
					+		'<th style="width:250px;">'
					+			'<a href="'+sort_link+'/single_order/trip_destination/single_direction/'+(query.single_order=='trip_destination' && query.single_direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Name</a>'
					+		'</th>'
					+		'<th>'
					+			'<a href="'+sort_link+'/single_order/trip_date_depart/single_direction/'+(query.single_order=='trip_date_depart' && query.single_direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Date</a>'
					+		'</th>'
					+		'<th>&nbsp;</th>'
					+	'</tr>';
			
			$.each( data.items, function(i, item){
				
				// this is the link to the tour details
				var details_link = '/tour/details/type/single/name/'+item.trip_web_url;
				
				list+='<tr class="'+((i%2==1) ? 'alt' : '' )+'">'
					+	'<td><a href="'+details_link+'" class="details">'+item.trip_destination+'</a></td>'
					+	'<td>'+item.trip_date_clean+'</td>'
					+	'<td style="width:80px; text-align:right;">'
					+		'<a href="'+details_link+'" class="list-details">VIEW DETAILS</a>'
					+	'</td>'
					+'</tr>'
			});
			
			list +='</table>';		
		}
		
		//add message
		$('#single-result-msg').html('There are <span class="result-num">'+data.items.length+'</span> results for One Day Getaways');
		
		// add list content
		$('#single-results').html(list);
		
	},'json');
	
	// ***************************************************************
	//do search for multi day
	
	// filter
	var filter = '';
	if( query.month ) filter+='&month='+query.month;
	if( query.year ) filter+='&year='+query.year;
	if( query.term ) filter+='&keyterm='+query.term;
	if( query.multi_order ) filter+='&order='+query.multi_order;
	if( query.multi_direction ) filter+='&direction='+query.multi_direction
	
	// if( query.departure ) filter+='&departure='+query.departure;
	
	if( query.departure )
	{
	  $('#multi-result-msg').css( 'display', 'none' );
	  $('#multi-box').css( 'display', 'none' );
	  $('#multi-results').css( 'display', 'none' );
	  $('#search-multi').css( 'display', 'none' );
	}
	else
	{	  
	  
  	//get tours
  	$.get(api+'/tour.getList.json?'+filter+'&callback=?', function(data){
  		if( data && data.items.length > 0 ) {
  			
  			var sort_link = '/tour/search';
  			if( query.month ) sort_link+='/month/'+query.month;
  			if( query.year ) sort_link+='/year/'+query.year;
  			if( query.term ) sort_link+='/term/'+query.term;
  			
  			// make tour list
  			var list='<table class="tour-list">'
  					+	'<tr>'
  					+		'<th style="width:250px;">'
  					+			'<a href="'+sort_link+'/multi_order/tours_destination/multi_direction/'+(query.multi_order=='tours_destination' && query.multi_direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Name</a>'
  					+		'</th>'
  					+		'<th style="width:120px; text-align:center">'
  					+			'<a href="'+sort_link+'/multi_order/tours_number_days/multi_direction/'+(query.multi_order=='tours_number_days' && query.multi_direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by # of days</a>'
  					+		'</th>'
  					+		'<th>'
  					+			'<a href="'+sort_link+'/multi_order/tours_date_departure/vdirection/'+(query.multi_order=='tours_date_departure' && query.multi_direction=='ASC'? 'DESC' : 'ASC' )+'">Sort by Date</a>'
  					+		'</th>'
  					+		'<th>&nbsp;</th>'
  					+	'</tr>';
  			
  			$.each( data.items, function(i, item){
  				
  				// this is the link to the tour details
  				var details_link = '/tour/details/type/multi/name/'+item.tours_web_url;
  				
  				list+='<tr class="'+((i%2==1) ? 'alt' : '' )+'">'
  					+	'<td><a href="'+details_link+'" class="details">'+item.tours_destination+'</a></td>'
  					+	'<td align="center">'+item.tours_number_days+'</td>'
  					+	'<td>'+item.tours_date_clean+'</td>'
  					+	'<td style="width:80px; text-align:right;">'
  					+		'<a href="'+details_link+'" class="list-details">VIEW DETAILS</a>'
  					+	'</td>'
  					+'</tr>'
  			});
  			
  			list +='</table>';
  		}
  		
  		//add message
  		$('#multi-result-msg').html('There are <span class="result-num">'+data.items.length+'</span> results for Multi-Day Getaways');
  		
  		// add list content
  		$('#multi-results').html(list);
  		
  	},'json');
	}
}

function search_toggle(obj, id)
{	
	if( $(obj).html() == 'VIEW RESULTS' )
	{
		$(obj).html('CLOSE');	
		$('#'+id).show();
	}
	else
	{
		$(obj).html('VIEW RESULTS');	
		$('#'+id).hide();
	}	
}

// ***************************************************************
// ***************************************************************
// Search Forms

function init_search_forms()
{
	//create year/month dropdowns
	$.get(api+'/tour.getTourYears.json?callback=?', function(data){
		if( data && data.items ) {
			if( data.items.years && data.items.years.length > 0 ) {
				var opts = '';
				$.each(data.items.years, function(i,item){
					opts+='<option value="'+item.year+'" '+(query.year && query.year == item.year ? 'selected="selected"' : '')+'>'+item.year+'</option>'							
				});	
				$('#search_year').html(opts);
			}
			//month dropdown
			if( data.items.months && data.items.months.length > 0  ){
				var opts = '';
				$.each(data.items.months, function(i,item){
					opts+='<option value="'+item.value+'" '+(query.month && query.month == item.value ? 'selected="selected"' : '')+'>'+item.label+'</option>'							
				});	
				$('#search_month').html(opts);
			}
		}
	}, 'json');
	
	//search submit
	$('#depart_search_button').click(function(){
		var month = $('#search_month').val();
		var year = $('#search_year').val();
		window.location.href = '/tour/search/month/'+month+'/year/'+year;
	});
	
	if( query.term )
		$('#search_term').val(query.term);
		
	$('#term_search_button').click(function(){
		var term = $('#search_term').val();
		if( term.length > 0 )
			window.location.href = '/tour/search/term/'+term;
	});
	
	$('#departure_search_button').click(function(){
    var departure = $('#search_departure').val();
    if( departure.length > 0 )
      window.location.href = '/tour/search/departure/'+departure;
  });
	
}

// ***************************************************************
// ***************************************************************
// Print Tour

function print_tour(url)
{
	//open print window
	print_win = window.open (url,"printwin","location=1,status=0,scrollbars=1,width=720,height=900");
	print_win.focus();	
}


// ***************************************************************
// ***************************************************************
// Show Details Page

function show_print_details_page()
{	
	var content = '';
	
	// ***************************************************************
	// ***************************************************************
	// Multi Day List
	if( query.type == 'multi' ) {
			
		//load the tour details
		$.get(api+'/tour.getEntry.json?tour='+query.name+'&callback=?', function(data){
			if(data && data.entry){
				entry = data.entry;
				
				//$('title').html(entry.tours_destination);
				document.title = entry.tours_destination;
				
				content +='<h1>'+entry.tours_destination+'</h1>'
				+ '<div class="tour-detail">'
				+	'<div>'
				+		'<h3>Tour Details</h3><br/>'
				+		'<strong>'+entry.tours_number_days+' Days:</strong> '+entry.tours_date_clean+'<br/>'
				+		'<strong>Tour Number:</strong> '+entry.tours_id+'<br/>'
				+	'</div>'
				+	'<div class="tour-description">'+entry.tours_description+'</div>'
				+ 	( entry.tours_flyer_itinerary ? '<div class="tour-detail">'+entry.tours_flyer_itinerary+'</div>' : '' )
				+	'<hr/>'
				+	( entry.tours_flyer_inclusions ? '<strong>Price Includes:</strong><br/>'+entry.tours_flyer_inclusions : '' )
				+	'<hr/>'
				+	'<table class="tour-price-list">'
				+		'<tr>'
				+			'<th colspan="4">PER PERSON PRICE</th>'			
				+		'</tr>'
				+		'<tr>'
				+			'<td class="first">Twin: '
				+			( entry.tours_twin_exc > 0 ? '$'+entry.tours_twin_exc : 'n/a' )
				+			'</td>'
				+			'<td>Triple: '
				+			( entry.tours_triple_exc > 0 ? '$'+entry.tours_triple_exc : 'n/a' )
				+			'</td>'
				+			'<td>Quad: '
				+			( entry.tours_quad_exc > 0 ? '$'+entry.tours_quad_exc : 'n/a' )
				+			'</td>'
				+			'<td>Single: '
				+			( entry.tours_single_exc > 0 ? '$'+entry.tours_single_exc : 'n/a' )
				+			'</td>'
				+		'</tr>'
				+	'</table>'
				+	'<div class="cnd-price">All prices in Canadian Dollars</div>'
				+	multi_note+'<br/>'
				+'</div>';
				
				add_content(content);
				
				$('#print-button').removeAttr('disabled');
				
			}
		},'json');
		
		
	// ***************************************************************
	// ***************************************************************
	// Single Day List
	} else if( query.type == 'single' ) {
		
		//load the tour details
		$.get(api+'/trip.getEntry.json?trip='+query.name+'&callback=?', function(data){
			if(data && data.entry){
				entry = data.entry;
				
				// $('title').html(entry.trip_destination);
				document.title = entry.trip_destination;
				
				// main trip details
				content +='<h1>'+entry.trip_destination+'</h1>'
						+ '<div class="tour-detail">'
						+	'<div>'
						+		'<h3>Tour Details</h3><br/>'
						+		'<strong>Tour Number:</strong> '+entry.trip_id+'<br/>'
						+		'<strong>Date:</strong> '+entry.trip_date_clean+'<br/>'
						+	'</div>'
						+	'<div class="tour-description">'+entry.trip_web_description+'</div>'
						+	'<hr/><br/>'
						+	( entry.trip_web_inclusions ? '<strong>Price Includes:</strong> '+entry.trip_web_inclusions+'<br/><br/>' : '' )
						+	( entry.trip_departure_clean ? '<strong>Departure Points:</strong> '+entry.trip_departure_clean+'<br/><br/>' : '' )
						+	( entry.trip_max_price ? '<strong>Maximum per person price for this tour:</strong> <span class="tour-price">$'+entry.trip_max_price+'</span><br/><br/>' : '' )
						+	'<div class="note-red"><strong>Note:</strong> Prices may vary depending on departure location. You will be given the exact price during the booking process.</div>'
						+	single_note+'<br/>'
						+ '</div>';
						
				add_content(content);			
				
				$('#print-button').removeAttr('disabled');
				
			}
		},'json');
		
	}
	
}

function do_print()
{
	window.print();	
	pTO = window.setTimeout( close_print, 5000 );
}
function close_print(){
	pTO = null;	
	window.close();		
}


