$(document).ready(function(){
	
	var NWZLageplan = {
		container: $('#nwz_lageplan'),
		containerWidth:0,
		containerHeight:0,
		ZOOM_DIV_WIDTH:200,
		ZOOM_DIV_HEIGHT:200,
		PIN_OFFSET_LEFT:22,
		PIN_OFFSET_TOP:55,
		zoomed:false,
		currPinColor:"red",
		currFloorId:0,
		floorDOMObjects:{
			0:{
				map:$("#lageplan_eg"),
				clickHandler:$("#lageplan_eg_link")
			},
			1:{
				map:$("#lageplan_ug"),
				clickHandler:$("#lageplan_ug_link")
			}
		},
		
		init:function(){
			this.containerWidth = this.container.width();
			this.containerHeight = this.container.height();
			for (var i in this.floorDOMObjects)
			{
				$(NWZLageplan.floorDOMObjects[i].clickHandler)
					.data("floorId", i)
					.focus(function(){this.blur();})
					.click(function(){
						NWZLageplan.showFloor($(this).data("floorId"));
					});
			}		
			
			this._createPin('pin1').appendTo(this.container);
			
			var lageplanPos = this.container.offset(); //jQuery.iUtil.getPosition(document.getElementById('nwz_lageplan'));
			//alert(lageplanPos1.y+", "+lageplanPos1.x+" - "+lageplanPos2.top+", "+lageplanPos2.left);
			$('<div id="zoom_div"></div>').css({
				opacity:0
			})
			.appendTo(this.container)
			.data('parentPos', {
				x:lageplanPos.left + ((NWZLageplan.ZOOM_DIV_WIDTH + 2) / 2),
				y:lageplanPos.top + ((NWZLageplan.ZOOM_DIV_HEIGHT + 2) / 2)
			});

			$('<div id="zoom_cont"></div>')
			.css({
				position:"absolute",
				opacity:0
			})
			.appendTo('#zoom_div');

			$('<div id="lageplan_hitarea"></div>')
			.css({
				position:'absolute',
				left:'0px',
				top:'0px',
				width:NWZLageplan.containerWidth + "px",
				height:NWZLageplan.containerHeight + "px",
				backgroundColor:'white',
				opacity:0
			})
			.appendTo(this.container)
			.mousemove(function(event){
				if (!NWZLageplan.zoomed)
					return false;
				if ($(event.target).parents('#nwz_lageplan').length < 1) 
				{
					$('#zoom_div').css('opacity', 0);
					return;
				}
				
				if (!$('#zoom_div').data('ready'))
					return;
				var w = NWZLageplan.ZOOM_DIV_WIDTH + 2;
				var h = NWZLageplan.ZOOM_DIV_HEIGHT + 2;
				var posObj = $("#zoom_div").data("parentPos");
				var posX = event.pageX - posObj.x;
				var posY = event.pageY - posObj.y;
				if (posX < -1)
					posX = -1;
				if (posX + w > NWZLageplan.containerWidth + 1)
					posX = NWZLageplan.containerWidth - w + 1;
				if (posY < -1)
					posY =- 1;
				if (posY + h > NWZLageplan.containerHeight + 1)
					posY =NWZLageplan.containerHeight - h + 1;
						
				$('#zoom_div').css({
					left:posX + "px",
					top:posY + "px"
				});
				var fx1 = $("#zoom_div").data("imgData").fx1;
				var fy1 = $("#zoom_div").data("imgData").fy1;
				//$('#debug').html('fx: '+fx+'<br />fy: '+fy);
				$('#zoom_cont').css({
					left:Math.round((posX + 1) * -fx1) + "px",
					top:Math.round((posY + 1) * -fy1) + "px"
				});
			});
			$("<a>Lupe aktivieren</a>")
				.attr("id", "toggle_zoom")
				.attr("href", NWZLageplan.floorDOMObjects[NWZLageplan.currFloorId].map.find("a:first").attr("href"))
				.insertAfter(NWZLageplan.container.find("dl:first"))
				.focus(function(){this.blur();})
				.click(function(event){
					event.preventDefault();
					if (!NWZLageplan.zoomed)
					{
						NWZLageplan.zoomed = true;
						NWZLageplan.updateZoomDiv();
						
						$('#zoom_div').css('display', 'block').animate({
							'opacity' :1
						},{
							duration:500,
							queue:false	
						});			
						$(this)
							.addClass('active')
							.text('Lupe deaktivieren');
					}else
					{
						$('#zoom_div').animate({
							'opacity' :0,
							width:'32px',
							height:'32px'
						},{
							duration:500,
							queue:false	
						});			
						NWZLageplan.zoomed = false;	
						$(this)
							.removeClass('active')
							.text('Lupe aktivieren');
					}
				});
			
			setInterval(this._pinBlink, 1000);
		},
		
		showPin:function(shopObj){
			var shop_id = shopObj["shopid"];
			var x = shopObj["x"];
			var y = shopObj["y"];
			var floorId = shopObj["floor"];
			if (floorId != NWZLageplan.currFloorId)
				return;
			
			if (x > 0 && y > 0)
			{
				$('#pin1')
					.animate({
						left:(x - NWZLageplan.PIN_OFFSET_LEFT) + "px",
						top:(y - NWZLageplan.PIN_OFFSET_TOP) + "px",
						opacity:1
					},
					{
						duration:500
					})
					.text(shop_id);
			}
			else {
				$('#pin1').css({opacity:0});
			}
			
			if (NWZLageplan.zoomed)
			{
				if ($("#zoom_div").data('imgData'))
				{
					if (x > 0 && y > 0)
					{
						var fx = $("#zoom_div").data("imgData").fx2;
						var fy = $("#zoom_div").data("imgData").fy2;
		
						$("#pin2")
							.css({
								left:(Math.round(fx * x) - NWZLageplan.PIN_OFFSET_LEFT) + "px",
								top:(Math.round(fy * y) - NWZLageplan.PIN_OFFSET_TOP) + "px",
								opacity:1
							})
							.text(shop_id);
						if (jQuery.browser.msie && $("#pin2").length > 0)
							$("#pin2").get(0).style.removeAttribute("filter");
					}
					else {
						$('#pin2').css({opacity:0});
					}
				}
			}
		},
		
		showFloor:function(floorId){
			var tab;
			
			if (floorId >= 0)
			{		
				if (floorId != NWZLageplan.currFloorId)
				{
					NWZLageplan.floorDOMObjects[floorId]["map"].addClass("active");
					NWZLageplan.floorDOMObjects[floorId]["clickHandler"].addClass("active");
					NWZLageplan.floorDOMObjects[NWZLageplan.currFloorId]["map"].removeClass("active");
					NWZLageplan.floorDOMObjects[NWZLageplan.currFloorId]["clickHandler"].removeClass("active");
				}
				NWZLageplan.currFloorId = floorId;
				
				if(NWZShopModule.currShop["floor"] == floorId)
				{
					$('#pin1').css({opacity:1});
					NWZLageplan.showPin(NWZShopModule.currShop);
				}
				else if($('#pin1').css('opacity') > 0){
					$('#pin1').css({opacity:0});
				}
				
				
				if (NWZLageplan.zoomed)
					NWZLageplan.updateZoomDiv();	
			}
		},
		
		_pinBlink:function(){
			if (NWZLageplan.currPinColor == "red")
				 NWZLageplan.currPinColor = "black";
			else
				NWZLageplan.currPinColor = "red"
			$('#pin1, #pin2').css('color', NWZLageplan.currPinColor);
		} ,
		_createPin:function(id){
			return $('<span id="'+id+'" class="lageplan_pin">PIN</span>').css('opacity', 0);
		},
		
		updateZoomDiv:function(){
			if (!NWZLageplan.zoomed)
				return;
			var imgSrc = NWZLageplan.floorDOMObjects[NWZLageplan.currFloorId].map.find("a:first").attr("href");
			$("#zoom_div")
				//.css('display', 'none')
				.data("ready", false);
				
			$("#zoom_cont").html('');
			$("<img src=\""+imgSrc+"\" />")
				.appendTo("#zoom_cont")
				.preload({
					onFinish:function(){
						
						$("#zoom_cont").css({
							opacity:1
						});
						if (jQuery.browser.msie)
							$("#zoom_cont").get(0).style.removeAttribute("filter");
						$("#zoom_div").animate({
							width:NWZLageplan.ZOOM_DIV_WIDTH + "px",
							height:NWZLageplan.ZOOM_DIV_HEIGHT+ "px"
						},{
							duration:500
						});;
						var imgWidth = $("#zoom_div img:first").width();
						var imgHeight = $("#zoom_div img:first").height();
						var factorX1 = (imgWidth - NWZLageplan.ZOOM_DIV_WIDTH) / (NWZLageplan.containerWidth - NWZLageplan.ZOOM_DIV_WIDTH);
						var factorX2 = imgWidth / NWZLageplan.containerWidth;
						var factorY1 = (imgHeight - NWZLageplan.ZOOM_DIV_HEIGHT) / (NWZLageplan.containerHeight - NWZLageplan.ZOOM_DIV_HEIGHT);
						var factorY2 = imgHeight / NWZLageplan.containerHeight;
						
						$("#zoom_div")
							.data('ready', true)
							.data('imgData', {
								w:imgWidth,
								h:imgHeight,
								fx1:factorX1,
								fy1:factorY1,
								fx2:factorX2,
								fy2:factorY2
							});
							
							if (NWZLageplan.currFloorId == NWZShopModule.currShop["floor"])
							{
								NWZLageplan._createPin("pin2").appendTo("#zoom_cont");				
								NWZLageplan.showPin(NWZShopModule.currShop);
							}
					}
				});
				
		}
	}
	
	var NWZShopModule = {
	
		currShop:null,
		container:$("#interactive_shop_mod"),
		subListCell:$("#sublist_cell"),
		mainListCell:$("#mainlist_cell"),
		detailCell:$("#detail_cell"),
		
		init:function(){
			this.mainListCell
				.add(this.subListCell)
				.add(this.detailCell)
				.append("<span class=\"loader\">Bitte warten ...</span>");
				
			this._initMainList();
			this._initSubList();
			
			$('#shop_mod_a-z_link, #shop_mod_rubriken_link').click(function(event){
				event.preventDefault();
				$('#shop_mod_a-z_link, #shop_mod_rubriken_link').removeClass('active');
				$(this).addClass('active');
				$('#mainlist_cell ul:first').css('opacity', 0.5);
				$('#sublist_cell div.content').css('opacity', 0.5);
				$('#shop_detail').css('opacity', 0.5);
				$('#interactive_shop_mod .loader').css('display', 'block');
				var url=$(this).attr('href');
				$.ajax({
					url:url,
					data:{
						type:555,
						ajax:1
					},
					type:'POST',
					dataType:'json',
					success:function(data){
						$('#mainlist_cell div.content').html(data.mainlist).css('opacity', 1);
						$('#sublist_cell div.content').html(data.sublist).css('opacity', 1);
						$('#shop_detail').html(data.detail).css('opacity', 1);
						if(jQuery.browser.msie) $('#detail_cell div:first').get(0).style.removeAttribute('filter');
						$('#interactive_shop_mod .loader').css('display', 'none');
						NWZShopModule._initMainList();
						NWZShopModule._initSubList();
						pageTracker._trackPageview(url+": AJAX" );
					}
				});
			});
	
			var activeLis = NWZShopModule.subListCell.find("li.active");
			if (activeLis.length > 0){
				var activeLi = activeLis.eq(0);
				var cont = NWZShopModule.subListCell.find("div.content");
				var h = activeLi.position().top + activeLi.height();
				if (cont.height() < h)
					cont.attr('scrollTop', h);
				
				var shop_uid = activeLi.attr("id").replace(/shop_/, "");
				this.currShop = shopAreas[shop_uid];
				NWZLageplan.showFloor(this.currShop["floor"]);
											
			}
		},
		_initMainList:function(){
			$(this.container).find(".mainlist > li").click(function(event){
				event.preventDefault();
				var THIS = $(this);
				if ($(this).hasClass("active")) return;
				
				NWZShopModule.subListCell
					.find("div:first").css('opacity', 0.5)
					.end()
					.find(".loader")
					.css("display", "block");
					
				var url = $(this).find("a:first").attr("href");
				$.ajax({
					url:url,
					data:{
						type:555,
						ajax:1
					},
					type:"POST",
					success:function(data){
						NWZShopModule.subListCell
							.find("div:first").html(data).css('opacity', 1)
							.end()
							.find(".loader")
							.css('display', 'none');
						NWZShopModule._initSubList();
						NWZShopModule.container.find(".sublist > li.active").trigger("click");
						pageTracker._trackPageview(url+": AJAX" );
					}
				});
				
				NWZShopModule.container.find(".mainlist li.active").removeClass("active");
				$(this).removeClass("hover");
				$(this).addClass("active");
			});
			NWZShopModule.container.find(".mainlist a").focus(function(){this.blur();});
		},
		
		_initSubList:function(){
			NWZShopModule.subListCell.find("div.content").attr("scrollTop", 0);
			NWZShopModule.container.find(".sublist > li").click(function(event){
				event.preventDefault();
				
				var shop_uid = $(this).attr('id').replace(/shop_/, "");
				NWZShopModule.currShop = shopAreas[shop_uid];
	
				NWZLageplan.showFloor(NWZShopModule.currShop["floor"]);
				if (!$(this).hasClass("active")){
					NWZShopModule.container.find(".sublist > li.active").removeClass("active");
					$(this).addClass("active");
				}
				NWZShopModule.detailCell
					.find("div:first").css("opacity", 0.5)
					.end()
					.find(".loader")
					.css("display", "block");
				var url = $(this).find("a:first").attr("href");
				$.ajax({
					url:url,
					data:{
						type:555,
						ajax:1
					},
					type:"POST",
					success:function(data){
						document.title = "NordWestzentrum – Geschäfte – " + NWZShopModule.subListCell.find("li.active:first a").text();
						NWZShopModule.detailCell.find("div:first").html(data).css('opacity', 1);
						if (jQuery.browser.msie)
							NWZShopModule.detailCell.find("div:first").get(0).style.removeAttribute('filter');
						NWZShopModule.detailCell.find(".loader").css('display', 'none');
						pageTracker._trackPageview(url);
					}	
				});
			});
			NWZShopModule.container.find(".sublist a").focus(function(){this.blur();});
		}
	}
	
	NWZLageplan.init();
	NWZShopModule.init();	
});