﻿/**
* jQuery.fn.emiSlider
* Fecha: Mayo 2011
*
* Autor Emilio Arcioni
* Version 1.0
* Basado en DualSlider by Rob Phillips
*
* Requerimientos:
* jquery.1.3.2.js - http://jquery.com/
* jquery.easing.1.3.js - http://gsgd.co.uk/sandbox/jquery/easing/
*
**/


(function($) {

    $.fn.emiSlider = function(options) {
		
        var defaults = {
            easing: 'easeOutBack',
            duration: 1000,
			next: ".next",
			previous: ".previous",
			carousel: ".backgrounds",
			galeria: ".details",
			auto: false
        };

        var options = $.extend(defaults, options);
		var interval ="";
        this.each(function() {
			
            var obj = $(this);
			var n;
			var dist;
			var accion;
            var carousel;
			var cant;
			var itemsCarousel = $(options.carousel,obj);
			var itemsGaleria = $(options.galeria, obj);
			
            var carouselTotal = $(options.carousel, obj).children().length;
            var carouselwidth = $(itemsCarousel).children(":first-child").outerWidth();
			var carouselheight = $(itemsCarousel).children(":first-child").outerHeight();
            var detailWidth = $(itemsGaleria).children(":first-child").outerWidth();
			var detailHeight = $(itemsGaleria).children(":first-child").outerHeight();
            var locked = false;
		
			

			$(itemsGaleria).wrap('<div style="overflow:hidden; width:'+carouselwidth+'px;height:'+detailHeight+'px; "></div>');
			$(itemsCarousel).wrap('<div style="overflow:hidden; width:'+carouselwidth+'px; height:'+carouselheight+'px;"></div>');
			
			$(itemsGaleria).children().wrap('<a href="#"></a>');
			
			$(itemsGaleria).append( $(itemsGaleria).children(":first-child") );

			
            $(itemsCarousel).css("width", ((carouselTotal*2) * carouselwidth) + 100 + "px");
            $(itemsGaleria).css("width", ((carouselTotal*2) * detailWidth) + 100 + "px");

			//$(itemsCarousel).find("img").show();
			$(itemsCarousel).find("img").each(function(){
				$(this).show();
			});
			
			$("img",$(itemsGaleria).children()).show();
            
            $(options.next, obj).click(function() {
                sig();
				automatico();
            });
            
            $(options.previous, obj).click(function() {
                carouselPage("prev",1);
                lock();
				automatico();
            });
			$(itemsGaleria).children().click(function() {
											   
				n=$(this).index()+1;
				dist=n/carouselTotal;
				
				if(dist>0.5){
					accion="prev";
					n=carouselTotal-n;
				}else{
					accion="next";	
				}

				carouselPage(accion,n);
				lock();
				
			});

            function lock() {
                locked = true;
            }

            function unLock() {
                locked = false;
            }
			
			function automatico(){
				if(options.auto){
					clearInterval(interval);
					interval=setInterval(sig,5000);
				}
			}
			
			function sig(){
				carouselPage("next",1);
                lock();
			}
			automatico();
			
			function adjust(accion,cant) {
				
				if(accion=="next"){
					for(i=0;i<cant;i++){
						$(itemsCarousel).children(":first-child").remove();
						$(itemsGaleria).children(":first-child",obj).remove();
						
					}
					
				}
				
				if(accion=="prev"){
					for(i=0;i<cant;i++){
						$(itemsCarousel).children(":last-child").remove();
						$(itemsGaleria).children(":last-child",obj).remove();
					}
				}
				
				$(itemsCarousel).css("margin-left","0px");
				$(itemsGaleria).css("margin-left","0px");

            }

            function carouselPage(accion,cant) {
				if (locked != true) {
                   
					if (accion=="next"){
						for(i=1;i<=cant;i++){
							$(itemsCarousel).append( $(itemsCarousel).children(":nth-child("+(i)+")").clone() );	
							$(itemsGaleria).append( $(itemsGaleria).children(":nth-child("+(i)+")").clone(true) );	
						}
						
						newPage = -carouselwidth * cant;
						newPageDetail = -detailWidth * cant;

					}
					if (accion=="prev"){
						
						for(i=carouselTotal;i>carouselTotal-cant;i--){
							$(itemsCarousel).prepend( $(itemsCarousel).children(":nth-child("+(carouselTotal)+")").clone() );
							$(itemsGaleria).prepend( $(itemsGaleria).children(":nth-child("+(carouselTotal)+")").clone(true) );
							
						}
						
						$(itemsCarousel).css("margin-left","-" + (carouselwidth*cant) + "px");
						$(itemsGaleria).css("margin-left","-" + (detailWidth*cant) + "px");
						newPage = 0;
						newPageDetail = 0;
					}
                    					
					
                    $(itemsCarousel).animate({
                        marginLeft: newPage
                    }, {
                        "duration": options.duration, "easing": options.easing,
                        complete: function() {
							if(itemsGaleria.get()==""){
								adjust(accion,cant);
								unLock();
							}
                        }
                    });
					
					$(itemsGaleria).animate({
						marginLeft: newPageDetail
					}, {
						"duration": options.duration, "easing": options.easing,
						complete: function() {
							if(itemsGaleria.get()!=""){
								adjust(accion,cant);
								unLock();
							}
						}
					});	
                }
            }

        });

    };

})(jQuery);
