formUrl = "/form/";

var Gallery = function(photos){
	var minHeight = 100;
	var minWidth = 140;
	var padding = 20;
	var border = 10;
	var panelH = 40;
	var loaded = {}
	var that = this;
	var box = jq(".box");
	box.click(function(){
		return false;
	});
	var loading = jq(".loading");
	var frame = jq(".frame");
	frame.click(function(){
		close();
	});
	var fullMode = false;
	var boxX;
	var boxY;
	var imgBox = jq(".big-img", box);
	var photoIndex = 0;
	var prev = jq(".prev", box);
	var next = jq(".next", box);
	var blocked = true;
	
	this.photos = {};
	this.photosArr = [];
	
	for(var i in photos){
		this.photosArr.push(photos[i]);
		this.photos[photos[i].id] = photos[i];
	}
		
	
	this.show = function(id){
		for(var i = 0; i < this.photosArr.length; i++)
			if(this.photosArr[i].id == id){
				photoIndex = i;
				break;
			}
		frame.css({width:jq(document.body).width(), height:jq(document).height(), opacity:0.8, display:"block"});
		boxY = getBodyScrollTop() + getClientHeight() / 2 - minHeight / 2;
		boxX = getClientWidth() / 2 - minWidth / 2;
		box.css({top:boxY,left:boxX, width:minWidth, height:minHeight});
		preload(id, showPhoto);
	}
	
	function preload(id, handler){
		if(!loaded[id]){
			var i = new Image();
			i.onload = function(){
				loaded[id] = true;
				loading.hide();
				handler(id);
			};
			var logoX = Math.floor(getClientWidth() / 2 - 16);
			var logoY = Math.floor(getBodyScrollTop() + getClientHeight() / 2 - 16);
			loading.css({left:logoX, top:logoY});
			loading.show();
			i.src = "/images/uploaded/" + id + ".jpg";
		}
		else
			handler(id);
	}
	
	function showPhoto(id){
		box.css({display:"block", opacity:1});
		var k1 = (getClientHeight() - (2 * padding + 2 * border + panelH)) / that.photos[id].height;
		var k2 = (getClientWidth() - (2 * padding + 2 * border)) / that.photos[id].width;
		var k = k1;
		if(k2 < k1)
			k = k2;
		if(k > 1)
			k = 1;
		var newW = Math.floor(k * that.photos[id].width);
		var newH = Math.floor(k * that.photos[id].height);
		var windowW = Math.ceil(newW + 2 * border);
		var windowH = Math.ceil(newH + 2 * border + panelH);
		var newX = Math.floor(getClientWidth() / 2 - windowW / 2);
		var newY = Math.floor(getBodyScrollTop() + getClientHeight() / 2 - windowH / 2);
		var smallW = minWidth - 2 * border;
		var smallH = minHeight - 2 * border;
		if(fullMode){
			smallW = newW;
			smallH = newH;
		}
		imgBox.html('<img src="/images/uploaded/' + id + '.jpg" width="' + smallW + '" height="' + smallH + '" />');
		jq(".descr", box).html(that.photos[id].title);
		//jq("img", imgBox).css({opacity:0});
		if(!fullMode){
			box.animate({top:newY, left:newX, width:windowW, height:windowH}, 900, function(){
				controlButtons();
				jq(".hide", box).show();
			});
			jq("img", imgBox).animate({opacity:1, width:newW, height:newH}, 900);
		}else{
			box.animate({top:newY, left:newX, width:windowW, height:windowH}, 200, function(){
				jq("img", imgBox).animate({opacity:1}, 200);
			});
		}
		fullMode = true;
		blocked = false;
	}
	
	jq(".close a", box).click(function(){
		close();
		return false;
	});
	
	function close(){
		fullMode = false;
		jq(".hide", box).hide();
		box.animate({opacity:0}, 500, function(){
			frame.css({width:0, height:0, opacity:0});
			jq("img", imgBox).html("");
			box.hide();
		});
	}
	
	function showNextPhoto(id){
		blocked = true;
		preload(id, function(){
			jq("img", imgBox).animate({opacity:0}, 200, function(){
				showPhoto(id);
			});
		});
	}
	
	prev.click(function(){
		if(photoIndex == 0 || blocked)
			return;
		photoIndex --;
		if(photoIndex == 0)
			prev.addClass("inactivel");
		next.removeClass("inactiver");
		showNextPhoto(that.photosArr[photoIndex].id);
	});
	
	next.click(function(){
		if(photoIndex == that.photosArr.length - 1 || blocked)
			return;
		photoIndex ++;
		if(photoIndex == that.photosArr.length - 1)
			next.addClass("inactiver");
		prev.removeClass("inactivel");
		showNextPhoto(that.photosArr[photoIndex].id);
	});
	
	function controlButtons(){
		if(that.photosArr.length < 2){
			prev.addClass("inactivel");
			next.addClass("inactiver");
			return;
		}
		prev.removeClass("inactivel");
		next.removeClass("inactiver");
		if(photoIndex == 0)
			prev.addClass("inactivel");
		if(photoIndex == that.photosArr.length - 1)
			next.addClass("inactiver");
	}
}

onInit(function(){
	var originals = {};
	var photos = jq(".photo a");
	if(photos.length == 0)
		return;
	photos.each(function(i, item){
		item = jq(item);
		var parts = item.attr("class").split('_');
		originals[parts[0]] = {id:parts[1], width:parts[2], height:parts[3], small:parts[0], title:jq("span", item).html()};
		item.click(function(){
			gallery.show(parts[1]);
			return false;
		});
	});
	var gallery = new Gallery(originals);
	
});

onInit(function(){
	jq("[id^=link]").each(function(i, item){
		item = jq(item);
		var id = item.attr("id").replace("link_", "");
		item.click(function(){
			jq("#" + id).toggle();
			return false;
		});
	});
});

function onOrder(d, data){
	alert('Ваш запрос был успешно отправлен. В ближайшее время с Вами свяжется наш менеджер.');
}