/**
 * @author Oleg
 */

var images;
var current = 0;

$().ready(function(){
	// Цвета при нарезке
	var border_color = ($('body').hasClass('g-tc')?'white':'#3f4d6a');
	
	var pieces = {'height' : 3, 'width': 4}
	images = $('.b-cutted-image img');
	$('.b-cutted-image').each(function(){
		$(this).css({
			'height' : $(this).find('img').attr('height')+'px',
			'overflow' : 'hidden'
		});
	})

	// Нарезка картинки на главной
	var f_image = images.eq(0);
	var div = $('<div></div>').addClass('cutted-image-div');
	div.css({'height' : f_image.attr('height')+"px", 'width' : f_image.attr('width')+"px"});
	
	var h_tmp = f_image.attr('height')-pieces.height+1;
	
	var computed_height = (h_tmp - h_tmp%3) / pieces.height;
	var computed_width = (f_image.attr('width')-pieces.width+1) / pieces.width;
	
	for (var j = 0; j < pieces.height; j++) {
		for (var i = 0; i < pieces.width; i++) {
			//alert(position);
			var in_div = $('<div></div>').css({
				'height' : computed_height + "px", 
				'width' : computed_width + "px", 
				'border-left' : (i==0?'none':'1px solid '+border_color), 
				'border-bottom' : (j==pieces.height-1?'none':'1px solid '+border_color),
				'position': 'relative'
			}).addClass('block');
			
			var in_div_back = $('<div></div>').css({
				'height' : computed_height + "px", 
				'width' : computed_width + "px",
				'background-image': "url("+f_image.attr('src')+")",
				'background-position' : "-"+(computed_width*i-i)+"px "+"-"+(computed_height*j-j)+"px",
				'position': 'relative'
			})
			
			div.append(in_div.append(in_div_back));
		}
	}
	div.find('.block');
	
	f_image.after(div);
	f_image.css('display', 'none');
	
	function myAnimation(){
		time = 400;
		var interval = 6000;
		animateRandom($('.cutted-image-div'), interval);
		
		current++;
		setTimeout(myAnimation, (interval > 0?interval:6000));
	}
	
	// Параметры — время анимации кадра, время смены всей картинки, тип анимации
	//setTimeout(myAnimation, 6000);
	
	/*$('.cutted-image-div').cuttedImageAnimation({
		'imageFullAnimationTime': 6000,
		'animationType'         : 'vertTopBottom',
		'animationEffect'       : 'horizontalJalousie'
	});*/
	
	/* Фиксим баг ие6 с морганием мыши */
	try{
		document.execCommand("BackgroundImageCache", false, true);} 
	
	catch(e) {
		}
});
	
$.fn.cuttedImageAnimation = function(callerSettings){
	var settings = $.extend({
		imageBlockAnimationTime : 400,
		imageFullAnimationTime  : 6000,
		animationType           : 'random',
		animationEffect         : 'verticalJalousie'
	}, callerSettings||{});
	
	var el = this;
	setInterval(function(){
		el.find('.block').each(function(){
			var eli = $(this);
			setTimeout(function(){
				eli.flip(settings.imageBlockAnimationTime, settings.animationEffect);
			}, cia[settings.animationType].call((el.find('.block')), eli, settings.imageBlockAnimationTime))
		})
		current++;
	}, settings.imageFullAnimationTime);
};

// Cutted Image Animations — способ высчитывания времени анимации для кадра eli за время time 
var cia = {
	random : function(eli, time){
		return parseInt(Math.random()*(time*12));
	},
	fromFirstToLast : function(eli, time){
		return ($(this).index(eli) * time);
	},
	fromLastToFirst : function(eli, time){
		return (($(this).length - $(this).index(eli)) * time);
	},
	horLeftRight : function(eli, time){
		return (($(this).index(eli)%4) * time);
	},
	horRightLeft : function(eli, time){
		return ((4 - this.index(eli)%4) * time);
	},
	vertTopToBottomAsync : function(eli, time){
		return ((this.index(eli) - this.index(eli)%4)/4 * time);
	},
	vertTopBottom : function(eli, time){
		return (((this.index(eli) - this.index(eli)%4)/4) * time);
	},
	vertBottomTop : function(eli, time){
		return ((2 - (this.index(eli) - this.index(eli)%4)/4) * time);
	}
}

$.fn.flip = function(time, effect){
	var width = this.css('width');
	var height = this.css('height');
	var pos = this.find('div').css('background-position');
	var el = this.find('div')
	$(el)[effect].call(el, width, height, pos, time);
	
	return this;
};

$.fn.verticalJalousie = function(width, height, pos, time){
	var el = this
	el.animate({'width': 0, 'left': '50%'}, (time>0?time/2:200), function(){
		el.css({
			'background-image' : 'url("'+images.eq(current%images.length).attr('src')+'")',
			'background-position' : pos
		}).animate({'width': width, 'left': '0'}, (time>0?time/2:200));
	});
	
	return el;
};

$.fn.leftToRight = function(width, height, pos, time) {
	var el = this;
	el.animate({'width': 0}, (time>0?time/2:200), function(){
		el.css({
			'background-image' : 'url("'+images.eq(current%images.length).attr('src')+'")',
			'background-position' : pos
		}).animate({'width': width}, (time>0?time/2:200));
	});
}

$.fn.rightToLeft = function(width, height, pos, time) {
	var el = this;
	el.animate({'width': 0, 'left': '100%'}, (time>0?time/2:200), function(){
		el.css({
			'background-image' : 'url("'+images.eq(current%images.length).attr('src')+'")',
			'background-position' : pos
		}).animate({'width': width, 'left' : '0'}, (time>0?time/2:200));
	});
}

$.fn.horizontalJalousie = function(width, height, pos, time) {
	var el = this;
	el.animate({'height': 0, 'top': '50%'}, (time>0?time/2:200), function(){
		el.css({
			'background-image' : 'url("'+images.eq(current%images.length).attr('src')+'")',
			'background-position' : pos
		}).animate({'height': height, 'top': '0'}, (time>0?time/2:200));
	});
}