var carousel_data = new Array;
var carousel_index = 100;
var carousel_current = 0;
var carousel_interval;
var carousel_active = false;
var carousel_working = false;
var load_check;

$(document).ready( function() {

	// home header carousel
	
	var carousel_items = $('.home-carousel .item');
	
	if($('body').width() < 995) $('.home-carousel').addClass('left-margin');
	
	for (var i = 0; i < carousel_items.length; i++) {
		carousel_data[i] = new Array();
		carousel_data[i] = ['', '', ''];
		for (var j = 0; j < carousel_data[i].length; j++) {
			carousel_data[i][j] = ['', '', ''];
		}
	}
		
	carousel_data[0][0] = true;
	
	$('.home-carousel .item:first').css('z-index', carousel_index-1);
	displayCarouselText('all', null, 'hide');
	
	$('.home-carousel .item').each(function(i) {
				
		var index = $('.home-carousel .item').index($(this));
		var item = $(this);
		var offset = 250;
				
		var img = new Image(); 
		var src = $('a.source', this).attr('href');
		
		$('.text.left.first', item).each(function() {
			$('.content', item).prepend($(this));
		 });
	
		$(img).load(function () {
			
			$('.content', item).append(this);
			$('a.source', item).remove();
			carousel_data[index][0] = true;
					
		}).attr('src', src);
		
		$('.text', this).each(function(i) {
			var pos = parseFloat($(this).css('top'));
			carousel_data[index][1][i] = pos;
			carousel_data[index][2][i] = ($(this).hasClass('up')) ? pos - offset : pos + offset;
		});
		
		$('p.text.left', this).each(function() {
			
			var url = $('p.left a.link', item).attr('href');
			
			$('p.text.left', item).click(function(){
				window.location.href = url;
			})
			
		})
		
		$('p.text.right', this).each(function() {
			
			var url = $('p.right a.link', item).attr('href');
			
			$('p.text.right', item).click(function(){
				window.location.href = url;
			})
			
		})
		
		$('p.text', this).hover(function(){
			$(this).css('cursor', 'pointer');
		})
				
	})
	
	
	$('.home-carousel-controls p a').click(function() {
				
		var items = $('.home-carousel .item');
		var parent = $(this).parent();
		var index = $('.home-carousel-controls p').index(parent);
		
		if(carousel_working) return false;
		if(index == carousel_current) return false;
		
		showItem(items, index);
		
		return false;
		
	})
	
	$('.home-carousel').mouseleave(function() {
		carousel_active = false;
		autoPlay();
	})
	
	$('.home-carousel, .home-carousel-controls').mouseenter(function() {
		carousel_active = true;
		clearInterval(carousel_interval);
	})
	
	loadCheck();
	
	autoPlay();
	
});

function loadCheck() {
	
	var load_check_interval = setInterval(function() {
						
		load_check = true;

		for (var i=0; i < carousel_data.length; i++) {
			if(carousel_data[i][0] != true) load_check = false;
		};
		
		if(load_check) {
			
			$('.home-carousel-controls').fadeIn(function() {
				$('.home-carousel .item').removeClass('sub');
			});
			
			clearInterval(load_check_interval);
			
		}
		
	}, 500);
	
}

function autoPlay() {
	
	carousel_interval = setInterval(function() {
		
		//console.log('interval');
						
		if(carousel_active) return false;
		if(!load_check) return false;
		
		var items = $('.home-carousel .item');
		var next_item = (items.length-1 <= carousel_current) ? 0 : carousel_current+1;
		
		showItem(items, next_item);
		
	}, 6500);
	
}

function showItem(items, index) {
	
	displayCarouselText(items, index, 'hide', false);
	
	carousel_working = true;
	
	var item = $('.home-carousel .item').eq(index);
	
	if($(item).hasClass('light')) {
		$('.home-carousel-controls').addClass('dark');
	} else {
		$('.home-carousel-controls').removeClass('dark');
	}
				
	$(item).css({'z-index': carousel_index});
	
	$('.content > img', item).hide();
	
	$('.content > img', item).fadeIn(500, function(){
		
		displayCarouselText(item, index, 'show', true);
		carousel_working = false;
		
	});
	
	removeFilter($(item)[0]);
					
	carousel_index ++;
	carousel_current = index;
	
	$('.home-carousel-controls p').removeClass('active');
	$('.home-carousel-controls p').eq(index).addClass('active');
	
}

function displayCarouselText(item, index, mode, animate) {
	
	var duration = 200;
	var new_pos;
	
	if(mode == 'hide') {
		
		var object = (item == 'all') ? $('.home-carousel .item') : $(item);
				
		$(object).each(function(i) {
						
			$('.text', this).each(function(x) {
				var pos = carousel_data[i][2][x];
				if (!isNaN(pos)) {
					if(animate) $(this).animate({top: pos}, duration, 'swing');
					else $(this).css({top: pos});
				};
				
			})
			
		});
		
	} else {
		
		var delay = 0;
		
		$('.text', item).each(function(i) {
			var pos = carousel_data[index][1][i];
			if (!isNaN(pos)) {
				var text = $(this);
				setTimeout(function() {text.animate({top: pos}, duration, 'swing')}, delay);
				delay += 100;
			};
		});
		
	}
	
}



function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}
