function calcScroll(){	
	var maxWidth=$('#small-imgs').width()
	var elWidth=0;
	$('#small-imgs a').each(function(){
		elWidth+=$(this).width()
	})
	if(elWidth>maxWidth)
		$('#scrright, #scrleft').show()
	else	
		$('#scrright, #scrleft').hide()
}

$(document).ready(function(){  
    var ie;
    if($.browser.msie && Math.floor($.browser.version)<9) ie='shit';	
	
	//show items
	if($('#scroller').length){
			var outer=$('#scroller');
			outer.html(outer.html().replace(/[\n\t\r]+/g, ''))
			outer.find('.item').each(function(){
				if(ie != 'shit'){
					$(this).delay(Math.floor(Math.random()*301))
						.animate({opacity:1},Math.floor(Math.random()*301));
				}
			})
			
			if($('body').width()<=1195)
					$('#scroller').width(900)
				else 
					$('#scroller').width(1125)
		
			$(window).resize(function(){
				if($(this).width()<=1195)
					$('#scroller').width(900)
				else 
					$('#scroller').width(1125)
			})
		
		var ended=true;
		$('#projects-index #scrright').click(function(){
			if (ended==true){
				ended=false;
		
				var items=outer.find('.item')
				var itemWidth=items.first().width()
				
				var clone=items.filter(':first').clone().addClass('clone-left').prependTo('#scroller')
	
				clone.animate({left:-(itemWidth)}, 500, "easeOutExpo",function(){
					clone.remove()
				})
				items.filter(':first')
					.css('visibility','hidden').animate({width:0,paddingRight:0}, 500, "easeOutExpo", function(){
						$(this).appendTo(outer).css({width:itemWidth,visibility:'visible',paddingRight:5})
						ended=true;
					})
			}
			
			return false
		})
	
		$('#projects-index #scrleft').click(function(){
			if (ended==true){
				ended=false;
		
				var items=outer.find('.item')
				var itemWidth=items.first().width()
				
				var clone=items.filter(':last').clone().addClass('clone-right').prependTo('#scroller')
	
				clone.animate({left:0}, 500, "easeOutExpo",function(){
					clone.remove()
				})
				items.filter(':last')
					.css({width:0, paddingRight:0,visibility:'hidden'})
					.prependTo(outer)
					.animate({width:220,paddingRight:5}, 500, "easeOutExpo", function(){
						$(this).css({visibility:'visible'})
						ended=true;
					})
			}
			
			return false
		})
	}
	var job;
	$('#vacancies .send-resume').click(function(){
		job=$(this).attr('rel');
		$.colorbox({href:'/agency/jobs/resume.php',initialWidth:408,initialHeight:322,opacity:0.6})
		return false
	})

	$('#vacancy-close').live('click',function(){
		$.colorbox.close()
		return false
	})

	$('#vacancy-send').live('click',function(){
		var i=0;
		$('#vacancy-popup input').each(function(){
			if($(this).val()=='' || $(this).val().length<4) $(this).css('border','1px solid red')
			else{
				$(this).css('border','1px solid #d1d2d4')
				i++;
			}
		})
		if(i>3){
			var form=$('#cboxLoadedContent form').serialize()+'&PROPERTY[12][0]='+job+'&iblock_submit=Отправить';
			$.post("/agency/jobs/resume.php",form,function(data) {
				$('#cboxLoadedContent').html(data);
			})
		}

		return false
	})

	$('#anketa-send').live('click',function(){
		var i=0;
		$('#anketa .inptext').each(function(){
			if($(this).val()=='' || $(this).val().length<4) $(this).css('border','1px solid red')
			else{
				$(this).css('border','1px solid #d1d2d4')
				i++;
			}
		})
		if(i>4){
			
		}else{
			return false
		}
		
	})








//gallery scriptz Oo

if($('#gallery').length){

	calcScroll();

	var imgList=$('#small-imgs a')

	$('#bigimg img').live('click',function(){
		$('#small-imgs a.selected').next().click()
	})
	
	//startup needs
	var imgHeight;

	if(ie!='shit' || $.browser.mozilla && Math.floor($.browser.version)<4){
		$('#bigimg img').load(function(){
			imgHeight=$('#bigimg img').height()
			$('#bigimg').height(imgHeight)
		})
	}
	else{
		$('#bigimg img').ready(function(){
			imgHeight=$('#bigimg img').height()
			$('#bigimg').height(imgHeight)
		})
	}

	//mini imgs handling
	var mouseX=0;
	imgList.first().addClass('selected')
	imgList.click(function(e){
		if(e.pageX){
			if(e.pageX > mouseX){
				$('#small-imgs').animate({scrollLeft:'+='+$(this).width()},200,function(){})
				mouseX=e.pageX-20;
			}
			else{
				$('#small-imgs').animate({scrollLeft:'-='+$(this).width()},200,function(){})
				mouseX=e.pageX+20;
			}
		}else $('#small-imgs').animate({scrollLeft:'+='+$(this).width()},200,function(){})
		
		imgList.removeClass('selected')
		$(this).addClass('selected')

		var path=$(this).attr('href')
		var imgId=$(this).attr('rel')
		$('#imgblock').load('/portfolio/getimage.php', {id:imgId, path:path},function(){
			$('#bigimg img').css('max-height', imgHeight)
			$('#bigimg').css({height: imgHeight, lineHeight: imgHeight+'px'})
			
		})

		
		return false
	})
	
	//document resizes gallery

	$(window).resize(function(){
		$('#bigimg').css({height:$('#bigimg img').height(), lineHeight: $('#bigimg img').height()+'px'})
		calcScroll()
	})
	
	//left-right
	$('#gallery #scrleft').click(function(){
		$('#small-imgs').animate({scrollLeft:'-=300'},300,function(){})
	})
	$('#gallery #scrright').click(function(){
		$('#small-imgs').animate({scrollLeft:'+=300'},300,function(){})
	})		

}
})
