/*
 * GENERAL EVENTS/SETUP USED ON EACH PAGE LOAD
 */
$(function() {
					 
	// do main nav highlighting
	//var base = dataApp.baseUrl;
	var url = window.location.href;
	var end = (url.indexOf('?')>-1) ? url.indexOf('?') : undefined ; 
	//url = url.slice(base.length-1, end);
	url = url.slice(0, end);
	$("#top_nav a[href='" + url + "']").addClass('active');
	
	// update itinerary counter on page load
	dataApp.itinerary.updateCounter();
	
	if ($(".slideshow").size()) {
		$.getScript(dataApp.baseUrl+'js/jquery.cycle.js', function() {
			$('.slideshow').cycle({
				fx:     'fade',
				speed:   500
			});
		});
	};
	
	if ($(".tabs")) {
		
		var hash = window.location.hash;
		
		if (hash && $(".tabs a[href='"+hash+"']").size()) {
			
			var $tab = $(".tabs a[href='"+hash+"']").parent();
			$tab.addClass('active');
			$(".pane").addClass('hide').eq($tab.index()).removeClass('hide');
			window.scrollTo(0, 250);
		
		} else {
		
			$(".tabs li:first-child").addClass('active');
			$(".pane").addClass('hide').filter(":first-child").removeClass('hide');
		
		}
		
		$('.tabs a').click(function (e) {
			e.preventDefault();
			$('.pane').addClass('hide').filter(this.hash).removeClass('hide');
			$(this).parent().addClass('active').siblings().removeClass('active');
			
			// sort out any google maps contained
			google.maps.event.trigger(dataApp.attractions.map, 'resize');
			var mapCenter = new google.maps.LatLng(50.75,-3.86);
			dataApp.attractions.map.setCenter(mapCenter);
			dataApp.attractions.map.setZoom( dataApp.attractions.map.getZoom() );

			return false;
		});
		
	};
					 
	$("#category_strip .first").mouseenter(function(e) {
		e.preventDefault();
		$("#category_strip .inner").animate({	left: '36px' }, 1000);
	});
	
	$("#category_strip .last").mouseenter(function(e) {
		e.preventDefault();
		var categoryStripContentWidth = $("#category_strip ul").width();
		var showLeft = (categoryStripContentWidth - 960) + 36;
		$("#category_strip .inner").animate({	left: '-'+showLeft+'px'	}, 1000);
	});
	
	$("a[rel='lightbox']").colorbox({photo:true, opacity:.3});
	
	$("#facility_filter a").click(function() {
		$(".attraction_snippet").hide().filter('.'+$(this).attr('rel')).show();
		// now hide/show the google map markers
		$(".attraction_snippet").each(function() {
			var i = $(this).index();
			var show = $(this).is(":visible");
			dataApp.attractions.markers[i].setVisible(show);
		});
	});
	
	$("#q").bind("focus click", function() {
		dataApp.focusSearch();
	});
	
	$("#q").autocomplete({
		source: dataApp.baseUrl + "search?f=json",
		minLength: 2,
		select: function(event, ui) {
			$("#q" ).val( ui.item.label );
			$("#attraction_search").submit();
		}
	});
	
	$(".pick_date").datepicker({
		dateFormat:'yy-mm-dd',
		showOn: 'both',
		buttonImage: dataApp.baseUrl + "/images/icons/calendar.png",
		buttonImageOnly: false
	});
	
	// fill .default_value input boxes with rel attribute
	$(".default_value").each(function() { 
		$(this).val($(this).attr('rel'));
		$(this).bind('focus', function() {
			if ($(this).val() == $(this).attr('rel')) $(this)[0].select();
		});
		$(this).bind('focusout', function() {
			if ($(this).val() == '') $(this).val($(this).attr('rel'));
		});
	});
	
	// Loop over each snippet, adding a click event that redirects to the first link contained
	$(".snippet").each(function(e){
			var link = $(this).contents().find("a").attr("href");
			$(this).bind ("click",function(e){		
				if (e.target == this || e.target == '<p>') window.location = link;	
			});
			$(this).css('cursor','pointer');
	});
	
	$(".add_to_itinerary").each(function(){
		$(this).click(function(e) {
			e.preventDefault();
			e.stopPropagation();
			var attractionID = $(this).attr('rel');
			dataApp.itinerary.add('attraction', attractionID);
		});
	});
	
});




/*
 * GENERAL APPLICATION FUNCTIONS
 */
(function(self, $, undefined) {
	
	self.locationMapLatlng = [50.452389,-3.557625];
	self.map = '';
	
	self.focusSearch = function()
	{
		$("#q")[0].select();
	}
	
	self.bookmark = function() 
	{
		bookmarkurl = document.location.href;
		bookmarktitle = document.title;
		if (document.all)
			window.external.AddFavorite(bookmarkurl,bookmarktitle);
		else if (window.sidebar) // firefox
			window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
	} 
					
}(window.dataApp = window.dataApp || {}, jQuery));
