$(function () {
	$('#rightnav nav ul>li').has('ul').each(function () {
		$(this).hover(function () {
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
			},
			function () {
				if (!$(this).is('.current') && !$(this).is('.has_current')) {
					$(this).removeClass('selected');
					$(this).siblings('.current').addClass('selected');
					$(this).siblings('.has_current').addClass('selected');
				}
			}
		);
	});

	if ($('#colecciones_galeria').length > 0) {
		$('#colecciones_galeria ul').anythingSlider({
			width               : 680,       // if resizeContent is false, this is the default width if panel size is not defined
			height              : 768,       // if resizeContent is false, this is the default height if panel size is not defined
			resizeContents      : false,     // If true, solitary images/objects in the panel will expand to fit the viewport
			autoPlay            : false,     // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
			buildNavigation     : false,      // If true, builds a list of anchor links to link to each panel
			buildStartStop      : false,      // ** If true, builds the start/stop button/
			infiniteSlides      : false,      // if false, the slider will not wrap & not clone any panels
			onSlideComplete : function (base) {
				$('#current_page').html(base.currentPage);
			},
			hashTags            : true      // Should links change the hashtag in the URL?
		})

		$('#colecciones_galeria').mousemove(function (e) {
			var relativeX = e.pageX - $(this).offset().left;
			var relativeY = e.pageY - $(this).offset().top;
			var medioY = parseInt($(this).height()/2);
			if (((relativeX < 80) && (relativeY > medioY-50) && (relativeY < medioY+50))
				|| ((relativeX > $(this).width()-80) && (relativeY > medioY-50) && (relativeY < medioY+50))) {
				$('#colecciones_galeria .arrow').removeClass('disabled');
			} else {
				$('#colecciones_galeria .arrow').addClass('disabled');
			}
		}).mouseout(function () {
			$('#colecciones_galeria .arrow').addClass('disabled');
		})
		$('#colecciones_galeria .arrow').mouseenter(function () {
			$('#colecciones_galeria .arrow').removeClass('disabled');
		}).addClass('disabled');


		$('#cat_siguiente').click(function () {
			$('#colecciones_galeria ul').data('AnythingSlider').goForward();
			return false;
		});
		$('#cat_anterior').click(function () {
			$('#colecciones_galeria ul').data('AnythingSlider').goBack();
			return false;
		});
	}
});

$(window).load(function() {
	if (window.google && window.google.maps && ($('#gmap_canvas').length > 0)) {

		function setMarker(map, location, index) {
			if (location.latlng) {
				var latlng = location.latlng;
			} else {
				var latlng = new google.maps.LatLng(location.lat, location.lng);
			}
			var marker = new google.maps.Marker({
				position: latlng,
				map: map,
				title: location.city + ", " + location.title
			});
			marker.set('location', location);
			google.maps.event.addListener(marker, "click", function(event) {
				var location = this.get('location');
				var infoWindowHtml = '<div id="content"><h3>'+location.city+'</h3><p>';
				infoWindowHtml += ((location.title)?'<b>'+location.title+'</b>':'');
				infoWindowHtml += ((location.dir && location.title)?', ':'');
				infoWindowHtml += ((location.dir)?location.dir:'');
				infoWindowHtml += '.</p></div>'
				infoWindow.setContent(infoWindowHtml);
				infoWindow.open(map, this);
			});
			markers[index] = marker;
		}

		function setMarkerFromGeocodeLocation(map, location, index) {
			if (geocoder && location) {
				var address = location.city;
				address += (location.dir)?' '+location.dir:'';
				address += ' '+location.country;
				geocoder.geocode({ 'address': address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						location.latlng = results[0].geometry.location;
						setMarker(map, location, index);
					} else {
						alert("No se pudo encontrar la dirección.");
					}
				});
			}
		}

		function setMarkers(map, locations) {
			for(var i = 0; i < locations.length; i++) {
				var location = locations[i];
				if (location.lat && location.lng) {
					setMarker(map, location, i);
				} else if (geocoder) {
					setMarkerFromGeocodeLocation(map, location, i)
				}
			}
		}

		function initMap(latlng, zoom) {
			var map_options = {
				'zoom': zoom,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			mapa = new google.maps.Map($('#gmap_canvas').get(0), map_options);
		}

		function clearMarkers() {
			infoWindow.close();
			for(var i = 0; i < markers.length; i++) {
				markers[i].setMap(null);
			}
			markers.length = 0;
		}

		geocoder = new google.maps.Geocoder();

		// INIT
		if (locations[0].lat && locations[0].lng) {
			var latlng = new google.maps.LatLng(locations[0].lat, locations[0].lng);
			initMap(latlng, 14);
			setMarkers(mapa, locations);
		} else {
			geocoder.geocode({'address': locations[0].dir+' '+locations[0].city+' '+locations[0].country}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var latlng = results[0].geometry.location;
					initMap(latlng, 14);
					setMarkers(mapa, locations);
				} else {
					var latlng = new google.maps.LatLng(-34.5996, -58.3926);
					initMap(latlng, 14);
					setMarkers(mapa, locations);
				}
			});
		}

		$('#donde_comprar_ciudades ul.direcciones li').click(function () {
			infoWindow.close();
			var marker_num = $(this).attr('rel');
			mapa.setCenter(markers[marker_num].position);
			google.maps.event.trigger(markers[marker_num], 'click');
		}).hover(function () {
			$(this).addClass('hover');
		},function () {
			$(this).removeClass('hover');
		});
	}
});
