/**
 * Zoom Image
 * 
 * @author PCSG - Henning
 * @copyright PCSG / www.pcsg.de
 * 
 * @version 1.3
 * @package com.pcsg.ptools.zoom
 * 
 * Tested on Browser 
 * 	IE6, IE7, 
 * 	Firefox 2
 * 	Firefox 3 
 * 	Opera 9.5 
 * 	SeaMonkey 1.1.11 
 * 	Safari 3.1.1
 * 
 * @todo Loading Image
 */

if( typeof _ptools == 'undefined' ) {
	var _ptools = {};
};

_ptools.Magnifier = function( settings )
{
	var t = this;
	var _settings = typeof settings != 'undefined' ? settings : {};
	var x=0, y=0, tempX, tempY, move = false, moveit = false, zoomx=1, zoomy=1;
	var Move = null, zImage = null, iImage = null, zParent = null, zLoader = null, iLoader = null;
	var Parent, _Move = {};
	
	// Image Size
	t.zWidth = 0;
	t.zHeight = 0;
	t.iWidth = 0;
	t.iHeight = 0;
	
	
	t.getAttribute = function(name)
	{
		if(_settings[name]) {
			return _settings[name];
		};
		
		return false;
	};
	
	t.setAttribute = function(name, value)
	{
		_settings[ name ] = value;
	};
	
	t.create = function() 
	{
		if(!t.getAttribute('bigImageParent')) {
			alert('I need a bigImageParent!');
		};
		
		if(!t.getAttribute('imageParent')) {
			alert('I need a imageParent!');
		};
		
		if(!t.getAttribute('image')) {
			alert('I need a image!');
		};
		
		if(!t.getAttribute('bigimage')) {
			alert('I need a bigimage!');
		};
		
		zParent = t.getAttribute('bigImageParent');
		var iParent = t.getAttribute('imageParent');
		var iSrc = t.getAttribute('image');
		var zSrc = t.getAttribute('bigimage');
		
		var Obj = document.createElement('div');
		
		// Loader baun
		Move = Obj.cloneNode(true);
		
		zImage = document.createElement('img');
		iImage = document.createElement('img');
		
		Move.className = 'PTMagnifierMove';
		
		zParent.style.overflowX = 'hidden';
		zParent.style.overflowY = 'hidden';
		zParent.style.overflow = 'hidden';
		
		zImage.src = zSrc;
		zImage.style.position = 'absolute';
		zImage.style.top = '0';
		zImage.style.left = '0';
		
		zImage.onload = function() 
		{
			t.zHeight = this.height;
			t.zWidth = this.width;
			
			t.resize();
		};
		
		iImage.onload = function() 
		{
			t.iHeight = this.height;
			t.iWidth = this.width;
			
			t.resize();
		};
		
		iImage.src = iSrc;
		
		if(!zParent.style.position) {
			zParent.style.position = 'relative';
		};
		
		if(!Obj.style.position) {
			Obj.style.position = 'relative';
		};
		
		var style = Move.style; 
		style.top = '0px';
		style.left = '0px';
		style.cursor = 'pointer';
		style.zIndex = '1000';
		style.position = 'absolute';
		//style.backgroundColor = '#fff';
		
		style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=60)';
		style.mozOpacity  = '0.6';
		style.opacity = '0.6';
	
		Obj.appendChild(iImage);
		Obj.appendChild(Move);
		
		iParent.appendChild( Obj );
		zParent.appendChild( zImage );
		
		// Drag&Drop
		Move.onmousedown = function( event ) {
			return t.onmousedown( event );
		};
		
		if(zImage.width) {
			t.zWidth = zImage.width;
		};
		
		if(zImage.height) {
			t.zHeight = zImage.height;
		};
		
		if(iImage.height) {
			t.iHeight = iImage.height;
		};
		
		if(iImage.width) {
			t.iWidth = iImage.width;
		};
		
		zLoader = _createLoader(zParent);
		//iLoader = _createLoader(iParent);
		
		t.resize();
	};
	
	t.clear = function() 
	{
		x=0;
		y=0;
		tempX = 0;
		tempY = 0;
		
		if(t.getAttribute('bigImageParent')) {
			t.getAttribute('bigImageParent').innerHTML = '';
		};
		
		if(t.getAttribute('imageParent')) {
			t.getAttribute('imageParent').innerHTML = '';
		};
	};
	
	t.refresh = function() 
	{
		t.clear();
		t.create();
	};
	
	t.resize = function() 
	{
		if(	t.zWidth == 0 || 
			t.zHeight == 0 || 
			t.iWidth == 0 ||
			t.iHeight == 0
		) {
			return false;
		};

		//iLoader.style.display = 'none';
		zLoader.style.display = 'none';
		
		var p = 0.5;
		if(t.getAttribute('size')) {
			p = t.getAttribute('size');
		};
		
		Move.style.width = Math.round(t.iWidth*p) +'px';
		Move.style.height = Math.round(t.iHeight*p) +'px';
		
		zParent.style.width = t.zWidth*p +'px';
		zParent.style.height = t.zHeight*p +'px';
		
		// Spezial Anpassungen
		if(t.getAttribute('zWidth')) {
			zParent.style.width = t.getAttribute('zWidth') +'px';
		};
		
		if(t.getAttribute('zHeight')) {
			zParent.style.height = t.getAttribute('zHeight') +'px';
		};
		
		if(t.getAttribute('mWidth')) 
		{
			Move.style.width = t.getAttribute('mWidth') +'px';
		} else
		{
			var _w = (parseInt(zParent.style.width) * 100 / parseInt(t.zWidth)) / 100 * t.iWidth;
			Move.style.width = _w +'px';
		};
		
		if(t.getAttribute('mHeight')) 
		{
			Move.style.height = t.getAttribute('mHeight') +'px';
		} else
		{
			var _h = (parseInt(zParent.style.height) * 100 / parseInt(t.zHeight)) / 100 * t.iHeight;
			Move.style.height = _h +'px';
		};
		
	};
	
	t.onmousedown = function( event ) 
	{
		if(move == true) {
			return false;
		};
		
		if(!event) {
			event = window.event;
		};
		
		// Size berechnen
		zoomx = (t.zHeight / t.iHeight);
		zoomy = (t.zWidth / t.iWidth);
		
		move = true;
		
		if(_ptools._Browser.isMSIE) 
		{
			tempX = window.event.clientX - x;
			tempY = window.event.clientY - y;
	  	} else if (_ptools._Browser.isOpera)
	  	{
	  		tempX = event.clientX - x;
			tempY = event.clientY - y;
			
	  	} else
	  	{
	  		tempX = event.pageX - x;
			tempY = event.pageY - y;
	 	};
	 	
	 	t.mousePos(event);
	 	
	 	Parent = t.getElementPos(t.getAttribute('imageParent'));
	 	Parent.width  = parseInt(t.getAttribute('imageParent').offsetWidth); 
		Parent.height = parseInt(t.getAttribute('imageParent').offsetHeight); 
	 	
		document.onmousemove = function(event) 
		{	
			if(!event) {
				event = window.event;
			};
			
			t.onmousemove(event);
			return t.cancelEvent(event);
		};
		
		document.onmouseup = function(event)
		{
			if(!event) {
				event = window.event;
			};
			
			t.onmouseup(event);
		};
		
		return t.cancelEvent(event)
	};
	
	t.onmousemove = function( event ) 
	{
		if(move == false) {
			return false;
		};
		
		moveit = true;
		
		t.mousePos(event);
		
	 	var tx = 0;
		var ty = 0;
		
		var _px = (x-tempX+Move.offsetWidth);
		var _py = (y-tempY+Move.offsetHeight);
		
		if((x-tempX > 0) && _px < Parent.width) {
			tx = x-tempX;
		};
		
		if((y-tempY > 0) && _py < Parent.height) {
			ty = y-tempY;
		};
		
		if(_py > Parent.height-2) {
			ty = Parent.height-2-Move.offsetHeight;
		};
		
		if(_px > Parent.width-2) {
			tx = Parent.width-2-Move.offsetWidth;
		};
		
		Move.style.left = tx +'px';
		Move.style.top = ty +'px';
		
		zImage.style.left = tx*-1*zoomx +'px';
		zImage.style.top = ty*-1*zoomy +'px';
		
		return true;
	};
	
	t.onmouseup = function( event ) 
	{
		move = false;
		
		document.onmousemove = function(event){};
		document.onmouseup = function(event){};
		
		x = parseInt(Move.style.left);
		y = parseInt(Move.style.top);
	
		moveit = false;
	   
	    return false;
	};
	
	t.getElementPos = function( elm ) 
	{
		var left = 0; 
		var top  = 0; 

		while (elm.offsetParent)
		{ 
			left += elm.offsetLeft; 
			top += elm.offsetTop; 
			elm = elm.offsetParent; 
	    };

		left += elm.offsetLeft; 
		top += elm.offsetTop; 
	 
	    return {
			x:left, 
			y:top
		};
	};
	
	t.mousePos = function(event)
	{
		x = document.all ? window.event.clientX : event.pageX;
		y = document.all ? window.event.clientY : event.pageY;
	};
	
	t.cancelEvent = function(event) 
	{
		if(typeof event.preventDefault == 'function')
		{
			event.preventDefault();
			event.stopPropagation(); 
		} else
		{
			event.returnValue = false;
		};
		
		return false;	
	};
	
	var _debug = function( t ) 
	{
		document.getElementById('debug').innerHTML = t;
	};
	
	var _createLoader = function( oParent )
	{
		var _Loader = document.createElement('div');
		_Loader.innerHTML = '&nbsp;';
		_Loader.style.width = oParent.offsetWidth +'px';
		_Loader.style.height = oParent.offsetHeight +'px';
		_Loader.className = 'pMagnifierContainer';
		
		var LoaderContainer = _Loader.cloneNode(true);
		var LoaderImage = _Loader.cloneNode(true);
		
		LoaderContainer.className = 'pMagnifierContainer pMagnifierOpacity';
		LoaderImage.className = 'pMagnifierContainer pMagnifierLoader';
		
		_Loader.appendChild(LoaderContainer);
		_Loader.appendChild(LoaderImage);
		
		oParent.appendChild( _Loader );
		
		return _Loader;
	}
	
	return t;
};
