(function ($) {
	$(document).ready(function() {
		
		// Admin menu
		if($.browser.msie && ($.browser.version=="6.0" || $.browser.version=="7.0")) {
			$("#edit-menu").hover(function() {
				$(this).addClass("over");
				$(this).css("left", "0px");
			}, function() {
				$(this).removeClass("over");
				$(this).css("left", "-176px");
			});
		}
		
		
		// Form
		$("input.default").live("focus", function(){
			if($(this).val() == $(this)[0].defaultValue){ $(this).val(""); }
			$(this).addClass("active");
			$(this).blur(function(){
				if($(this).val() == ""){
					$(this).val($(this)[0].defaultValue);
					$(this).removeClass("active");
				}
			});
		});

		
		// Slideshow
		if($("#slideshow-controls").length) {
			$("#slideshow-controls a").click(function() {
				if(!$(this).hasClass("selected")) {
					var id = $(this).attr("id").split("_");
					
					$("#slideshow .slide:visible").css({zIndex:99}).fadeOut(300);
					$("#slideshow #slide_"+id[1]).css({zIndex:90}).fadeIn(300);
					$("#slideshow #slide_"+id[1]).css("left", "0").css("top", "0").css("position", "absolute");
					
					$("#slideshow-controls a.selected").removeClass("selected");
					$(this).addClass("selected");
					
					var height = $("#slideshow #slide_"+id[1]).show().height();
					$('#slideshow').animate({ height: height}, 300, function() {
						if(typeof(google) !== 'undefined') {
							//var myLatlng = map.getCenter();
							google.maps.event.trigger(map, "resize");
    	    		map.setCenter(mapCenter);
							if(bounds) {
								map.fitBounds(bounds);
							}
						}
					});
				}
			});
		}		
	});
})(jQuery);


// Legendary places navigation
function init_jcarousel(sagen_nr) {
	function mycarousel_initCallback(carousel) {
		jQuery('#sagen_nav a.next').bind('click', function() {
			carousel.next();
			return false;
		});
		
		jQuery('#sagen_nav a.prev').bind('click', function() {
			carousel.prev();
			return false;
		});
		
		jQuery("li.jcarousel-item-"+sagen_nr).addClass("selected");
	}
	
	var start = 1;
	if(sagen_nr == 0) {
		jQuery("#sagen_nav a.start").addClass("selected");
	} else {
		start = sagen_nr - 3;
	}
	
	jQuery('#sagen_nav ul').jcarousel({
  	scroll: 1,
		initCallback: mycarousel_initCallback,
    buttonNextHTML: null,
    buttonPrevHTML: null,
		wrap: 'circular',
		start: start
   });
}


var bounds;
var mapCenter;
function initialize_gmap(mapId, locations, image_path, sagen_nr) {
	if(typeof(google) !== 'undefined') {		
		var multiple = locations.length > 1 ? true : false;
		if(sagen_nr && sagen_nr != 0) {
			multiple = false;
			var myLatlng = new google.maps.LatLng(locations[sagen_nr-1][2], locations[sagen_nr-1][3]);
		} else {
			var myLatlng = new google.maps.LatLng(locations[0][2], locations[0][3]);
		}		
		
		var myOptions = {
			zoom: 11,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		map = new google.maps.Map(document.getElementById(mapId), myOptions);
		infowindow = new google.maps.InfoWindow({content: ""});
		mapCenter = myLatlng;
		
		var image = new google.maps.MarkerImage(
			image_path+'marker.png',
			new google.maps.Size(20,19),
			new google.maps.Point(0,0),
			new google.maps.Point(10,19)
		);
		var image_selected = new google.maps.MarkerImage(
			image_path+'marker-selected.png',
			new google.maps.Size(20,19),
			new google.maps.Point(0,0),
			new google.maps.Point(10,19)
		);
		var shadow = new google.maps.MarkerImage(
			image_path+'marker-shadow.png',
			new google.maps.Size(34,19),
			new google.maps.Point(0,0),
			new google.maps.Point(10,19)
		);
		
		if(multiple) {
			bounds = new google.maps.LatLngBounds();
		}
		for (var i = 0; i < locations.length; i++) {
			var location = locations[i];
			
			var myLatlng = new google.maps.LatLng(location[2], location[3]);
      
			var marker_image = image;
			if(sagen_nr != 0 && sagen_nr == i+1) {
				marker_image = image_selected;
			}
			
			var marker = new google.maps.Marker({
				position: myLatlng, 
				map: map,
				icon: marker_image,
				shadow: shadow,
				draggable: false,
				title: urlDecode(location[0]).trim(),
				html: urlDecode(location[1])
			});
			google.maps.event.addListener(marker, 'click', function(){
				if(this.html && this.html != "") {
					infowindow.content = '<div class="marker-info">'+this.html+'</div>';
					infowindow.open(map, this);
				}
			});
			
			if(multiple) {
				bounds.extend(marker.position);
			}
  	}
		if(multiple) {
			map.fitBounds(bounds);
		}
	}
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function urlDecode(s) {
	return decodeURIComponent( s.replace( /\+/g, '%20' ).replace( /\%21/g, '!' ).replace( /\%27/g, "'" ).replace( /\%28/g, '(' ).replace( /\%29/g, ')' ).replace( /\%2A/g, '*' ).replace( /\%7E/g, '~' ) );
};

