/*
styledEffects v0.6
Coded by Rustam Khodjaev.
Requires handler.js - see http://styledhtml.com/framework/ for details
This code is the part of copyrighted content and not for public use.
*/

var preloading,preloadImg,preloadTimer,zoomerBusy
var slideStep=20,zSteps=15,shadowSize=10,currentStep=0,stepsMade=0,defaultWidth=150
var zoomParent,lastZoomed,zoomTimer,zoomStep,closeButton,viewFrame,forward=new Array()

function slideIn(obj) {
	obj=typeof obj=='object'?obj:get(obj)
	photos=getByTag(obj,'img').length
	if(photos>4) {
		obj.initPosition=obj.offsetLeft
		allowStep=((photos-4)-currentStep)>=2?2:(photos==currentStep+4?0:1)
		obj.sTimer=setInterval( function() { new sHandler(obj,allowStep,false) } ,1)
		currentStep+=allowStep
	}
}

function slideOut(obj) {
	obj=typeof obj=='object'?obj:get(obj)
	photos=getByTag(obj,'img').length
	if(currentStep>0) {
		obj.initPosition=obj.offsetLeft
		allowStep=(currentStep>=2?2:(currentStep==0?0:1))
		obj.sTimer=setInterval( function() { new sHandler(obj,allowStep,true) } ,1)
		currentStep-=allowStep
	}
}

function sHandler(obj,steps,forward) {
	position=obj.offsetLeft
	stepsMade+=slideStep
	if(!obj.imgWidth) obj.imgWidth=defaultWidth
	if(stepsMade<=(steps*obj.imgWidth)) {
		setStyle(obj,'left',forward?position+slideStep:position-slideStep)
	} else {
		stepsMade=0
		clearInterval(obj.sTimer)
		setStyle(obj,'left',forward?obj.initPosition+steps*obj.imgWidth:obj.initPosition-steps*obj.imgWidth)
		if(obj.afterSlideIn) {
			obj.afterSlideIn()
			obj.afterSlideIn=false
		}
	}
}

////////////////////////////////////////////    Zoom Effect    //////////////////////////////////////////////

function preparePhotoEffects() {
	zoomerBusy=false
	var eimgs=getByTag(document,'img')
	for(i=0;i<eimgs.length;i++) {
		if(eimgs[i].getAttribute('rel') && eimgs[i].getAttribute('rel').charAt(0)=='z') {
			eimgs[i].onclick=function() { 
				if(!zoomerBusy) {
					zoomerBusy=true
					var coords=getPosition(this)
					var ccpos=getPosition(getByTag(document,'div',0))
					var preAnimator=appendElement('div',this.parentNode,{
						position:'absolute',
						top:coords[1],
						left:coords[0],
						width:this.offsetWidth,
						height:10,
						backgroundImage:'url(images/sEffects/preload.gif)',
						opacity:60
					})
					preloadPhoto(this, function(elem) {
							preAnimator.parentNode.removeChild(preAnimator)
							elem.onclick=function() {
								zoomIn(this)
							}
							zoomIn(elem)
							zoomerBusy=false
					})
				}
			}
			eimgs[i].onmouseover=function() { preloadPhoto(this) }
		}
	}
	preLoader([
		'images/sEffects/preload.gif',
		'images/sEffects/closeimg.png',
		'images/sEffects/cursors/zout.cur',
		'images/sEffects/shadow/bl.png',
		'images/sEffects/shadow/rt.png',
		'images/sEffects/shadow/b.png',
		'images/sEffects/shadow/r.png',
		'images/sEffects/shadow/c.png',
		'images/sEffects/c1.png',
		'images/sEffects/c2.png',
		'images/sEffects/c3.png',
		'images/sEffects/c4.png',
	])
}

function preLoader(mixed) {
	if(typeof mixed == 'string') {
		var preloadImgExt=new Image()
		preloadImgExt.src=mixed
	} else {
		var preloadImgExt=new Array()
		for(i=0;i<mixed.length;i++) {
			preloadImgExt[i]=new Image()
			preloadImgExt[i].src=mixed[i]
		}
	}
}

function preloadPhoto(elem, action) {
	imgLink=elem.getAttribute('rel').replace('zoom:','')
	preloadImg=new Image()
	preloadImg.onload=function() { if(typeof action=='function') action(elem) }
	preloadImg.src=imgLink
}

function zoomIn(elem) {
	if(lastZoomed) closeImage(lastZoomed)
	zoomParent=elem
	img=appendElement('img',document.body,{})
	img.src=elem.getAttribute('rel').replace('zoom:','')
	destSize=img.fullSize=[img.width,img.height]
	forward=[elem.offsetWidth<destSize[0],elem.offsetHeight<destSize[1]]
	zoomStep=[forward[0]?destSize[0]/zSteps:elem.offsetWidth/zSteps,forward[1]?destSize[1]/zSteps:elem.offsetHeight/zSteps]
	setStyles(img,{
		position:'absolute',
		left:getPosition(elem)[0],
		top:getPosition(elem)[1],
		height:elem.offsetHeight,
		width:elem.offsetWidth,
		cursor:'pointer',
		zIndex:3
	})
	img.imageid=elem.getAttribute('imgid')
	img.type=elem.getAttribute('type')
	img.zoomTimer=setInterval(function(){
		new zoomHandler(img,destSize,function(img,location,size) {
			appendElements(img,location,size)
			setOpacity(img,100)
			lastZoomed=img
		},0)
	},1)
}

function zoomOut(img) {
	destSize=[zoomParent.width,zoomParent.height]
	currentSize=[img.offsetWidth,img.offsetHeight]
	forward=[currentSize[0]<destSize[0],currentSize[1]<destSize[1]]
	zoomStep=[forward[0]?destSize[0]/zSteps:currentSize[0]/zSteps,forward[1]?destSize[1]/zSteps:currentSize[1]/zSteps]
	img.zoomTimer=setInterval(function(){new zoomHandler(img,destSize,function(){img.parentNode.removeChild(img)},0)},1)
}

function zoomHandler(img,destSize,onComplete,reverseOpacity) {
	currentSize=[img.offsetWidth,img.offsetHeight]
	currentLocation=getPosition(img)
	preSize=[forward[0]?currentSize[0]+zoomStep[0]:currentSize[0]-zoomStep[0],forward[1]?currentSize[1]+zoomStep[1]:currentSize[1]-zoomStep[1]]
	newSize=[
		forward[0]?(preSize[0]>destSize[0]?destSize[0]:currentSize[0]+zoomStep[0]):(preSize[0]<destSize[0]?destSize[0]:currentSize[0]-zoomStep[0]),
		forward[1]?(preSize[1]>destSize[1]?destSize[1]:currentSize[1]+zoomStep[1]):(preSize[1]<destSize[1]?destSize[1]:currentSize[1]-zoomStep[1])
	]
	preLocation=[(forward[0]?newSize[0]-currentSize[0]:currentSize[0]-newSize[0])/2,(forward[1]?newSize[1]-currentSize[1]:currentSize[1]-newSize[1])/2]
	newLocation=[forward[0]?currentLocation[0]-preLocation[0]:currentLocation[0]+preLocation[0],forward[1]?currentLocation[1]-preLocation[1]:currentLocation[1]+preLocation[1]]
	completion=[currentSize[0]==destSize[0],currentSize[1]==destSize[1]]
	newOpacity=currentSize[0]*100/(forward[0]?destSize[0]:img.fullSize[0])
	if(!completion[0]) setStyles(img,{width:newSize[0],left:newLocation[0]})
	if(!completion[1]) setStyles(img,{height:newSize[1],top:newLocation[1]})
	if(completion[0] && completion[1]) {
		clearInterval(img.zoomTimer)
		onComplete(img,newLocation,newSize)
	} else setOpacity(img,reverseOpacity?(90-newOpacity):newOpacity)
}

function appendElements(img,location,size) {
	img.shadows=new Array()
	createShadowPiece(img,[location[1]+size[1],location[0]],[shadowSize,shadowSize],[1,'bl'])
	createShadowPiece(img,[location[1]+size[1],location[0]+shadowSize],[size[0]-shadowSize,shadowSize],[2,'b'])
	createShadowPiece(img,[location[1],location[0]+size[0]],[shadowSize,shadowSize],[3,'rt'])
	createShadowPiece(img,[location[1]+shadowSize,location[0]+size[0]],[shadowSize,size[1]-shadowSize],[4,'r'])
	createShadowPiece(img,[location[1]+size[1],location[0]+size[0]],[shadowSize,shadowSize],[5,'c'])
	closeButton=appendElement('img',document.body,{
		position:'absolute',
		left:location[0]+size[0]-15,
		top:location[1]-10,
		cursor:'pointer',
		zIndex:4
	})
	closeButton.src='images/sEffects/closeimg.png'
	if(img.type!='noload') {
		viewFrame=appendElement('div',document.body,{
			backgroundColor:'black',
			position:'absolute',
			color:'white',
			height:20,
			width:size[0],
			textAlign:'center',
			left:location[0],
			top:location[1]+size[1]-20,
			opacity:70,
			zIndex:5
		})
		viewFrame.innerHTML='Загрузка'
		viewFrame.id='viewFrame'
	}
	Request('tools/reply.php','id='+img.imageid+'&type='+img.type,'',function(text) {
		if(viewFrame) viewFrame.innerHTML='Просмотров: '+text
	})
	if(IE6) iePNGFix()
	img.onclick=closeButton.onclick=function(){closeImage(img)}
	setStyle(img,'cursor','url(images/sEffects/cursors/zout.cur),pointer')
}

function createShadowPiece(img,location,size,names) {
	img.shadows[names[0]]=appendElement('img',document.body,{
		position:'absolute',
		height:size[1],
		width:size[0],
		left:location[1],
		top:location[0]
	})
	img.shadows[names[0]].src='images/sEffects/shadow/'+names[1]+'.png'
}

function closeImage(img) {
	if (closeButton.parentNode) closeButton.parentNode.removeChild(closeButton)
	if (viewFrame && viewFrame.parentNode) viewFrame.parentNode.removeChild(viewFrame)
	index=1
	while(img.shadows[img.shadows.length-1].parentNode) {
		img.shadows[index].parentNode.removeChild(img.shadows[index])
		index++
	}
	zoomOut(img)
	lastzoomed=null
}