/*
 * 
 * Usage:
 * 
 * $("#ss3").yesSS({align:"bl"});
 * var ss3 = $("#ss3").data("yesSS");
 * ss3.init();
 * 
 * ss1.resize({width: maxWidth, height: maxHeight});
 * 
 */

(function($){

    var YesSS = function(element, options){
        
        var elem = $(element);
        var obj = this;
        var settings = $.extend({
            animate: false,
            spacing: 0,
            width: 700,
            height: 500,
            maxWidth: 1200,
            maxHeight: 800,
            align: 'tl',

            initDelay: 0,
            delay: 3000,
            speed: 500,
            method: 'fade',
            crossfade: false,
            startSlide: 0
        }, options || {});

        var _this = elem;

        var firstSlide = true;

        var interval;
        var totalSlides;
        var currentIndex;

        var prevSlide;
        var currentSlide;
        var nextSlide;
                
        this.init = function(options){
            
            if (options) { 
                $.extend(settings, options);
            }

            totalSlides = $(".slide", _this).size();
            currentIndex = settings.startSlide;

            $(".slide", _this).hide();
            $(".slide", _this).css("position", "absolute");

            if (settings.align == 'bl') {
                $(".slide", _this).css("bottom", 0);
            } else if (settings.align == 'br') {
                $(".slide", _this).css("right", 0);
                $(".slide", _this).css("bottom", 0);
            } else if (settings.align == 'tr') {
                $(".slide", _this).css("right", 0);
            }

            $("img", _this).addClass("not-loaded");
            $("img", _this).css("position", "absolute");

            $("img", _this).load(function(){
                $(this).removeClass("not-loaded");
                if ($(".not-loaded", $(this).closest(".slide")).size() == 0) {
                    if (firstSlide) {
                        firstSlide = false;
                        doFade(true);
                    } else {
                        doFade();
                    }
                }
            });

            if (settings.delay > 0) {
                setTimeout(function(){
                    interval = setInterval(function(){
                        obj.next();
                    }, settings.delay);                    
                }, settings.initDelay);
            }

            obj.goToSlide(currentIndex);
        }

        this.resize = function(options){

            if (options) { 
                $.extend(settings, options);
            }

            doResize(prevSlide);
            doResize(nextSlide);
        }

        var doResize = function(target){
            
            var width = settings.width > settings.maxWidth ? settings.maxWidth : settings.width;
            var height = settings.height > settings.maxHeight ? settings.maxHeight : settings.height;

            var isPair = false;

            var leftImage = $("img", target).eq(0);

            if ($("img", target).size() > 1) {
                isPair = true;
                var rightImage = $("img", target).eq(1);
            }

            if (isPair) {
                width -= settings.spacing;
                totalWidth = Number(leftImage.attr("w")) + Number(rightImage.attr("w"));
            } else {
                totalWidth = Number(leftImage.attr("w"));
            }

            totalHeight = Number(leftImage.attr("h"));

            availableRatio = width / height;
            actualRatio = totalWidth / totalHeight;

            if (availableRatio < actualRatio) {
                newLeftWidth = Math.floor(((leftImage.attr("w") / totalWidth) * width));
                newLeftHeight = Math.floor((newLeftWidth / leftImage.attr("w")) * leftImage.attr("h"));
            } else {
                newLeftHeight = Math.floor(height);
                newLeftWidth = Math.floor((newLeftHeight / leftImage.attr("h")) * leftImage.attr("w"));
            }

            if (isPair) {
                if (availableRatio < actualRatio) {
                    newRightWidth = Math.floor(((rightImage.attr("w") / totalWidth) * width));
                    newRightHeight = Math.floor((newRightWidth / rightImage.attr("w")) * rightImage.attr("h"));
                } else {
                    newRightHeight = Math.floor(height);
                    newRightWidth = Math.floor((newRightHeight / rightImage.attr("h")) * rightImage.attr("w"));
                }
            }

            var slideWidth = newLeftWidth;
            var slideHeight = newLeftHeight;

            if (settings.animate) {
                leftImage.animate({width:newLeftWidth, height:newLeftHeight});
            } else {
                leftImage.width(newLeftWidth);
                leftImage.height(newLeftHeight);
            }

            if (isPair) {
                slideWidth = newLeftWidth + newRightWidth + settings.spacing;

                if (settings.animate) {
                    rightImage.animate({width:newRightWidth, height:newRightHeight});
                } else {
                    rightImage.width(newRightWidth);
                    rightImage.height(newRightHeight);
                }
                rightImage.css("margin-left", settings.spacing);
            }

            $(target).width(slideWidth);
            $(target).height(slideHeight);

            _this.width(width);
            _this.height(height);
        }

        var loadSlide = function(i){ 
            $(".slide:eq("+i+") img[img]", _this).each(function(){
                $(this).attr("src", $(this).attr("img"));
                $(this).removeAttr("img");
            });
        }

        this.goToSlide = function(i){
            prevSlide = $(".current-slide", _this);
            nextSlide = $(".slide", _this).eq(i);

            prevSlide.removeClass("current-slide");
            nextSlide.addClass("current-slide");

            if ($(".not-loaded", nextSlide).size() > 0) {
                loadSlide(i);
            } else {
                doFade();
            }

            currentIndex = i;                        
        }

        this.next = function(){
            var i = currentIndex == totalSlides - 1 ? 0 : currentIndex + 1;
            obj.goToSlide(i);
        }

        this.prev = function(){
            var i = currentIndex == 0 ? totalSlides - 1 : currentIndex - 1;
            goToSlide(i);
        }

        var doFade = function(force){
            prevSlide.fadeOut(settings.speed);
            if (settings.crossfade || force) {
                nextSlide.fadeIn(settings.speed);
            } else {
                nextSlide.delay(settings.speed).fadeIn(settings.speed);
            }
            obj.resize();
        }

//        obj.init();
    };

    $.fn.yesSS = function(options){
        return this.each(function(){
            var element = $(this);
            
            if (element.data('yesSS')) {
                return;
            }

            var yesSS = new YesSS(this, options);

            element.data('yesSS', yesSS);
        });
    };
    
})(jQuery);
