/*
img1 = new Image();  img1.src = view_path+"images/img_inspiration.jpg";
img2 = new Image();  img2.src = view_path+"images/img_legacy.jpg";
img3 = new Image();  img3.src = view_path+"images/img_community.jpg";
*/
$(document).ready(function() {
		
		
		
		$('.handle').addClass('open');
				
		$('.kwicks li .description').each(function(index){
			var bgPath = "#fff url("+view_path+"images/desc_"+(index+1)+".gif) no-repeat";
			//var bgPath = "#fff url("+view_path+"images/desc_blank.png) no-repeat";
			$('h3', $(this)).hide();
			$(this).css('background',bgPath);
		});
		
		$('.kwicks li:eq(0)').append('<img src="'+view_path+'images/left_corners.png" class="left-corners" />');
		$('.kwicks li:eq(2)').append('<img src="'+view_path+'images/right_corners.png" class="right-corners" />');
		
		$('.kwicks li').append('<div class="direction-indicator"></div>');
		
		$('.kwicks').kwicks({
			max : 700,
			spacing : 0,
			duration: 1000,
			isVertical: false,
			//sticky : true,
			easing: 'easeOutSine'
		});
		
		$('.kwicks').unbind('mouseleave');
		
		setTimeout ( "autoslide()", 500 );
		
		$('.kwicks').mousemove(function(){
			$(this).stopTime('controlled');
		});
	
		$('.kwicks').css('background','#000').bind('mouseleave',function(){
			hovering($('.kwicks li.active'));
		});
		
		
		
		$('.handle').toggle(function(){
			$(this).parent('.description').animate({
				left:'-360px'
			}, 1000, 'easeInSine');
			$(this).css('background-position','-13px 0');
			$(this).removeClass('open').addClass('closed');
		},function(){
			$(this).parent('.description').animate({
				left:0
			}, 1000, 'easeInSine');
			$(this).css('background-position','0 0');
			$(this).removeClass('closed').addClass('open');
		});
		
		
}); //end of ready()

function hovering(obj){
			
		$('.direction-indicator', obj ).hide('slow');
		
		$('.direction-indicator', obj.prevAll()).removeClass('after').addClass('previous');
		$('.direction-indicator', obj.nextAll()).removeClass('previous').addClass('after');
		
		if ($('.handle',obj).is(".open"))//if description was not closed by user
		{
			$('.description', obj).animate({
				left:0
			}, 1000, 'easeOutSine');
			$('.handle',obj).css('background-position','0 0'); 
		}
		if ($('.handle',obj).is(".closed"))
		{
			$('.description',obj).animate({
				left:'-360px'
			}, 1000, 'easeOutSine');
			$('.handle', obj).css('background-position','-13px 0'); 
		}
		
		hideInactivePanelDescriptions();
			
}
		
function hideInactivePanelDescriptions(){
	$('.kwicks li:not(.active)  .description').fadeOut('300',function(){
		$(this).css({'left':'-373px','display':'block'});
    });
	$('.kwicks li:not(.active)  .direction-indicator').show('slow');
}
	
function firstPanelAnimation(){
	var $firstPanel = $('ul.kwicks li:eq(0)');
	$firstPanel.trigger('mouseover');
	$('.description', $firstPanel).animate({
	left:0
	}, 1000, 'easeOutSine');
	hideInactivePanelDescriptions();
			
}

function delayHover(){
	$('.kwicks li').hoverIntent(function(){
		hovering($(this));
	},function(){});
	
	}
	
function autoslide(){

	var count = parseInt($('.kwicks >li').length);
	
	setTimeout ( "firstPanelAnimation()", 1000 );
	
	var i = 1;
	setTimeout( function(){
		setTimeout ( "delayHover()", 5000 );
		$('ul.kwicks').everyTime(5000,'controlled', function() {

		var $currentLi = $('li',$(this)).eq(i);
		var $previous = $currentLi.prevAll();
		var $after= $currentLi.nextAll();
		
		$currentLi.trigger('mouseover');
		$('.direction-indicator', $currentLi).hide('slow');
		
		$('.direction-indicator', $previous).removeClass('after').addClass('previous');
		$('.direction-indicator', $after).removeClass('previous').addClass('after');
		
		if(i<count-1)
		{
			
			
			$('.description', $currentLi).animate({
			left:0
			}, 1000, 'easeOutSine');
 			
			i++;
		}
		else
		{	
			$('.description', $currentLi).animate({	
			left:0
			}, 1000, 'easeOutSine');
 
			i = 0;
		}
		hideInactivePanelDescriptions();
	});
	},1000); //end of setTimeout
	
}

