/*********************************************/
/**											**/
/**              sc-showcase                **/
/**											**/
/**-----------------------------------------**/
/**											**/
/** v: 		1.0	/ 02-Set-2011				**/
/**											**/
/** por: 	rogério gonçalves				**/
/**			(rogerio@digitale.com.br)		**/
/**											**/
/** desc:	bib js para vitrines web		**/
/**			(requer jquery)					**/
/**											**/
/** to do:	.outros formatos de animação	**/
/**			.otimização de código			**/
/**											**/
/**	(c):	creative commons				**/
/**											**/
/*********************************************/

var sc_Config = Object;

$.fn.showcaseSetInteractionsTo = function( bool ){
	
	return;
	
	if( bool ){

		$( this ).find( "a.prev" ).bind( "click", function(){ $( this ).parent().find( "ul" ).showcasePrev(); } );
		$( this ).find( "a.next" ).bind( "click", function(){ $( this ).parent().find( "ul" ).showcaseNext(); } );
		$( this ).find( "p.container a" ).bind( "click", function(){ $( this ).showcaseGoToMe(); } );
		
	} else{
		
		$( this ).find( "a.prev" ).unbind( "click" );
		$( this ).find( "a.next" ).unbind( "click" );
		$( this ).find( "p.container a" ).unbind( "click" );
		
	}
	
}

$.fn.showcaseSetMouseOverTo = function( bool ){
	
	sc_Config.mouseover = bool;
	
	if( bool ){
		
		clearInterval( sc_Config.timer );
		
	} else{
	
		sc_Config.timer = setInterval( function(){ $( document ).showcaseAuto(); }, ( sc_Config.td ) );
		
	}
		
}

$.fn.showcaseAuto = function(){
	
	if( sc_Config.auto && !sc_Config.mouseover ){
		
		$( "div.sc_showcase" ).showcaseNext();
	
	}
	
}

$.fn.showcasePrev = function(){
	
	var prev = ( sc_Config.current == 1 ? sc_Config.n : sc_Config.current - 1 );
	
	$( this ).parent().showcaseGoTo( prev );
	
}

$.fn.showcaseNext = function(){
	
	var next = ( sc_Config.current == sc_Config.n ? 1 : sc_Config.current + 1 );
	
	$( this ).parent().showcaseGoTo( next );
	
}

$.fn.showcaseGoToMe = function(){

	var n = parseInt( $( this ).attr( "rel" ) );
	
	if( n == sc_Config.current ) return;
	
	$( this ).parent().parent().showcaseGoTo( n );
	
}

$.fn.showcaseGoTo = function( n ){
	
	if( n == sc_Config.current ) return;
	
	$( this ).parent().parent().showcaseSetInteractionsTo( false );
									
	$( this ).parent().parent().find( "p.container a.bull_" + sc_Config.current ).removeClass( "bull_current" );
	
	$( this ).parent().parent().find( "a.next" ).animate( { right:( sc_Config.w_nav * -1 ) }, sc_Config.ts, function(){} );
	$( this ).parent().parent().find( "a.prev" ).animate( { left:( sc_Config.w_nav * -1 ) }, sc_Config.ts, function(){} );
	
	$( this ).parent().parent().find( "p.container" ).animate( { bottom:-50 }, sc_Config.ts, function(){
		
		$( this ).parent().parent().find( "ul" ).animate( { left: ( ( n - 1 ) * sc_Config.w ) * -1 }, sc_Config.ts, 
			
			function(){
				
				sc_Config.current = n;		
				
				$( this ).parent().parent().find( "p.container a.bull_" + sc_Config.current ).addClass( "bull_current" );
				
				$( this ).parent().parent().find( "a.next" ).animate( { right:0 }, sc_Config.ts, function(){} );
				$( this ).parent().parent().find( "a.prev" ).animate( { left:0 }, sc_Config.ts, function(){} );
				
				$( this ).parent().parent().find( "p.container" ).animate( { bottom:sc_Config.b_cont }, sc_Config.ts, function(){} );
				
				$( this ).parent().showcaseSetInteractionsTo( true );																	
											
			}
			
		);
		
	});	
	
}

$.fn.showcaseInstall = function( param ){

	// cria um objeto com as configurações //

	for( var atual in param ){
		
		eval( "sc_Config." + atual + "=" + param[atual] );
		
	}
	
	sc_Config.n = parseInt( $( this ).find( "ul li" ).length );
	
	sc_Config.current = ( sc_Config.rnd ? Math.floor( Math.random() * ( sc_Config.n ) ) + 1 : 1 );
		
	// validações e padrões //
	
	if( isNaN( sc_Config.n ) || sc_Config.n <= 0 ) return;
	
	sc_Config.ts = ( sc_Config.ts == undefined ? 500 : sc_Config.ts );
	sc_Config.td = ( sc_Config.td == undefined ? 5000 : sc_Config.td );
	sc_Config.rnd = ( sc_Config.rnd == undefined ? false : sc_Config.rnd );
	sc_Config.auto = ( sc_Config.auto == undefined ? true : sc_Config.auto );
	sc_Config.w_nav = ( sc_Config.w_nav == undefined ? 50 : sc_Config.w_nav );
	sc_Config.b_cont = ( sc_Config.b_cont == undefined ? 0 : sc_Config.b_cont );
	
	// configura //
		
	for( var x = 1; x <= sc_Config.n; x++ ){
	
		$( this ).find( "p.container" ).append( "<a class='bull bull_" + x + "' rel='" + x + "' href='javascript:void(0);'>&bull;</a>" );
		$( this ).find( "p.container a.bull_" + x ).bind( "click", function(){ $( this ).showcaseGoToMe() } );
							
	}
					
	$( this ).addClass( "sc_showcase" );
				
	if( sc_Config.w > 0 ) $( this ).css( "width", sc_Config.w );
	if( sc_Config.h > 0 ) $( this ).css( "height", sc_Config.h );
	
	$( this ).find( "ul" ).css( "width", ( sc_Config.w * sc_Config.n ) );
	$( this ).find( "ul" ).css( "height", sc_Config.h );
	$( this ).find( "ul" ).css( "left", ( sc_Config.w * ( sc_Config.current - 1 ) * -1 ) );

	$( this ).find( "ul li" ).css( "width", sc_Config.w );
	$( this ).find( "ul li" ).css( "height", sc_Config.h );

	$( this ).find( "ul li a" ).css( "display", "block" );
	$( this ).find( "ul li a" ).css( "width", sc_Config.w );
	$( this ).find( "ul li a" ).css( "height", sc_Config.h );
	
	$( this ).find( "ul li a img" ).css( "display", "block" );
	$( this ).find( "ul li a img" ).css( "border", "0" );
	$( this ).find( "ul li a img" ).css( "width", sc_Config.w );
	$( this ).find( "ul li a img" ).css( "height", sc_Config.h );
	
	$( this ).find( "p.container" ).css( "width", sc_Config.w );
	$( this ).find( "p.container" ).css( "bottom", sc_Config.b_cont );
	
	$( this ).find( "p.container a.bull_" + sc_Config.current ).addClass( "bull_current" );
	
	$( this ).find( "a.next, a.prev" ).css( "width", sc_Config.w_nav );
	
	if( sc_Config.n == 1 ){
		
		$( this ).find( "p.container" ).css( "display", "none" );
		$( this ).find( "a.prev" ).css( "display", "none" );
		$( this ).find( "a.next" ).css( "display", "none" );
		
		return;
		
	}		

	// bind navegacao //
	
	$( this ).find( "ul" ).bind( "mouseover", function(){ $( document ).showcaseSetMouseOverTo( true ); } );
	$( this ).find( "ul" ).bind( "mouseout", function(){ $( document ).showcaseSetMouseOverTo( false ); } );

	$( this ).find( "p.container" ).bind( "mouseover", function(){ $( document ).showcaseSetMouseOverTo( true ); } );
	$( this ).find( "p.container" ).bind( "mouseout", function(){ $( document ).showcaseSetMouseOverTo( false ); } );

	$( this ).find( "a.next" ).bind( "mouseover", function(){ $( document ).showcaseSetMouseOverTo( true ); } );
	$( this ).find( "a.next" ).bind( "mouseout", function(){ $( document ).showcaseSetMouseOverTo( false ); } );

	$( this ).find( "a.prev" ).bind( "mouseover", function(){ $( document ).showcaseSetMouseOverTo( true ); } );
	$( this ).find( "a.prev" ).bind( "mouseout", function(){ $( document ).showcaseSetMouseOverTo( false ); } );
	
	$( this ).find( "a.prev" ).bind( "click", function(){ $( this ).parent().find( "ul" ).showcasePrev() } );
	$( this ).find( "a.next" ).bind( "click", function(){ $( this ).parent().find( "ul" ).showcaseNext() } );

	$( this ).find( "a.prev" ).attr( "href", "javascript:void(0);" );
	$( this ).find( "a.next" ).attr( "href", "javascript:void(0);" );
	
	// auto //
					
	if( sc_Config.auto ){
	
		sc_Config.timer = setInterval( function(){ $( document ).showcaseAuto(); }, ( sc_Config.td ) );
		
	}
	
}
