var stuffCounter = 0;

var zoomStep = 1.2;
var effs = [];
moveStuff = function(id) {
	var eff = new Draggable(id, {
		revert: true,
		onEnd: function(item) {
			var _target;
			var _x = $(item.element.id).parentNode.positionedOffset().left + $(item.element.id).positionedOffset().left + $(item.element.id).getWidth() / 2;
			var _y = $(item.element.id).parentNode.positionedOffset().top + $(item.element.id).positionedOffset().top + $(item.element.id).getHeight() / 2;
			$$('.cell-field td div').each( function(el) {
				if ( _x > el.positionedOffset().left && _x < ( el.positionedOffset().left + el.getWidth() ) && _y > el.positionedOffset().top && _y < ( el.positionedOffset().top + el.getHeight() ) ) _target = el;
			});
			if (typeof _target != 'undefined') {
				eff.destroy();
				var newStuff = $(item.element.id).clone();
				_id = $(item.element.id).id;
				$(item.element.id).remove();
				newStuff.id = _id;
				if (Prototype.Browser.ltIE9) {
					newStuff.style.filter = 'none';
					newStuff.style.zIndex = '1111';
					newStuff._opacity = '';
				}
				if ($(_target).select("img").length > 1) {
					$(_target).select("img").each(function(el) {
						if ( el.src.indexOf('blank') < 0 ) el.remove();
					});
				}
				_target.insert(newStuff);
				$(_id).style.left = _target.getWidth() / 2 - $(_id).getWidth() / 2 + 'px';
				$(_id).style.top = _target.getHeight() / 2 - $(_id).getHeight() / 2 + 'px';
				moveStuff(_id);
			}
			else {
				$(item.element.id).remove();
			}
		}
	});
}

var Window={size:function(){var C=window.innerWidth||(window.document.documentElement.clientWidth||window.document.body.clientWidth);var B=window.innerHeight||(window.document.documentElement.clientHeight||window.document.body.clientHeight);var A=window.pageXOffset||(window.document.documentElement.scrollLeft||window.document.body.scrollLeft);var D=window.pageYOffset||(window.document.documentElement.scrollTop||window.document.body.scrollTop);return{width:C,height:B,x:A,y:D}}};

var FancyZoomBox2={directory:"images",zooming:false,setup:false,init:function(C){if(FancyZoomBox2.setup){return }FancyZoomBox2.setup=true;var E=navigator.userAgent.match(/MSIE\s(\d)+/);if(E){var B=parseInt(E[1]);Prototype.Browser["IE"+B.toString()]=true;Prototype.Browser.ltIE7=(B<7)?true:false;Prototype.Browser.ltIE9=(B<9)?true:false}var D='<div class="zoom-box" id="zoom2" style="z-index:1000; display:none;"><table id="zoom_table2" style="border-collapse:collapse; width:100%; height:100%;"><tbody><tr><td class="tl" style="background:url('+FancyZoomBox.directory+'/tl.png) 0 0 no-repeat; width:16px; height:16px; overflow:hidden;" /><td class="tm" style="background:url('+FancyZoomBox.directory+'/tm.png) 0 0 repeat-x; height:16px; overflow:hidden;" /><td class="tr" style="background:url('+FancyZoomBox.directory+'/tr.png) 100% 0 no-repeat; width:16px; height:16px; overflow:hidden;" /></tr><tr><td class="ml" style="background:url('+FancyZoomBox.directory+'/ml.png) 0 0 repeat-y; width:16px; overflow:hidden;" /><td class="mm" style="background:#fff; vertical-align:top; padding:10px;"><div style="width: 100%;"><div class="zoom-box-content" id="zoom_content2"><!-- --></div></div></td><td class="mr" style="background:url('+FancyZoomBox.directory+'/mr.png) 100% 0 repeat-y;  width:16px; overflow:hidden;" /></tr>' +

'<tr class="ha"><td class="ml" style="background:url('+FancyZoomBox.directory+'/gray/ml.png) 0 0 no-repeat; width:16px; overflow:hidden;" /><td class="mm" style="background: #ededed url('+FancyZoomBox.directory+'/gray/back.gif) 0 0 repeat-x; vertical-align:top; padding:10px 10px 0;"><div style="width: 100%;"><div id="info_content"><!-- --></div></div></td><td class="mr" style="background:url('+FancyZoomBox.directory+'/gray/mr.png) 100% 0 repeat-y;  width:16px; overflow:hidden;" /></tr>'

+ '<tr><td class="bl" style="background:url('+FancyZoomBox.directory+'/gray/bl.png) 0 100% no-repeat; width:16px; height:16px; overflow:hidden;" /><td class="bm" style="background:url('+FancyZoomBox.directory+'/gray/bm.png) 0 100% repeat-x; height:16px; overflow:hidden;" /><td class="br" style="background:url('+FancyZoomBox.directory+'/gray/br.png) 100% 100% no-repeat; width:16px; height:16px; overflow:hidden;" /></tr></tbody></table><a href="#" title="Close" id="zoom_close2" style="display:none;position:absolute; top:0; left:0;"><img src="'+FancyZoomBox.directory+'/closebox.png" alt="Close" style="border:none; margin:0; padding:0;" /></a></div>';var A=$$("body").first();A.insert(D);FancyZoomBox2.zoom=$("zoom2");FancyZoomBox2.zoom_table=$("zoom_table2");FancyZoomBox2.zoom_close=$("zoom_close2");FancyZoomBox2.zoom_content=$("zoom_content2");FancyZoomBox2.zoom_info=$("info_content");
FancyZoomBox2.middle_row=$A([$$("td.ml"),$$("td.mm"),$$("td.mr")]).flatten();
FancyZoomBox2.cells=FancyZoomBox2.zoom_table.select("td");
//$(document).observe("keyup",function(G){var F=FancyZoomBox2.zoom.getStyle("display");if(G.keyCode==Event.KEY_ESC&&F=="block"){FancyZoomBox2.hide(G)}});
if(Prototype.Browser.ltIE7){FancyZoomBox2.switchBackgroundImagesTo("gif")}},

show:function(E){

Element.insert($$('body')[0],{
	top: new Element('div', {
		className: 'overlay'
	})
});
$$('.overlay')[0].style.height = $$('.container')[0].getDimensions().height + 'px';

//$$(".overlay")[0].observe("click",function(H){var G=H.findElement("#zoom"),F=FancyZoomBox2.zoom.getStyle("display");if(F=="block"/*&&!G*/){FancyZoomBox2.hide(H)}});

E.stop();
if(FancyZoomBox2.zooming){return }FancyZoomBox2.zooming=true;var C=E.findElement("a");var D=C.content_div;FancyZoomBox2.dd=D;var D_info=C.content_div_info;FancyZoomBox2.dd_info=D_info;var A=(C.zoom_width||D.getWidth())+60;var I=(C.zoom_height||D.getHeight())+60;var F=Window.size();var B=document.viewport.getScrollOffsets()[1];var G=Math.max((F.height/2)-(I/2)+B,0);var H=(F.width/2)-(A/2);
if (isNaN(!E.pointerY())) {
	FancyZoomBox2.curTop=E.pointerY();
}
else {
	FancyZoomBox2.curTop = 10;
}
if (!isNaN(E.pointerX())) {
	FancyZoomBox2.curLeft=E.pointerX();
}
else {
	FancyZoomBox2.curLeft = 10;
}

FancyZoomBox2.moveX=-(FancyZoomBox2.curLeft-H);FancyZoomBox2.moveY=-(FancyZoomBox2.curTop-G);FancyZoomBox2.zoom.hide().setStyle({position:"absolute",top:FancyZoomBox2.curTop.px(),left:FancyZoomBox2.curLeft.px()});new Effect.Parallel([new Effect.Appear(FancyZoomBox2.zoom,{sync:true}),new Effect.Move(FancyZoomBox2.zoom,{x:FancyZoomBox2.moveX,y:FancyZoomBox2.moveY,sync:true}),new Effect.Morph(FancyZoomBox2.zoom,{style:{width:A.px(),height:I.px()},sync:true,beforeStart:function(J){
//if(Prototype.Browser.IE){FancyZoomBox2.middle_row.invoke("setStyle",{height:(I-40).px()})}
FancyZoomBox2.fixBackgroundsForIE()
},
	afterFinish:function(J){
		FancyZoomBox2.zoom_content.innerHTML=D.innerHTML;
		/**/$(D).update('');/**/
		FancyZoomBox2.zoom_info.innerHTML=D_info.innerHTML;
		/**/$(D_info).update('');/**/
		FancyZoomBox2.unfixBackgroundsForIE();
		$$('#zoom2 .close_button')[0].observe("click",FancyZoomBox2.hide);

if ( $$('#zoom2 .photoeditor').length == 1 ) {
	$$('#zoom2 .photoeditor .img')[0].id = $$('#zoom2 .photoeditor .img')[0].alt;

	$$("#zoom2 .photoeditor .img")[0].clonePosition($$("#zoom2 .photoeditor .img-photo")[0]);
	$$("#zoom2 .photoeditor .img")[0].style.zIndex = "1111";

/*
	imgp = $$("#zoom2 .photoeditor .img-photo")[0].getDimensions();
	$$('#zoom2 .photoeditor .img')[0].style.width = imgp.width + 'px';
	$$("#zoom2 .photoeditor .img")[0].style.height = imgp.height + "px";
*/

	new Draggable($$('#zoom2 .photoeditor .img')[0].id, {
		snap: function(x, y, d) {
			pe = d.element.up().getDimensions();
			//img = $$("#zoom2 .photoeditor .img")[0].getDimensions();

/*
			console.log("pe: " + pe.width + "x" + pe.height);
			console.log("img: " + img.width + "x" + img.height);
*/
/*
			if (x > 0) {
				theX = 0;
				if (y > 0) {
					theY = 0;
					d.element.style.cursor = "nw-resize";
				} else if (y < pe.height - d.element.height) {
					theY = pe.height - d.element.height;
					d.element.style.cursor = "sw-resize";
				} else {
					theY = y;
					d.element.style.cursor = "w-resize";
				}
			} else if (x < pe.width - d.element.width) {
				theX = pe.width - d.element.width;
				if (y > 0) {
					theY = 0;
					d.element.style.cursor = "ne-resize";
				} else if (y < pe.height - d.element.height) {
					theY = pe.height - d.element.height;
					d.element.style.cursor = "se-resize";
				} else {
					theY = y;
					d.element.style.cursor = "e-resize";
				}
			} else {
				theX = x;
				if (y > 0) {
					theY = 0;
					d.element.cursor = "n-resize";
				} else if (y < pe.height - d.element.height) {
					theY = pe.height - d.element.height;
					d.element.style.cursor = "s-resize";
				} else {
					theY = y;
					d.element.style.cursor = "move";
				}
			}
*/

			theX = x < pe.width - d.element.width   ? pe.width - d.element.width   : (x > 0 ? 0 : x);
			theY = y < pe.height - d.element.height ? pe.height - d.element.height : (y > 0 ? 0 : y);

			return [theX, theY];
		},
		onDrag: function(d, e) {
			img = $$("#zoom2 .photoeditor .img-photo")[0].positionedOffset();
			$$('#zoom2 .photoeditor .img-photo')[0].style.left = $$('#zoom2 .photoeditor .img')[0].style.left;
			$$('#zoom2 .photoeditor .img-photo')[0].style.top = $$('#zoom2 .photoeditor .img')[0].style.top;
		},
		onEnd: function() {
			$$("#zoom2 .input_cropX")[0].value = img.left;
			$$("#zoom2 .input_cropY")[0].value = img.top;
		}
	});
/*
	$$('#zoom2 .photoeditor .zoom-in')[0].observe('click', function() {
		inp = $$("#zoom2 .input_scale")[0];
		v = isNaN(parseInt(inp.value)) ? 1 : inp.value;
		inp.value = v * zoomStep;

		olds = $$('#zoom2 .photoeditor .img-photo')[0].getDimensions();
		oldo = $$("#zoom2 .photoeditor .img-photo")[0].positionedOffset();

		$$('#zoom2 .photoeditor .img-photo')[0].style.width = olds.width * zoomStep + 'px';
		news = $$('#zoom2 .photoeditor .img-photo')[0].getDimensions();

		$$('#zoom2 .photoeditor .img-photo')[0].style.left = (oldo.left - 30) * zoomStep + "px";
		$$('#zoom2 .photoeditor .img-photo')[0].style.top = (oldo.top - 30) * zoomStep + 'px';
		newo = $$('#zoom2 .photoeditor .img-photo')[0].positionedOffset();

		$$('#zoom2 .photoeditor .img')[0].style.width = news.width + 'px';
		$$("#zoom2 .photoeditor .img")[0].style.height = news.height + "px";
		$$('#zoom2 .photoeditor .img')[0].style.left = newo.left + 'px';
		$$('#zoom2 .photoeditor .img')[0].style.top = newo.top + 'px';
	});
	$$('#zoom2 .photoeditor .zoom-out')[0].observe('click', function() {
		inp = $$("#zoom2 .input_scale")[0];
		v = isNaN(parseInt(inp.value)) ? 1 : inp.value;
		inp.value = v / zoomStep;

		olds = $$('#zoom2 .photoeditor .img-photo')[0].getDimensions();
		oldo = $$("#zoom2 .photoeditor .img-photo")[0].positionedOffset();

		$$('#zoom2 .photoeditor .img-photo')[0].style.width = olds.width / zoomStep + 'px';
		news = $$('#zoom2 .photoeditor .img-photo')[0].getDimensions();

		$$('#zoom2 .photoeditor .img-photo')[0].style.left = (oldo.left + 30) / zoomStep + "px";
		$$('#zoom2 .photoeditor .img-photo')[0].style.top = (oldo.top + 30) / zoomStep + 'px';
		newo = $$('#zoom2 .photoeditor .img-photo')[0].positionedOffset();

		$$('#zoom2 .photoeditor .img')[0].style.width = news.width + 'px';
		$$("#zoom2 .photoeditor .img")[0].style.height = news.height + "px";
		$$('#zoom2 .photoeditor .img')[0].style.left = newo.left + 'px';
		$$('#zoom2 .photoeditor .img')[0].style.top = newo.top + 'px';
	});
*/
}
//var stuffCounter = 0;
if ( $$('#zoom2 .cell-content').length == 1 ) {
	$$('#zoom2 .drag').each( function(i,j) {
		i.id = "stuffCounter" + stuffCounter;
		moveStuff("stuffCounter" + stuffCounter);
		stuffCounter++;
	});
	if ( $$('.draggableStuff').length > 0 ) {
		$$('.draggableStuff').each( function(el) {
			el.remove();
		})
	}
	/*if ( $$('#zoom2 .cell-map .draggableStuff').length > 0 ) {
		$$('#zoom2 .cell-map .draggableStuff').each( function(el) {
			el.remove();
		})
	}*/
	$$('#zoom2 .stuff').each( function(i) {
		var newElement = new Element('img',
			{
				src: i.src,
				id: i.alt,
				style: "position: absolute; z-index: 1111",
				className: 'draggableStuff'
			}
		)
		Element.insert($$('#zoom2 .cell-map')[0],newElement);
		$(i.alt).style.left = i.cumulativeOffset().left - $(i.alt).parentNode.cumulativeOffset().left + 'px';
		$(i.alt).style.top = i.cumulativeOffset().top - $(i.alt).parentNode.cumulativeOffset().top + 'px';
			//alert(i.cumulativeOffset().top + " - " + $(i.alt).parentNode.cumulativeOffset().top + " = " + (i.cumulativeOffset().top - $(i.alt).parentNode.cumulativeOffset().top) )
		new Draggable(i.alt,{
			revert: true,
			onEnd: function() {
				var _target;
				var _x = $(i.alt).positionedOffset().left + $(i.alt).getWidth() / 2;
				var _y = $(i.alt).positionedOffset().top + $(i.alt).getHeight() / 2;
				$$('.cell-field td div').each( function(el) {
					if ( _x > el.positionedOffset().left && _x < ( el.positionedOffset().left + el.getWidth() ) && _y > el.positionedOffset().top && _y < ( el.positionedOffset().top + el.getHeight() ) ) _target = el;
				});
				if (typeof _target != 'undefined') {
					var newElement = new Element('img',
						{
							src: i.src.replace('_small\.p','_big.p'),
							id: "stuffCounter" + stuffCounter,
							style: "position: absolute; z-index: 1111",
							className: 'drag'
						}
					)
					if ($(_target).select("img").length > 1) {
						$(_target).select("img").each(function(el) {
							if ( el.src.indexOf('blank') < 0 ) el.remove();
						});
					}
					Element.insert(_target,newElement);
					$("stuffCounter" + stuffCounter).style.left = _target.getWidth() / 2 - $("stuffCounter" + stuffCounter).getWidth() / 2 + 'px';
					$("stuffCounter" + stuffCounter).style.top = _target.getHeight() / 2 - $("stuffCounter" + stuffCounter).getHeight() / 2 + 'px';
					moveStuff("stuffCounter" + stuffCounter);
					stuffCounter++;
				}
			}
		});
	});
}

FancyZoomBox2.zooming=false}})],{duration:0.5})},

hide:function(A){A.stop();if(FancyZoomBox2.zooming){return }FancyZoomBox2.zooming=true;
new Effect.Parallel([new Effect.Move(FancyZoomBox2.zoom,{x:FancyZoomBox2.moveX*-1,y:FancyZoomBox2.moveY*-1,sync:true}),new Effect.Morph(FancyZoomBox2.zoom,{style:{width:"1".px(),height:"1".px()},sync:true,
	beforeStart:function(B){
		FancyZoomBox2.fixBackgroundsForIE();
		$(FancyZoomBox2.dd).update(FancyZoomBox2.zoom_content.innerHTML);
		FancyZoomBox2.zoom_content.innerHTML="";
		$(FancyZoomBox2.dd_info).update(FancyZoomBox2.zoom_info.innerHTML);
		FancyZoomBox2.zoom_info.innerHTML='';
		FancyZoomBox2.zoom_close.hide();
	},
	afterFinish:function(B){
		FancyZoomBox2.unfixBackgroundsForIE();
		FancyZoomBox2.zooming=false;
		if($$('.overlay')){$$('.overlay')[0].remove()};
		//if (this.options.
	}}),new Effect.Fade(FancyZoomBox2.zoom,{sync:true})],{duration:0.5})},

switchBackgroundImagesTo:function(C){FancyZoomBox2.cells.each(function(E){var D=E.getStyle("background-image").gsub(/\.(png|gif|none)\)$/,"."+C+")");E.setStyle("background-image: "+D)});var B=FancyZoomBox2.zoom_close.firstDescendant();var A=B.readAttribute("src").gsub(/\.(png|gif|none)$/,"."+C);B.writeAttribute("src",A)},fixBackgroundsForIE:function(){if(Prototype.Browser.IE7){FancyZoomBox2.switchBackgroundImagesTo("gif")}},unfixBackgroundsForIE:function(){if(Prototype.Browser.IE7){FancyZoomBox2.switchBackgroundImagesTo("png")}}};

var FancyZoom2=Class.create({
	initialize:function(A){
		this.options=arguments.length>1?arguments[1]:{};
		FancyZoomBox2.init();
		this.element=$(A);
		if(this.element){
			this.element.content_div=$(this.element.readAttribute("href").gsub(/^.*#/,""));
			this.element.content_div.hide();
			this.element.content_div_info=$(this.element.readAttribute("href").gsub(/^.*#/,"")+"_info");
			this.element.content_div_info.hide();
			this.element.zoom_width=this.options.width;
			this.element.zoom_height=this.options.height;
			this.element.observe("click",FancyZoomBox2.show);
		}
	}
});

