links = Array(); X0 = Array(); Y0 = Array(); Hh=Array(); Hw=Array(); Pad = Array();
nLinks=0; Link = Array();

curAnchor = 1;

function setAnchor(number) {
		
		//document.location.replace(document.URL.substring(0,document.URL.indexOf('#'))+'#'+number);
		document.location.hash = '#'+number;
		//curAnchor = number; 
	}
	
function getAnchor()
{
	
	if(document.location.toString().indexOf('#')>0)
	{
		anchor = (document.location.toString().substring(document.location.toString().indexOf('#')+1, document.location.toString().length));
	} else {
		anchor = 1;
	}
	return anchor;
}	



function moveMenu(){
	$(document).mousemove(function(event){
		xm = event.pageX;
		ym = event.pageY;
		
		for (i = 0; i < nLinks; i++) {
			x0 = X0[i];
			y0 = Y0[i];
			dx = xm - x0;
			dy = ym - y0;
			r = Math.sqrt(dx * dx + dy * dy);
			if (r < max_rad) {
				if (r > 20) {
					d = 66 - 900 / r - r / 5;
					xshift = dx * d / (r * 2);
					yshift = dy * d / r;
				}
				else {
					xshift = dx;
					yshift = dy;
				}
			}
			else {
				xshift = 0;
				yshift = 0;
			}
			
			x = x0 + xshift - Hw[i];
			y = y0 + yshift - Hh[i];
			if (y < 0) 
				y = 0;
			links[i].css({
				left: x
			});
			Link[i].css({
				marginBottom: y + Pad[i]
			});
		}
	});
}



$(window).load(function()
{
	function loadWork(num, loaded)
	{
		current = $('div.big_preview[number='+num+']');
		workid = current.attr('workid');
		
		current.load("/portfolio/prework/"+workid+"/"+num+"/", function(){
			loaded++;
			percent = 100 * loaded / $("div.work_list").attr("max_position");
			percenttext = (''+percent+'.').substring(0,(percent+'.').indexOf('.'));
			if (loaded == $("div.work_list").attr("max_position")) {
				process();
			} else {
				$(".loading .text").text("Загрузка... "+percenttext+"%"); 
				loadWork(num+1,loaded);
			}
		});
		
	}
	$(".big_preview").each(function(){
		
	});
	if ($("div.work_list").attr("max_position") > 0) {
		setTimeout(function(){
			loadWork(1, 0);
			},100);
	} else {
		$(".loading").text("Нет работ для отображения.");
	}

	lastX = 0; lastW = 0;
	$(".menu div.item").each(function(){
		if(lastX == 0)
		{
			Width = $(this).width();
			$(this).css({left:50, top:0, width:Width});
			lastX=50; lastW = Width;
			
			$(this).find(".line").css({
				width: Width, height: 3, padding:0
			});
		} else {
			Width = $(this).width();
			$(this).css({
				left:lastX + lastW + 50,
				top:0,
				width: Width
			});
			
			$(this).children(".line").css({
				width: Width, height: 3, padding:0
			});
			
			lastX = lastX + lastW + 50; lastW = Width;
		}
	});

	$(".menu div.item").each(function(){

		x = $(this).offset().left + ($(this).width() / 2);
		y = $(this).offset().top + ($(this).height() / 2);

		pad = $(this).children("a,span").css("padding-top"); pad = pad.substr(0,pad.indexOf("px"))-0;;
		links[nLinks]=$(this);
		X0[nLinks]=x; Y0[nLinks]=y;
		Hh[nLinks]= pad + $(this).height() / 2;
		Hw[nLinks] = $(this).width() / 2;
		Link[nLinks] =  $(this).children(".line");
		$(this).children(".line").css({marginBottom:pad});
		Pad[nLinks] = pad;
		nLinks++;

	});
	max_rad = 300;
	min_rad = 15;
	k=0;
	setTimeout("moveMenu()",0);
	
	
	
	function process(){
		$(".loading").css({
						display: "none"
					});
					
					$(".sub_list").css({
						display: "block"
					});
					
		position = 1;
		active = true;
		setInterval(function(){
			if(active)
			{
				if(getAnchor()!=(position-0))
				{
					position=getAnchor();
					$(".work_list").scrollTo($('div[number=' + position +']'), 800, {
						axis: 'x',
						offset:-($("div.work_list").width()/2) + ($('div[number=' + (position)+']').width())/2
					});
					active = false;
					setTimeout("active=true", 800);
				}			
			}
		}, 100);
		if ($("div.work_list").width() > 700) {
			$("#work_0").css({
				width: ($("div.work_list").width() / 2) - $('div[number=1]').width() / 2
			});
			$("#work_" + ($("div.work_list").attr("max_position") - 0 + 1)).css({
				width: ($("div.work_list").width() / 2) - $('div[number=1]').width() / 2
			});
		}
		
		$("#scrollRight").click(function(){
			if ((position-0) < $("div.work_list").attr("max_position") && active) {
				setAnchor((position-0)+1);
				position++;

				$(".work_list").scrollTo($('div[number=' + position + ']'), 800, {
					axis: 'x',
					offset: -($("div.work_list").width() / 2) + ($('div[number=' + (position) + ']').width()) / 2
				});
				active = false;
				setTimeout("active=true", 800);
				if ((position-0) != 1) {
					//$(".parrayleft").css({"background-image": "url(\"/img/left_off.png\")"});
				}
				if (position == $("div.work_list").attr("max_position")) {
					//$(".parrayright").css({"background-image": "url(\"/img/right_off_disabled.png\")"});
				}
				else {
					//$(".parrayright").addClass("rightpressed");
				}
			}
		});
		$("#scrollLeft").click(function(){
			if (((position-0)-0) > 1 && active) {
				setAnchor((position-0)-1);
				position--;
				
				$(".work_list").scrollTo($('div[number=' + position + ']'), 800, {
					axis: 'x',
					offset: -($("div.work_list").width() / 2) + ($('div[number=' + (position) + ']').width()) / 2
				});
				active = false;
				setTimeout("active=true", 800);
				if ((position-0) != $("div.work_list").attr("max_position")) {
					//$(".parrayright").css({"background-image": "url(\"/img/right_off.png\")"});
				}
				if ((position-0) == 1) {
					//$(".parrayleft").css({"background-image": "url(\"/img/left_off_disabled.png\")"});
				}
				else {
					//$(".parrayleft").addClass("leftpressed");
				}
				
			}
		});
		
		
		
		
		
		
		//$(".parrayleft").css({"background-image": "url(\"/img/left_off_disabled.png\")"});
	}
	
	$(".menu div.item").each(function(){
			$(this).slideDown("slow");
		});
		$(".menu div.item").mouseover(function(){
			$(this).children(".line").css({
				backgroundColor: "#AAAAAA"
			});
		});
		$(".menu div.item").mouseout(function(){
			$(this).children(".line").css({
				backgroundColor: "#FFFFFF"
			});
		});
	//$(".withborder").corner("10px");
	//$(".catlist ul li").corner("5px");
});

var isCtrl = false;
$(document).keyup(function (e)
	{
		if (e.which == 17) {
			isCtrl = false;
			$(".ctrlButton").removeClass("pressed");
		}
		
		if (e.which == 37 && isCtrl)//arrow left
		{
			//$(".parrayleft").removeClass("leftpressed");
		}
		if(e.which == 39 && isCtrl)//arrow right
		{
			//$(".parrayright").removeClass("rightpressed");
		}
	}).keydown(function(e){
		if (e.which == 17) {
			isCtrl = true;
			$(".ctrlButton").addClass("pressed");
		}
		if (e.which == 37 && isCtrl)
		{
			if ((position-0) > 1 && active) {
				
				setAnchor((position-0)-1);	
				position--;		
				$(".work_list").scrollTo($('div[number=' + position+']'), 800, {
					axis: 'x',
					offset:-($("div.work_list").width()/2) + ($('div[number=' + (position)+']').width())/2
				});
				active=false;
				setTimeout("active=true",800);
				if(position!=$("div.work_list").attr("max_position"))
				{
					//$(".parrayright").css({"background-image": "url(\"/img/right_off.png\")"});
				}
				if((position-0)==1)
				{
					//$(".parrayleft").css({"background-image": "url(\"/img/left_off_disabled.png\")"});
				} else {
					//$(".parrayleft").addClass("leftpressed");	
				}
				
			}
		}
		if(e.which == 39 && isCtrl)
		{
			if ((position-0) < $("div.work_list").attr("max_position") && active) {
				setAnchor((position-0)+1);
				position++;
				
				$(".work_list").scrollTo($('div[number=' + position+']'), 800, {
					axis: 'x',
					offset:-($("div.work_list").width()/2) + ($('div[number=' + (position)+']').width())/2
				});
				active=false;
				setTimeout("active=true",800);
				if(position!=1)
				{
					//$(".parrayleft").css({"background-image": "url(\"/img/left_off.png\")"});
				}
				if(position==$("div.work_list").attr("max_position"))
				{
					//$(".parrayright").css({"background-image": "url(\"/img/right_off_disabled.png\")"});
				} else {
					//$(".parrayright").addClass("rightpressed");
				}
				
			}



		}
});


$(window).resize(function(){
	if ($("div.work_list").width() > 700) {
		$("#work_0").css({
			width: ($("div.work_list").width() / 2) - $('div[number=1]').width() / 2
		});
	$("#work_"+($("div.work_list").attr("max_position")-0+1)).css({
			width: ($("div.work_list").width() / 2) - $('div[number=1]').width() / 2
		});
	}
});

$(document).ready(function(){
	$(".catalog_item").mouseover(function(){
		$(this).find("img").css({
			display: "block"
		});
	});
		
	$(".catalog_item").mouseout(function(){
		$(this).find("img").css({
			display: "none"
		});
	});
});


			 