$(document).ready(function() {
	doc_w = $(window).width();
	doc_h = $(window).height();
	if(doc_w - $('.projects_thumb').width() > 0){
		var ml = doc_w/2 - $('.projects_thumb').width()/2;
		$('table.projects_thumb').css('margin-left',ml);
	}

	$('.bg_intro').width(doc_w);
	$('.bg_intro').height(doc_h);
	$(".bg_intro img").each(function(){
		$(this).width('100%');
		$(this).height('100%');
	});
	$(".bg_intro img").aeImageResize({width: '100%'}); 
 
	$('.wrap_content').width(doc_w-7);
	$('.wrap_content').height(doc_h-6);
	 $('.about').css('min-height',doc_h-6);
	 $('.about .pos_rel').css('min-height',doc_h-6);
			$('.wrap_content').height(doc_h-6);
		
		$(window).resize(function() {
			doc_w = $(window).width();
			doc_h = $(window).height();
			$('.bg_intro').width(doc_w);
			$('.bg_intro').height(doc_h);
			$('.bg_intro1').width(doc_w);
			$('.bg_intro1').height(doc_h);
			$(".bg_intro img").aeImageResize({width: doc_w }); 
			$(".bg_intro1 img").aeImageResize({width: doc_w }); 
			$('.about').css('min-height',doc_h-6);
			 $('.about .pos_rel').css('min-height',doc_h-6);
			$('.wrap_content').width(doc_w-7);
			$('.wrap_content').height(doc_h-6);
			$('.slider_about>div').width((doc_w-215)*0.49);
			if(doc_w - $('.projects_thumb').width() > 0){
				var ml = doc_w/2 - $('.projects_thumb').width()/2;
				$('table.projects_thumb').css('margin-left',ml);
	   		 }
			
			 
		});
		
	var i = 0;
	sum = 0;
	$('.wrap_slide .block').each(function(){
		i++;
	});
	 $('.slider_blog ul li').each(function(){
		 	sum = sum + $(this).width();
	 });
	 
	 if ($.browser.msie &&  $.browser.version == '7.0') {
		 $('.slider_blog ul').width(sum);
	 } else {
		 $('.slider_blog').width(sum); 
	 }
	$('.wrap_slide').width(i*444);
	$('.square .inner').hover(function(){
		
//		 Cufon.replace('.square .inner h3 a', {
//				fontFamily : 'BebasNeueCyrillic'
//		 });
		 
		}, function(){
		
//			 Cufon.replace('.square .inner h3 a', {
//					fontFamily : 'BebasNeueCyrillic'
//			 });
//		 
	});
	if ( $('.wrap').find('.JMyCarousel').length > 0) {
		$(".JMyCarousel").jMyCarousel({  
	        visible: '100%',  
	        speed : 1900,
	        circular: true,
	        eltByElt: true,
	        evtStart: 'mousedown',
	        evtStop: 'mouseup'  
	    }); 
	}
	blog_li = 0;
	$('.slider_blog ul li .blog_block').each(function(){
		blog_li = blog_li + $(this).width(); 	
	});
	 
	$('#bl_next').click(function(){
		mtstep2 = $('.slider_blog ul').css('margin-left');
		
		mtstep2 = mtstep2.split('p');
		 
		if ((-1)*parseInt(mtstep2[0]) <= blog_li-290) {
			$('.slider_blog ul').animate({marginLeft: parseInt(mtstep2[0])+(-580)+'px'});
		}
		
	});
	$('#bl_prev').click(function(){
		mtstep3 = $('.slider_blog ul').css('margin-left');
		mtstep3 = mtstep3.split('p');
		 
		if ((-1)*parseInt(mtstep3[0]) >= 290) {
			$('.slider_blog ul').animate({'margin-left': parseInt(mtstep3[0])+(580)});
		} else {
			$('.slider_blog ul').animate({'margin-left': 0});
		}
		
	});
	
	$('.bg_intro').cycle({ 
		   fx:     'scrollHorz', 
		   speed:  900, 
		   slideResize:   1, 
		   timeout: 0, 
		   pause:  1,  
		   next: '#next',
		   clip:   'l2r',
		   pager:  '#nav',
		   allowPagerClickBubble: true,
		   pagerAnchorBuilder: function(idx, slide) {
		
				return '#nav td:eq(' + (idx) + ')';
	   	   },
	   	   after:   onAfter 
	}); 
	$('.slider_about>div').width((doc_w-215)*0.49);
	$('.slider_about').cycle({ 
		   fx:     'scrollHorz', 
		   speed:  1900,
		   slideResize:   1,
		   timeout: 0, 
		   pause:  1,  
		   next: '#team_next',
		   prev: '#team_prev',
		   clip:   'l2r'
		   
	}); 
	$('.bg_intro1').cycle({ 
		   fx:     'scrollHorz', 
		   speed:  900, 
		   slideResize:   1, 
		   timeout: 5000, 
		   pause:  1,  
		   clip:   'l2r' 
	   	   
	}); 
	
	function onAfter() { 
		var sum = 0;
		$('.projects_thumb td').each(function(){
			sum++;
		});
		if (sum < 10){
			var count = '0'+ sum;
		}else{
			var count = sum;
		}
		val_class = $(this).attr('class');
		if (val_class < 10){
		   var active_td = '0'+val_class;
		   }else{
			var active_td = val_class;
		   }
		 

		$('.status_project p').removeClass('active');
		$('.status_project p.st_'+val_class).addClass('active');
		$('.project_name div').removeClass('active');
		$('.project_name div.pr_'+val_class).addClass('active');
	    $('.count p').text(active_td+"/"+count); 
	}
	$('.more_info').click(function(){
		pos = $('.other_projects table').css('margin-top');
		 if ( pos == '0px') {
			 $('.other_projects table').animate({"margin-top": '70px' }, 500); 
		 } else {
			 $('.other_projects table').animate({"margin-top": '0px' }, 500);
		 }
		
	});

	maxli = 0;
	$('.slider_video .block_video').each(function(){
		maxli++; 
	});
	video_li_h = 0; 
	$('.video_link #nav li').each(function(i){
		video_li_h = video_li_h + $(this).height()+20+1; 
		 
	});
	 
	
	$('.slider_video').cycle({ 
		   fx:     'scrollHorz', 
		   speed:  900, 
		
		   timeout: 0, 
		   pause:  1,  
		   next: '.nx_v',
		   prev: '.pr_v',
		   clip:   'l2r',
		   pager:  '#nav',
		   allowPagerClickBubble: true,
		   pagerAnchorBuilder: function(idx, slide) {
		
				return '#nav a:eq(' + (idx) + ')';
	   	   },
	   	   after:   onAfterVideo 
	}); 
	index_last = 0;
	function onAfterVideo() { 
		 Cufon.replace('.link_pub ul li a', {
				fontFamily : 'aglettericacondl-regular'
		 });
		 
	}
	
	$('.nx_v').click(function(){
		step = -video_li_h/maxli
		mt = $(' .video_link #nav').css('margin-top');
		mt = mt.split('p');
	 
		idnext = $('.video_link #nav li.activeSlide').index();
	 
		if (parseInt(idnext) != 0) {
			$(' .video_link #nav').animate({'margin-top': parseInt(mt[0])+ step}, 300);
		} else {
			$(' .video_link #nav').animate({'margin-top': 0}, 300);
		}
	});
	$('.pr_v').click(function(){
		step = video_li_h/maxli-10
		mtp = $(' .video_link #nav').css('margin-top');
		mtp = mtp.split('p');
	 
		idnext = $('.video_link #nav li.activeSlide').index();
	 
			if (parseInt(idnext)+1 != maxli && parseInt(mtp[0]) < -9) {
				$(' .video_link #nav').animate({'margin-top': parseInt(mtp[0])+ step}, 300);
			} else {
				if (parseInt(idnext)+1 == maxli ) {
					$(' .video_link #nav').animate({'margin-top': -(video_li_h-540)}, 300);
				} else if(parseInt(mtp[0]) >= 0 ) {
				 $(' .video_link #nav').animate({'margin-top': 0}, 300);
				}
			}
		 
	});
	$('#video_down').click(function(){
		stepto = parseInt(video_li_h/540);
		mtstep = $(' .video_link #nav').css('margin-top');
		mtstep = mtstep.split('p');
		
		if( (-1)*(parseInt(mtstep)) < 540*stepto ) {
			$(' .video_link #nav').animate({'margin-top': -540}, 300);
		} else {
			$(' .video_link #nav').animate({'margin-top': 0}, 300);
		}
	});
//	maxli = 0;
//	$('.slider_video .block_video').each(function(){
//		maxli++; 
//	});
	pub_li_h = 0; 
	$('#nav_pub li').each(function(i){
		pub_li_h = pub_li_h + $(this).height()+20+1; 
		 
	});
	
	$('#pub_down').click(function(){
		stepto = parseInt(pub_li_h/540);
		mtstep = $('#nav_pub').css('margin-top');
		mtstep = mtstep.split('p');
		
		if( (-1)*(parseInt(mtstep)) < 540*stepto ) {
			$('#nav_pub').animate({'margin-top': -540}, 300);
		} else {
			$('#nav_pub').animate({'margin-top': 0}, 300);
		}
	});
	
	pub_li = 0;
	
	$('.publisher_ul li .blog_block').each(function(){
		pub_li = pub_li + $(this).width(); 	
	});
    pub_li_ar = new Array();
	
	$('.publisher_ul li').each(function(i){
		pub_li_ar[i] = $(this).width(); 	
	});
	 
	$('#next_prev').click(function(){
		mtstep2 = $('.publisher_ul').css('margin-left');
		
		mtstep2 = mtstep2.split('p');
		 
		if ((-1)*parseInt(mtstep2[0]) <= blog_li-350) {
			$('.publisher_ul').animate({marginLeft: parseInt(mtstep2[0])+(-600)+'px'});
		}
		
	});
	$('#pub_prev').click(function(){
		mtstep3 = $('.publisher_ul').css('margin-left');
		mtstep3 = mtstep3.split('p');
		 
		if ((-1)*parseInt(mtstep3[0]) >= 350) {
			$('.publisher_ul').animate({'margin-left': parseInt(mtstep3[0])+(600)});
		} else {
			$('.publisher_ul').animate({'margin-left': 0});
		}
		
	});
	
	$('#nav_pub li').click(function(){
		index_li = $(this).index();
		$('#nav_pub li').removeClass('active'); 
		
		$(this).addClass('active');
		marg = 0;
		for (i = 0; i < index_li; i++) {
			marg+=pub_li_ar[i];
		}
	
		$('.publisher_ul ').animate({'margin-left': -marg}); 
		 Cufon.replace('.link_pub ul li a', {
				fontFamily : 'aglettericacondl-regular'
		 });
		
	});
 
	$('.hover_info_up').click(function(){
      if ($('.wrap_footer_info').is(":visible")){
		  $('.wrap_footer_info').slideUp("slow");
		  $('.other_projects').animate({
		  "bottom": "34"}, "slow");
		   $('.count').animate({
		  "bottom": "106"}, "slow");
	  }else{
		  $('.wrap_footer_info').slideDown("slow");
		  $('.other_projects').animate({
		  "bottom": "153"}, "slow");
		   $('.count').animate({
		  "bottom": "226"}, "slow");
	  }
	});
	
	
	var ml = 0;
	var _max = 0;
	$('.select_3 ul li').each(function(){
		if (_max < $(this).children().length)
			{ _max = $(this).children().length}
	  });
	$('.select_3 ul').width(_max * 441+12);
	
	$('.next_select_3').click(function(){
	  ml = ml - 441; 
	  block_last = Math.floor(doc_w/441)*441;
	  var ml_last = 441 -(doc_w - block_last); 
	 if ( $('.select_3 ul').width()> doc_w ){
		 
	
	 if ( $('.select_3 ul').width() + ml < block_last + 441 ){
		 ml = ml - ml_last + 441;
	
		 $('.select_3 ul').animate({
		  "margin-left": ml}, "slow");
		 ml = 441;
	  }else{
		  $('.select_3 ul').animate({
		  "margin-left": ml}, "slow");
		}
		 }
	});	
 });
