/*
 *  Listings Javascripts
 *
 *  @version $Id$
 */

var fListingsBtnPrev    = 'premiumBtnsPrev';
var fListingsBtnNext    = 'premiumBtnsNext';
var fButtonsContainer   = $('premiumBtns');
var fSwitchContainer    = $('premiumSwitch');
var FeaturedSlideshowWrapper = 'FeaturedSlideshowWrapper';
var fSlideshowContent	= 'FeaturedSlideshowContent';
var fSlideshowMax       = 4;

var indicatorStart      = 1;
var indicatorCurrent    = indicatorStart;
var triggerType         = '';

var fSlides     = $(fSlideshowContent).select('div[class~=slide]');
var	triggers	= $('premiumNav').select('a.carousel-control', 'a.carousel-jumper');
var jumpStatus = 0;

var options		= {
					wheel : false,
					duration : 0.5,
					selectedClassName : '',
					afterMove: function(){setJumpStatus(1);}
					}

var fSlideshow = new Carousel('FeaturedSlideshowWrapper', $$('#FeaturedSlideshowContent [class~=slide]'), triggers, options);

function setJumpStatus(SET)
{
    jumpStatus = SET;
}

function watchJumpers()
{
    var targetSlide = 'LoopSlidesLink_' + indicatorCurrent;

    $$('#premiumNav a.carousel-control').each(function(s){
        s.observe('click', function(e){
            Event.stop(e);
            if ( jumpStatus == 1 ) {
                var jumperId = s.identify();
                if ( jumperId == fListingsBtnPrev ) {
                    indicatorCurrent--;
                }
                else {
                    indicatorCurrent++;
                }
                indicatorCurrent = Math.min(indicatorCurrent, fSlideshowMax);
                indicatorCurrent = Math.max(indicatorCurrent, indicatorStart);
                $(targetSlide).removeClassName('active');
                targetSlide = 'LoopSlidesLink_' + indicatorCurrent;
                //console.log("slide="+targetSlide);
                $(targetSlide).addClassName('active');
                setJumpStatus(0);
            }
        });
    });
}

function watchIndicators()
{
    triggers.each(function(s){

        s.observe('click', function(e){
            var target = $('LoopSlidesLink_'+indicatorCurrent);
            if ( s.hasClassName('carousel-control') ) {
                var triggerType = 'control';
                if ( s.identify() == fListingsBtnPrev ) indicatorCurrent--;
                else indicatorCurrent++;
                if ( indicatorCurrent < indicatorStart )
                    indicatorCurrent = indicatorStart;
                else if ( indicatorCurrent > fSlideshowMax )
                    indicatorCurrent = fSlideshowMax;
                //  clear previous target
                target.removeClassName('active');
                //  set new target
                target =  $('LoopSlidesLink_'+indicatorCurrent);
                //  highlight new target
                target.addClassName('active');
            } else {
                var triggerType = 'jumper';
                indicatorCurrent = s.identify().replace('LoopSlidesLink_','');
                console.log(indicatorCurrent);
            }
        });

    });
}


//  Run functions on DOM ready
document.observe('dom:loaded', function(){
    //watchIndicators();
    watchJumpers();
});
