var Panorama = new function() {
    this.move_type = 0;
    this.image_src = '';
    this.image = null;
    this.width = 0;
    this.framerate = 40;
    this.speed = 3;
    this.margin_start = 3367;
    
    this.preInit = function() {
        this.image = new Image();
        $(this.image).bind('load', function() {
            Panorama.init();
        });
        this.image.src = this.image_src;
    }
    
    this.init = function() {
        if (this.image == null) return;
        $('#panorama_preload').css('display', 'none');
        if (this.image.width > 2000) {
            this.width = this.image.width;
        } else {
            var i = Math.ceil(2000 / this.image.width);
            this.width = this.image.width * i;
        }
        var window_width = $(document).width();
        var start_offset = parseInt(this.margin_start - (window_width / 2));
        $('#panorama_slider').css({
            'background-image': 'url('+this.image.src+')',
            'margin-left': -start_offset
        });
        
        $(document).bind('mousemove', function(e) {
            if (!e) e = window.event;
            var elem = e.target || e.srcElement;
            var type = e.type;
            
            Panorama.mMove(elem, type);
        });
        window.setInterval('Panorama.update();', this.framerate);
    }
    
    this.mMove = function(elem, type) {
        this.move_type = 0;
        do {
            if (elem.id == 'panorama_left') {
                this.move_type = -1;
                break;
            }
            if (elem.id == 'panorama_right') {
                this.move_type = 1;
                break;
            }
        } while (elem = elem.parentNode);
        if (this.move_type == 0) {
            this.speed = 3;
        }
    }
    
    this.update = function() {
        var speed = this.speed;
        var offset = $('#panorama_slider').css('margin-left');
        if (!offset.match(/[0-9]+/)) {
            offset = 0;
        }
        if (this.move_type == 1) {
            offset = parseInt(offset) - speed;
        } else if (this.move_type == -1) {
            offset = parseInt(offset) + speed;
        }
        if (offset > 0) offset = offset - this.width;
        if (offset < -this.width) offset = offset + this.width;
        
        $('#panorama_slider').css('margin-left', offset);
    }
    
    this.addImage = function(src) {
        this.image_src = src;
    }
    
    this.speedUp = function() {
        this.speed = 6;
    }
    
    return this;
}();
$(document).ready(function(){
    Panorama.preInit();
});

