// JavaScript Document
// author = Mauricio Llerena
// libreria de plugins
var rockandroll;
var stopRock;
var startRock;
(function($){
    /// slide de imágenes opciones de configuración, "width" = ancho del contenedor principal, #slide
    /// "auto" = true por defecto, false evita el desplazamiento automático
    $.fn.slideShow = function(opciones){
        var settings = {
            width:0,
            auto:true,
            carousel:true,
            top:0
        };
        $.extend(settings,opciones);
        this.each(function(){
            $(this).css("width",settings.width + "px");
            $(this).css("margin-top",settings.top + "px");
            var anchoSlideScroll = 0;
            anchoSlideScroll = settings.width - 66;
            $(this).find("#slideScroll").css("width",anchoSlideScroll.toString() + "px");
            $(this).find("#slideScroll").before("<div class='arrow'><a href='#' class='right'><img src='/static/img/recursos/left.png' border='0'/></a></div>");
            $(this).find("#slideScroll").after("<div class='arrow'><a href='#' class='left'><img src='/static/img/recursos/right.png' border='0'/></a></div>");
            var lista = new Array();
            var anchoRoller = 0;
            lista = $(this).find("#slideShow li");
            var width_li = $(this).find("#slideShow li").outerWidth();
            var left_value = width_li * - 1;
            anchoRoller = (lista.length * 139) + 66;
            $(this).find("#roller").css("width",(anchoRoller.toString()) + "px");
            for(i=0;i<lista.length;i++){
                $(lista[i]).find("img").attr("width","125");
                $(lista[i]).find("img").addClass("pic");
                var titulo = $(lista[i]).attr("title");
                $(lista[i]).find("img").after("<div style='clear: both;'></div><div class='title'>" + titulo + "</div>").before("<div class='shadow'><img src='/static/img/recursos/sombra.png' width='138'/></div>");
            }
            var i = 1;
            $("div.arrow a").live("click",function(e){
                var clase = $(this).attr("class");
                var posSlideUl = $("#slideShow").position();
                if(clase == "left"){
                    if(settings.carousel){
                        lista = $("#slideShow li");
                        anchoRoller = (lista.length * 139) + 66;
                        $("#roller").css("width",(anchoRoller.toString()) + "px");
                        var left_indent = parseInt($("#slideShow").css('left')) - width_li;
                        $("#slideShow").animate({
                            "left":"-=139"
                        },500,function(){
                            $("#slideShow li:last").after($("#slideShow li:first"));
                            $("#slideShow").css({
                                'left':"0"
                            });
                        });
                    }else{
                        var position = -anchoRoller + settings.width;
                        if(posSlideUl.left <= position.toString()){

                        }else{
                            $("#slideShow").animate({
                                "left":"-=139"
                            });
                        }
                    }
                }else{
                    if(settings.carousel){
                        lista = $("#slideShow li");
                        anchoRoller = (lista.length * 139) + 66;
                        $("#roller").css("width",(anchoRoller.toString()) + "px");
                        var left_indent = parseInt($("#slideShow").css('left')) + width_li;
                        $("#slideShow").animate({
                            "left":"+=139"
                        },500,function(){
                            $("#slideShow li:first").before($("#slideShow li:last"));
                            $("#slideShow").css({
                                'left':left_value
                            });
                        });
                    }else{
                        if(posSlideUl.left == "0"){

                        }else{
                            $("#slideShow").animate({
                                "left":"+=139"
                            });
                        }
                    }

                }
                return false;
                e.preventDefault();
            });
            if(settings.auto){
                if(settings.carousel){
                    rockandroll = setInterval(function(){
                        lista = $("#slideShow li");
                        anchoRoller = (lista.length * 139) + 66;
                        $("#roller").css("width",(anchoRoller.toString()) + "px");
                        var left_indent = parseInt($("#slideShow").css('left')) - width_li;
                        console.log(left_indent);
                        $("#slideShow").animate({
                            "left":"-=139"
                        },500,function(){
                            $("#slideShow").css({
                                'left':"0"
                            });
                            $("#slideShow li:last").after($("#slideShow li:first"));
                        });
                    },5000);
                }else{
                    rockandroll = setInterval(function(){
                        $("#slideShow").animate({
                           "left":"-=139"
                        });
                    },5000);
                }
            }
            stopRock = function stopRockAndRoll(){
                clearInterval(rockandroll);
            };
            startRock = function startRockAndRoll(){
                rockandroll = setInterval(function(){
                    lista = $("#slideShow li");
                    anchoRoller = (lista.length * 139) + 66;
                    $("#roller").css("width",(anchoRoller.toString()) + "px");
                    var left_indent = parseInt($("#slideShow").css('left')) - width_li;
                    console.log(left_indent);
                    $("#slideShow").animate({
                        "left":"-=139"
                    },500,function(){
                        $("#slideShow").css({
                            'left':"0"
                        });
                        $("#slideShow li:last").after($("#slideShow li:first"));
                    });
                },5000);
            };                        
        });
        return this;
    }
})(jQuery);

