var base_colours = [];
base_colours.push('fba328');
base_colours.push('fbac3d');
base_colours.push('feb145');
base_colours.push('fdb857');

var current_scroller = null;
var scroll_interval = null;

$(document).ready(function() {
	//scroll_interval = setTimeout(changeBoxes,1000);
	$('.img_carousel').bind('mouseover',function(e) {
		animIn(this,null)
	});

	$('.img_carousel').bind('mouseout',function(e) {
		animOut(this);
	});
	
	$('.img_carousel').bind('click',function(e) {
		e.preventDefault();
		window.location.href = $(this).find('h3 a').attr('href');
	});
	
	$('.img_carousel h3').bind('mouseover',function(e) {
		e.stopPropagation();
		e.preventDefault();
		return false;
		//$(this).parent('.img_carousel').trigger('mouseover');
	});
	
	$('.img_carousel span.hidden_text').bind('mouseover',function(e) {
		e.stopPropagation();
		e.preventDefault();
		return false;
		//$(this).parent('.img_carousel').trigger('mouseover');
	});
	
	$('.img_carousel span.hidden_text ul').bind('mouseover',function(e) {
		e.stopPropagation();
		e.preventDefault();
		return false;
	//$(this).parent('.img_carousel').trigger('mouseover');

	});
	
	$('.img_carousel span.hidden_text ul li').bind('mouseover',function(e) {
		e.stopPropagation();
		e.preventDefault();
		return false;
		//$(this).parent('.img_carousel').trigger('mouseover');
	});
	
	$('span.carousel_to').css({'opacity':'0'});
	
});

function changeBoxes() {
	if(current_scroller!=null)  {
		animOut($('.img_carousel:eq('+current_scroller+')'));
	} else {
		current_scroller = -1;
	}
	current_scroller++;
	if(current_scroller > 3)  { current_scroller = 0; }
	
	animIn($('.img_carousel:eq('+current_scroller+')'),true);
}


function animIn(o,t) {
	var c = {};
	c.x = $(o).offset().left;
	c.y = $(o).offset().top;
	
	var new_x = (-470 + ($(o).index() * 178)) - 13;
	
	$('.img_carousel').css({'z-index':'1'});
	
	$(o).find('span.carousel_to').stop().animate({'opacity':'1','height':'346px'},200);
	$(o).find('span.carousel_from').stop().animate({'height':'346px'},200);	
	
	$(o).css({'z-index':'2'});
	$(o).css({'box-shadow':'0px 0px 10px rgba(0,0,0,0.8)'});
	$(o).find('img').stop().animate({'margin-left':'0px','margin-top':'0px'},200);
	$(o).stop().animate({'top':'146px','width':'204px','height':'579px','backgroundColor':'#1e315c','margin-left':new_x+'px'},200,function() {
		
		$(o).find('.hidden_text').stop().animate({'opacity':'1'},200);
		
		$(o).find('.hidden_text ul li').each(function(i,el) {
			$(el).stop().animate({'margin-left':'0px','opacity':'1'},200*(i+1));
		});			
		
	});
	
}

function animOut(o) {
	
	var new_x = (-470 + ($(o).index() * 178));
	$('.hidden_text').stop().animate({'opacity':'0'},180, function() {
		$('.hidden_text ul li').css({'margin-left':'-204px'});	
	});
	
	$(o).find('span.carousel_to').stop().animate({'opacity':'0','height':'328px'},100);
	$(o).find('span.carousel_from').stop().animate({'height':'328px'},200);	

	$(o).find('img').stop().animate({'margin-left':'-13px','margin-top':'-14px'},100);
	$(o).stop().animate({'top':'160px','width':'178px','height':'509px','backgroundColor':'#'+base_colours[$(o).index()],'margin-left':new_x+'px'},100,function() {
		$(o).css({'box-shadow':'none'});
	});
}


