// JavaScript Document

/*
	Author : Nick Cyrus Lemus Duque
	07 JUN 2010
*/

(function ($) {
	
	var $slidePulpo,
	property ,
	ul,
	slides,
	ident,
	cSlide,
	Interval,
	
	defaults = {
		active_class : 'activo',
		speed : 1000,
		sumX : 0,
		SumY: 0,
		autoPlay : true,
		speedPlay : 7000
	};
	
	$.fn.slidePulpo = function (options, callback) {
	 this.each(function(){ 
			ident = $.fn.rand();
		
			property = $.extend({}, defaults, options);		
			ul = $(this)
			slides = ul.children('li');
			/*slides.css({
					   'display':'inline-block'
					  });*/
			slides.eq(0).addClass(property.active_class);
			
			$.fn.navegacion();
		})
	}
	
	
	$.fn.navegacion =function(){
		
		var size_li = slides.eq(0).size();
		var ULsize = (size_li[0]*slides.length+size_li[0]);
		ul.css({
			   'width':ULsize,
			   'height':size_li[1],
			   'position':'relative'
			  });
			
		ul.wrap('<div id="'+ident+'"></div>')
		cSlide = $('#'+ident);
		
		
		ul.after('<div class="paginacion_slide" align="center"></div>');
		var paginacion_slide = cSlide.children('.paginacion_slide')
		for(i=0;i<slides.length;i++){
			paginacion_slide.append('<a href="#" class="slide_paginacion" rel="'+i+'"></a>')
		}
		
		paginacion_slide.wrapInner('<div class="contentPaginator" ></div>')
		
		var hp = $('#'+ident+' .paginacion_slide').size('h');
		cSlide.css({
				   'width':size_li[0],
				   'height':size_li[1]+(hp+40),
				   'overflow':'hidden'
				  });
		
		
		
		var control_nav = $('#'+ident+' .slide_paginacion');
			
			control_nav.eq(0).addClass('slide_activo');
			
			control_nav.each(function(){
			
				$(this).click(function(e){
					control_nav.removeClass('slide_activo')
					$(this).addClass('slide_activo');
					var por = this.rel;
					var size_li = slides.eq(0).size();
					left = parseInt((size_li[0]*por))+parseInt(property.sumX)
					ul.animate({
							   	left : '-'+left
							   },property.speed)
					
					e.preventDefault();
				})
				
				
			})
			
		if (property.autoPlay){
			
			Interval =	setInterval(function(){
					var actual;
				
					control_nav.each(function(){
						if ($(this).is(".slide_activo")){
							actual = this.rel
						}
					})

					if (actual<(control_nav.length)-1)
								control_nav.eq(parseInt(actual)+1).click();
							else
								control_nav.eq(0).click();
					
					
							
							
				},property.speedPlay)
			
		}
		
	
	}
	
	
	$.fn.size = function(c){
		var size = 0
		if (!c){
			size = Array($(this).width(),
						 $(this).height())
	
		}else{
			if (c=='w'){
				size = $(this).width()
			}else if (c=='h'){
				size = $(this).height()
			}
		}
		return size;
	}
	
	$.fn.rand = function(){
		return 'slide_'+(parseInt(Math.random()*10000000000000000));
	}
	
	
}(jQuery));

