$(window).load(function(){
	$('.backgrounds .images').kickFade({delay:8000});
});

$(document).ready(function(){
	
	$('.menu li.has_subs').hover(
		function(){
			$(this).children('.dropdown').stop(false,true).slideToggle();
		}, function(){
			$(this).children('.dropdown').stop(false,true).slideToggle();
		}
	);
	
});

$(document).ready(function(){
	
	$(".fancybox").fancybox({
		prevEffect		: 'none',
		nextEffect		: 'none',
		arrows:false,
		fixed:true,
		closeBtn		: false,
		helpers		: { 
			title	: { type : 'inside' },
			buttons	: {}
		}
	});
		
});

$(document).ready(function(){
	
	var
		$feature = $('.cms_layout_home .content .feature'),
		$images = $feature.children('.images').children('img'),
		$texts = $feature.children('.captions').children('.text'),
		$subfeatures = $('.cms_layout_home .content .sub_feature .image');
	
	var options = {
		image_speed: 800,
		text_speed: 400,
		is_changing: false,
		current:null,
		previous:null,
		can_loop:true,
		auto_change:true,
		auto_change_time: 7000,
		auto_change_timer:null,
		text_delay:0
	}
	
	// stop text fading in ie 8 and less
	if( $.browser.msie && $.browser.version <= 8 ) options.text_speed = 0;	
	
	// hide first image and text
	$images.eq(0).hide();
	$texts.eq(0).hide();
	
	$subfeatures.bind('click',function() {
		var index = $(this).index();
		if( options.is_changing || index==options.current ) return;
		options.previous = options.current;
		options.current = index;
		bannerChange();
	});	
	
	function nextImage() {
		if( options.is_changing ) return;
		options.previous = options.current;							  
		options.current+=1 ;
		if(options.current>$images.size()-1 && options.can_loop==true) {
			options.current=0;
			options.previous=$images.size()-1;
			bannerChange();
		} else if (options.current>$images.size()-1 && options.can_loop==false) {
			options.current=$images.size()-1;
		} else {
			bannerChange();
		}
	}
			
		
	function autoChange() {
		if( $images.size() -1 < 1 || !options.auto_change || options.is_changing ) return; 
		if ( options.auto_change_timer ) window.clearTimeout( options.auto_change_timer );
		options.auto_change_timer = window.setTimeout( function(){ nextImage(); } , options.auto_change_time );
	}
		
	function bannerChange() {
		options.is_changing=true;
		$texts.eq(options.previous).fadeOut(options.text_speed,function(){						
			$images.eq(options.previous).fadeOut(options.image_speed);
			$images.eq(options.current).fadeIn(options.image_speed,function(){
				$texts.eq(options.current).delay(options.text_delay).fadeIn(options.text_speed,function(){
					options.is_changing=false;
					autoChange();
				});																 
			});
		});
	}
	
	// if ie add the date to image src so .load works with cached images
	if( $.browser.msie ) $images.eq(0).attr( 'src' , $images.eq(0).attr('src') + '&' + new Date().getTime() );
	
	// on first image load fade in
	$images.eq(0).load( function(){ 
		$(this).fadeIn( options.image_speed , function(){
			$texts.eq(0).fadeIn(options.text_speed,function(){
				//$controls.eq(0).addClass('active');
				autoChange();
			});
		});
	});
	
});
