$.maydream = {
	initGoogleMap: function() {
		var mapData = {
				"markers": [
				{
					"longitude": 50.21944, 
					"latitude": 12.88155, 
					"html": '<h2>Azra family store</h2>'
				},
				
				{
					"longitude": 50.22431, 
					"latitude": 12.88320, 
					"html": '<div class="gMapsInfoWindow"><img height="181" width="284" src="/user/public/default/images/butiqueImgGmaps.jpg" alt="maydream" /><h2>May Dream - butique</h2><ul><li>Lázeňská 3</li><li>360 01 Karlovy Vary</li></ul></div>'
				}
			]
		};
		
		var myLatlng = new google.maps.LatLng(50.22236, 12.88241);
		var myOptions = {
			zoom: 15,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			disableDefaultUI: true
		};
		
		var map = new google.maps.Map(document.getElementById("map"), myOptions);

		var dataSize = mapData.markers.length;
		var i;
		for (i = 0; i < dataSize; i++) {
			var markerData = mapData.markers[i];
			var marker = new google.maps.Marker({
				position: new google.maps.LatLng(markerData.longitude, markerData.latitude), 
				map: map,
				title: markerData.name
			});   
			
			markerClick(marker, markerData.html, map);
	    };  
		
	},
	
	initScrollPane: function(element) {
		$(element).jScrollPane({
			
		});

	},
		
	baseAnimation: function(element) {
		$(".logo", element).fadeOut(800, function() {
			$(".info", element).fadeIn(1000, function() {
				element.addClass("finished");
				
			});
			$.maydream.initScrollPane($(".info", element));
			
			$.maydream.initGoogleMap();
			
		});

	},

	wihoutSlide: function(element) {
		$(".hover .image, .hover .anotation", element).fadeOut(700, function() {
			element.addClass("open").animate({paddingLeft: "250px", paddingRight: "250px"}, "slow", function() {				
				$.maydream.baseAnimation(element);
				
			});
		});
	},
	
	withBoxesSlide: function(element, left) {
		$(".hover .image, .hover .anotation", element).fadeOut(700, function() {
			$("#boxes").animate({left: "-250px"}, 700);
			
			element.addClass("open").animate({paddingLeft: "250px", paddingRight: "250px"}, 700, function() {
				$("#boxes").animate({left: left+"px"}, "slow", function() {
					$.maydream.baseAnimation(element);
				});
			});
			
		});
	},
	
	onlyBalancer: function(element, left) {
		$(".hover .image, .hover .anotation", element).fadeOut(700, function() {
			$("#boxes").animate({left: "-211px"}, 700);
			
			element.addClass("open").animate({paddingLeft: "250px", paddingRight: "250px"}, 700, function() {
				$.maydream.baseAnimation(element);
			});
			
		});
	},
	
	reverse: function(element) {
		$(".info", element).fadeOut(1000, function() {
			$(".logo", element).fadeIn(1000, function() {
				element.animate({paddingLeft: "0px", paddingRight: "0px"}, "slow", function() {
					element.removeAttr("style").removeClass("open finished");
					$("#boxes").animate({left: "0px"}, "slow");
					$(".hover", element).fadeOut("slow");
					$(".hover .image, .hover .anotation", element).removeAttr("style");
					$("h2.claim").fadeIn(500);
					
					var openElement = $(".info", element);
					var api = openElement.data('jsp');
					
					api.destroy();
					
				});
			});
		});
	}
};

function markerClick(marker, code, map) {
    var infowindow = new google.maps.InfoWindow({
        content: code,
        maxWidth: 284
    });

	google.maps.event.addListener(marker, 'click', function(e){
	    infowindow.open(map, marker);
	    
	    $(".gMapsInfoWindow").parent().css("overflow", '');
	});

}

$(document).ready(function() {
	
	$("a[rel=lightbox]").fancybox();

	
	$.preLoadImages(['historyBg.jpg', 'butiqueBg.jpg', 'collectionsBg.jpg', 'contactsBg.jpg', 'historyInfoBg.jpg'], '/user/public/default/images/');
	
	Cufon.replace("#contact, .info h3, #news .date, .contacts .info .contactBox h4");
	
	if (!$("body").hasClass("ru")) {
		Cufon.replace(".box .anotation p");
		
	}
	
	Cufon.replace(".mutations a, #archiveWrap h1, #archiveWrap .date", {hover: true});
	
	$(".box .title").live("mouseenter", function() {
		if (!$(".box.open").length) {
			var element = $(this).parents(".box");
			
			$(".hover:visible").fadeOut(500);
			
			$("body").css("cursor", "pointer");
			$(".hover", element).fadeIn(500);
			$("h2.claim").fadeOut(500);
		}
		
	});
	
	$(".box:not(.open) .hover").live("mouseleave", function() {
		var element = $(this).parents(".box");
		
		$("body").removeAttr("style");
		$(".hover").fadeOut(500);
		
		
	});

	$(".box").live("mouseenter", function(e) {
		if (!$(e.target).hasClass("title") && !$(e.target).hasClass("logo") && !$(e.target).hasClass("blackTitle")) {
			if (!$(".box.open").length) {
				$("h2.claim").fadeIn(500);
			}
		}
	});

	$("#menuBox").live("mouseenter", function(e) {
		if (!$(".box.open").length) {
			$("h2.claim").fadeIn(500);
		}
	});

	$(".box").click(function(e) {
		e.preventDefault();
		
		if (!$(this).hasClass("open")) {
			if ($(".box.finished").length) {
				$.maydream.reverse($(".box.finished"));
				
			}
		}
	});
	
	
	$(".box .hover").click(function(e) {
		e.preventDefault();
		
		if (!$(".box.open").length) {
			var element = $(this).parents(".box");
			element.addClass("open");
			
			$(".helper").hide();
			
			if (element.index() == 0) {
				$.maydream.wihoutSlide(element);
				
			} else if (element.index() == 1) {
				var left = -(parseInt((210*element.index()))+1);
				
				$.maydream.onlyBalancer(element, left);
			} else {
				var left = -(parseInt((210*element.index()))+1);
				
				$.maydream.withBoxesSlide(element, left);
				
			}

		}
	});
	
	$("#contact").live("click", function(e) {
		e.preventDefault();
		var element = $(this);
		
		$.maydream.reverse($(".box.open"));
		
	});
	
	$(".box:not(.open)").live("mousemove", function(e) {
		if ($(".box.open").length) {
			$("#helperClose").css({top: e.clientY+30, left: e.clientX+30, display: "block"});
		} 
	});
	
	$(".box:not(.open) .hover").live("mousemove", function(e) {
		$("#helperOpen").css({top: e.clientY+30, left: e.clientX+30, display: "block"});

	});	
	
	
	$(".box").mouseleave(function() {
		$(".helper").hide();
	});
	
	$(".box.open").live("mousemove", function() {
		$("body").removeAttr("style");
	});
	
	$(".box .close").click(function(e) {
		e.preventDefault();
		
		$.maydream.reverse($(".box.open"));
	});
	
	$("#menuBox").hover(
		function() {
			$("#logo, #contact").stop(true).animate({opacity: 0.1});
			$("#news").stop(true).animate({opacity: 1});
			
			if (!$(".box.open").length) {
				$("h2.claim").stop(true).animate({opacity: 0.1});
			}
		}, 
		function() {
			
			$("#logo, #contact").stop(true).animate({opacity: 1}, function() {
				$("#logo, #contact").removeAttr("style");
			});
			
			if (!$(".box.open").length) {
				$("h2.claim").stop(true).animate({opacity: 1}, function() {
					$("h2.claim").removeAttr("style");
				});
			}
			
			$("#news").stop(true).animate({opacity: 0.4}, function() {
				$("#news").removeAttr("style");
			});
		}
	);

});


audiojs.events.ready(function() {
var as = audiojs.createAll();
});
