jQuery(document).ready(
	function(){
		$().mousemove(function(e){
			$('.pupil').css({left:eval(e.pageX/13) + '%', top:eval(e.pageY/7) + '%'}
		);
	});
});

$(document).ready(
	function(){
		var slideInterval = setInterval(slideLogos,50);
		var jSlide = $('#client_logos .slide');
		var iLeftPosition = 0;
		function slideLogos(){
			jSlide.css({
				left: - iLeftPosition
			});
			if(iLeftPosition > 1565){
				iLeftPosition = 0;
			} 
			iLeftPosition++;
			
//			if(iLeftPosition == 200){
//				jSlide.find('img').clone(true).appendTo(jSlide)
//				napr = '+'
//			}
		}
	}
	
	
)

	function slidIt(iPageIndex){
			if(!iPageIndex){
				if(!window.location.hash){iPageIndex=0}
				if(window.location.hash == '#main'){iPageIndex=0}
				if(window.location.hash == '#rims'){iPageIndex=1}
				if(window.location.hash == '#frames'){iPageIndex=2}
				if(window.location.hash == '#lens'){iPageIndex=3}
				if(window.location.hash == '#diagnostics'){iPageIndex=4}
				if(window.location.hash == '#contacts'){iPageIndex=5}
			} else {
				if(iPageIndex==0){window.location.hash = '#main'}
			}
				
			$('#navigation a').parent().siblings('li').removeClass('selected').end();	
			$('#li'+iPageIndex).addClass('selected');
			
			$('#content').animate({left:-100*iPageIndex+'%'},1200,'swing');
	
	}

var iScrollInterval;
$(
	function(){
		resize();
		$(window).resize(resize);
		
		slidIt();
				
				
		
		
		$('#navigation a').click(
			function(){
				
				var jElement = $(this);
				var iPageIndex = $('#navigation a').index(jElement);
//				var iPageIndex=0
				
				jElement.parent().siblings('li').removeClass('selected').end().addClass('selected');
				
				$('#content').stop();
				$('#content').animate(
					{
						left:-100*iPageIndex+'%'
					},1200,'swing'
				);
				window.location = jElement.value();
				return false
				
			}
		);
		
		$('.tabs .pseudo').click(
			function(){
				var jElement = $(this);
				var jParentElement = jElement.parent('li');
				var iSheetIndex = jElement.parents('.tabs').find('li').index(jParentElement[0]);
				jParentElement.siblings().removeClass('selected').end().addClass('selected');
				jElement.parents('.page').find('.sheet').removeClass('show').eq(iSheetIndex).addClass('show');
				text=jElement.parents('.page').find('.info').eq(iSheetIndex).html();
				title=jElement.text();
				$("#infoblock").html("<h2>"+title+"</h2>"+text);
			}
		);
		
		$('.one .thumbs a, .two .thumbs a, .six .thumbs a').click(
			function(){
				var jElement = $(this);
				animateThumb(jElement);
				var loadcont=jElement.parents('.page').find('.picture');
				loadcont.addClass("loader");
				
				jElement.parents('.page').find('.picture img').animate(
					{
						top:'700px'
					},
					360
					, function(){

						$(this).attr('src',jElement.attr('href')).load(function(){
							loadcont.removeClass("loader");
							$(this).animate(
								{
									top:'0px'
								},300
							)
						});
						
						
					}
				);
				return false
			}
		);
		
		
		$('.three .thumbs li a').click(
			function(){
				var jElement = $(this);
				var iPictureIndex = jElement.parents('.thumbs').find('a').index(jElement);
				
				animateThumb(jElement);
				$('.interiors .picture').filter('.current').animate({
					top:'900px'
				},600);
				$('.interiors .picture').eq(iPictureIndex).addClass('current').animate({
					top:'0px'
				}, 800);
			
				return false;
			}
		);
		
		$('.thumbs ins').mousedown(
			function(){
				var jElement = $(this); 
				var sDirection = jElement.is('.left')?17:-17;
				var jThumbsContainer = jElement.parent();
				var jThumbs = jThumbsContainer.find('ul');
			
				var iThumbsWidth = jThumbs.find('li').size()*148;
				

				iScrollInterval = setInterval(
					function(){
						var iCurrentLeft = parseInt(jThumbs.css('left'))?parseInt(jThumbs.css('left')):0;
						if(sDirection > 0 && iCurrentLeft == 0) {
							jThumbsContainer.addClass('start');
							clearInterval(iScrollInterval);
						} else {
							if(iThumbsWidth+iCurrentLeft <= 750 && sDirection < 0){
								jThumbsContainer.addClass('stop');
								clearInterval(iScrollInterval);
							} else {
								jThumbsContainer.removeClass('stop');
								jThumbsContainer.removeClass('start');
								jThumbs.css('left',iCurrentLeft+sDirection)
							}
						}
					},30
				);

			}
		);
		
		$('.thumbs ins').mouseup(
			function(){
				clearInterval(iScrollInterval);
			}
		)
		
		function resize() {
			if(document.body.clientHeight < 600) {
				$('#content').css({height:600 + 'px'});
			}
			if(window.document.body.clientWidth >= 600) {
				$('#content').css({height:100+'%'});
			}
		}
		
//		function resize(){
//			if(document.body.clientHeight < 300){
 //               $('#content').css({height:document.body.offsetHeight+300 + 'px'});
//            };
//            console.log(document.body.clientWidth)
//		};
		
		function animateThumb(jElement){
			var jCurrentThumb = jElement.parent().siblings().filter('.current');
			
			jCurrentThumb.removeClass('current');
			jElement.parent().addClass('current');
			jCurrentThumb.find('img').animate({
				top:'0px'
			},400,
				function(){
					jElement.find('img').animate({
						top:'-350px'
					},350);
				}
			);
		
		};
		
	}
)
