/*
	Vroomzoom - Das Zoom-Javascript mit dem blöden Namen!
	
	2010 Fritjof Bohm für Hochzwei
	
*/

function showInfoElement(infobox)
{
	if ($(infobox).hasClass("canshow") && !$(infobox).hasClass("hascontent") )
	{
		var infodiv = document.createElement("div");
		$(infodiv).append("Zum Schlie&szlig;en klicken");
		$(infodiv).css("color", "#333333")
				  .css("padding", "10px")
				  .css("background-color", "#ffffff")
				  .css("font-family", "arial, verdana, sans-serif");
		
		$(infobox).addClass("hascontent").append(infodiv);
	}
}

function closeVroom(wrap)
{
	wrap = $(wrap);
	var origimg = $("a img", wrap.parent());
	
	$(wrap).animate({
		height: $(origimg).height() + "px",
		width:	$(origimg).width() + "px"
	
	}, 400, function() {
		$(this).remove();
	});
}

var currentVroom;


$(document).ready(function() {
	
	$("a[rel]").each(function()
	{
		el = $(this);
		
		if (el.attr('rel') == 'vroom[]')
		{
			el.addClass("vroom-enabled");
		
			el.click(function() { return false; });
			el.mouseover(function() {
				
				var wrapper = document.createElement("div");
				$(wrapper).addClass("wrapper")
						  .attr("vroomid", new Date().getTime());
				
				currentVroom = $(wrapper).attr("vroomid");
				
				$(this).parent().prepend(wrapper);

				var origimg = $("img", this);
				var img = document.createElement("img");
					
				$(wrapper).css("position", "absolute")
						  .css("z-index", "1001")
						  .css("height", $(origimg).height() + "px")
						  .css("width", $(origimg).width() + "px")
						  .css("margin-top", "-1px")
						  .css("padding", "1px")
						  .css("border", "1px solid #ddd")
						  .css("background-color", "white")
						  .addClass("vroom-loading");
								  
				$(wrapper).append(img);
				
				$(img).attr("src", $(this).attr("href"))
										  .css("position", "absolute")
										  .css("z-index", "1002")
										  .css("margin-left", "-10000px");
				
				var infobox = document.createElement("div");
				$(infobox).css("position", "absolute")
						  .css("z-index", "1004")
						  .css("height", $(origimg).height() + "px")
						  .css("width", $(origimg).width() + "px")
						  .css("background", "white url(fileadmin/assets/img/loading.gif) no-repeat 50% 50%")
						  .addClass("vroom-info");
				
				$(wrapper).prepend(infobox);
				
				$(img).load(function() {
					
					$(".vroom-loaded, .vroom-loading").each(function()
							{
								if (currentVroom != $(this).attr("vroomid"))
								{
									closeVroom(this);
								}
								$(".vroom-info", this).css("background", "none");
							});
										
					var wrap = $(this).parent();
					
					$(this).css("margin-left", "0");
					$(wrap).css("margin-left", "-1px")
						   .removeClass("vroom-loading")
						   .addClass("vroom-loaded")
						   .animate({
							   
						height: $(this).height() + "px",
						width:	$(this).width() + "px"
					
					}, 400, function() {
						
						var img = $("img", this)
						$(".vroom-info", this).css("height", $(img).height() + "px")
											  .css("width",  $(img).width() + "px")
											  .addClass("canshow");
						
					
					});
				});
				
				$(infobox).mouseenter(function() {
					showInfoElement(this);
				});
				$(infobox).mousemove(function() {
					showInfoElement(this);
				});
				
				$(img).mousemove(function() {
					showInfoElement($(".vroom-info", $(img).parent()));
				});
				
				$(infobox).mouseleave(function() {
					$(this).removeClass("hascontent");
					$("div", this).remove();
					
					//var wrap = $(this).parent();
					//closeVroom(wrap);
				});
				
				$(infobox).click(function() {
					var wrap = $(this).parent();
					closeVroom(wrap);
				});
				
				$(img).click(function() {
					var wrap = $(this).parent();
					closeVroom(wrap);
				});
			});
		}
	});
	


});

