/**
 * @brief Simple slideshow
 * @author Mikheev Rostislav hacenator@gmail.com
 * @date 9 Feb 2011
 */
jQuery(document).ready(function() {
    var display = jQuery('#slideshow ul#slides');

    // mode
    // var mode = 'slide';
    var mode = 'fade';

    // calculate carusel width
    var elements = jQuery(display).find('li');
    var elementsCount = elements.length;
    var elementsSize = parseInt(jQuery(elements).css('width'));
    var maxSize = (elementsCount * elementsSize);

    var reverseSpeed = maxSize / 5;
    var slideSpeed = elementsSize / 2;

    // disable image dragging
    jQuery(display).find('img').each(
    function() {
        jQuery(this).mousedown(function (event) { event.preventDefault(); });
    });

    // show
    jQuery(display).css('width', maxSize + 40).fadeIn(2000); // add 40px back zone after

    var autoShow = 0;
    //var autoShowDelay = 10 * 1000;
    var autoShowDelay = 3000;

    // opacity mode
    if (mode == 'fade') {
        var pos = jQuery(display).offset();

        jQuery(elements).each(function(index) {
            var params = {
                'position': 'absolute',
                'left': pos.x,
                'top': pos.y
            };

            if (index != 0) {
                jQuery(this).css(params).fadeOut(0);
            } else {
                jQuery(this).css(params);
            }


        });
    }

    // auto Show function
    function autoShowStop() {
        clearInterval(autoShow);
        autoShow = 0;
    }

    function autoShowStart() {
        if (autoShow != 0)
           autoShowStop();
        autoShow = setInterval(showNext, autoShowDelay);
    }

    function autoShowRestart() {
        autoShowStop();
        autoShowStart();
    }

    // show function
    showNext = function() {
        show(true);
    }

    showPrev = function() {
        show(false);
    }

    show = function(next) {
        if (next == undefined)
            next = true;

        autoShowStop();

        jQuery(display).stop(true, true);

        if (mode == 'slide') {
            var margin = parseInt(jQuery(display).css('margin-left'));
            var current =  Math.round(-margin / elementsSize) + 1;

            // show first
            if (next && current >= elementsCount) {
                jQuery(display).animate({'margin-left': '0'}, reverseSpeed);
                current = 1;
            // slide last
            } else if (!next && current == 1 ) {
                jQuery(display).animate({'margin-left': -(maxSize - elementsSize)}, reverseSpeed);
                current = elementsCount;
            // show next/prev
            } else {
                if (next) {
                    jQuery(display).animate({'margin-left': '-='+elementsSize}, slideSpeed);
                } else {
                    jQuery(display).animate({'margin-left': '+='+elementsSize}, slideSpeed);
                }
            }
        } else if (mode == 'fade') {
            var current =  0;
            jQuery(elements).each(function(index) {
                if (jQuery(this).css('display') != 'none') {
                    current = index;
                    return false;
                }
            });

            var nextSlide = (next ?
                    (current >= elementsCount - 1 ? 0 : current + 1 ) :
                    (current == 0 ? elementsCount - 1 : current - 1));

            var from = elements[current];
            var to = elements[nextSlide]

            //alert(current + ' -> ' + nextSlide);

            jQuery(to).css('z-index:', '1').fadeIn(slideSpeed);
            jQuery(from).css('z-index:', '0').fadeOut(slideSpeed, function() {  });            
        }

        autoShowRestart();
    }

    autoShowStart();

    // event binding
    jQuery('#slideshow #next').click(showNext);
    jQuery('#slideshow #prev').click(showPrev);
    
    // madskills :)
    if (mode == 'slide') {
		var drag = false;
		var dragX = 0;
		jQuery('#slideshow').mousedown(function(event) {
			drag = true;
			dragX = event.pageX;

			autoShowStop();
		});
		//
		upAndOut = function(event) {
			drag = false;
			dragX = 0;

			var origMargin = parseInt(jQuery(display).css('margin-left'));
			var id = origMargin / elementsSize;
			var whole = Math.round(id);
			var margin = elementsSize * whole;

			jQuery(display).animate({'margin-left': margin}, 250, function() { autoShowStart(); });
		}
		jQuery('#slideshow').mouseup(function(event) {
			upAndOut(event);
		});
		jQuery('#slideshow').mousemove(function(event) {
			if (drag) {
				var x = event.pageX;
				var delta = x - dragX;
				dragX = x;

				var margin = parseInt(jQuery(display).css('margin-left'));
				margin += delta;

				if (margin > -(maxSize - elementsSize + 20) && margin < 20)
					jQuery(display).css('margin-left', margin);
			}
		});
		jQuery('#slideshow').hover(
			function() {},
			function(event) {
				upAndOut();
			}
		);
	}
});
