$(function() {
	// Highlight the page in the menu
	var navFound = false;
	$('#nav a').each(function() { 
		if($(this).attr('href') == window.location.href) {
			navFound = true;
			// Loop through any parents and highlight them
			$(this).parents('#nav li').children('a:first-child').addClass('navPage');
		}
	});
	// If the navigation has a query string then it won't be recognised above so find the first match without
	if(!navFound && window.location.href.indexOf('?') > 0) {
		var locationHref = window.location.href.substring(0, window.location.href.indexOf('?'));
		$('#nav a').each(function() { 
			if($(this).attr('href') == locationHref) {
				navFound = true;
				// Loop through any parents and highlight them
				$(this).parents('#nav li').children('a:first-child').addClass('navPage');
			}
		});
	}
	// Hack to get rid of main title on suggested itineraries page
	if(window.location.href.indexOf("suggested_itinerary.php?itineraryid=") > 0) {
		$('h1:first-child').hide();
	}
	// Enable the slideshow on the accommodation/activity page
	if($('#galleria').length) {
		Galleria.loadTheme("/includes/galleria/themes/ktnz/galleria.ktnz.js");
		$("#galleria").galleria({
			clicknext: true,
			imageCrop: true
		}).css({visibility: 'visible'});
	}
	// Quick search selection boxes
	if($('.quick-search').length) {
		$('.quick-search .selection div').click(function() {
			if($(this).hasClass('selected')) {
				$(this).removeClass('selected');
				$(this).children('input').attr('checked', '');
			} else {
				$(this).addClass('selected');
				$(this).children('input').attr('checked', 'checked');
			}
			// Highlight the search button
			if($('.results').length) {
				$('#refine-search').removeClass('Blue-Button').addClass('Orange-Button').html('Refine Search &raquo;');
			}
		});
		// Divide the table up into pages
		if($('.results').length) {
			$('.results').tablesorter().tablesorterPager({
				container: $('#pager'),
				size: $('#pager .pagesize').val(),
				seperator: ' of ',
				positionFixed: false
			});
		}
	}
});

function currencyConverter() {
	popupWindow('http://www.travelplanner.co.nz/currency/smlconv.cfm', 240, 330);
}

function zoomProperty(id) {
	for(var i in markers) {
		var marker = markers[i];
		if(marker.id == id) {
			var point = marker.getPoint();
			map.setCenter(new GLatLng(point.lat(), point.lng()), 15);
			if(window.location.hash) {
				window.location.href = window.location.href;
			} else {
				window.location.href = window.location.href + '#maptop';
			}
			showGoogleBubble(marker);
		}
	}
}

function showGoogleBubble(marker) {
	if(marker.description) marker.openInfoWindowHtml(marker.description);
}

function changeResultPage(button) {
	var page = $(button).html();
	$('.pagecurrent').val(page).focus();
	$('#pager .page').removeClass('pageSelected');
	$(button).addClass('pageSelected');
}

function showPopular(type, showPopular) {
	if(showPopular) {
		$('#accommodation-search .' + type + ' div.hide').css({display: 'inline-block'});
		$('#accommodation-search div.popular-' + type + ' a.popular').removeClass('selected');
		$('#accommodation-search div.popular-' + type + ' a.all').addClass('selected');
	} else {
		$('#accommodation-search .' + type + ' div.hide').hide();
		$('#accommodation-search div.popular-' + type + ' a.popular').addClass('selected');
		$('#accommodation-search div.popular-' + type + ' a.all').removeClass('selected');
	}
}
