$(function() {
	var movingDistance	    = 94;
	var fullDistance 		= 582;
	var curWidth			= 232;
	var curHeight			= 140;
	var widthCoef			= 0.8;
	var alphaCoef			= 0.7;
	var ajaxFile			= "/script/ajax.php";
	var panels				= $('#slider > img');
	var images				= new Array();
	var changingPlus 		= false;

	panels.css({'position' : 'absolute'});
	$("#slider").data("currentlyMoving", false);

	var activePanel = Math.ceil(panels.length / 2) - 1;
	
	function getWidth(n){
		n = Math.abs(n - activePanel);
		var width = curWidth;
		for (var i = 0; i < n; i++) width *= 0.8;
		return width;
	}
	
	function getHeight(n){
		n = Math.abs(n - activePanel);
		var height = curHeight;
		for (var i = 0; i < n; i++) height *= 0.8;
		return height;
	}


	function setDimensions(){
		for (var i = 0; i < panels.length; i++){
			$(panels[i]).attr('width', getWidth(i));			
			$(panels[i]).attr('height', getHeight(i));
		}
	}
	
	function setZIndex(){
		for (var i = 0; i < panels.length; i++) $(panels[i]).css('z-index', (1000 - Math.abs(i - activePanel)));
	}
	
	function setAlpha(){
		for (var i = 0; i < panels.length; i++){
			$(panels[i]).css('filter', 'alpha(opacity=' + (i == activePanel ? 100 : 100 * alphaCoef) + ')');
			$(panels[i]).css('-moz-opacity', i == activePanel ? 1 : alphaCoef);
		}
	}
	
	function getAlpha(n){
		return n == activePanel ? 1 : alphaCoef;
	}

	function replaceTags(n, m){
		var start = 0;
		var end = panels.length;
		if (n >= 0 && m >= 0){
			start = n;
			end = m;
		}
		for (var i = start; i < end; i++){
			var text = $(panels[i]).attr('title');
			$(panels[i]).data('titlehtml', text);
			
			text = text.replace(/<br\s?\/?>/, " - ");
			while (text.match(/<.*?>/)) text = text.replace(/<.*?>/, "");
			$(panels[i]).attr('title', text);
		}
	}
	
	function getLeft(n){
		var pos = Math.ceil((fullDistance - getWidth(activePanel)) / 2);
		for (var i = activePanel; i < n; i++) pos += Math.ceil(getWidth(i) * (1 - 0.5 * widthCoef));
		for (var i = activePanel - 1; i >= n; i--) pos -= Math.ceil(getWidth(i) / 2);
		return pos;
	}
	
	function getTop(n){
		var h = 20;
		for (i = 0; i < Math.abs(n - activePanel); i++) h += Math.ceil(curHeight * (1 - widthCoef) / 2);
		return h;
	}

	function returnToNormal(){
		for (var i = 0; i < panels.length; i++) $(panels[i]).animate({ 
			'width': getWidth(i),
			'left': getLeft(i),
			'top': getTop(i),
			'opacity': getAlpha(i)
		}, function() { if ($("#slider").data("currentlyMoving") == true){ $("#slider").data("currentlyMoving", false); } }).end();
	};
	
	function setSliderText(){
		var text = $(panels[activePanel]).data('titlehtml');
		var re = new RegExp('<a', '');
		text = text.replace(re, "<a target='_blank'");
		$('#slider-text').html(text);
	}
	
	function getImages(callback){
		$.ajax({
			type: "POST",
			url: ajaxFile,
			data: "cat=" + ids,
			success: function(msg){
				images = eval(msg);
				eval(callback);
			}
		});
	}
	
	function prependPicture(n){
		if (images.length >= n){
			for (var i = 0; i < n; i++){
				var obj = $('#slider > img:last');
				$(obj).prependTo('#slider');
				$(obj).attr(images[0]);
				images.splice(0, 1);
			}
			
			panels = $('#slider > img');
			activePanel += n;
			returnToNormal();
			setZIndex();
			replaceTags(0, n);
			bindActions();
		} else getImages('prependPicture(' + n + ')');
	}
	
	function appendPicture(n){
		if (images.length >= n){
			for (var i = 0; i < n; i++){
				var obj = $('#slider > img:first');
				$(obj).appendTo('#slider');
				$(obj).attr(images[0]);
				images.splice(0, 1);
			}
			
			panels = $('#slider > img');
			activePanel -= n;
			returnToNormal();
			setZIndex();
			replaceTags(panels.length - n, panels.length);
			bindActions();
		} else getImages('appendPicture(' + n + ')');
	}
	
	function goActiveHref(){
		var text = $(panels[activePanel]).data('titlehtml');
		var href = text.match(/href='(.*?)'/i);
		window.open(href[1]);
	}
	
	function preChange(obj){
		for (var i = 0; i < panels.length; i++) if (this == panels[i]) alert(i);
	}
	
	function bindActions(){
		panels.unbind();
		for (var i = 0; i < panels.length; i++){
			var ch = i - activePanel;
			switch (ch){
				case -2: $(panels[i]).click(function(){ change(-2); }); break;
				case -1: $(panels[i]).click(function(){ change(-1); }); break;
				case  0: 
					$(panels[i]).click(function(){ goActiveHref(); }); 
					$(panels[i]).mouseover(function(){ growCenter(); });
					$(panels[i]).mouseout(function(){ retCenter(); });
					break;
				case  1: $(panels[i]).click(function(){ change(1); }); break;
				case  2: $(panels[i]).click(function(){ change(2); }); break;
			}
		}
	}
	
	function growCenter(){
		if (changingPlus) return;
		changingPlus = true;
		$(panels[activePanel]).animate({ width: '287', top: '-=10', left: '-='+Math.ceil((287-curWidth) / 2) }, function(){ changingPlus = false; });
	}
	
	function retCenter(){
		if ($("#slider").data("currentlyMoving") == false) returnToNormal();
	}
	

	replaceTags(-1, -1);
	change(0);
	
	function change(direction){
        if ($("#slider").data("currentlyMoving") == false){
			$("#slider").data("currentlyMoving", true);
			activePanel += direction;	
			setSliderText();
			panels.css('cursor', 'default');
			$(panels[activePanel]).css('cursor', 'pointer');
			
			if (!direction){
				returnToNormal();
				setZIndex();
				bindActions();
			}
			
			if (direction > 0) appendPicture(direction);
			if (direction < 0) prependPicture(direction * (-1));
		}
			
		
	}

	
	$(".scroll2right").click(function(){ change(1); });	
	$(".scroll2left").click(function(){ change(-1); });
	
	$(window).keydown(function(event){
		switch (event.keyCode) {
			case 37: $(".scroll2left").click(); break;
			case 39: $(".scroll2right").click(); break;
		}
	});
});