﻿/// <reference path="jquery-1.4.3.min.js" />
/// <reference path="xtn-1.0.js" />
(function ($) {

    var undefined;

    $.maxHeight = function (selector) {
        var currentTallest = 0;
        $(selector).children().each(function () {
            if ($(this).height() > currentTallest) { currentTallest = $(this).outerHeight(); }
        });
        return currentTallest;
    };

    xtn.fn.email = function (e) {
        if (e == '') return;

        var email = e.indexOf('@') > -1 ? e : e + "@swissolympic.ch";
        document.write("<a href='mailto:" + email + "'>" + email + "</a>");
    };

    // scrollable plugin for jquery, Author: tch
    $.fn.scrollable = function (settings) {

        // defaults
        var config = {
            height: undefined,
            cssClass: '',
            autoPlay: true,
            speeds: {
                scroll: 'slow', /* the time from moving ltr, rtl */
                autoplay: 3500,
                timeout: 7000
            },
            offset: {
                left: 0,
                right: 0
            },
            arrows: false,
            events: {
                onBeforeSlide: function () { },
                onAfterSlide: function () { },
                onVisible: function () { },
                onHide: function () { }
            }
        };

        if (settings) $.extend(config, settings);

        var fn = {
            slide: function (context, config) {
                var currentPage = parseInt(context.data('currentPage')),
                    viewPort = parseInt(context.data('viewPort'))
                pages = parseInt(context.data('pages'));

                if (currentPage + 1 >= pages) {
                    if (config.arrows) {
                        $("a.arrow-container-left", context.parent()).addClass("blocked");
                        $("a.arrow-container-right", context.parent()).removeClass("blocked");
                    }

                    var startPos = parseInt(context.data('startPos'));
                    xtn.fn.log("[ui-slider] reset: " + ($("li:first", context).position().left - startPos) * -1);
                    this.move(context, config, '+=', ($("li:first", context).position().left - startPos) * -1, 0);
                } else {
                    if (config.arrows) {
                        $("a.arrow-container-right", context.parent()).removeClass("blocked");
                    }
                    this.moveLeft(context, config);
                }
            },
            moveLeft: function (context, config) {
                var currentPage = parseInt(context.data('currentPage')),
					pages = parseInt(context.data('pages')),
					viewPort = parseInt(context.data('viewPort')),
                    lastSlide = parseInt(context.data('lastSlide'));

                xtn.fn.log("[ui-slider] left");

                if (currentPage + 2 >= pages && config.arrows) {
                    $("a.arrow-container-right", context.parent()).addClass("blocked");
                }

                if (currentPage + 1 < pages) {
                    if (config.arrows) {
                        $("a.arrow-container-left", context.parent()).removeClass("blocked");
                    }

                    var scroll = viewPort;

                    if (currentPage == pages - 2 /* compensation */ && lastSlide > 0) {
                        var gt = $("li", context).length - lastSlide; // because operator should be >=
                        scroll = 0;
                        $("li", context).each(function (indx) { if (indx >= gt) { scroll += $(this).outerWidth(); } });
                    }

                    this.move(context, config, '-=', scroll, currentPage + 1);
                }
            },
            moveRight: function (context, config) {
                var currentPage = parseInt(context.data('currentPage')),
					pages = parseInt(context.data('pages')),
					viewPort = parseInt(context.data('viewPort')),
                    lastSlide = parseInt(context.data('lastSlide'));

                xtn.fn.log("[ui-slider] right");

                if (currentPage - 2 < 0 && config.arrows) {
                    $("a.arrow-container-left", context.parent()).addClass("blocked");
                }

                if (currentPage - 1 >= 0) {
                    if (config.arrows) {
                        $("a.arrow-container-right", context.parent()).removeClass("blocked");
                    }

                    var scroll = viewPort;

                    if (currentPage == pages - 1 && lastSlide > 0) {
                        var gt = $("li", context).length - lastSlide; // because operator should be >=
                        scroll = 0;
                        $("li", context).each(function (indx) { if (indx >= gt) { scroll += $(this).outerWidth(); } });
                    }

                    this.move(context, config, '+=', scroll, currentPage - 1);
                }
            },
            move: function (context, config, operator, value, page) {
                if (config.events.onBeforeSlide)
                    config.events.onBeforeSlide.call(context);

                xtn.fn.log("[ui-slider] move: " + operator + " " + value);

                $("li", context).each(function () {

                    $(this).stop(true, true).animate({
                        left: operator + value
                    }, config.speeds.scroll, function () {
                        var left = parseFloat($(this).css("left"));
                        if (left >= 0.0 && left < parseInt(context.data('viewPort'))) {
                            $(this).addClass("slide-visible");
                            if (config.events.onAfterSlide)
                                config.events.onAfterSlide.call(this);
                        } else {
                            $(this).removeClass("slide-visible");
                        }


                    });


                });

                context.data('currentPage', page);
            },
            timeout: function (ul, config, speed, callback) {
                return setTimeout(function () {
                    fn.slide(ul, config);

                    if (callback) {
                        callback.call(this);
                    }
                }, speed);
            }
        };

        this.each(function () {

            var ul = $(this),
				cheight = config.height,
				cWidth = ul.width(),
				li = $("li:first", this);


            if (!cheight /* 0 or undefined */) {
                cheight = $.maxHeight(ul);
            }

            ul.wrap( /* wrap in a container >> create a viewport */
				$('<div />')
					.addClass("scoll-container")
					.addClass(config.cssClass)
					.addClass('arrow-' + (config.arrows || 'none'))
					.css("height", cheight + "px")
			);

            var children = $("li", ul).css("height", cheight).length;

            // if only one element, we can then leave everything else out
            if (children <= 1) {
                return;
            }

            // arrows

            if (config.arrows) {
                ul
				.before($('<a class="arrow-container arrow-container-left clickable block blocked" />').html("<span class='arrow arrow-left block'>Previous</span>").css("height", cheight).click(function () { fn.moveRight(ul, config); }))
				.after($('<a class="arrow-container arrow-container-right clickable block" />').html("<span class='arrow arrow-right block'>Next</span>").css("height", cheight).click(function () { fn.moveLeft(ul, config); }));

                if (config.arrows == 'static') {
                    config.offset.left += $("a.arrow-container-left", ul.parent()).outerWidth();
                    config.offset.right += $("a.arrow-container-right", ul.parent()).outerWidth();
                } else if (config.arrows == 'overlay') {
                    $("a.arrow-container", ul.parent()).fadeOut(0);
                }
            }

            var fullWidth = config.offset.left;
            var viewPort = viewport = ul.width() - (config.offset.left + config.offset.right);

            var startPos = 0;

            xtn.fn.log("[ui-slider] viewport: " + viewPort);
            $("li", ul).each(function (indx) {
                $(this)
					.css("left", fullWidth + "px");

                if (indx == 0) {
                    startPos = fullWidth;
                }

                var left = fullWidth;
                if (left >= 0 && left < viewport) {
                    $(this).addClass("slide-visible");
                } else {
                    $(this).removeClass("slide-visible");
                }

                fullWidth += $(this).outerWidth();
            });

            if (!config.arrows.overlay) {
                fullWidth -= config.offset.right;
            }


            var totalPages = Math.ceil((fullWidth - 1) / viewport);
            var completePages = Math.floor((fullWidth - 1) / viewport);
            var completeWidth = completePages * viewPort;

            var lastSlide = 0;

            $("li", ul).each(function () {
                if ($(this).position().left > completeWidth) {
                    lastSlide++;
                }
            });

            // add some data
            ul
				.data('fullWidth', fullWidth)
				.data('viewPort', viewport)
				.data('pages', totalPages)
                .data('lastSlide', lastSlide)
                .data('startPos', startPos)
				.data('currentPage', 0);

            fn.move(ul, config, '+=', 0, 0);

            // if autoplay
            if (config.autoPlay) {

                var clbck = function () {
                    ul.timeout = fn.timeout(ul, config, config.speeds.timeout, clbck /* fancy ey? */);
                };

                ul.timeout = fn.timeout(ul, config, config.speeds.autoplay, clbck);

                // stop playing when user has clicked on anything in container
                ul.parent().click(function () {
                    clearTimeout(ul.timeout);
                });

                // fade-In/Out effect for overlaying arrows
                if (config.arrows == 'overlay') {
                    ul.parent().hover(function () {
                        $("a.arrow-container", this).fadeIn('slow', function () { });
                    }, function () {
                        $("a.arrow-container", this).fadeOut('slow');
                    });
                }
            }
        });

        return this;
    };

})(jQuery);
