/**
 * Carrossel de imagens
 * 
 * @author: Thomaz Takashi Oda Toyama.
 * @retrun void
 */
 
(function($){
	$.fn.jcarrossel = function(options){
		var settings 	= $.extend({},$.fn.jcarrossel.defaults,options);
		var parent		= $(this);
		var li 			= $('li',parent);
		var images 		= $('img',parent);
		var imgArray	= new Array();
		var imgCount	= 0;
		var index 		= 0;
		var cssSize		= function(index,p,c){
			
			var image 	= imgArray[index];
			var iWidth	= image.width;
			var iHeight	= image.height;
			var width 	= Math.ceil(((parent.outerWidth() * p)/100) > iWidth) ? iWidth : ((parent.outerWidth() * p)/100);
			var height 	= Math.ceil((iHeight*width)/iWidth);
			var left 	= Math.ceil((parent.outerWidth() / 2) - (width / 2));
			var top	 	= Math.ceil((parent.outerHeight() / 2) - (height / 2));
			
			return $.extend({
					width 	: width
					,height	: height
					,top	: top
					,left	: left
			},{},c);
			
		};
		
		var mov = function(index){
			var obj 		= li.eq(index); 
			var movNext 	= (parent.outerWidth() - (cssSize(index,20).width + 5));
			var movPrev 	= 5;
			
			li.find('a').removeClass('lightwindow').find('img').removeClass('jc-destaque');
			
			obj.find('a').addClass('lightwindow').find('img:first').addClass('jc-destaque').css({'z-index':2,'text-align':'center'}).animate(cssSize(index,50,{opacity:'1.0'}),settings.speedy);
			
			if((index+1) <= (li.length-1))
				obj.next().find('img:first').css({'z-index':1}).animate(cssSize((index+1),20,{left:movNext,top:(parent.outerHeight()/2),opacity:'0.5'}),settings.speedy);
			if((index+2) <= (li.length-1))
				obj.next().next().find('img:first').animate(cssSize((index+2),10,{opacity:'0.0'}), function(){ li.eq(index+2).css({'z-index':0}); });
			
			if((index-1) >= 0)
				obj.prev().find('img:first').css({'z-index':1}).animate(cssSize((index-1),20,{left:movPrev,top:(parent.outerHeight()/2),opacity:'0.5'}),settings.speedy);
			if((index-2) >= 0)
				obj.prev().prev().find('img:first').animate(cssSize((index-2),10,{opacity:'0.0'}),settings.speedy,function(){ li.eq(index-2).css({'z-index':0}); });
			
			if(index < (li.length-1) ){ 
				$(settings.setLeft).css({background:'url(arquivos/87815/images/set_carrossel_left.png) no-repeat center'}).fadeIn('slow');
			 }else{
				$(settings.setLeft).css({background:'none'}).fadeOut('slow');	 
			};
		
		 	if(index > 0){ 
				$(settings.setRight).css({background:'url(arquivos/87815/images/set_carrossel_right.png) no-repeat center'}).fadeIn('slow');
			}else{
				$(settings.setRight).css({background:'none'}).fadeOut('slow');	 
			};
		};
		
		parent.css({position:'relative',visibility:'hidden'});
		
		var start = function(){
		
			li.click(function(){
				var i = $('li',parent).index(this);
				if(index != i){ 
					index = i; 
					mov(index);
					
					return false;
				}
				myLightWindow.activateWindow({
					href: $(this).find('a').attr('href'),
					title: $(this).find('a').attr('title')		 
				});
				return false;
			});
			
			$(settings.setLeft).click(function(){ if(index < (li.length-1) ){ index +=1; mov(index);} return false; })
			.mouseenter(function(){
				 if(index < (li.length-1) ){ 
					$(this).css({'background':'url(arquivos/87815/images/set_carrossel_left_hover.png) no-repeat center'});
				 }else{
					$(this).css({background:'none'});	 
				};
			}).mouseleave(function(){
				 if(index < (li.length-1) ){ 
					$(this).css({background:'url(arquivos/87815/images/set_carrossel_left.png) no-repeat center'});
				 }else{
					$(this).css({background:'none'});	 
				};
			});
			
			$(settings.setRight).click(function(){ if(index > 0){  index -=1; mov(index); } return false; })
			.mouseover(function(){
				 if(index > 0){ 
					$(this).css({background:'url(arquivos/87815/images/set_carrossel_right_hover.png) no-repeat center'});
				 }else{
					$(this).css({background:'none'});	 
				};
			}).mouseout(function(){
				 if(index > 0){ 
					$(this).css({background:'url(arquivos/87815/images/set_carrossel_right.png) no-repeat center'});
				 }else{
					$(this).css({background:'none'});	 
				};
			});
			
		}
		
		images.each(function(){
			var img = new Image();
			var imgLoad = $(img).load(function(){
				imgArray.push(this);
				if(imgCount == (images.length-1)){
					$('img',parent).css(cssSize(index,10,{'z-index':0,opacity:'0.0'}));
					parent.css({visibility:'visible',display:'none'}).fadeIn(settings.speedy,function(){
						start();
						mov(index);
					});
				};
				imgCount++;
			}); 
			imgLoad.attr({'src':$(this).attr('src')});
		});
	};
	$.fn.jcarrossel.defaults = {
		speedy		: 500
		,setLeft 	: '#cj-left'
		,setRight 	: '#cj-right'
	};
})(jQuery);
