	var blankCount1 = 0; //процент "битых" в 1 ряду
	var blankCount2 = 0.07; //процент "битых" в 2 ряду
	var blankCount3 = 0.05; //процент "битых" в 3 ряду
	var blankCount4 = 0.1; //процент "битых" в 4 ряду
	var blankCount5 = 0.2; //процент "битых" в 4 ряду
	var finalBlankCount1 = 0; //процент "битых" в 1 ряду
	var finalBlankCount2 = 0.05; //процент "битых" в 2 ряду
	var finalBlankCount3 = 0.1; //процент "битых" в 3 ряду
	var finalBlankCount4 = 0.5; //процент "битых" в 4 ряду
	var x = 12; //фиксируем размер кубика (ширина)
	var y = 13; //фиксируем размер кубика (высота)
	var ny =5; //фиксируем количество рядов
	var speed = 4000;
	var interval = 1000;
	var devastHeight = 140;
	// --- Images --- //
	var path = "/images/";
	var img1 = path+"cube1.png";
	var img2 = path+"cube2.png";
	var img3 = path+"cube3.png";
	var img4 = path+"cube4.png";
	var img5 = path+"cube5.png";
	// --- Images --- //
	
	// --- slider --- //
	var slideIsActive = 0;
	// --- slider --- //
	var sliderTimer ;
	var height;
	var width;
	var h;
	var w;
	var nx;
	var n;
	var count;
	var cubeId = 0;
	var allBlankCount = 0;
	var mouseSpeed = 0;
	var curY = 0;
	var curX = 0;
	var blankCube = new Array();
	var mouseMoveCount = 0;
	var cubeIsUp = 0;
	var direction = 0;	
	var openedSlide;
	var params = $.extend({
      speed: 1500,
      headerclass: "header",
      contentclass: "content",
	  delta: 55,
	  countDelta: 3,
	  countDeltaWho: 2,
	  countDeltaHow: 3, 
	  countDeltaWhat : 3
    },params);
	var devastIsAlive = 1;
	var moveTimer;	
	var moveTimeDelay = 300;
	var scroll;
	var firstHieght = height-devastHeight-40;
	var intervalId ;
	var text2height = 350;
	var maxTextWidth = 710;
	var minTextWidth = 500;

	function acordion(el, width){
		var p = el.next();
		if(!p.hasClass('opened')){
			$(".cube").stop(true, true);
			$(".cube").remove();
			$('.'+params.headerclass).css('background', '#fff');
			if(!jQuery.browser.msie || jQuery.browser.version >7)
			clearInterval(intervalId);
			var parent = el.parent();
			var prev = el.prev();
			p.parent().find('.'+params.headerclass).each(function (){
				if($(this).hasClass('opened'))
				prev2 = $(this);
			});
			
			var next = p.next().next();
			var header = parent.find('.'+params.headerclass);
			//header.addClass('opened');
			//p.nextUntil().addClass('opened');
			prev.find('.names, .descript').fadeTo(1, 0);
			if(parent.parent().attr('id') == 'whoRu' || parent.parent().attr('id') == 'whatRu')
			fullWidth = width-params.delta*params.countDeltaWho;
			else
			fullWidth = width-params.delta*params.countDeltaHow;
			if(jQuery.browser.msie && jQuery.browser.version <8){
				p.fadeTo(1, 0);
				next.fadeTo(1, 0);
				//p.css('visibility', 'hidden');
				//next.css('visibility', 'hidden');
			}
			
			else
			p.fadeTo(1, 0.5);
			
			
			if(prev.prev().hasClass('opened')){	
				prev.find('.names, .descript').fadeTo(1, 0);
				el.find('.images').fadeTo(1,0);
				if(el.find('.first').css('display') != 'none')
				el.find('.first').hide();
				if(p.next().find('.first').css('display') == 'none')
				p.next().find('.first').show();
				
				mdelta = 0;
				var vvv = parseInt(fullWidth/2)-60;
				//prev.find('.text').css('width', vvv);
				//p.find('.text').css('width', vvv);
				//prev.prev().fadeTo(1,0);
				//prev.prev().show();
				prev.prev().hide();
				prev.css({width:fullWidth, 'margin-left': 0, 'overflow': 'hidden'});
				p.css({width:0, 'margin-left':0, 'display': 'block', 'overflow': 'hidden'});
				parent.find('.'+params.headerclass).removeClass('opened');
				ddd = setInterval(function (){
					if(parseInt(prev.css('margin-left'))<=-fullWidth || parseInt(p.css('width'))>=fullWidth){
						clearInterval(ddd);
						//prev.prev().fadeTo(1,1);
						p.find('.names, .descript').fadeTo(1, 1);

						prev.prev().show();
						p.nextUntil().removeClass('opened');
						el.hide();
						el.find('.images').fadeTo(1,1);
						el.addClass('opened');
						if(!jQuery.browser.msie)
						p.fadeTo(1000, 1);
						
						parent.find('.'+params.contentclass).css({width:0, 'margin-left':0, 'display': 'block', 'overflow': 'hidden'});
						p.css({width:fullWidth, 'margin-left': 0, 'overflow': 'hidden','filter': '', 'opacity': ''}); //'overflow': 'auto'
						next.css({'width': 0, 'margin-left':0, 'display': 'none'});
						if(!jQuery.browser.msie || jQuery.browser.version >7)
						intervalId = setInterval(cubeDown, interval);
						
						/*if(jQuery.browser.msie && jQuery.browser.version <8){
				
							p.css('visibility', 'visible');
							next.css('visibility', 'visible');
						}*/
						$('.'+params.headerclass).css('background', '');
					}else{
					prev.css({'margin-left': -mdelta});
					p.css({width: mdelta});
					

					mdelta = mdelta + 30;
					}
				}, 10);
			}
				
			else if(next.prev().hasClass('opened')){
				el.hide();
				
				//if(next.next().find('.first').css('display') != 'none')
				next.next().find('.first').hide();
				next.find('.names, .descript').fadeTo(1, 0);
				var vvv = parseInt(fullWidth/2)-60;
				next.prev().find('.images').fadeTo(1,0);
				next.prev().show();
				//next.find('.text').css('width', vvv);
				//p.find('.text').css('width', vvv);
				mdelta = fullWidth;
				p.css({width:fullWidth, 'margin-left': -fullWidth, 'display': 'block', 'overflow': 'hidden'});
				next.css({width:fullWidth, 'margin-left':0, 'display': 'block', 'overflow': 'hidden'});
				parent.find('.'+params.headerclass).removeClass('opened');
				ddd = setInterval(function (){
					if(parseInt(p.css('margin-left'))>=0 || parseInt(next.width())<0){
						clearInterval(ddd);
						p.find('.names, .descript').fadeTo(1, 1);
						p.nextUntil().removeClass('opened');
						next.prev().find('.images').fadeTo(1,1);
						p.next().find('.first').show();
						next.prev().fadeTo(1,1);
						
						el.addClass('opened');
						if(!jQuery.browser.msie)
						p.fadeTo(1000, 1);
						parent.find('.'+params.contentclass).css({width:0, 'margin-left':0, 'overflow': 'hidden'});
						next.css({'width': 0, 'margin-left':0, 'display': 'none'});
						p.css({width:fullWidth, 'margin-left': 0, 'overflow': 'hidden','filter': '', 'opacity': ''}); //'overflow': 'auto'
						
						if(!jQuery.browser.msie || jQuery.browser.version >7)
						intervalId = setInterval(cubeDown, interval);
						
						
						/*if(jQuery.browser.msie && jQuery.browser.version <8){
				
							p.css('visibility', 'visible');
							next.css('visibility', 'visible');
						}*/
						$('.'+params.headerclass).css('background', '');
					}else{
					p.css({'margin-left': -mdelta});
					next.css({'width': mdelta});
					//console.log(parseInt(next.css('width')));
					mdelta = mdelta - 30;
					}
				}, 10);	


			}
			
						//next.prev().style.removeAttribute('filter');
		}
	}
	
	function doIt(p){
		p.css({width:fullWidth, 'margin-left': 0, 'overflow': 'auto'});

	
	}
				function whoWhatHow (el){
					
					slideIsActive = 1;
					var item = el.attr('rel');
						if(!$('#'+item+'Ru').hasClass('displayed')){						
							$('.item').stop(true,true);	
							$('.item').hide();
							devastIsAlive = 0;
							$('#devastator').hide();
							switch(item){
								case 'who': 
									if($('#whoRu').css('display') == 'none'){
											$('#howRu').hide();
											$('#whatRu').hide(); 
											$('.item').removeClass('displayed');
											$('#whoRu').addClass('displayed');
											$("#whatLink").attr('src', path+'what.png');
											$("#howLink").attr('src', path+'how.png');
											$("#whoLink").attr('src', path+'who_o.png');
											if(jQuery.browser.msie && jQuery.browser.version < 8){
												$("#alert").hide();
												$('#whoRu').show();										
											}else
											$('#whoRu').slideDown('slow');  
										}
									break;
								case 'how': 
									if($('#howRu').css('display') == 'none') {
										$('#whoRu').hide();
										$('#whatRu').hide();
										$('.item').removeClass('displayed'); 
										$('#howRu').addClass('displayed');
										$("#whatLink").attr('src', path+'what.png');
										$("#howLink").attr('src', path+'how_o.png');
										$("#whoLink").attr('src', path+'who.png');
										if(jQuery.browser.msie && jQuery.browser.version < 8){
											$("#alert").hide();
											$('#howRu').show();										
										}else
											$('#howRu').slideDown('slow');
									}
								break;
								case 'what': 
									if($('#whatRu').css('display') == 'none'){
										$('#howRu').hide();
										$('#whoRu').hide();
										$('.item').removeClass('displayed'); 
										$('#whatRu').addClass('displayed');
										$("#whatLink").attr('src', path+'what_o.png');
										$("#howLink").attr('src', path+'how.png');
										$("#whoLink").attr('src', path+'who.png');
										if(jQuery.browser.msie && jQuery.browser.version < 8){
											$("#alert").hide();
											$('#whatRu').show();		
										}else
											$('#whatRu').slideDown('slow'); 
									}
								break;
							}
						//$('.menu').css('color', '#000');
						//el.css('color', '#f58220');
						//Cufon.refresh($('.menu'));
						
					}
		}
		$(document).ready(function (e){	
			$('.openProject').click(function (e){
				e.preventDefault();
				window.open($(this).attr('href'));
			});
			if(jQuery.browser.msie)
			$('.names, .images').css('background', '#fff');
			
			setTimeout(function () {
				$('.blank').remove();
				$('.cube').remove();
				if(!jQuery.browser.msie || jQuery.browser.version > 7)
				clearInterval(intervalId);
				filThePx();
			}, 360000);
			$('.menu').click(function(e) {e.preventDefault()});
			$('.'+params.headerclass).bind('mouseover', function (){
				clearTimeout(moveTimer);
				el = $(this);
				moveTimer = setTimeout(function () {
					acordion(el, width)
				}, moveTimeDelay);
				}).mouseout(function (){
					clearTimeout(moveTimer);
				});

				Cufon.replace($('#lang'), { fontFamily: 'CastleT' });
				
				Cufon.replace($('.title'), { fontFamily: 'CastleT' });
				//Cufon.replace($('.menu'), { fontFamily: 'Amelia_DG' });
				$('.lang').mouseover(function () {
					$(this).css('color', '#f58220');
					Cufon.replace('#lang');
					
				}).mouseout(function () {
					$(this).css('color', '#000');
					Cufon.replace('#lang');
				});
			
				$('#menu a').mouseenter(function () {
					clearTimeout(moveTimer);
					el = $(this);
					moveTimer = setTimeout(function () {
						whoWhatHow (el);
						
					}, moveTimeDelay);
					
				}).mouseleave(function (){
					clearTimeout(moveTimer);
				});
				
				$('#logo >img').mouseover(function () {
					slideIsActive = 0;
					moveTimer = setTimeout(function () {
						if(jQuery.browser.msie && jQuery.browser.version < 8){
							$('.item').hide();
							$("#alert").show();
							
						}else
						$('.item').slideUp(500);
						//$('.menu').css('color', '#000');
						//Cufon.refresh($('.menu'));
						$("#whatLink").attr('src', path+'what.png');
						$("#howLink").attr('src', path+'how.png');
						$("#whoLink").attr('src', path+'who.png');
						devastIsAlive = 1;
						$('#devastator').show();
						$('.item').removeClass('displayed'); 
						
					}, moveTimeDelay);
				}).mouseout(function (){
					clearTimeout(moveTimer);
				});
				filThePx();
				openRondomPortfolio ();

				//openRondomPortfolio ();	

			
			$('.textUp').mouseover(function () {
				
				
				$(this).parent().parent().find('.textDown').css('visibility', 'visible');
				//if($(this).parent().parent().parent().attr('id') == 'whoRu')
				//var index = 1.5;
				//else 
				//var index = 1.7;
				var elm = $(this);
				var text = $(this).parent().parent().find('.text2');
				var index = text.find('.scrollIndex').val();
				if(index<1)
				index = 1;
				//index=parseFloat(index)+0.1;
				//alert(index);
				if(parseInt(text.css('margin-top')) != 0){
					scrollDelay = - parseInt(text.css('margin-top'))+2;
					text.css('margin-bottom', -2);
					text.css('margin-bottom', 0);
				}
				else{
					scrollDelay = -4;
				}
				scroll = setInterval(function () {
				textHeight = parseInt(text.height());
				
				if(textHeight >=index*(height-devastHeight-40)){
					clearInterval(scroll);
					elm.css('visibility', 'hidden');
				}else{
						text.css('margin-top', -scrollDelay);
						text.css('height', textHeight+2);
						scrollDelay = scrollDelay+2;
					}
				}, 10);
				
			}).mouseout(function (){
				clearInterval(scroll);
			});	
			
			$('.textDown').mouseover(function () {
				$(this).parent().parent().find('.textUp').css('visibility', 'visible');
				var elm = $(this);
				var text = $(this).parent().parent().find('.text2');
				//alert(text.html())
				if(parseInt(text.css('margin-top')) != 0){
					scrollDelay = - parseInt(text.css('margin-top'))-2;
					//text.css('margin-bottom', -2);
					//text.css('margin-bottom', 0);
				}
				else{
					scrollDelay = - 4;
				}
				index = 85;
				scroll = setInterval(function () {
				textHeight = parseInt(text.height());
				//console.log(textHeight+' '+height);
				if(textHeight <=height-devastHeight-index){
					//alert('');
					clearInterval(scroll);
					elm.css('visibility', 'hidden');
				}else{
						text.css('margin-top', -scrollDelay);
						text.css('height', textHeight-2);
						scrollDelay = scrollDelay-2;
					}
				}, 10);
				
			}).mouseout(function (){
				clearInterval(scroll);
			});			
			


			$('#ssubmit').click(function (){
				sendMail(1);
				
			});
			$('#submit').click(function (){
				sendMail(0);
				
			});
			$(window).bind('resize', function() {
				$('.blank').remove();
				$('.cube').remove();
				clearInterval(intervalId);
				filThePx();
				openRondomPortfolio ();
			});
			
			function changePic (el){
				var str = el.attr('src');
				var path = str.substring(0, str.lastIndexOf('/'));
				var img = str.substring(str.lastIndexOf('/')+1, str.lastIndexOf('.jpg'));
				
				
				img = parseInt(img)+1;
				if(img>4)
				img =1;
				el.fadeTo(1000, 0, function () {
					el.attr('src', path+'/'+img+'.jpg');
					el.fadeTo(1000, 1);
					//changePic (el);
				});
			}
			$('.picBlock img').mouseenter( function (){
				el = $(this);
				var str = el.attr('src');
				var path = str.substring(0, str.lastIndexOf('/'));
				var img = str.substring(str.lastIndexOf('/')+1, str.lastIndexOf('.jpg'));
				
				
				img = parseInt(img)+1;
				if(img>4)
				img =1;
				el.attr('src', path+'/'+img+'.jpg');
				
			}).mouseout( function (){
				$('.picBlock img').stop(true, true);
				
			});
			$(document).mousemove(function (e) {
			//clearTimeout(moveTimer);
				if(devastIsAlive == 1){
					mouseSpeed = curX-e.pageX;
					
					curY = e.pageY;
					curX = e.pageX;
					mouseMoveCount++;
					
					if(curX<0)
					curX = 0;
					if(curX>width-118)
					curX=width-118;
					
					$('#devastator').css('left',curX);		
				}
				else if(devastIsAlive != 1){
					//$('#devastator').animate({left:width/2-60}, 2000);
				}
			});

		});
		
		function cubeIsCrashed (leftCoord, cube, offset){
				leftCoord = cube.offset().left;
				xN = parseInt(leftCoord/x);
				x0 = xN*x;
				xN1 = parseInt((leftCoord+x)/x);
				x1 = xN1*x;
				xN2 = parseInt((leftCoord-x)/x);
				x2 = xN2*x;
				
				xLast  = Math.floor( Math.random( ) * (0 - leftCoord + 1) ) + leftCoord;
				xLast1 = Math.floor( Math.random( ) * (leftCoord - width + 1) ) + width;	
				xLast2 = Math.floor( Math.random( ) * (0 - leftCoord + 1) ) + leftCoord;					
				
				if(xLast>width)
				xLast = width;		
				
				var placeForCrush = 0;
				var placeForCrush1 = 0;
				var placeForCrush2 = 0;
				
				$('.blank').each(function (){
					
					if(parseInt($(this).css('left'))==x0 && parseInt($(this).css('top')) == y*4 && x0>=0 && x0<width-x)
					placeForCrush++;
					
					if(parseInt($(this).css('left'))==x1 && parseInt($(this).css('top')) == y*4 && x1>=0 && x1<width)
					placeForCrush1++;
					
					if(parseInt($(this).css('left'))==x2 && parseInt($(this).css('top')) == y*4 && x2>=0 && x2<width-x)
					placeForCrush2++;
						
				});
				cubeIsGoingDownCount = 0;
				if(parseInt(placeForCrush)==0){
					$OuterDiv = $('<div class="cube" style="top:'+y*4+'px;left:'+x0+'px;background:url('+img5+')"></div>');
					$('#fifth').append($OuterDiv);
					$('#fifth').append('<div class="blank" style="top:'+y*4+'px;left:'+x0+'px;" ></div>');
					
					$OuterDiv.animate({'top':(height-31)}, 2000, 
					function () {
						/*var offset = $(this).offset();
						var devastLeft = parseInt($('#devastator').css('left'));
						if(offset.left>=devastLeft-12 && offset.left<=devastLeft+118){
							//cubeIsGoingUp($(this), offset);
						}
						else*/
						$(this).fadeOut('slow', function (){$(this).remove();});
					});
					cubeIsGoingDownCount++;
				}
				
				if(placeForCrush1==0 && x1<width-x){
					$OuterDiv1 = $('<div class="cube" style="top:'+y*4+'px;left:'+x1+'px;background:url('+img5+')"></div>');
					$('#fifth').append($OuterDiv1);
					$('#fifth').append('<div class="blank" style="top:'+y*4+'px;left:'+x1+'px;" ></div>');
					
					$OuterDiv1.animate({'top':(height-31)}, 2000, 
					function () {
						$(this).fadeOut('slow', function (){$(this).remove();});
					});
					cubeIsGoingDownCount++;
				}
				
				if(placeForCrush2==0 && x2>0){
					$OuterDiv2 = $('<div class="cube" style="top:'+y*4+'px;left:'+x2+'px;background:url('+img5+')"></div>');
					$('#fifth').append($OuterDiv2);
					$('#fifth').append('<div class="blank" style="top:'+y*4+'px;left:'+x2+'px;" ></div>');
					
					$OuterDiv2.animate({'top':(height-31)}, 2000, 
					function () {
						$(this).fadeOut('slow', function (){$(this).remove();});
					});
					cubeIsGoingDownCount++;
				}
				
				//$OuterDiv.animate({'top':height-31, 'left':leftCoord-x},2000,function () {});
				//$OuterDiv1.animate({'top':height-31, 'left':leftCoord+x},2000,function () {});
				
				if(cubeIsGoingDownCount>0){
					
					var dif = parseInt(cube.css('left'))-offset.left;
					var left = parseInt(cube.css('left')) - Math.abs(dif);
					goingDown(cube.attr('id'), left);
					/*cube.animate({'top':(height-31), 'left': left}, 2000, 
						function () {
							/*var offset = $(this).offset();
							var devastLeft = parseInt($('#devastator').css('left'));
							if(offset.left>=devastLeft-12 && offset.left<=devastLeft+118){
								cubeIsGoingUp($(this), offset);
							}
							else
							$(this).fadeOut('slow', function (){$(this).remove();});
						});
					*/
				}
				else 
				setTimeout(function () {cube.fadeOut('slow', function (){$(this).remove();})}, 1000);
		}
		
		function cubeIsGoingUp(cube, offset, direction, s, leftCoord){
			var reflex = 0;
			if(!s)
			s = 2000;
			if(!leftCoord){
				var limitLeftCoord = width-y*2;
				var leftCoord = Math.floor( Math.random( ) * (0 - width + 1) ) + width;	
				cube.css('left', offset.left);
				
				mouseSpeed = Math.abs(mouseSpeed);
				
				switch(direction){
					case 1:  leftCoord = parseInt($('#devastator').css('left'))-300; break;
					case -1: leftCoord = parseInt($('#devastator').css('left'))+300; break;
					case 0:  leftCoord = parseInt(offset.left); break;
				}
				leftCoord = parseInt(leftCoord/x);
				leftCoord = leftCoord*x;
			}
			else
				reflex = 1;
			
			
			cube.animate(
				{'top':0, 'left':leftCoord}, 
				{duration:s, 
				step: function() {
					var noffset = $(this).offset();
					if(noffset.left>=width-13 && reflex == 0){
						cube.stop();
						var ntop = noffset.top;
						var oleft = offset.left;
						var z = parseInt((width-oleft)*ntop)/parseInt(height-ntop);
						var newLeft = width - z;
						ns = ntop*s/(height-y*5);
						cubeIsGoingUp(cube, noffset, direction, ns, newLeft);
					}
					else if(noffset.left<0 && reflex == 0){
						cube.stop();
						var ntop =  noffset.top;
						var oleft = offset.left;
						var newLeft = parseInt((ntop*oleft)/parseInt(height-ntop));		
						ns = ntop*s/(height-y*5);	
						cubeIsGoingUp(cube, noffset, direction, ns, newLeft);
					}				
					getThemNah(cube, noffset, offset);					
				}
				}
			)				
		}
		function getThemNah(cube, noffset, offset){
			if(noffset.top<=y*5 && noffset.top>=y*4)
				chechBeforeCrash(cube, 'fifth', y*4, offset);
			else if(noffset.top<=y*4 && noffset.top>=y*3)
				chechBeforeCrash(cube, 'fouth', y*3, offset);
			else if(noffset.top<=y*3 && noffset.top>=y*2)
				chechBeforeCrash(cube, 'third', y*2, offset);
			else if(noffset.top<=y*2 && noffset.top>=y)
				chechBeforeCrash(cube, 'second', y, offset);
			else if(noffset.top<=y && noffset.top>=0)
				chechBeforeCrash(cube, 'first', 0, offset);
		}
		function chechBeforeCrash(cube, layer, top, offset){
			var noffset = cube.offset();
			var cubeIsStoped = 0;
			placeForCrush0 = 0;
			placeForCrush1 = 0;
			
				//console.log(noffset.left+'->'+offset.left);	
			$('#'+layer+' .blank').each( function (){							
				var planOffset = $(this).offset();
				var leftCornerP = planOffset.left;
				var rightCornerP = planOffset.left+x;
				var leftCornerC = noffset.left;
				var rightCornerC = noffset.left+x;
				
				if(noffset.left==offset.left){
					if(leftCornerP<=leftCornerC && rightCornerP>=rightCornerC && planOffset.top == top)
						placeForCrush0++;
					placeForCrush1++;
				}
				
				else{
					if(leftCornerP<=leftCornerC && rightCornerP>=leftCornerC && planOffset.top == top)
						placeForCrush0++;
					
					else if(leftCornerP<=rightCornerC && rightCornerP>=rightCornerC && planOffset.top == top)
						placeForCrush1++;
				}
			});
						
			if(placeForCrush0 == 0){
				cube.stop();
				countleft = parseInt(noffset.left/x);
				newCube(cube, countleft*x, top, layer);
				cubeIsStoped++;
			}
						
			if(placeForCrush1 == 0){
				cube.stop();
				countleft = parseInt((noffset.left+x)/x);
				newCube(cube, countleft*x, top, layer);
				cubeIsStoped++;
			}	
			if(cubeIsStoped > 0)
				goingDown(cube.attr('id'), 2*noffset.left-offset.left, noffset);
				
		}
		function newCube(cube, left, top, layer){
			
			$OuterDiv = $('<div class="cube" style="top:'+top+'px;left:'+left+'px;background:url('+img5+')"></div>');
			$('#'+layer).append($OuterDiv);
			$('#'+layer).append('<div class="blank" style="top:'+top+'px;left:'+left+'px;" ></div>');
			
			if(jQuery.browser.msie && jQuery.browser.version == 7)
			var devided = 1;
			else
			var devided = 0;
			$OuterDiv.animate({'top':(height-y)}, {duration: 2500,
				step: function () {
					var topC = $(this).offset().top;
					var leftC = $(this).offset().left;
					if(topC>=height/4 && topC<=height/3 && devided == 0){
						$(this).stop();
						$OuterDivN0 = $('<div></div>');
						$OuterDivN0.css({'width': parseInt(x/2), 'height': parseInt(y/2), 'background': 'url('+img5+')', 'position': 'absolute', 'left': left, 'top': $(this).offset().top });
						$OuterDivN1 = $('<div></div>');
						$OuterDivN1.css({'width': parseInt(x/2), 'height': parseInt(y/2), 'background': 'url('+img5+')', 'position': 'absolute', 'left': left+x/2, 'top': $(this).offset().top});
						$('body').append($OuterDivN0);
						$('body').append($OuterDivN1);
						$(this).remove();
						$OuterDivN0.animate({'top': height-y}, {duration: 2500,
							step: function () {
								var topCC = $(this).offset().top
								if(topCC>=height/2 && topCC<=height){
									$(this).stop();
									$OuterDivN0 = $('<div></div>');
									$OuterDivN0.css({'width': parseInt(x/4), 'height': parseInt(y/4), 'background': 'url('+img5+')', 'position': 'absolute', 'left': left, 'top': $(this).offset().top });
									$OuterDivN1 = $('<div></div>');
									$OuterDivN1.css({'width': parseInt(x/4), 'height': parseInt(y/4), 'background': 'url('+img5+')', 'position': 'absolute', 'left': left+x/4+1, 'top': $(this).offset().top});
									$('body').append($OuterDivN0);
									$('body').append($OuterDivN1);
									$(this).remove();
									$OuterDivN0.animate({'top': height-y}, {duration: 2500, 
										//step: function (){checkForOldBroken($(this));}, 
										complete: function () {$(this).addClass('broken');$(this).remove();}
									});
									$OuterDivN1.animate({'top': height-y}, {duration: 2000, 
										//step: function (){checkForOldBroken($(this));}, 
										complete: function () {$(this).addClass('broken');$(this).remove();}
									});
								}
							}						
						});
						devided11 = 0;
						$OuterDivN1.animate({'top': height-y}, {duration: 2500,
							step: function () {
								var topCC = $(this).offset().top
								if(topCC>=height/2 && topCC<=height){
									$(this).stop();
									$OuterDivN0 = $('<div></div>');
									$OuterDivN0.css({'width': parseInt(x/4), 'height': parseInt(y/4), 'background': 'url('+img1+')', 'position': 'absolute', 'left': left+x/4+2, 'top': $(this).offset().top });
									$OuterDivN1 = $('<div></div>');
									$OuterDivN1.css({'width': parseInt(x/4), 'height': parseInt(y/4), 'background': 'url('+img1+')', 'position': 'absolute', 'left': left+x/4+3, 'top': $(this).offset().top});
									$('body').append($OuterDivN0);
									$('body').append($OuterDivN1);
									$(this).remove();
									$OuterDivN0.animate({'top': height-y}, {duration: 2500, 
										//step: function (){checkForOldBroken($(this));},
										complete: function () {$(this).addClass('broken'); $(this).remove();}
									});
									$OuterDivN1.animate({'top': height-y}, {duration: 2000,
										//step: function (){checkForOldBroken($(this));},
										complete: function () {$(this).addClass('broken');$(this).remove();}
									});
								}								
							}
						});
						devided++;
					}
				},				
				complete: function () {
					$(this).remove();
				}
			}	
			);
		}
		function checkForOldBroken(cube){
			var brokenOffset = cube.offset();
			$('.broken').each(function (){
				var oldBrokenOffset = $(this).offset();
				if(oldBrokenOffset.top<=brokenOffset.top+y/4 && ((brokenOffset.left>=oldBrokenOffset.left && brokenOffset.left<=oldBrokenOffset.left+x/4) || (brokenOffset.left+x/4>=oldBrokenOffset.left && brokenOffset.left+x/4<=oldBrokenOffset.left+x/4)))
					cube.stop();
			});
		}
		function cubeIsCaptured(block){
			
			var cube = $('#'+block);
			var offset = cube.offset();
			
			if(offset){
				//alert(offset.top);
				var devastLeft = parseInt($('#devastator').css('left'));
				if(parseInt(offset.top)>=height-31){
					cube.stop();
					//cube.css({top: height-31});
					//cubeIsGoingUp(x, offset);
				}
			}
		}
		function goingDown(block, left, oldoffset, s, newLeft){//функция отвечает за падение кубиков
			var cube = $('#'+block);
			if(!left)
			left = cube.offset().left;
			
			if(newLeft)
			left = newLeft;
			
			
			if(!s){
			if(slideIsActive == 1)
				s = 5000;
			else
				s = 2000;			
			}
			$('#'+block).animate(
				{'top':height+31, 'left':left},
				{duration:  s,
				step: function (){
					
					var devastLeft = parseInt($('#devastator').css('left'));
					var offset = cube.offset();
					var ntop = offset.top;
					if(offset.left>=width-13 && oldoffset){
						var oleft = oldoffset.left;
						var otop = oldoffset.top;
						$('#'+block).stop();
						var newLeft = parseInt(width - ((width-oleft)*(height-ntop)/ntop));
						//ns = ntop*s/(height-y*5);	
						ns = s*ntop/(height+31);
						
						goingDown(cube.attr('id'), null, null, ns, newLeft);
					}
					else if(offset.left<0 && oldoffset){
						var oleft = oldoffset.left;
						var otop = oldoffset.top;
						$('#'+block).stop();
						var newLeft = (oleft*31)/otop ;
						ns = s*ntop/(height+31);	
						goingDown(cube.attr('id'), null, null, ns, newLeft);
					}	

					
					if(devastIsAlive == 1 && parseInt(offset.top)>=height-31 && parseInt(offset.top)<=height-13 && offset.left>=devastLeft-12 && offset.left<=devastLeft+118){
						cube.stop();
						cube.css({top: height-31});
						

						if(mouseSpeed>0)
						direction = 1; //'left';
						else if (mouseSpeed<0)
						direction = -1 //'right';
						else
						direction = 0;	
						
						

						cubeIsGoingUp(cube, offset, direction);
						mouseSpeed = 0;
					}
					
				},
				complete: function () {$(this).remove();}
				}
			);
		}
		function cubeDown(){
			var x0 = Math.floor( Math.random( ) * (0 - w-x + 1) ) + w-x;
					xN = parseInt(x0/x);
					x0 = xN*x;
			 

			var count = 0
			
			var cc = Math.floor( Math.random( ) * (0 - allBlankCount + 1) ) + allBlankCount;
			$('.blank').each(function (){
				if(count==cc){
					$(this).remove();
				}
				count++;
			});
			var row = parseInt(Math.floor( Math.random( ) * (1 - 5) ) + 5);
			switch(row){
				case 1: var div = $('#first'); var top = 0; var cube = img1; break;
				case 2: var div = $('#second'); var top = y; var cube = img2; break;
				case 3: var div = $('#third'); var top = y*2; var cube = img3; break;
				case 4: var div = $('#fouth'); var top = y*3; var cube = img4; break;
				case 5: var div = $('#fifth'); var top = y*4; var cube = img5; break;
			
			}
			div.append('<div class="cube" style="top:'+top+'px;left:'+x0+'px;background:url('+cube+')" id="'+cubeId+'"></div>'); 
			div.append('<div class="blank" style="top:'+top+'px;left:'+x0+'px;" ></div>');
			goingDown(cubeId);
			cubeId++;
			
			
		};
	function cutPx(count, animate){
		switch(count){
					case 1: var N = parseInt(nx*blankCount1); var div = $('#first'); var top = 0; var str = ''; break;
					case 2: var N = parseInt(nx*blankCount2); var div = $('#second'); var top = y; var str = ''; break;
					case 3: var N = parseInt(nx*blankCount3); var div = $('#third'); var top = y*2; var str = ''; break;
					case 4: var N = parseInt(nx*blankCount4); var div = $('#fouth'); var top = y*3; var str = '<div class="blank" style="top:'+top+'px;left:0px"></div><div class="blank" style="top:'+top+'px;left:'+nx*x+'px"></div><div class="blank" style="top:'+top+'px;left:'+(nx-1)*x+'px"></div>'; break;
					case 5: var N = parseInt(nx*blankCount5); var div = $('#fifth'); var top = y*4; var str = '<div class="blank" style="top:'+top+'px;left:0px"></div><div class="blank" style="top:'+top+'px;left:'+(nx-1)*x+'px"></div><div class="blank" style="top:'+top+'px;left:'+(nx-2)*x+'px"></div>'; break;
				};
				str = '';
				for(var i=0; i<N; i++){
					var x0 = Math.floor( Math.random( ) * (0 - w-x + 1) ) + w-x;
					xN = parseInt(x0/x);
					x0 = xN*x;
					str = str+'<div class="blank" style="top:'+top+'px;left:'+x0+'px"></div>';
					
				}	
				div.append(str);
				count = 0;
				if(animate==true){
					if(!jQuery.browser.msie || jQuery.browser.version > 7){
					allBlankCount = 0;
					$('.blank').each(function (){allBlankCount++}); 
					intervalId = setInterval(cubeDown, interval);
					}
				}				
	};
	function openRondomPortfolio (){
		$("."+params.contentclass).css({width:0, 'margin-left':0, 'overflow': 'hidden', 'display' :'block'});
		$(".haccordion, .haccordion .header, .haccordion .content, #main, #howRu").css('height', height-devastHeight);
		$(".header .borders").css('height', height-devastHeight-32);
		$('.'+params.headerclass).css({'display' :'block'});
		$('.'+params.headerclass).removeClass('opened');
		$('.text').width(width-params.delta*params.countDelta-550);
		//$('.whoTextRight, .whoTextLeft').width(parseInt((width-params.delta*params.countDelta-55)/2));
		//$('.whoTextCenter').width(parseInt(width-params.delta*params.countDelta));
		$('.first').hide();
		
		/* ------------------- TEXT ------------------- */
		var text2Allwidth = parseInt(width-params.delta*params.countDelta);
		var text2width = parseInt(0.8*text2Allwidth);
		var text2margin = parseInt(0.1*text2Allwidth);
		$('.textUpDiv').hide();
		
		if(text2width>maxTextWidth)
		text2width = maxTextWidth;
		else if(text2width<minTextWidth)
		text2width = minTextWidth;
		//alert(text2width);
		$('.text2').css({height:parseInt(height-devastHeight-85), 'margin-top': 0});
		//$('#howRu .text2').css({height:parseInt(height-devastHeight-45), 'margin-top': 0});
		//$('.text2').css({width: text2width, 'margin-left': 'auto'});
		$('#whoRu .text2all, #whatRu .text2all').css({width: text2width, 'margin-left': 'auto'});
		
		var whoIsSHown = 0;
		var whatIsSHown = 0;
		var howIsSHown = 0;
		if($('#whoRu').css('display')!='none')
		 whoIsSHown = 1;
		else if($('#whatRu').css('display')!='none')
		whatIsSHown = 1;
		else if($('#howRu').css('display')!='none')
		howIsSHown = 1;

		$('#whoRu').show();
		$('#whatRu').show();
		$('#howRu').show();
		var count = 0;
		$('.text2').each(function(){
			count++;
			var text2Top = $(this).offset().top;
			var endText2Top = $(this).find('.end').offset().top;
			var text2height = parseInt(endText2Top-text2Top+50);
			var text2space = parseInt(height-devastHeight-40);
			if(count==6)
			text2space = text2space-40;
			//console.log(endText2Top+' '+text2Top);
			if(text2space<=text2height){
				$(this).parent().parent().find('.textUpDiv').show();
				$(this).find('.scrollIndex').val(parseFloat(text2height/text2space).toFixed(1));
				//console.log($(this).html()+'->'+parseFloat(text2height/text2space).toFixed(1));
			}
			
		});
		if(whoIsSHown == 0)
		$('#whoRu').hide();
		if(whatIsSHown == 0)
		$('#whatRu').hide();
		if(howIsSHown == 0)
		$('#howRu').hide();
		
		/* ------------------- TEXT ------------------- */
		//openedSlide = 
					
		$('.names, .descript').fadeTo(1, 0);
		if(!isNaN(openedSlide))
		var currentAccordionIsOpen = openedSlide;
		else
		var currentAccordionIsOpen = Math.floor( Math.random( ) * (1 - 4) ) + 4;

			var accordionCount = 1;
			$("#howRu ."+params.contentclass).each(function (){
				if(accordionCount == currentAccordionIsOpen){
					$(this).show();
					openedSlide = currentAccordionIsOpen;
					$(this).css({width:width-params.delta*params.countDeltaHow, 'display': 'block', 'margin-left': 0, 'overflow': 'hidden'});
					$(this).prev().hide();
					//$(this).prev().prev().css({width:0, 'display': 'block', 'margin-left': 0, 'overflow': 'hidden'});
					$(this).prev().addClass('opened');
					$(this).next().find('.first').show();
					$(this).find('.names, .descript').fadeTo(1, 1);
				}
				accordionCount++;
			
			});
			var accordionCount = 1;
			currentAccordionIsOpen = 1;
			$("#whoRu ."+params.contentclass).each(function (){
				if(accordionCount == currentAccordionIsOpen){
					$(this).show();
					openedSlide = currentAccordionIsOpen;
					$(this).css({width:width-params.delta*params.countDeltaWho, 'display': 'block', 'margin-left': 0, 'overflow': 'hidden'});
					$(this).prev().hide();
					//$(this).prev().prev().css({width:0, 'display': 'block', 'margin-left': 0, 'overflow': 'hidden'});
					$(this).prev().addClass('opened');
					$(this).next().find('.first').show();
					$(this).find('.names, .descript').fadeTo(1, 1);
				}
				accordionCount++;
			
			});
			var accordionCount = 1;
			currentAccordionIsOpen = 1;
			$("#whatRu ."+params.contentclass).each(function (){
				if(accordionCount == currentAccordionIsOpen){
					$(this).show();
					openedSlide = currentAccordionIsOpen;
					$(this).css({width:width-params.delta*params.countDeltaWho, 'display': 'block', 'margin-left': 0, 'overflow': 'hidden'});
					$(this).prev().hide();
					//$(this).prev().prev().css({width:0, 'display': 'block', 'margin-left': 0, 'overflow': 'hidden'});
					$(this).prev().addClass('opened');
					$(this).next().find('.first').show();
					$(this).find('.names, .descript').fadeTo(1, 1);
				}
				accordionCount++;
			
			});
			
			//$('.cons').css({height: height-devastHeight-40, 'margin-top' :0});
			$('.textDown').css('visibility', 'hidden');
			$('.textUp').css('visibility', 'visible');

			
	}
	function filThePx(){	
			height = $(window).height();
			width = $(window).width();
			count = parseInt(width/12);
			
			h = height/5;
			w = parseInt(width);
			
			nx = parseInt(w/x); //количество кубиков в ряду
			//var ny = parseInt(h/y); //количество рядов
			n = parseInt(nx*ny-1);

			//$('#devastator').css('z-index',5000);
			$('#devastator').css('top',height-20);
			$('#devastator').css('left',width/2-25);
			//if(intervalId !=false)
			clearInterval(intervalId);
			//$('.item').fadeTo(1, 0.5);
			//выкалываем кубики
			cutPx(1, false);
			cutPx(2, false);
			cutPx(3, false);
			cutPx(4, false);
			cutPx(5, true);
			
				
			
		}

