var jsCitytrip =
{
	tips: null,
	map: null,
	markers: [],
	bounds: null,

	init: function() {
		$('a:external').attr('target', '_blank');

		// init category map
		if($('#mapCategory').length > 0)
		{
			jsCitytrip.initMap();
			jsCitytrip.getTips();
		}

		if($('.scrolltothis').length > 0)
		{
			$.scrollTo('.scrolltothis', 1000);
		}

		$('.topTips ol li a').click(function (e) {
			if($('#' + $(this).data('link')).length > 0)
			{
				e.preventDefault();
				$.scrollTo('#' + $(this).data('link'), 500);
			}
		});

		// hover menus
		$('#nav ul li').mouseover(function (e) {
			$(this).find('a').addClass('hover');
			$(this).find('.dropdownMenu').show();
		});

		// hover menus
		$('#nav ul li').mouseout(function (e) {
			$(this).find('a').removeClass('hover');
			$(this).find('.dropdownMenu').hide();
		});

		// add tip
		$('.shareButton').click(function (e) {
			e.preventDefault();
			$(this).hide();
			$(this).parent().find('.addTip').show('fast');
		});

		$('#videoModal').dialog({
			resizable: false,
			autoOpen:false,
			modal:true,
			width:770,
			zIndex: 99999
		});

		// video
		$('.playVideo').click(function (e) {
			var $this = $(this);
			e.preventDefault();
			if(typeof($this.data('video')) == 'undefined') return;
			if($this.find('iframe').length == 0)
			{
				var $videoModal = $('#videoModal');
				var $iframe = $videoModal.find('iframe');
				$iframe.attr('src', $iframe.attr('src').replace('autoplay=0', 'autoplay=1'));
				$videoModal.dialog('open');
			}
		});
		$('#closeVideo').click(function (e) {
			e.preventDefault();
			$('#videoModal').dialog('close');
		});

		// readmore
		if($('#readMoreButton').length > 0)
		{
			$('#readMoreButton').on('click', function(e) {
				e.preventDefault();
				$('#readMoreIntro').slideDown();
				$('#readMoreButton').parents('p').hide();
			});
		}

	},

	initMap: function () {
		// set default center
		var latlng = new google.maps.LatLng($('#mapCategory').data('lat'), $('#mapCategory').data('lng'));

		// options
		var options = {
				backgroundColor: 'transparent',
	  			center: latlng,
	  			mapTypeControl: true,
	  			mapTypeId: google.maps.MapTypeId.ROADMAP,
	  			streetViewControl: true,
	  			zoom: 8,
	  			zoomControl: true
			};

		// create map
		jsCitytrip.map = new google.maps.Map(document.getElementById('mapCategory'), options);
	},

	getTips: function () {

		jsCitytrip.tips  = [];

		$('.hint').each(function () {
			var hint = {};
			hint.id = $(this).data('id');
			hint.name = $(this).find('h4').html();
			hint.lat = $(this).data('lat');
			hint.lng = $(this).data('lng');
			hint.url = $(this).attr('id');

			if(hint.lat != 0 && hint.lng != 0 && hint.lat != 123 && hint.lng != 123) jsCitytrip.tips.push(hint);
		});

		jsCitytrip.mapDraw(jsCitytrip.tips);
	},

	// draw markers on the map
	mapDraw: function(tips)
	{
		// get icon
		var iconURL = $('#mapCategory').data('icon');

		// clear all markers
		for(i in jsCitytrip.markers) jsCitytrip.markers[i].setMap(null);
		jsCitytrip.tips = tips;
		jsCitytrip.bounds = new google.maps.LatLngBounds();

		// loop markers
		for(var i in tips)
		{
			var point = new google.maps.LatLng(tips[i].lat, tips[i].lng);
			var marker = new google.maps.Marker({
				position: point,
				map: jsCitytrip.map,
				title: tips[i].name,
				url: '/#' + tips[i].url,
				icon: iconURL,
				id: tips[i].id,
				animation: google.maps.Animation.DROP
			});

			// bind click
			google.maps.event.addListener(marker, "click", function(event) {
				for(var i in jsCitytrip.tips)
				{
					if(jsCitytrip.round(jsCitytrip.tips[i].lat,3) == jsCitytrip.round(event.latLng.lat(),3) && jsCitytrip.round(jsCitytrip.tips[i].lng,3) == jsCitytrip.round(event.latLng.lng(),3))
					{
						$.scrollTo('#' + jsCitytrip.tips[i].url, 500);
					}
				}
			});

			// get bounds
			jsCitytrip.bounds.extend(point);
			jsCitytrip.markers.push(marker);
		}

		if(tips.length > 0) jsCitytrip.map.fitBounds(jsCitytrip.bounds);
	},

	/**
	 *  This function rounds a number to a specific number of decimal points,
	 *  As opposed to Math.round.
	 *  Why this is not a standard function, no one will ever know.
	 *
	 **/
	round: function(x, decimals)
	{
		x = x * Math.pow(10, decimals);
		x = Math.round(x);
		return x / Math.pow(10, decimals);
	},

	eoo: true
};

$.expr[':'].external = function(obj){ return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname); };

$(jsCitytrip.init);
