(function($){
    var defaults = {
        auto: false,
        cont: false,
        speed: 600,

        slideCurrent: 0,
        slideNext: 0,
        slideCount: 0,
        animating: false,
        clicked: false,

        autoScrollTimeout: false,
        autoScrollPause: 10000
    };
    var options = {};
    var $this = null;
    var slidesData = [];

    $.fn.slideShow = function(sShowOpts){
        var sShowOpts;
        $this = $(this);
        options = $.extend(defaults, sShowOpts);

        loadSlides();
        if(options._ctrls.enable) buildControls();
        bindEvents();

        options.autoScrollTimeout = setTimeout(function(){$.fn.slideShow.changeSlide(false, 'next');}, options.autoScrollPause);
    };

    $.fn.slideShow.changeSlide = function(event, nIndex){
        var event,
            curIndex = 0,
            newIndex = 0;

        if(!event.data){
            curIndex = nIndex;
        }else{
            curIndex = event.data.newIndex;

            if(event.data.clicked) options.clicked = true;
        }

        if(options.animating) return;
        if(typeof curIndex == 'number') newIndex = curIndex;
        if(curIndex == 'next') newIndex = options.slideCurrent + 1;
        if(curIndex == 'prev') newIndex = options.slideCurrent - 1;
        if(options.slideCurrent == newIndex) return;

        if(newIndex > (options.slideCount-1))newIndex = 0;
        if(newIndex < 0)newIndex = (options.slideCount-1);

        options.slideNext = newIndex;
        animateChange(newIndex);
    };

    function animateChange(newIndex){
        var newIndex, curIndex = options.slideCurrent;
        options.animating = true;

        $('.slide-'+newIndex).css({'display':'block','z-index':'50'});

        updateButtonControl(options.slideCurrent, newIndex);
        updateControlText();
        $('.slide-'+curIndex).animate({opacity:0.0},{queue:false,duration:400,complete:function(){animateCleanup(newIndex);}});

        if(event.data.clicked) clearTimeout(options.autoScrollTimeout);
        if(!event.data.clicked) options.autoScrollTimeout = setTimeout(function(){$.fn.slideShow.changeSlide(false, 'next');}, options.autoScrollPause);
    };

    function animateCleanup(newIndex){
        var newIndex, curIndex = options.slideCurrent;

        $('.slide-'+curIndex).css({'display':'none','z-index':'50','opacity':'1.0'});
        $('.slide-'+newIndex).css({'display':'block','z-index':'100'});

        options.slideCurrent = newIndex;
        options.animating = false;
    };

    function loadSlides(){
        $(options.slideClass).each(function(){
            function _parseClassAttr(cAttr){
                var cAttr = cAttr.split(' ').slice(1).toString().split(',').slice(0);
                return {i:cAttr[0].split('-')[0],n:cAttr[0]};
            };

            var slideData = {};
            var slideClassData = _parseClassAttr($(this).attr('class'));

            slideData.index = slideClassData.i;
            slideData.cName = slideClassData.n;

            var slideText = $("input[name='"+slideClassData.n+"-text']").val().split('||');
            slideData.title = slideText[0];
            slideData.text = slideText[1];

            slidesData.push(slideData);
            ++options.slideCount;
        });
    };

    function buildControls(){
        var $ctrls = $(options._ctrls.sel),
            $ctrlsBtn = $(options._ctrls.btnSel),
            $ctrlsTxt = $(options._ctrls.txtSel);
            ctrlsHTML = []
            cB = 0;

        for(var cA=0;cA<options.slideCount;++cA){
            var ctrlsBtnClass = '';
            if(options.slideCurrent == cA) ctrlsBtnClass = 'cur';

            ctrlsHTML[++cB] = '<li><a id="slideShow-button-' + cA + '" href="javascript:void(0);" class="' + ctrlsBtnClass + '">';
            ctrlsHTML[++cB] = cA+1;
            ctrlsHTML[++cB] = '</a></li>';
        }

        $ctrlsBtn.append(ctrlsHTML.join(''));

        updateControlText();
    };

    function updateControlText(){
        $(options._ctrls.txtSel).empty();
        $(options._ctrls.txtSel).append('<span class="b">' + slidesData[options.slideNext].title + '</span> - ' + slidesData[options.slideNext].text);
    };

    function updateButtonControl(thisIndex, nextIndex){
        var thisIndex, nextIndex;
        $('#slideShow-button-'+thisIndex).removeClass('cur');
        $('#slideShow-button-'+nextIndex).addClass('cur');
    };

    function bindEvents(){
        if(options._ctrls.enable){
            for(var cA=0;cA<options.slideCount;++cA)$('#slideShow-button-'+cA).bind('click',{newIndex:cA,click:true},$.fn.slideShow.changeSlide);
            $('.arrow-left').bind('click',{newIndex:'prev',click:true},$.fn.slideShow.changeSlide);
            $('.arrow-right').bind('click',{newIndex:'next',click:true},$.fn.slideShow.changeSlide);
        }
    };
})(jQuery);
