var Lvs;

if (Lvs == undefined) {
    Lvs = {};
}

if (Lvs.Personal == undefined) {
    Lvs.Personal = {};
}

Lvs.Personal.Configure = {
    FOOTER_ID:          'footerInnerBrandnew',
    GET_URL:            '/ajax/favorite_or_recent',
    CHECK_URL:          '/ajax/has_favorite_or_recent',
    SLIDER_SELECTOR:    '.footerSlider',
    JQUERY_SRC_PATH:    '/js/jquery.js',
    SLIDER_SRC_PATH:    '/js/easySlider1.5.js',
    SLIDER_SPEED:       400,
    SLIDER_PREV_BTN_ID: 'fstPrevBtn',
    SLIDER_NEXT_BTN_ID: 'fstNextBtn',
    LOADING_IMG_TMPL:   '<div style="padding-bottom:50px; padding-top:50px; text-align:center;"><img src="/img/result/ajax_loading_photo.gif"></div>'
};

Lvs.Personal.Loader = Class.create({
    loaded: false,

    lock: false,

    initialize : function(elem, config) {
        this.elem   = $(elem);
        this.config = config;
    },

    wasLoaded: function() {
        return this.loaded;
    },

    isBelowTheFold: function() {
        return (Prototype.Browser.Opera ? window.innerHeight : document.viewport.getHeight()) + document.viewport.getScrollOffsets().top <= this.elem.cumulativeOffset().top;
    },

    isAboveTheTop: function() {
        return document.viewport.getScrollOffsets().top >= this.elem.cumulativeOffset().top + this.elem.height;
    },

    lazyload: function() {
        if (!this.wasLoaded()) { this.load(); }
    },

    load: function() {
        if (!this.isAboveTheTop() && !this.isBelowTheFold()) {
            if (this.lock) {
                return;
            }
            this.lock = true;
            if (document.cookie != '') {
                this.elem.update(this.config.LOADING_IMG_TMPL);
                this.doLoad.bind(this).delay(0.5);
            }
        }
    },

    doLoad: function() {
        new Ajax.Updater(this.elem.id, this.config.GET_URL, {
            method: 'post',
            onComplete: this.onComplete.bind(this)
        });
    },

    onComplete: function () {
        this.loaded = true;
        this.setupSlider();
        this.lock = false;
        if (navigator.userAgent.indexOf('MSIE 6.0') != -1) {
            $$('#footerInner .footRight div.freeword')[0].style.position = 'relative';
        }
    },

    setupSlider: function () {
        $j(this.config.SLIDER_SELECTOR).easySlider(this.getSliderOptions());
    },

    getSliderOptions: function () {
        return {
            speed:  this.config.SLIDER_SPEED,
            prevId: this.config.SLIDER_PREV_BTN_ID,
            nextId: this.config.SLIDER_NEXT_BTN_ID
        };
    }
});

Lvs.Personal.LoaderHandler = Class.create({
    initialize: function(loader, config) {
        this.loader = loader;
        this.config = config;
        if (!('jQuery' in window)) {
            this.loadJavascripts([this.config.JQUERY_SRC_PATH, this.config.SLIDER_SRC_PATH]);
        } else if (jQuery.fn.easySlider == undefined) {
            this.loadJavascripts([this.config.SLIDER_SRC_PATH]);
        }
        if (!loader.isBelowTheFold()) {
            loader.load();
        }
        this.setEventListener();
    },

    loadJavascripts: function (srcs) {
        var div = new Element('div');
        $A(srcs).map(function (src) { return this.createScriptTag(src); }.bind(this)).each(function (script) {
            div.appendChild(script);
        });
        document.body.appendChild(div);
    },

    createScriptTag: function (src) {
        var script = new Element('script');
        script.charset = 'UTF-8';
        script.src = src;
        return script;
    },

    setEventListener: function() {
        Event.observe(window, 'scroll', this.lazyload.bindAsEventListener(this));
    },

    lazyload: function() {
        this.loader.lazyload();
    }
});

Lvs.Personal.init = function (options) {
    if (options == undefined) {
        options = {};
    }
    var config = Object.extend(Object.clone(Lvs.Personal.Configure), options);
    var elem = $(config.FOOTER_ID);
    if (elem) {
        var loader = new Lvs.Personal.Loader(elem, config);
        new Lvs.Personal.LoaderHandler(loader, config);
    }
};

Lvs.Personal.setup = function (options) {
    Event.observe(window, 'load', function () {
        Lvs.Personal.init();
    });
};

Lvs.Personal.setup();
