


(function($) {

    $.accordionSlider = {
	defaults: {
	    onEvent: 'mouseover',	// Slide animation trigger event - click/mouseover
	    isAutoScroll: false,
	    sliderFullWidth: 960,	// Slider width
	    slideWidth: 700,		// Slide width
	    slideHeight: 400,
	    slideClosedWidth: 70,	// Closed state width - this option is computed automatically
	    selSlide: 0,		// selected slide
	    noSlides: 0,		// number of slides - auto detected
	    autoScrollTimer: 0,		// the duration after the slider changes the slide automatically (in seconds)
	    sliderContainer: '',
	    animSpeed: 1000,
	    animType: 'easeOutQuint',
	    borderColor: '#222',
	    borderThickness: '0px',
	    padding: 20,
	    descriptionOpacity: 0.7,
	    descriptionAnimSpeed: 800,
	    descriptionHeight: 100,
	    markSelected: true,
	    markedOpacity: 1,
	    markAll: false,
	    displayShort: true,
	    blurUnselectedSlides: false,
	    blockerOpacity: 0.4,
	    elasticMode: false,
	    slideHasShadow: true
	}
    };


    $.fn.extend({
	accordionSlider:function(config) {
	    var config = $.extend({}, $.accordionSlider.defaults, config);
	    config.sliderContainer = this.attr('id');
	    initializeSlider(config);
	    return this;
	}
    });


    function initializeSlider(config) {
	var list = $('#' + config.sliderContainer + ' li');
	config.noSlides = list.length;
	config.slideClosedWidth = Math.floor( (config.sliderFullWidth - config.slideWidth) / (config.noSlides - 1) );

	
	list.css('width', config.slideClosedWidth);
	list.eq(config.selSlide).css('width', config.slideWidth);
	list.find('span.text').css('display', 'none');

	/*
	 * Container presets */

	$('#'+ config.sliderContainer).css({
	    width:  config.sliderFullWidth + 'px',
	    height: config.slideHeight + 'px'
	});
	

	list.each(function(curInd) {
//	    var curInd = list.index($(this));
	    var pos = curInd * config.slideClosedWidth;
	    if ( config.selSlide < curInd ) {
		pos = (curInd - 1) * config.slideClosedWidth + config.slideWidth;
	    }

	    if ( config.slideHasShadow === true ) {
		$(this).prepend('<div class="shadow"></div>');
		$(this).find('.shadow').css({
		    marginRight: config.padding + 'px'
		});
	    }
	    

	    /*
	     * Presetting some attributes */

	    $(this).css({
		left: pos + 'px',
		zIndex: (curInd + 1),
		height: config.slideHeight,
		position: 'absolute',
		borderLeft:  config.borderThickness + ' solid ' + config.borderColor,
		paddingLeft: config.padding + 'px'
	    });
	    $(this).find('a').css({
		width: config.slideWidth + 'px',
		marginLeft: '-'+config.padding + 'px',
		height: config.descriptionHeight
	    });

	    if ( config.elasticMode === true ) {
		setToEqualWidths(curInd, $(this), config);
	    }
	    

	    /*
	     * Adding the opacity blocker to the unselected slides */
	    
	    if ( config.blurUnselectedSlides === true ) {
		$(this).append('<span class="blocker"></span>');
	    }

	    if ( curInd != config.selSlide || config.elasticMode === true ) {
		shortDescription($(this), config);
	    } else {
		fullDescription($(this), config);
	    }
	    

	    /*
	     * Binding to event */
	    

	    $(this).bind(config.onEvent, function(event) {
		var index = list.index($(this));

		var thisSlide = $(this),
		leftDist = index * config.slideClosedWidth;

		thisSlide.stop().animate({
		    width: config.slideWidth,
		    left: leftDist
		}, config.animSpeed, config.animType);

		if ( index != config.selSlide ) {
		    shortDescription(list.eq(config.selSlide), config);
		    fullDescription($(this), config);
		}

		list.each(function(j){
		    if ( index != j ) {

			var pos = j * config.slideClosedWidth;
			if ( index < j ) {
			    pos = (j - 1) * config.slideClosedWidth + config.slideWidth;
			}

			$(this).stop().animate({
			    width: config.slideClosedWidth,
			    left: pos + 'px'
			}, config.animSpeed, config.animType);
		    }
		});

		config.selSlide = index;
	    });

	});

	if ( config.elasticMode === true ) {
		list.parent().bind('mouseleave', function(event) {
		    list.each(function(k) { 
			setToEqualWidths(k, $(this), config);
			shortDescription($(this), config);
	    });
	  });
	}

    }


    /*
     * Functions used to set the description on the slides */

    function shortDescription(oldSlide, config) {
	var generalDesc = oldSlide.find('a').attr('title');
	var secondDesc = oldSlide.find('a').attr('rel');
	if ( config.displayShort === false ) {
	    generalDesc = '';
	}
	var holder = '<span class="background"></span><span class="description short">' + generalDesc + '</span>';
	if ( secondDesc != '' ) {
	    holder += '<span class="description second-desc">'+ secondDesc +'</span>';
	}
	
	oldSlide.find('a').empty().append(holder).find('span.description').hide().fadeIn(config.descriptionAnimSpeed);
	if ( config.blurUnselectedSlides === true ) {
	    oldSlide.find('span.blocker').css('opacity', config.blockerOpacity).fadeIn();
	}
	if ( config.markAll === false ) {
	    oldSlide.find('a span.background').css({
		opacity: config.descriptionOpacity
	    });
	}
    }
    function fullDescription(thisSlide, config) {
	var description = thisSlide.find('span').html();
	var title = thisSlide.find('a:first').attr('title');
	var holder = '<span class="background"></span><span class="description"><strong>' + title + '</strong>'  + description + '</span>';
	thisSlide.find('a').empty().append(holder).find('span.description').hide().fadeIn(config.descriptionAnimSpeed);
	
	if ( config.markSelected === false && config.markAll === false ) {
	    thisSlide.find('a span.background').css({
		opacity: config.descriptionOpacity
	    });
	} else {
	    thisSlide.find('a span.background').css({
		opacity: config.markedOpacity
	    });
	}
	if ( config.blurUnselectedSlides === true ) {
	    thisSlide.find('span.blocker').fadeOut();
	}
    }

    function setToEqualWidths(index, thisSlide, config) {
	config.selSlide = -1;
	var slidePercentage = 100 / config.noSlides;
	var totalWidth = ((config.noSlides - 1) * config.slideClosedWidth) + config.slideWidth;
	var width = slidePercentage * totalWidth / 100;
	thisSlide.stop().animate({
	    width: width + 'px',
	    left: index * width + 'px'
	}, config.animSpeed, config.animType);
    }



    function startSlider(config) {
	if ( config.hasNavBar == true ) {
	    createNavBar(config);
	}
	var elem = $("#slider-nav").find("li a");
	elem.addClass('png');
	elem.eq(config.selSlide).addClass(config.cssSelectedClass);


	if ( config.autoScrollTimer > 0 ) {
	    setInterval(function() {
		}, config.autoScrollTimer * 1000);
	}
    }


    function nextSlide(config) {
	var prev = config.selSlide;
	var next = config.selSlide + 1;
	if ( next > config.noSlides - 1 ) {
	    next = 0;
	    prev = config.noSlides - 1;
	}
	onClick(config, prev, next);
	config.selSlide = next;
    }

    function prevSlide(config) {
	var prev = config.selSlide;
	var next = config.selSlide - 1;
	if ( next < 0 ) {
	    next = config.noSlides - 1;
	    prev = 0;
	}
	onClick(config, prev, next);
	config.selSlide = next;
    }


    function onClick(config, prev, next) {

	if ( 'HORIZONTAL' == config.sliderType ) {
	    animate(next, config.slideSpeed, config);
	} else {
	    animateFade(next, config.slideSpeed, config);
	}
	var elem = $("#slider-nav").find("li a");
	elem.eq(prev).removeClass(config.cssSelectedClass);
	elem.eq(next).addClass(config.cssSelectedClass);
    }


})(jQuery);



