function dbg(str)
{
	return;
	console.log(str);
}

function Slide()
{
	this.col = 0;
	this.row = 0;
}

Slide.prototype.init = function() { this.setDefault(); }
Slide.prototype.getRel = function() { return this.col+'-'+this.row; }
Slide.prototype.setDefault = function() { this.row = 0; this.col = 0;}
Slide.prototype.isDefault = function() { return (this.col == 0 && this == 0)? true : false; }
Slide.prototype.setup = function(rel){ 
	var tmp = rel.split('-');
	this.col = tmp[0];
	this.row = tmp[1]; 
	
}



/*var openSlide = 1;
var overSlide = 0;*/

function reset()
{
	// если ничего не открыть и не "выделено"
	/*if (overSlide.col == 0 && openSlide.col == 0)
		return;*/
	
	dbg('close_all? '+close_all);
	
	if (close_all)
	{
		return;
	}
	
	if (in_process)
	{
		dbg('reset busy? '+in_process);
		setTimeout('reset()', 100);
		return;
	}
	
	if (openSlide.col > 0 && difSlides(openSlide, overSlide) && openSlide.col != overSlide.col)// && openSlide.col != prevSlide.col)
	//if (prevSlide.col > 0)
	{
		dbg('reset: openSlide.col = ' + openSlide.col + ' openSlide.row = ' + openSlide.row + ' overSlide.col = ' + overSlide.col + ' overSlide.row = ' + overSlide.row);
		//$("#slide" +  openSlide.col + '-1' + ' > div').fadeOut(300);	
		$("#slide" +  openSlide.col + '-1' + ' > div').hide();	
		//dbg(prevSlide.col + '-' + prevSlide.row);
		
		in_process = true;
		
		var open_col = openSlide.col;
		
		$("#slide" + openSlide.col + '-1').stop().animate(
			{backgroundPosition:"(0 100%)", height:"196px"}, 
			//{duration:500},
			'fast',
			function (){
				dbg('reset block '+open_col+'-1 done!');
				//alert(openSlide.col);
				//$("#slide" + openSlide.col + '-1' + ' > div').hide();
				in_process = false; 
				$("#slide" +  open_col + '-1' + ' > div').hide();
			}
		);
		
		
		
		$("#slide" +  openSlide.col + '-2' + ' > div').hide();
		
		
		//fadeOut(300);
		
		$("#slide" + openSlide.col + '-2').stop().animate(
			{backgroundPosition:"(0 0)", height:"196px"}, 
			//{duration:500},
			'fast',
			function (){
			in_process = false; 
			dbg('reset block '+open_col+'-2 done!');
			//console.log('in_process = '+in_process);
			//$("#slide" +  openSlide.col + '-2' + ' > div').hide();
			$("#slide" +  open_col + '-2' + ' > div').hide();
			
			}
		);
		
		/*if (overSlide.col == 0)
		{
			openSlide.col = 0;
			openSlide.row = 0
		}*/
	}
	
}





function slide_animate()
{
	if ((overSlide.col == 0 && openSlide.col == 0 )|| in_process)
	{
		setTimeout('slide_animate()', 100);	
		return;
	}
	
	
	if (overSlide.col == 0 && openSlide.col > 0)
	{
		//dbg('slide_animate run reset');
		reset();
		close_all = true;
	}
	//if (openSlide.row != overSlide.row)// && (overSlide.row > 1 || openSlide.row > 1))
	//if (openSlide.col == overSlide.col && openSlide.row != overSlide.row && 
	if (overSlide.col > 0 && in_process == false && difSlides(openSlide, overSlide))
	{
		if (overSlide.col != openSlide.col)
		{
			reset();
		}
	
		dbg('------------');
		dbg('reset: openSlide.col = ' + openSlide.col + ' openSlide.row = ' + openSlide.row + ' overSlide.col = ' + overSlide.col + ' overSlide.row = ' + overSlide.row);		
		
		in_process = true;
		
		var row = (overSlide.row == 1)? 2 : 1;
		
		var style;
		var id_slide1 = "#slide" + overSlide.col + '-' + row; 
		var slide1 = $(id_slide1);
		// тут row invert
		style = (overSlide.row == 2)? {backgroundPosition:"(0 -286px)", height:"106px"} : {backgroundPosition:"(0 0)", height:"106px"}; 
		
		//$("#slide" + overSlide.col + '-' + row + ' > div').fadeOut(duration.fadeOut);
		$(id_slide1 + ' > div').hide();
		slide1.stop().animate(
			style, 
			//{duration:500},
			'fast',
			function(){
				//alert('callback'); 
				dbg('animate');
				in_process = false;
				$(id_slide1 + ' > div').hide();
			}		
		);
		
		//$("#slide" + overSlide.getRel() + ' > div').fadeIn(duration.fadeIn);
		
		
		var id_slide2 = "#slide" + overSlide.getRel();
		var slide2 = $(id_slide2);
		style = (overSlide.row == 1)? {backgroundPosition:"(0 0)", height:"286px"} : {backgroundPosition:"(0 -196px)", height:"286px"}; 
		slide2.stop().animate(
			style, 
			//{duration:500},
			'fast',
			function (){
				in_process = false;
				close_all = false;
				$(id_slide2 + ' > div').show('fast');
			}
			);
		
		prevSlide.col = openSlide.col;
		prevSlide.row = openSlide.row;
		
		openSlide.row = overSlide.row;
		openSlide.col = overSlide.col;
	}
/*	else
		dbg('not animated');*/
	
	setTimeout('slide_animate()', 100);	
}

function difSlides(s1, s2){ return (s1.col == s2.col && s1.row == s2.row)? false : true; }


var in_process = false;
var close_all = true;
var arr_cols = [1, 2, 3];
var overSlide = new Slide();
var openSlide = new Slide();
var prevSlide = new Slide();
var duration = { close: 400, open: 400, fadeIn: 600, fadeOut: 400 };
openSlide.init();



$(document).ready(function(){
	


	$('.main_pic div.slide').mouseover(function(){
		overSlide.setup($(this).attr('id').replace('slide', ''));
		
		//console.log('overSlide.row = ' + overSlide.row);
		//console.log('overSlide.col = ' + overSlide.col);
		//alert(overSlide);
		//alert($(this).attr('rel'));
		//overSlide.setup($(this).attr('rel'));
		
	})
	
	$('.main_pic div.slide').mouseout(function(){
		overSlide.setDefault();
		//reset();
	});
	
	slide_animate();
})
