(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);

function markerEvents(marker, code, map) {
    var infowindow = new google.maps.InfoWindow({
        content: code,
        maxWidth: 345,
        pixelOffset: new google.maps.Size(0, 30)

    }); 

	google.maps.event.addListener(marker, 'click', function(e){
	    infowindow.open(map, marker);
	    map.panTo(marker.getPosition());

	    $(".infoWindowWrap").parent().css("overflow", '');
	    
		$(".parkDetail").click( function(e) {
			e.preventDefault();
			
			var href = $(this).attr("href");
			infowindow.close();
			
			$("#info .inside").html("");
			$("#info").addClass("loading");
			
			$("#menu a.active").removeClass("active");
			
			window.location.hash = "#!"+$(this).attr("href");

		});
		
	});
	
	google.maps.event.addListener(marker, 'mouseover', function(e){
		marker.setIcon('/user/public/default/images/parkIconHover.png');
		
	});
	
	google.maps.event.addListener(marker, 'mouseout', function(e){
		marker.setIcon('/user/public/default/images/parkIcon.png');
		
	});

}

/*function imagesEvents(marker, code, image, map) {
    var imagebubble = new google.maps.InfoWindow({
        content: '<div class="imageBubbleWrap"><h2>'+code+'</h2><img height="180" width="237" src="/user/public/default/images/'+image+'" /></div>',//code,
        maxWidth: 305,
        pixelOffset: new google.maps.Size(0, 30)

    });
    
	google.maps.event.addListener(marker, 'click', function(e){
		imagebubble.open(map, marker);
	    map.panTo(marker.getPosition());

	    $(".infoWindowWrap").parent().css("overflow", '');

	});
	
}*/

function countSizes() {
	var result = {};
	
	var mapWidth = parseInt($(window).width())-parseInt($("#info").width());
	var mapHeight = parseInt($(window).height())-(parseInt($("#head").outerHeight(true))+parseInt($("#menu").outerHeight(true)));
		
	result['height'] = mapHeight;
	result['width'] = mapWidth;
	
	return result;
}

function setSizes() {
	$("#map").css({
		height: countSizes().height,
		width: countSizes().width
	});
	
	$("#content").css({height: countSizes().height});	
	
}

var windowHash = window.location.hash;
if (windowHash) {
	var redir = windowHash.substring(3);
	window.location = '/'+redir;

}

$(document).ready(function() {
	setSizes();
	
	$(window).resize(function() {
		setSizes();
	});
	
	$(window).hashchange(function() {
		var windowHash = location.hash;
		if (windowHash) {
			var redir = windowHash.substring(3);
			
			$("#info .inside").html("");
			$("#info").addClass("loading");
			var href = $(this).attr("href");
			Cufon.refresh();
			
			$.get('/'+redir, function(returnData) {
				$("#info").removeClass("loading");
				$("#info .inside").html(returnData);
				Cufon.refresh("h1, h2, h3");
				
				if ($('#info .inside a[rel="gallery"]').length) {
					$('a[rel="gallery"]').fancybox({
						'titleShow'     : false,
						'transitionIn'	: 'elastic',
						'transitionOut'	: 'elastic',
						'easingIn'      : 'easeOutBack',
						'easingOut'     : 'easeInBack'
					});
					
				}

			});
		}
	});
	
	Cufon.replace("#menu li a", {hover: true});
	Cufon.replace("h1, h2, h3");
	
	$(".submenu").hover(
		function() {
			$("ul", $(this)).stop(true, true).show("fast", "easeOutBack");
		}, 
		function() {
			$("ul", $(this)).stop(true, true).hide("fast", "easeInBack");
		}
	
	);
	
	 	
	var myLatlng = new google.maps.LatLng(50.22481, 12.88151);
	var myOptions = {
		zoom: 15,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false,
		scaleControl: false,
		streetViewControl: false,
		panControl: false,
		zoomControlOptions: {
			position: google.maps.ControlPosition.TOP_RIGHT
		}

	};
	
	var map = new google.maps.Map(document.getElementById("map"), myOptions);

	$("#mapType li a").live("click", function() {
		if (!$(this).hasClass("active")) {
			$("#mapType .active").removeClass("active");
			$(this).addClass("active");
			var newMapTypeId = $(this).attr("rel");

			map.setMapTypeId(google.maps.MapTypeId[newMapTypeId]);
		}
	});
	
	var markersArray = {};

	var dataSize = mapData.markers.length;
	var i;
	for (i = 0; i < dataSize; i++) {
		var markerData = mapData.markers[i];
	    var icon = '/user/public/default/images/parkIcon.png';

		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(markerData.longitude, markerData.latitude), 
			map: map,
			title: markerData.name,
			icon: icon,
			id: markerData.id
		});   
		
		markerEvents(marker, markerData.html, map);
		
		markersArray[markerData.id] = marker;
    };  
    
    
	var imagesSize = mapImages.images.length;
	var i;
	for (i = 0; i < imagesSize; i++) {
		var imagesData = mapImages.images[i];
	    var icon = '/user/public/default/images/'+imagesData.icon;

	    
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(imagesData.longitude, imagesData.latitude), 
			map: map,
			title: imagesData.name,
			icon: icon
		});   
		
		
		//imagesEvents(marker, imagesData.name, imagesData.image, map);
    }; 
        
	$(".submenu ul a").click(function(e) {
		e.preventDefault();
		var markerElement = $(this).attr("rel");
		
		$("#menu a.active").removeClass("active").addClass("ordinary");
		
		window.location.hash = "#!/"+$(this).attr("rel");
		google.maps.event.trigger(markersArray[markerElement], 'click');

	});

	
	$("#menu ul > li a").click(function(e) {
		e.preventDefault();
		var element = $(this);
		
		if ($(this).attr("href") != "#") {
			if (!$(this).hasClass("active")) {
				
				$("#menu a.active").removeClass("active").addClass("ordinary");
				
				$(this).addClass("active");
				
				window.location.hash = "#!"+$(this).attr("href");
				Cufon.refresh();

			}
			
		}
		
	});
	
	$("#directionFrom").live("focus", function() {
		$(this).removeClass("error");
	});
	
	$("#sendContact").live("click", function(e) {
		e.preventDefault();
		var clicked = false;
		var error = false;
		
		$.each($("#contactForm .required"), function() {
			if (!$(this).val()) {
				$(this).addClass("error");
				error = true;
			}

		});
		
		if (!error) {
			$.post("/mail/send", $("#contactForm").serialize(), function() {
				$("#contactForm .required").val("");
			});
			
		}
	});
	
	$("#contactForm .required").live("focus", function() {
		$(this).removeClass("error");
	});
	
	
	$("#calcDirections").live("click", function(e) {
		e.preventDefault();
		
		if ($("#directionFrom").val()) {
			$("#directionPanel .error").remove();
			
			var directionsService = new google.maps.DirectionsService();
			var directionsDisplay = new google.maps.DirectionsRenderer();
			 
			directionsDisplay.setMap(map);
			directionsDisplay.setPanel(document.getElementById("directionPanel"));
			
			var request = {
				origin: $("#directionFrom").val(), 
				destination: $("#directionTo").val(),
				travelMode: google.maps.DirectionsTravelMode.DRIVING
			};
			
			directionsService.route(request, function(response, status) {
				if (status == google.maps.DirectionsStatus.OK) {
					$(".directionClear").remove();
					$("#directionPanel").prepend('<p class="directionClear noPadding"><a id="clearDirection" href="#">Vyčistit mapu</a></p>');
					
					$("#clearDirection").click(function(e) {
						e.preventDefault();
						
						directionsDisplay.setMap(null);
						$("#directionPanel").html("");
						map.panTo(myLatlng);
						map.setZoom(15);
						$("#directionFrom").val("");
						
					});
					
					directionsDisplay.setDirections(response);
					
				} else {
					$("#directionPanel").prepend('<p class="error quote noPadding red">Neexistující místo <strong>"'+$("#directionFrom").val()+'"</strong>, zkuste prosím zadat jíné.</p>');
					
				}
			});
			
		} else {
			$("#directionFrom").addClass("error");
			
		}
		
		
	});
	
});

