/* ::---------------------------------------------------------------
:: +	leemZoom.js
	2009.07.14
	roberto cornice
	cornice@lemm.de

:: +	neuer ImageZoom 

:: +	[c] 2009 by Lemm Werbeagentur GmbH

	------------------------------------------------------------::
	
	
:: +	Aufruf

		jQuery(document).ready(function() {
    		
		});

*/

function lemmZoom(selecter) 		{
	
	this.selecter		= selecter;
	
	this.origWidth		= "0";
	this.origHeight		= "0";
	this.origX			= "0";
	this.origY			= "0";
	this.zIndex			= "0";
	
	this.targetWidth	= "0";
	this.targetHeight	= "0";
	this.targetX		= "0";
	this.targetY		= "0";
	
	this.thepic			= new Image();
	this.thepic.id		= "lemmZoomStar";
	
	this.link;
	this.bild;
	
	this.init	= function()
	{
		lz	= this;
		
		$(this.selecter).find("img").each(function() {
			
			if  ($(this).parent().get(0).tagName == "A" )	{
				var bild	= this;	
				var link	= $(this).parent().attr("href");				
				$(this).parent().removeAttr("href");
				
					
				$(bild).css("cursor", "pointer");
				$(bild).bind("click", function()	{
					lz.lemmzoomit(bild, link);
				});
				
			}			
		});
		
		
		
		$('body').append("<div id=\"lemmZoomStage\"></div>");
		$('body').append(this.thepic);
		$('#lemmZoomStage').css(
			{
				"position"		: "fixed",
				"top"			: "0",
				"left"			: "0",
				"height"		: "100%",
				"width"			: "100%",
				"background"	: "#000000",
				"opacity"		: 0.0,
				"display"		: "none",
				"cursor"	: "pointer"
				
			}
		);
		
		$('#lemmZoomStar').css(
			{
				"position"				: "fixed",
				"background"			: "#FFFFFF url(../bilder/layout/lemmZoomClose.gif) no-repeat",
				"background-position"	: "top right",
				"padding"				: "0px",
				"opacity"				: 0.0,
				"cursor"	: "pointer"
			}
		);
				
		
	}
	
	
	this.lemmzoomit		= function(bild, link)	{
		this.bild		= bild;
		this.link		= link;

		$('#lemmZoomStar').removeAttr("src");
		dis	= this;
		this.thepic.onload = function(){ 
			dis.dothemagic(bild, link, dis);
		}
		$('#lemmZoomStar').attr("src", link);
	}
	
	
	this.dothemagic			= function(bild,link, dis)	{
		
			var origPosition	= $(bild).position();
			var realPos			= this.getPosition(bild);
		
			$('#lemmZoomStar').css(
				{
					"width"		: "auto",
					"height"	: "auto"
				}
			);
			
			
			
			dis.targetWidth	= $('#lemmZoomStar').width();
			dis.targetHeight	= $('#lemmZoomStar').height();
			dis.origWidth	= $(bild).width();
			dis.origHeight	= $(bild).height();
			dis.origX		= parseInt(realPos[0]);	//parseInt(origPosition.left);
			dis.origY		= parseInt(realPos[1]);	//parseInt(origPosition.top);
			
			
			
			var centerPosition	= dis.getCenterPosition(dis.targetWidth, dis.targetHeight);
			
			$('#lemmZoomStar').css(
				{
					"left"		: dis.origX,
					"top"		: dis.origY,
					"width"		: dis.origWidth,
					"height"	: dis.origHeight,
					"border"	: "1ps solid #000000",
					"z-index"	: 6000
				}
			);
			$('#lemmZoomStage').css(
				{				
					"top"			: "0",
					"left"			: "0",
					"display"		: "block",				
					"height"		: "100%",
					"width"			: "100%",
					"z-index"		: 5000//(dis.highestZ()+1)
				}
			);
			$('#lemmZoomStage').animate({ 
					"opacity"		: 0.5
				}, "normal", "swing"
			);
			
			$('#lemmZoomStar').animate({ 
				left				: centerPosition[0],
				top					: centerPosition[1],
				opacity				: 1,
				width				: dis.targetWidth,
				height				: dis.targetHeight,
				padding				: "20px"
				}, "normal", "swing"
			);
			
			$("#lemmZoomStage, #lemmZoomStar").bind("click", function()	{
				$('#lemmZoomStar').stop();
				$('#lemmZoomStage').stop();
				$('#lemmZoomStar').css(
					{
						"display"		: "none",
						"opacity"		: 0
					}
				);
				$('#lemmZoomStage').css(
					{				
						"display"		: "none",
						"opacity"		: 0					
					}
				);
				
			});
		
	}
	
	this.getCenterPosition	= function (width, height)	{
		var xc	= ( $(window).width() - width ) / 2;
		var yc	= ( $(window).height() - height ) / 2;
		//alert (height+" "+$(window).height()+" "+yc);
		var backarr	= Array ( xc, yc );
		return backarr;
		
	}
	
	this.highestZ	= function()
	{
	    var divs = document.getElementsByTagName('div');
	    var highest = 0;
	    for (var i = 0; i < divs .length; i++)
	    {
	        var zindex = divs[i].style.zIndex;
	        if (zindex > highest) {
	            highest = zindex;
	        }
	    }
	    return highest;
	}

	this.getPosition	= function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			
			curtop	= curtop-$(window).scrollTop();
			curleft	= curleft-$(window).scrollLeft();
			return [curleft,curtop];
		}
	}
	
}