var mDown=false,
	mMove=false,
	timeouted=false,
	iteration=0,
	scrolls=new Array();
window.onresize=resizeScrollbars;
window.onload=function(){
	setTimeout("resizeScrollbars()",100);
}
$(function() {
	$('#layout > div').each(function(index){
		var plus=$(this).attr('id')=='work'?200:0;
		scrolls[index]='-'+$(this).offset().top+',-'+($(this).offset().left+plus);
	});

	window.maxSLeft=($('#layout').width()-$('#wrapper').width())*-1;
	window.maxSTop=-3000;

	$('#wrapper').css({overflow: 'hidden',marginTop: 0,marginLeft: 0});
	checkScrollEn();
    $(document).mousewheel(function(event,delta){
		if (enableMouseScroll) {
			var lastTop=parseInt($('#layout').css('marginTop')),
				scrl=delta>0?lastTop+100:lastTop-100,
				scrl=scrl>0?0:scrl<maxSTop?maxSTop:scrl;
			$("#layout").stop().css({marginTop: scrl+'px'});
			showScroll(false);
			return false;
		}
	});

	$('*').not('a').mousedown(function(event) {
		event=event||window.event;
		if (enableMouseScroll) event.preventDefault();
	});
		
	if ($.browser.msie && $.browser.version=="6.0") $(".nav-line").css({paddingTop: "7px",height: "23px"});
	setTimeout("moveToHash()",0);
	startCheck();
	
	$('.logo').click(function() {
	    location.href="#show:meet";
		if (enableMouseScroll) {
			var newTop=scrolls[1].split(',')[0],
				newLeft=scrolls[1].split(',')[1],
				newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop,
				newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft;
			$('#layout').stop().animate({marginTop: newTop+'px',marginLeft: newLeft+'px'},800);
			window.iteration=0;
			showScroll(true);
		}
		else $('#wrapper').stop().scrollTo('#meet',800,{axis:'xy'});
	});
	
	$("a[href*=#]").live('click', function() {
		if (cursorX && startX) {
			/*alert(startX+', '+cursorX);
			if (startX!=cursorX || startY!=cursorY) return false;*/
		}
		var hash=$(this).attr("href").substring($(this).attr("href").indexOf('#')+1);
		if (a = $("div[id="+hash+"]")) {
			if (enableMouseScroll) {
				var need=$(a).index(),
					newTop=scrolls[need].split(',')[0],
					newLeft=scrolls[need].split(',')[1],
					newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop,
					newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft;
				$('#layout').stop().animate({marginTop: newTop+'px',marginLeft: newLeft+'px'},800);
				window.iteration=0;
				showScroll(true);
			}
			else $('#wrapper').stop().scrollTo(a,800,{axis:'xy'});
			location.href="#show:"+hash;
		}
		return false;
	});
	
	$(window).bind('scroll', function(){
		var ss = document.documentElement.offsetWidth,
			sl = document.documentElement.scrollLeft;
		$('.nav-box').stop().animate({
			left: sl+45
		}, 'fast');
		
		var selhash = (sl<700) ? '#main' : ( (sl+ss<2700 ) ? '#programm' : '#whereis' );
		selElementFromNav(selhash);
		
	});

/* mouse dragging =) */
	$(document).mousedown(function(event){
		window.mDown=true;
		event=event||window.event;
		elem=event.target||event.srcElement;
		if (!$(elem).hasClass('sideMoveBlock')) {
			window.startX=event.x||event.clientX;
			window.startY=event.y||event.clientY;
			$(document).bind('mousemove',function(event){
				if (enableMouseScroll) {
					window.mMove=true;
					event=event||window.event;
					window.cursorX=event.x||event.clientX;
					window.cursorY=event.y||event.clientY;
					var lastLeft=parseInt($('#layout').css('marginLeft')),
						lastTop=parseInt($('#layout').css('marginTop')),
						newLeft=lastLeft+cursorX-startX,
						newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft,
						newTop=lastTop+cursorY-startY,
						newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop;
					$('#layout').stop().css({marginLeft: newLeft+'px',marginTop: newTop+'px'});
					showScroll(false);
					window.startX=event.x||event.clientX;
					window.startY=event.y||event.clientY;
					return false;
				}
			});
		}
	}).dblclick(function(event){
		if (enableMouseScroll) {
			event=event||window.event;
			cursorX=event.x||event.clientX;
			cursorY=event.y||event.clientY;
			var lastLeft=parseInt($('#layout').css('marginLeft')),
				lastTop=parseInt($('#layout').css('marginTop')),
				newLeft=lastLeft-(cursorX-$('#wrapper').width()/2),
				newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft,
				newTop=lastTop-(cursorY-$('#wrapper').height()/2),
				newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop;
			$('#layout').stop().animate({marginLeft: newLeft+'px',marginTop: newTop+'px'},800);
			window.iteration=0;
			showScroll(true);
		}
	}).mouseup(function(event){
		$(document).unbind('mousemove');
		window.mDown=false;
		window.mMove=false;
		event=event||window.event;
		elem=event.target||event.srcElement;
		if (!$(elem).hasClass('sideMoveBlock') && sideMoving) {
			$('#layout').stop();
			window.clearInterval(sideMoveTimer);
			window.sideMoving=false;
		}
	});
	
	/* side-screen move */
	window.sideSpace=100;
	window.sideSpaceRight=$(window).width()-sideSpace;
	window.sideSpaceBottom=$(window).height()-sideSpace;
	window.sideMoving=false;
	window.sideMovingDelay=false;
	window.spdSideMove=50;
	$('.sideMoveBlock')
		.bind('mousemove',function(event){
			if (!mDown && enableMouseScroll) {
				event=event||window.event;
				window.xPos=event.x||event.clientX;
				window.yPos=event.y||event.clientY;
				if ((xPos>=sideSpaceRight || xPos<=sideSpace || yPos>=sideSpaceBottom || yPos<=sideSpace) && !sideMoving && !sideMovingDelay) {
					window.sideMoveTimerDelay=window.setTimeout(function(){
						window.sideMoveTimer=window.setInterval(sideMove,spdSideMove);
						window.sideMoving=true;
					},300);
					window.sideMovingDelay=true;
				}
			}
		})
		.hover(
			function(){$(this).addClass('arrowed');},
			function(){
				$(this).removeClass('arrowed');
				if (!mDown && enableMouseScroll) {
				if (sideMovingDelay) {
					window.clearTimeout(sideMoveTimerDelay);
					window.sideMovingDelay=false;
				}
					if (sideMoving) {
						$('#layout').stop();
						window.clearInterval(sideMoveTimer);
						window.sideMoving=false;
						window.sideMovingDelay=false;
					}
				}
			}
		);
	function sideMove() {
		if (xPos>sideSpaceRight) var stepScrollX=xPos-sideSpaceRight;
		else if (xPos<sideSpace) var stepScrollX=xPos-sideSpace;
		else var stepScrollX=0;
		if (yPos>sideSpaceBottom) var stepScrollY=yPos-sideSpaceBottom;
		else if (yPos<sideSpace) var stepScrollY=yPos-sideSpace;
		else var stepScrollY=0;
		var lastTop=parseInt($('#layout').css('marginTop')),
			lastLeft=parseInt($('#layout').css('marginLeft')),
			newTop=lastTop-stepScrollY,
			newTop=newTop>0?0:newTop,
			newLeft=lastLeft-stepScrollX,
			newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop,
			newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft;
		if (sideMoving) {
			$('#layout').stop().css({marginLeft: newLeft+'px',marginTop: newTop+'px'});
			showScroll(false);
		}
	}


	$(".drop-down").click(function(){
		$(this).next().slideToggle("fast");
	});
	
	// Scrollbars
	
	window.allHeight=$('#work').outerHeight()+parseInt($('#work').css('top'));
	window.allWidth=$('#layout').width();
	window.visHeight=$('#wrapper').height();
	window.visWidth=$('#wrapper').width();

	var scrWidth=visWidth*visWidth/allWidth-15,
		scrHeight=visHeight*visHeight/allHeight-15;

	$("body").prepend("<div id=\"scrollX\"></div><div id=\"scrollY\"></div>");
	$("#scrollX, #scrollY").css({overflow: "hidden",background: "#333",border: '1px solid #ffffff',position: "absolute",opacity:"0",zIndex:"10"});
	$("#scrollX").css({width: scrWidth+"px",height: "5px",left:"0",bottom:"5px",margin:"0 10px 0 5px"});
	$("#scrollY").css({width: "5px",height: scrHeight+"px",right:"5px",top:"0",margin:"5px 0 15px"});

	$(".nav-box .toggle").click(function(){
		if ($(".nav-box .nav-line").css("marginRight")=="0px"){
			var rightPx=parseInt($(".nav-box .nav-line").width())+15;
			$("#checkMouse").animate({marginRight: "-"+rightPx+"px"},"slow");
			$(".nav-box .nav-line").animate({marginRight: "-"+rightPx+"px"},"slow",function(){
				$(".nav-box .toggle").css("backgroundPosition","left top").attr("title","Развернуть");
			});
		}
		else {
			$("#checkMouse").animate({marginRight: "0px"},"slow");
			$(".nav-box .nav-line").animate({marginRight: "0px"},"slow",function(){
				$(".nav-box .toggle").css("backgroundPosition","left bottom").attr("title","Свернуть");
			});
		}
	});
	
	$("#mouseDragEnable").change(function(){
		checkScrollEn();
	}).click(function(){
		checkScrollEn();
	});
	
	setInterval(function(){
		$("#work .comments div").each(function(index){
			if (index>0) {
				var need=index-1;
				$(this).css({marginTop: $("#work .imgs img:eq("+need+")").height()-0+"px"});
			}
		});
	},1000);

});

function checkScrollEn() {
	window.enableMouseScroll=!$("#mouseDragEnable").attr("checked");
	if ($("#mouseDragEnable").attr("checked")) {
		var sLeft=parseInt($('#layout').css('marginLeft'))*-1,
			sTop=parseInt($('#layout').css('marginTop'))*-1;
		$('#layout').css({marginLeft: 0,marginTop: 0});
		if (sLeft!=0 && sTop!=0)
			$("#wrapper").css({overflow: 'auto'}).scrollTo({top: sTop, left: sLeft},0,{axis:'xy'});
		else $("#wrapper").css({overflow: 'auto'});
		$('.sideMoveBlock').hide();
		$(".nav-box").animate({right:"17px"},"slow");
		$("body").css("-moz-user-select","text");
		hideScroll();
	}
	else {
		var sLeft=$('#wrapper').scrollLeft()*-1,
			sTop=$('#wrapper').scrollTop()*-1;
		$("#wrapper").css("overflow","hidden").scrollTo({top: 0, left: 0},0,{axis:'xy'});
		if (sLeft!=0 && sTop!=0)
			$('#layout').css({marginLeft: sLeft+'px',marginTop: sTop+'px'});
		$('.sideMoveBlock').show();
		$(".nav-box").animate({right:"0"},"slow");
		$("body").css("-moz-user-select","none");
	}
} // End of checkScrollEn

function resizeScrollbars() {
	window.allHeight=$('#work').outerHeight()+parseInt($('#work').css('top'));
	window.allWidth=$('#layout').width();
	window.visHeight=$('#wrapper').height();
	window.visWidth=$('#wrapper').width();

	var scrWidth=visWidth*visWidth/allWidth-15,
		scrHeight=visHeight*visHeight/allHeight-15,
		left=parseInt($('#layout').css('marginLeft'))*-1,
		top=parseInt($('#layout').css('marginTop'))*-1;
	$("#scrollX").css({width: scrWidth+"px",left: left*visWidth/allWidth+"px"});
	$("#scrollY").css({height: scrHeight+"px",top: top*visHeight/allHeight+"px"});

	window.maxSLeft=visWidth-allWidth;
	window.maxSTop=visHeight-allHeight;

// sidemoving
	window.sideSpaceRight=$(window).width()-sideSpace;
	window.sideSpaceBottom=$(window).height()-sideSpace;
} // End of resizeScrollbars


function showScroll(repeat) {
	if (enableMouseScroll) {
		checkArrows();
		if (timeouted) window.clearTimeout(hide);
		window.timeouted=false;
		if ($("#scrollX").css("opacity")==0) {
			$("#scrollX").css({opacity:"0.6"});
			$("#scrollY").css({opacity:"0.6"});
		}
		var left=parseInt($('#layout').css('marginLeft'))*-1,
			top=parseInt($('#layout').css('marginTop'))*-1;
		$("#scrollX").css({left: left*visWidth/allWidth+"px"});
		$("#scrollY").css({top: top*visHeight/allHeight+"px"});
		if (repeat && iteration<80) {
			window.iteration++;
			animScroll=window.setTimeout('showScroll(true)',10);
		}
		tryHideScroll();
	}
} // End of showScroll

function tryHideScroll() {
	hide=window.setTimeout("hideScroll()",500);
	window.timeouted=true;
} // End of tryHideScroll

function hideScroll() {
	$("#scrollX").animate({opacity:"0"},"fast");
	$("#scrollY").animate({opacity:"0"},"fast");
}

function startCheck() {
    window.ls=location.hash;
    checkHash();
} // End of startCheck

function checkHash() {
	if (ls!=location.hash) {
		if (location.hash) {
			window.ls=location.hash;
			if (enableMouseScroll) {
				var need=$('#'+ls.substr(6)).index(),
					newTop=scrolls[need].split(',')[0],
					newLeft=scrolls[need].split(',')[1],
					newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop,
					newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft;
				$('#layout').stop().animate({marginTop: newTop+'px',marginLeft: newLeft+'px'},800);
				window.iteration=0;
				showScroll(true);
			}
			else $('#wrapper').stop().scrollTo('#'+ls.substr(6),800,{axis:'xy'});
		}
		else {
			window.ls=location.hash;
			if (enableMouseScroll) {
				$('#layout').stop().animate({marginTop: 0,marginLeft: 0},800);
				window.iteration=0;
				showScroll(true);
			}
			else $('#wrapper').stop().scrollTo('#'+ls.substr(6),800,{axis:'xy'});
		}
	}
	setTimeout(checkHash,1);
} // End of chechHash

function moveToHash() {
	if (location.hash) {
		if (enableMouseScroll) {
			var need=$('#'+location.hash.substr(6)).index(),
				newTop=scrolls[need].split(',')[0],
				newLeft=scrolls[need].split(',')[1],
				newTop=newTop>0?0:newTop<maxSTop?maxSTop:newTop,
				newLeft=newLeft>0?0:newLeft<maxSLeft?maxSLeft:newLeft;
			$('#layout').stop().animate({marginTop: newTop+'px',marginLeft: newLeft+'px'},800);
			window.iteration=0;
			showScroll(true);
		}
		else $('#wrapper').stop().scrollTo('#'+location.hash.substr(6),800,{axis:'xy'});
	}
	else {
		if ($.browser.msie) $('.nav-line a:first-child').click();
	}
} // End of moveToHash

function checkArrows() {
	var left=parseInt($('#layout').css('marginLeft')),
		top=parseInt($('#layout').css('marginTop'));
	if (left==maxSLeft) {
		$('#sideMoveRight').hide();
		if (top==0) $('#sideMoveRightTop').hide();
		else $('#sideMoveRightTop').show();
		if (top==maxSTop) $('#sideMoveRightBottom').hide();
		else $('#sideMoveRightBottom').show();
	}
	else if (left==0) {
		$('#sideMoveLeft').hide();
		if (top==0) $('#sideMoveLeftTop').hide();
		else $('#sideMoveLeftTop').show();
		if (top==maxSTop) $('#sideMoveLeftBottom').hide();
		else $('#sideMoveLeftBottom').show();
	}
	else {
		$('#sideMoveRightTop').show();
		$('#sideMoveRight').show();
		$('#sideMoveRightBottom').show();
		$('#sideMoveLeftTop').show();
		$('#sideMoveLeft').show();
		$('#sideMoveLeftBottom').show();
	}
	if (top==0) $('#sideMoveTop').hide();
	else if (top==maxSTop) $('#sideMoveBottom').hide();
	else {
		$('#sideMoveTop').show();
		$('#sideMoveBottom').show();
	}
} // End of chackArrows