
$(function(){
	// Remove the coda-slider-no-js class from the body
	$("body").removeClass("coda-slider-no-js");
	// Preloader

});


//Variable de control de Slider
var sliderCount = 1;
var panelToLoad=1;
var panelEstado='';
var autoHeightLoad=false;
var activeTabs=true;


function TabsCount()
{
    return $('#coda-slider-1').find(".panel").size();
}


//Pestaņa Actual
function CurrentTabs()
{
    var a= location.hash.slice(1);
    if (a!="")
    {
        return parseInt(location.hash.slice(1));
    }
    else
    {
        return 1;
    }
}


function SetActiveTabs(active)
{
    activeTabs=active;
}


//Panel de Inicio a presentar:
function SetPanelLoad(panelInicio)
{
    panelToLoad=  parseInt(panelInicio);
}

function SetAutoHeight(auto)
{
    autoHeightLoad=auto;
}


$.fn.codaSlider = function (settings) {
    settings = $.extend({
        autoHeight: autoHeightLoad,
        autoHeightEaseDuration: 1000,
        autoHeightEaseFunction: "easeInOutExpo",
        autoSlide: true,
        autoSlideInterval: 3000,
        autoSlideStopWhenClicked: true,
        crossLinking: true,
        dynamicArrows: false,
        dynamicArrowLeftText: "&#171;",
        dynamicArrowRightText: "&#187;",
        dynamicTabs: false,
        dynamicTabsAlign: "right",
        dynamicTabsPosition: "top",
        externalTriggerSelector: "a.xtrig",
        firstPanelToLoad: panelToLoad,
        panelTitleSelector: "h2.title",
        slideEaseDuration: 1000,
        slideEaseFunction: "easeInOutExpo",
        slideId:1
    }, settings);

    return this.each(function () {
            

        var slider = $(this);

        // If we need arrows
        if (settings.dynamicArrows) {
            slider.parent().addClass("arrows");
            slider.before('<div class="coda-nav-left" id="coda-nav-left-' + settings.slideId + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
            slider.after('<div class="coda-nav-right" id="coda-nav-right-' + settings.slideId + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
        };

        var panelWidth = slider.find(".panel").width();
        var panelCount = slider.find(".panel").size();
        var panelContainerWidth = panelWidth * panelCount;
        var navClicks = 0; // 

        // Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
        $('.panel', slider).wrapAll('<div class="panel-container"></div>');
        // Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
        $(".panel-container", slider).css({ width: panelContainerWidth });

        // Specify the current panel.
        // If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
        if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
            var currentPanel = parseInt(location.hash.slice(1));
            var offset = -(panelWidth * (currentPanel - 1));
            $('.panel-container', slider).css({ marginLeft: offset });
            // If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
        } else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
            var currentPanel = settings.firstPanelToLoad;
            var offset = -(panelWidth * (currentPanel - 1));
            $('.panel-container', slider).css({ marginLeft: offset });
            // Otherwise, we'll just set the current panel to 1...
        } else {
            var currentPanel = 1;
        };

        // Left arrow click
        $("#coda-nav-left-" + settings.slideId + " a").click(function () {

            if (currentPanel == 1) {
                location.hash = 1;
                return false;
            }




            navClicks++;
            if (currentPanel == 1) {
                offset = -(panelWidth * (panelCount - 1));
                alterPanelHeight(panelCount - 1);
                currentPanel = panelCount;

                slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('li:last a').addClass('current');

            } else {
                currentPanel -= 1;
                alterPanelHeight(currentPanel - 1);
                offset = -(panelWidth * (currentPanel - 1));
                slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().prev().find('a').addClass('current');
            };
            
            $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
            if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
            return false;
        });

        // Right arrow click
        $('#coda-nav-right-' + settings.slideId + ' a').click(function () {

            /*if (currentPanel == panelCount) {
                location.hash = panelCount;
                return false;
            }*/

            navClicks++;
            if (currentPanel == panelCount) {
                offset = 0;
                currentPanel = 1;
                alterPanelHeight(0);
                slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('a:eq(0)').addClass('current');
            } else {
                offset = -(panelWidth * currentPanel);
                alterPanelHeight(currentPanel);
                currentPanel += 1;

                slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().next().find('a').addClass('current');
               
            };
            $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
            if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
            return false;
        });




        //************************************************************
        //*************** Click en las pestaņas **********************
        //************************************************************
        $('#coda-nav-' + settings.slideId + ' a').each(function (z) {
            // What happens when a nav link is clicked
            $(this).bind("click", function () {

                //Action Tabs
                if (!activeTabs)
                    return;

                //Comprobamos que no este bloqueada la Tabs			
                var bacceso = '1';

                var control = document.getElementById(this.id);
                if (control != null) {
                    if (control.attributes["class"] != null) {
                        var cTexto = control.attributes["class"].value;
                        if (cTexto != null && cTexto == 'current_locked') {
                            bacceso = '0';
                        }
                    }
                }

                if (bacceso != '1')
                    return;

                navClicks++;
                $(this).addClass('current').parents('ul').find('a').not($(this)).removeClass('current');
                offset = -(panelWidth * z);
                alterPanelHeight(z);
                currentPanel = z + 1;
                $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
                if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified    


                $(".pnlEstado").each(function (indice) {
                    var control = $("#" + this.id);
                    if (control != null) {
                        if (z == indice) {
                            control.show("fast");
                        }
                        else {
                            control.hide("slow");
                        }
                    }
                });


            });
        });





        // External triggers (anywhere on the page)
        $(settings.externalTriggerSelector).each(function () {
            // Make sure this only affects the targeted slider
            if (settings.slideId == parseInt($(this).attr("rel").slice(12))) {
                $(this).bind("click", function () {



                    navClicks++;

                    targetPanel = parseInt($(this).attr("href").slice(1));
                    offset = -(panelWidth * (targetPanel - 1));
                    alterPanelHeight(targetPanel - 1);
                    currentPanel = targetPanel;
                    // Switch the current tab:
                    slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a').addClass('current');
                    // Slide
                    $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
                    if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
                });
            };
        });

        // Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
        if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
            $("#coda-nav-" + settings.slideId + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
            // If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
        } else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
            $("#coda-nav-" + settings.slideId + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("current");
            // Otherwise we must be loading Panel 1, so make the first tab the current one.
        } else {
            $("#coda-nav-" + settings.slideId + " a:eq(0)").addClass("current");
        };

        // Set the height of the first panel
        if (settings.autoHeight) {
            panelHeight = $('.panel:eq(' + (currentPanel - 1) + ')', slider).height();
            slider.css({ height: panelHeight });
        };

        // Trigger autoSlide
        if (settings.autoSlide) {
            slider.ready(function () {
                setTimeout(autoSlide, settings.autoSlideInterval);
            });
        };

        function alterPanelHeight(x) {
            if (settings.autoHeight) {
                panelHeight = $('.panel:eq(' + x + ')', slider).height()
                slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
            };
        };

        function autoSlide() {
            if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
                if (currentPanel == panelCount) {
                    var offset = 0;
                    currentPanel = 1;
                } else {
                    var offset = -(panelWidth * currentPanel);
                    currentPanel += 1;
                };
                alterPanelHeight(currentPanel - 1);
                // Switch the current tab:
                slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a').addClass('current');
                // Slide:
                $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
                setTimeout(autoSlide, settings.autoSlideInterval);
            };
        };

        // Kill the preloader
        $('.panel', slider).show().end().find("p.loading").remove();
        slider.removeClass("preload");

        sliderCount++;
    });
};
