/* 
Moving canvas with posters
Requires Leechy's common.js

Author: Sergey Muratov (hans@design.ru)
09.03.2007
*/ 

var postersCount = 0;


if (!aWindowSize)
	aWindowSize = getWindowSize();
function Canvas(x,y)
{
	this.oElem = $('MainContainer');
	//$('Menu').style.top = '0px';
	
	this.aPosters = [];
	this._zWidth = this._width = this.oElem.offsetWidth;
	this._zHeight = this._height = this.oElem.offsetHeight;
	this._style = this.oElem.style;
	this.oElem.style.left = (this._x = x ? x : this.oElem.style.left ? parseInt(this.oElem.style.left) : 0) + 'px';
	this.oElem.style.top = (this._y = y ? y : this.oElem.style.top ? parseInt(this.oElem.style.top) : 0) + 'px';
	this._zoomed = true;
	this._drag = false;
	this._zoomTweenType = Math.easeOutQuad;
	this._zoomDuration = 10;
	this._zoomStrength = aWindowSize[1] > this._height ? 1 : aWindowSize[1]/(this._height+250);
	
	this._cZoom = 1;
	this._mIndexes = [];
	
	this.blockDrag = false;
	this.zoomOutCenter = false;
	this._snap = false;
	
	
	this._xyTween = this._zTween = false;
	var oThisCopy = this;
	window.onload = function () {oThisCopy.__construct(); fOnLoad();};
	
	
}

Canvas.prototype.__construct = function () {}

Canvas.prototype.concentrateOn = function (img)
{
		
	
		
	var menuContainer = $('Menu');

	var dX = aWindowSize[0]/2 - (img.offsetLeft + img.offsetWidth/2) - this._x;
	var dY = aWindowSize[1]/2 + menuContainer.offsetHeight/2 + (parseInt(menuContainer.style.top) ? parseInt(menuContainer.style.top) : 0) - (img.offsetTop + (img.offsetHeight/2)) - this._y;
	
	if (this.oOffsets)
	{
		var cOffset;
		if (cOffset = this.oOffsets.getOffsetById(img.id))
		{
			dX -= cOffset.x;
			dY -= cOffset.y;
		}
	}
	
	
	if (this._restrictions)
	{
		var endX = Math.max(this._restrictions.minX,Math.min(this._x + dX,this._restrictions.maxX));
		var endY = Math.min(Math.max(this._restrictions.minY,this._y + dY),this._restrictions.maxY);
		dX = endX - this._x;
		dY = endY - this._y;
	}
	
	//alert(Math.round(dX) + ' x ' + Math.round(dY));
	//debug(oPoster._x + ' : ' + oPoster._y + ' : ' + oPoster.oImg.src);
	var tweenType = Math.easeOutBounce;
	
	this._xyTween = new Tween(this,'',Math.easeInCubic,0,1,20);
	this._xyTween._startX = this._x;
	this._xyTween._startY = this._y;
	this._xyTween.dX = dX;
	this._xyTween.dY = dY;
	this._xyTween.onMotionChanged = function ()
	{
		this.target.setX(this._startX + this.dX*this.position);
		this.target.setY(this._startY + this.dY*this.position);
		this.target.onDrag();
	}
	
	

}

Canvas.prototype.startDrag = function (ev)
{
	if (!this._zoomed && this.blockDrag || this._cZoom > 1)
		return false;
	if (ev = checkEvent(ev))
	{
		
		var menuTop = parseInt($('Menu').style.top);
		if (menuTop < 0 || ev.clientY > 100)
		{
		
			this._drag = true;
			var oDelta={x: ev.clientX, y: ev.clientY};
			var oPos=getAbsolutePos(this.oElem);
			oOffset={x: oPos.x - oDelta.x, y: oPos.y - oDelta.y};
			if (this._xyTween)
			{
				this._xyTween.stop();
				this._xyTween = false;
			}
	
			addEvent(document, 'mousemove', doDrag);
		}
		return CancelEvent(ev);
		
	}
	return false;
}
Canvas.prototype.doDrag = function (ev)
{
	//&& this._zoomed
	if(this._drag && (ev=checkEvent(ev))){
		if (!this._restrictions)
		{
			this.oElem.style.left=(this._x = ev.clientX + oOffset.x)+'px';
			this.oElem.style.top=(this._y = ev.clientY + oOffset.y)+'px';
		}
		else
		{
			this.oElem.style.left = (this._x = Math.min(Math.max(this._restrictions.minX,ev.clientX + oOffset.x),this._restrictions.maxX)) + 'px';
			this.oElem.style.top = (this._y = Math.min(Math.max(this._restrictions.minY,ev.clientY + oOffset.y),this._restrictions.maxY)) + 'px';
		}
		//debug(this._x + ' : ' + this._y);
		this.onDrag(ev);
	}
}
Canvas.prototype.stopDrag = function ()
{
	removeEvent(document, 'mousemove', doDrag);
	this._drag = false;
}

Canvas.prototype.onDrag = function (ev)
{
	if (this._restrictions)
	{
		this.oElem.style.left = (this._x = Math.min(Math.max(this._restrictions.minX,ev.clientX + oOffset.x),this._restrictions.maxX)) + 'px';
		this.oElem.style.top = (this._y = Math.min(Math.max(this._restrictions.minY,ev.clientY + oOffset.y),this._restrictions.maxY)) + 'px';
	}
}
Canvas.prototype.onResize = function ()
{
}

Canvas.prototype.onZoomStart = function ()
{
	
	if (this._zoomed && $('OrderBanner'))
	{
		$('OrderBanner').style.display = 'none';
	}
	
}
Canvas.prototype.onZoomEnd = function ()
{
	if (this._zoomed && $('OrderBanner'))
	{
		$('OrderBanner').style.display = 'block';
	}
}


Canvas.prototype.doZoom = function (ev)
{
	if (this._zTween && this._zTween._animate)
	{
		return false;
	}
	
	this.onZoomStart();
	
	
	
	if (this._zoomed)
	{
		if (ev && ev.type != 'dblclick')
		{
			
			return false;
		}
		this._zTween = new Tween(this,'',this._zoomTweenType,this._cZoom,this._zoomStrength,this._zoomDuration);
		this._zTween.onMotionFinished = function () {this.target._zoomed = !this.target._zoomed; this.target.onZoomEnd();}
		if (!this.zoomOutCenter)
		{
			this._zTween.dX = -Math.round((this._x - aWindowSize[0]/2)*(1 - this._zoomStrength/this._cZoom));
			this._zTween.dY = -Math.round((this._y - aWindowSize[1]/2)*(1 - this._zoomStrength/this._cZoom));
		}
		else
		{
			this._zTween.dX = -Math.round(this._x - (aWindowSize[0]/2 - this._zWidth*(this._zoomStrength/this._cZoom)/2));
			this._zTween.dY = -Math.round(this._y - (aWindowSize[1]/2 - this._zHeight*(this._zoomStrength/this._cZoom)/2));
		}
	}
	else
	{
		this._zTween = new Tween(this,'',this._zoomTweenType,this._cZoom,1,this._zoomDuration);
		this._zTween.onMotionFinished = function () {this.target._zoomed = !this.target._zoomed; this.target.onZoomEnd();}
		this._zTween.dX = -Math.round((this._x - aWindowSize[0]/2)*(1 - 1/this._cZoom));
		this._zTween.dY = -Math.round((this._y - aWindowSize[1]/2)*(1 - 1/this._cZoom));
		
		if (ev !== 0 && (ev = checkEvent(ev)))
		{
			var menuContainer = $('Menu');
			
			var evX, evY, targetId;
			
			if (ev.target.id && ev.target.id.match(/-toplink$/i))
			{
				targetId = ev.target.id.replace(/-toplink$/i,'');
				var aCoord = getAbsolutePos($(targetId));
				evX = aCoord.x;
				evY = aCoord.y;
			}
			else if (ev.target.parentNode.id && ev.target.parentNode.id.match(/-toplink$/i))
			{
				targetId = ev.target.parentNode.id.replace(/-toplink$/i,'');
				var aCoord = getAbsolutePos($(targetId));
				evX = aCoord.x;
				evY = aCoord.y;
			}
			else if(ev.target.parentNode.id && ev.target.parentNode.id.match(/-head$/i))
			{
				targetId = ev.target.parentNode.id;
				var aCoord = getAbsolutePos($(targetId));
				evX = aCoord.x;
				evY = aCoord.y; 
			}
			else if(ev.target.parentNode.parentNode.id && ev.target.parentNode.parentNode.id.match(/-head$/i))
			{
				targetId = ev.target.parentNode.parentNode.id;
				var aCoord = getAbsolutePos($(targetId));
				evX = aCoord.x;
				evY = aCoord.y; 
			}
			else
			{
				if (ev.target.nodeName.toLowerCase() == 'img')
				{
					targetId = ev.target.parentNode.parentNode.id;
				}
				evX = ev.clientX;
				evY = ev.clientY;
			}
			
			
			if (this.oOffsets)
			{
				var cOffset;
				if (cOffset = this.oOffsets.getOffsetById(targetId))
				{
//					alert(cOffset.x);
					evX += cOffset.x;
					evY += cOffset.y;
				}
			}
			
			
			
			var pdX = (evX - this._x)*(1 - 1/this._cZoom);
			var pdY = (evY - this._y)*(1 - 1/this._cZoom) + ( menuContainer.offsetHeight/2 + (parseInt(menuContainer.style.top) ? parseInt(menuContainer.style.top) : 0));
			
			this._zTween.dX = pdX;
			this._zTween.dY = pdY;
		}
		
		
		if (this._restrictions)
		{
			var endX = Math.max(this._restrictions.minX,Math.min(this._x + this._zTween.dX,this._restrictions.maxX));
			var endY = Math.min(Math.max(this._restrictions.minY,this._y + this._zTween.dY),this._restrictions.maxY);
			this._zTween.dX = endX - this._x;
			this._zTween.dY = endY - this._y;
		}
		
	}
	
	
	//debug(this._cZoom);
	//debug(this._zTween.dX + ' === ' + this._zTween.dY);
	//this._zTween._canvas = this;
	this._zTween.startX = this._x;
	this._zTween.startY = this._y;
	
	this._zTween.onMotionChanged = function ()
	{
		this.target.setWidth(this.target._zWidth*this.position);
		this.target.setHeight(this.target._zHeight*this.position);
		this.target._cZoom = this.position;
		
		this.target.setX(this.startX + this.dX*getRel(this._start,this.finish,this.position));
		this.target.setY(this.startY + this.dY*getRel(this._start,this.finish,this.position));
		this.target.onDrag();
		this.target.oElem.style.fontSize = this.position * 100 + '%';
	}
	
	
}

Canvas.prototype.doZoomById = function (id)
{
	if (this._zTween && this._zTween._animate)
	{
		return false;
	}
	
	this.onZoomStart();
	
	
	
	this._zTween = new Tween(this,'',this._zoomTweenType,this._cZoom,1,this._zoomDuration);
	this._zTween.onMotionFinished = function () {this.target._zoomed = !this.target._zoomed; this.target.onZoomEnd();}
	this._zTween.dX = -Math.round((this._x - aWindowSize[0]/2)*(1 - 1/this._cZoom));
	this._zTween.dY = -Math.round((this._y - aWindowSize[1]/2)*(1 - 1/this._cZoom));
		
	if (id !== '') {
		
		var menuContainer = $('Menu');
		
		var evX, evY, targetId;
		
		targetId = id;
		var aCoord = getAbsolutePos($(targetId));
		evX = aCoord.x;
		evY = aCoord.y;
		
		if (this.oOffsets)
		{
			var cOffset;
			if (cOffset = this.oOffsets.getOffsetById(targetId))
			{
				evX += cOffset.x;
				evY += cOffset.y;
			}
		}
		
		
		
		var pdX = (evX - this._x)*(1 - 1/this._cZoom);
		var pdY = (evY - this._y)*(1 - 1/this._cZoom) + ( menuContainer.offsetHeight/2 + (parseInt(menuContainer.style.top) ? parseInt(menuContainer.style.top) : 0));
		
		this._zTween.dX = pdX;
		this._zTween.dY = pdY;
	}
	
	
	if (this._restrictions)
	{
		var endX = Math.max(this._restrictions.minX,Math.min(this._x + this._zTween.dX,this._restrictions.maxX));
		var endY = Math.min(Math.max(this._restrictions.minY,this._y + this._zTween.dY),this._restrictions.maxY);
		this._zTween.dX = endX - this._x;
		this._zTween.dY = endY - this._y;
	}
	
	
	this._zTween.startX = this._x;
	this._zTween.startY = this._y;
	
	this._zTween.onMotionChanged = function ()
	{
		this.target.setWidth(this.target._zWidth*this.position);
		this.target.setHeight(this.target._zHeight*this.position);
		this.target._cZoom = this.position;
		
		this.target.setX(this.startX + this.dX*getRel(this._start,this.finish,this.position));
		this.target.setY(this.startY + this.dY*getRel(this._start,this.finish,this.position));
		this.target.onDrag();
		this.target.oElem.style.fontSize = this.position * 100 + '%';
	}
	
	
}


Canvas.prototype.setX = function (x)
{
	this.oElem.style.left = (this._x = x ? x : 0) + 'px';
}
Canvas.prototype.setY = function (y)
{
	this.oElem.style.top = (this._y = y ? y : 0) + 'px';
}
Canvas.prototype.setWidth = function (w)
{
	this._width = w;
	this.oElem.style.width = this._width + 'px';
}
Canvas.prototype.setHeight = function (h)
{
	this._height = h;
	this.oElem.style.height = this._height + 'px';
}



function startDrag(ev)
{
	
	if (!oCanvas)
		return false;
	
	var newEv = checkEvent(ev);
	
	if (hasParentWithId(newEv.target.parentNode,'subContent')
		 &&
		 (!oCanvas.oNavigator
		 ||
		 (
		   newEv.clientX < oCanvas.oNavigator._MMx
		   ||
		   newEv.clientX > oCanvas.oNavigator._MMx+oCanvas.oNavigator._MMwidth
		   ||
		   newEv.clientY < oCanvas.oNavigator._MMy
		   ||
		   newEv.clientY < oCanvas.oNavigator._MMy+oCanvas.oNavigator._MMheight
		  )
		 )
		
		)
		return true;
	
	if (oCanvas.oNavigator)
	{
		oCanvas.oNavigator.startDrag(ev);
	}
	else
	{
		oCanvas.startDrag(ev);
	}
}
function doDrag(ev)
{
	if (oCanvas.oNavigator)
	{
		oCanvas.oNavigator.doDrag(ev);
	}
	else
	{
		oCanvas.doDrag(ev);
	}
	//return oCanvas ? oCanvas.doDrag(ev) : false;
}
function stopDrag(ev)
{
	if (oCanvas.oNavigator)
	{
		oCanvas.oNavigator.stopDrag(ev);
	}
	else
	{
		oCanvas.stopDrag(ev);
	}
	//return oCanvas ? oCanvas.stopDrag(ev) : false;
}



/**
Poster
**/
function Poster(parent,pId,pSrc,Xi,Yi,ext)
{
	
	this._canvas = parent;
	this._id = pId;
	var strId = pSrc;
	
	this._strId = strId;
	this._Xi = Xi ? Xi : 0;
	this._Yi = Yi ? Yi : 0;
	
	this._ext = ext ? ext : null;
	
	this._posX = this._negX = this._posY = this._negY = 0;
	
	var z = this._canvas.getPosterById(pId);
	
	this._src = 'i/index/_poster' + strId + (ext ? '.' + ext : ((z && z._ext) ? '.' + z._ext : '.jpg'));
	
	if (isMSIE)
	{
		var usemap = null;
		if ($('postermap' + strId))
		{
			usemap = 'usemap="#posterlinks' + strId + '"';
			
			$('postermap' + strId).onmouseover = function (){oCanvas.oElem.style.cursor = 'pointer';};
			$('postermap' + strId).onmouseout = function (){oCanvas.oElem.style.cursor = 'move';};
		}
		
		
		this.oImg = document.createElement('<img src="' + this._src + '" ' + (usemap ? usemap : '') + ' />');
	}
	else
	{
		this.oImg = document.createElement('img');
		this.oImg.src = this._src;
		if ($('postermap' + strId))
		{
			this.oImg.setAttribute('usemap','#posterlinks' + strId);
		}
//		this.oImg.src = 'i/_blank.gif';
//		this.oImg.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this._src + "', sizingMethod='scale')";
	}
	this.oImg.className = 'poster ' + ('post' + pId);
	
	//this.oImg.onclick = function () {zoom(this);}
		
	if (z)
	{
		this._width = z._width;
		this._height = z._height;
		this.oImg.style.width = z.oImg.style.width;
		this.oImg.style.height = z.oImg.style.height;
		
		this.setX(z._baseX*this._canvas._cZoom + this._canvas._width*this._Xi);
		this.setY(z._baseY*this._canvas._cZoom + this._canvas._height*this._Yi);
		this._baseX = z._baseX;
		this._baseY = z._baseY;
		this._canvas.oElem.appendChild(this.oImg);
	}
	else
	{
		this._canvas.oElem.appendChild(this.oImg);
	
		this.oImg.style.width = (Math.round(((this._width = this.oImg.offsetWidth)/this._canvas._width)*10000*this._canvas._cZoom)/100) + '%';
		this.oImg.style.height = (Math.round(((this._height = this.oImg.offsetHeight)/this._canvas._height)*10000*this._canvas._cZoom)/100) + '%';
		
		
		this.setX(this.oImg.offsetLeft*this._canvas._cZoom + this._canvas._width*this._Xi);
		this.setY(this.oImg.offsetTop*this._canvas._cZoom + this._canvas._height*this._Yi);
		this._baseX = this._x;
		this._baseY = this._y;
	}
		
	
	

}

Poster.prototype.getCoord = function ()
{
	return [this._x,this._y];
}

Poster.prototype.onDrag = function ()
{
	if (this._negX && this._posX && this._negY && this._posY)
		return;
	
	if (!this._negX && this._x + this._canvas._x - this._canvas._width + this._width*2 > 0)
	{
		this._canvas.duplicatePoster(this,-1,0);
		this._negX = 1;
	}
	if (!this._posX && this._x + this._canvas._x + this._canvas._width < aWindowSize[0] + this._width)
	{
		this._canvas.duplicatePoster(this,1,0);
		this._posX = 1;
	}
	if (!this._negY && this._y + this._canvas._y - this._canvas._height + this._height*2 > 0)
	{
		this._canvas.duplicatePoster(this,0,-1);
		this._negY = 1;
	}
	if (!this._posY && this._y + this._canvas._y + this._canvas._height < aWindowSize[1] + this._height)
	{
		this._canvas.duplicatePoster(this,0,1);
		this._posY = 1;
	}
}
Poster.prototype.setX = function (x)
{
	this.oImg.style.left = Math.round(((this._x = x)/this._canvas._width)*10000)/100 + '%';
}
Poster.prototype.setY = function (y)
{
	this.oImg.style.top = Math.round(((this._y = y)/this._canvas._height)*10000)/100 + '%';
}


function canvasOffsets()
{
	this.aStack = [];
}

canvasOffsets.prototype.add = function (id,oX,oY)
{
	if (!this.aStack[id])
	{
		this.aStack[id] = {x: isNaN(oX) ? 0 : parseInt(oX), y: isNaN(oY) ? 0 : parseInt(oY)};
	}
}

canvasOffsets.prototype.getOffsetById = function (id)
{
	return this.aStack[id] ? this.aStack[id] : false;
}

function fOnLoad(){
}
