/*
 *	Created  1 Abril de 2009
 *	Modified 4 Noviembre de 2009
 *	By Emiliano Mateu
 **/
function PhotoGallery(conf){
    this.window                 = null;
    this.gallery                = null;
    var _this                   = this;

    this.init = function (){
        _this.setConf(conf);
        _this.gallery                   = {};
        _this.gallery.wrapper           = jQuery(_this.conf.wrapper);
        _this.gallery.ul                = jQuery(_this.conf.wrapper+" ul:first");
        _this.gallery.li                = _this.gallery.ul.children();
        _this.gallery.numbers           = {};
        _this.gallery.numbers.wrapper   = null;
        _this.gallery.numbers.li        = null;
        _this.gallery.pickTimer         = null;
        _this.gallery.looping           = false;
        _this.gallery.paused            = false;


        if (1 < _this.gallery.li.length){
            _this.setup();
            _this.loop();
            _this.bindNavigator();
            _this.bindNext();
            _this.bindPrev();
            _this.bindPlayPause();
        }
        if (_this.conf.alignCenter){
            _this.setAlignCenter();
        }
    };

    this.setup = function(){
        _this.gallery.li.css("z-index", "98");
        _this.gallery.li.first().addClass("first active");
        _this.gallery.li.last().addClass("last");
        _this.gallery.li.not(":first").hide();
		
        var k = 1, j = "", ref = _this.conf.wrapper.substr(1);

        _this.gallery.li.each(function(){            
            jQuery(this).attr("id", ref+"Item"+k);
            j += "<span class='"+ref+"Picker' id='"+ref+"No"+k+"'>"+k+"</span>";
            k++;
        });
        _this.gallery.wrapper.append("<div id='"+ref+"PlayPause'>"+_this.conf.playpauseText+"</div> <div id='"+ref+"Next' class='"+ref+"Arrow'>"+_this.conf.nextText+"</div><div id='"+ref+"Prev' class='"+ref+"Arrow'>"+_this.conf.prevText+"</div><div id='"+ref+"Numbers'>"+j+"</div>");

        _this.gallery.numbers.wrapper   = jQuery(_this.conf.wrapper+"Numbers");
        _this.gallery.numbers.li        = _this.gallery.numbers.wrapper.children();

        _this.gallery.numbers.li.first().addClass("active");
        _this.gallery.numbers.wrapper.width(_this.gallery.numbers.li.outerWidth(true)*k);
    };

    this.loop = function(){
        _this.gallery.looping = clearInterval(_this.gallery.looping);
        _this.gallery.looping = setInterval(function(){
            var current                 = _this.gallery.ul.find("li.active");
            var picked                  = null;
            if (current.hasClass("last")) {
                picked = _this.gallery.li.first();
            } else {
                picked = current.next();
            }
            var pickedId		= picked.attr("id").match(/\d+/);
            var pickedNumber 	= jQuery(_this.conf.wrapper+"No"+pickedId);

            _this.galleryTransition(current, picked, pickedNumber);
        },  _this.conf.picDelay);
    };

    this.bindPlayPause = function(){
        _this.gallery.btnPlayPause = jQuery(_this.conf.wrapper+"");
        _this.gallery.btnPlayPause.bind("click", function(){
            if (_this.gallery.paused) {
                _this.resumeSlider();
            } else {
                _this.pauseSlider();
            }
        });
    };

    this.bindNavigator = function(){
        _this.gallery.numbers.li.click(function(){
            _this.pauseSlider();
            var id = parseInt(jQuery(this).attr("id").match(/\d+/));
            var currentId = parseInt(_this.gallery.numbers.wrapper.find("span.active").attr("id").match(/\d+/));
            if (id !== currentId) {
                _this.pick(id);
            }
        });
    };

    this.bindNext = function(){
        jQuery(_this.conf.wrapper+"Next").click(function(){
            _this.pauseSlider();
            var current                 = _this.gallery.ul.find("li.active");
            var picked                  = null;
            if (current.hasClass("last")) {
                picked = _this.gallery.li.first();
            } else {
                picked = current.next();
            }
            var pickedId                = picked.attr("id").match(/\d+/);
            _this.pick(pickedId);
        });
    };
    
    this.bindPrev = function(){
        jQuery(_this.conf.wrapper+"Prev").click(function(){
            _this.pauseSlider();
            var current                 = _this.gallery.ul.find("li.active");
            var picked                  = null;
            if (current.hasClass("first")) {
                picked = _this.gallery.li.last();
            } else {
                picked = current.prev();
            }
            var pickedId                = picked.attr("id").match(/\d+/);
            _this.pick(pickedId);
        });
    };

    this.pick = function(id){
        _this.gallery.looping = clearInterval(_this.gallery.looping);
        var current         = _this.gallery.ul.find("li.active");
        var picked          = jQuery(_this.conf.wrapper+"Item"+id)
        var pickedNumber    = jQuery(_this.conf.wrapper+"No"+id);
        _this.galleryTransition(current, picked, pickedNumber);
        //_this.loop();
    };

    this.galleryTransition = function(current, picked, pickedNumber){
        _this.gallery.pickTimer = clearInterval(_this.gallery.pickTimer);
        _this.gallery.pickTimer = setTimeout(function(){
            pickedNumber.addClass("active");
            pickedNumber.siblings().removeClass("active");

            current.siblings().hide();
            picked.css("z-index", "99");
            picked.fadeIn(_this.conf.fxDelay);
            picked.addClass("active");
            current.removeClass("active");
            current.css("z-index", "98");
        }, _this.conf.fxDelay);
    };

    this.setAlignCenter = function(){
        _this.gallery.alignCenterData = {
            offsetTop : _this.gallery.wrapper.offset().top,
            wrapperWidth: _this.gallery.wrapper.width()
        };
        _this._window = jQuery(window);
        _this.gallery.ul.css("overflow", "hidden");
        _this.alignCenter();

        _this._window.resize(function() {
            setTimeout(function(){
                _this.alignCenter();
            }, 100);
        });
        _this.gallery.wrapper.css("visibility", "visible");
    };

    this.alignCenter = function(){
        var screenWidth = _this._window.width();
        var xPos        = parseInt((_this.gallery.alignCenterData.wrapperWidth-screenWidth)/2);
        _this.gallery.ul.width(screenWidth);
        _this.gallery.wrapper.width(screenWidth);
        _this.gallery.wrapper.offset({left: 0, top: _this.gallery.alignCenterData.offsetTop});
        _this.gallery.li.css("left", (-xPos)+"px");
        _this.gallery.li.find(".successful").css("right", (xPos)+"px");
    };

    this.pauseSlider = function(){
        if (!_this.gallery.paused){
            _this.gallery.paused = true;
            _this.gallery.looping = clearInterval(_this.gallery.looping);
            _this.gallery.btnPlayPause.addClass("paused");
        }
    };

    this.resumeSlider = function(){
        if (_this.gallery.paused){
            _this.gallery.paused = false;
            _this.loop();
            _this.gallery.btnPlayPause.removeClass("paused");
        }
    };

    this.setConf = function(conf){
        if (conf == null) conf = {};
        _this.conf = {
            alignCenter     : (conf.alignCenter != null)    ? conf.alignCenter : false,
            picDelay        : (conf.picDelay != null)       ? conf.picDelay : 7500,
            fxDelay         : (conf.fxDelay != null)        ? conf.fxDelay : 900,
            wrapper         : (conf.wrapper != null)        ? conf.wrapper : "#photogallery",
            nextText        : (conf.nextText != null)       ? conf.nextText : "»",
            prevText        : (conf.nextText != null)       ? conf.nextText : "«",
            playpauseText   : (conf.playpauseText != null)  ? conf.playpauseText : "Play / Pause"
        };
    }

    this.init();
}
