var blur_cursor;
var current_el;
var blur_cursor_out;
var process_enable = 1;
var fix_size = 0;
var check_fix_size = 0;
var step = 1;

$(function(){
	
	var whole_width = 0;
	var visible = 5;
	var all = $(".gallery-item-2");
	for(var i = 0; i < all.length; i ++) {
		whole_width += parseInt(all.eq(i).width());
	}

	$(".gallery-inner-2").css('width',whole_width);

	whole_width_move = whole_width - parseInt($(".page-center-carousell-content-inner-2").width());

	setInterval(function() {
		if(process_enable == 1){
			var new_left;
			var old_left = parseInt($(".gallery-inner-2").css("left"));
			if(old_left <= whole_width_move * (-1)) {
				var tdiv = $(".gallery-inner-2 .gallery-item-2"), td = tdiv.size();
				var add_size = 0;
				var i = 0;
				while(add_size < whole_width_move){
					add_size += tdiv.eq(i).width();
					i++;
				}
				$(".gallery-inner-2").append(tdiv.slice(0,i).clone(true));
				check_fix_size = 0;
				fix_size = 0;
				if(add_size >  whole_width_move) {
					fix_size_save = fix_size = add_size - whole_width_move;
					check_fix_size = 1;
					i = i - 1;
				}
				$(".gallery-inner-2 div").slice(0,i).remove();
				new_left = old_left + add_size - fix_size;
				$(".gallery-inner-2").css("left", new_left);
			}
			old_left = parseInt($(".gallery-inner-2").css("left"));
			new_left = old_left - step + 'px';
			if(check_fix_size == 1) {
				fix_size -= step;
				if(fix_size <= 0) {
					$(".gallery-inner-2 div").eq(0).remove();
					check_fix_size = 0;
					new_left = 0;
				}
			}
			$('.gallery-inner-2').css("left", new_left);
		}
	}, 50);

	$(".gallery-item-2").mouseover(function(){
		clearTimeout(blur_cursor);
		current_el = this;
		process_enable = 0;
		blur_cursor = setTimeout(function (){
			process_enable = 0;
			$(current_el).removeClass("gallery-item-2");
			$(".gallery-item-2").fadeTo(700,0.3);
			$(current_el).addClass("gallery-item-2");
		},500);
	});


	$(".gallery-item-2").mouseout(function(){
		clearTimeout(blur_cursor_out);
		current_el = this;
		blur_cursor_out = setTimeout(function (){
			process_enable = 1;
			$(this).removeClass("gallery-item-2");
			$(".gallery-item-2").fadeTo(700,1);
			$(this).addClass("gallery-item-2");
		},500);
	});
});
