var Shadow = Class.create({
    initialize: function(imgPath, width, height, top, left) {
        this.imgPixel = 8;
        this.shadowParts = [{
            position: 'absolute',
            width: width + 'px',
            height: height + 'px',
            top: top + 'px',
            left: left + 'px',
            backgroundColor: '#7f7f7f'
        },
        {
            width: width + this.imgPixel + 'px',
            height: this.imgPixel + 'px',
            top: -this.imgPixel + 'px',
            left: -this.imgPixel + 'px',
            backgroundImage: 'url(' + imgPath + 'tl.png)'
        },
        {
            width: this.imgPixel + 'px',
            height: this.imgPixel + 'px',
            top: -this.imgPixel + 'px',
            right: -this.imgPixel + 'px',
            backgroundImage: 'url(' + imgPath + 'tr.png)'
        },
        {
            width: this.imgPixel + 'px',
            height: height + 'px',
            top: '0px',
            left: -this.imgPixel + 'px',
            backgroundImage: 'url(' + imgPath + 'l.png)',
            backgroundRepeat: 'repeat-y'
        },
        {
            width: this.imgPixel + 'px',
            height: height + 'px',
            top: '0px',
            right: -this.imgPixel + 'px',
            backgroundImage: 'url(' + imgPath + 'r.png)',
            backgroundRepeat: 'repeat-y'
        },
        {
            width: width + this.imgPixel + 'px',
            height: this.imgPixel + 'px',
            top: height + 'px',
            left: -this.imgPixel + 'px',
            backgroundImage: 'url(' + imgPath + 'bl.png)'
        },
        {
            width: this.imgPixel + 'px',
            height: this.imgPixel + 'px',
            top: height + 'px',
            right: -this.imgPixel + 'px',
            backgroundImage: 'url(' + imgPath + 'br.png)'
        }];
        this.container = new Element('div', {id: 'shadowContainer'});
        this.container.setStyle(this.shadowParts.shift());
        for (i = 0; i < this.shadowParts.length; i++) {
            var elem = new Element('div');
            elem.addClassName('iepngfix');
            elem.setStyle(Object.extend({position: 'absolute', backgroundRepeat: 'no-repeat'}, this.shadowParts[i]));
            this.container.insert(elem);
        }
    },
    sizeAndPositionChanged: function(width, height, top, left) {
        var parts = $A(this.container.childNodes);
        parts[0].setStyle({width: width + this.imgPixel + 'px'});
        parts[2].setStyle({height: height + 'px'});
        parts[3].setStyle({height: height + 'px'});
        parts[4].setStyle({width: width + this.imgPixel + 'px', top: height + 'px'});
        parts[5].setStyle({top: height + 'px'});
        this.container.setStyle({
            width: width + 'px',
            height: height + 'px',
            top: top + 'px',
            left: left + 'px'
        });
    },
    moved: function(aimedWidth, aimedHeight, aimedTop, aimedLeft, frequency) {
        this.container.setOpacity(0.3);
        var that = this;
        var width = parseInt(this.container.getStyle('width'));
        var height = parseInt(this.container.getStyle('height'));
        var top = parseInt(this.container.getStyle('top'));
        var left = parseInt(this.container.getStyle('left'));
        var moveWidth = (aimedWidth - width) / frequency;
        var moveHeight = (aimedHeight - height) / frequency;
        var moveTop = allocationIsChanged((aimedTop - top), frequency);
        var moveLeft = allocationIsChanged((aimedLeft - left), frequency);
        setTimeout(function() {
            width += moveWidth;
            height += moveHeight;
            top += moveTop.shift();
            left += moveLeft.shift();
            that.sizeAndPositionChanged(width, height, top, left);
            frequency--;
            if (frequency > 0) setTimeout(arguments.callee, 15);
            else that.container.setOpacity(1.0);
        }, 15);
    }
});

var ShadowApply = Class.create({
    initialize: function(imgPath, elem, styles) {
        this.elem = elem;
        this.dim = this.elem.getDimensions();
        this.shadowWrapper = new Element('div', {id: 'shadowWrapper'});
        this.shadowWrapper.setStyle(Object.extend({
            position: 'relative',
            width: this.dim.width + 'px',
            height: this.dim.height + 'px'            
        }, styles || {}));
        this.shadow = new Shadow(imgPath, this.dim.width - 8, this.dim.height - 8, 14, 4);
        this.elem.up().insert(this.shadowWrapper);
        this.shadowWrapper.insert({top: this.shadow.container, bottom: this.elem});
        this.shadowWrapper.insert(this.elem);
        this.elem.setStyle({
            position: 'absolute',
            top: 0,
            left: 0
        });
    }
});

var MoveObj = Class.create({
    initialize: function(menu, container, color) {
        this.container = container;
        this.menu = menu;
        if (menu.getElementsByClassName('current')[0]) {
            this.currentObj = menu.getElementsByClassName('current')[0];
            this.initPosLeft = this.currentObj.positionedOffset().left;
            this.initWidth = this.currentObj.getWidth();
        } else {
            this.initPosLeft = 0;
            this.initWidth = 0;
        }
        this.mobj = new Element('div');
        this.mobj.setStyle({
            position: 'relative',
            backgroundColor: color,
            width: this.initWidth + 'px',
            height: this.container.getDimensions().height + 'px',
            left: this.initPosLeft + 'px'
        });
        this.container.insert(this.mobj);
        this.initLeft = this.mobj.positionedOffset().left;
        this.move = this._move.bind(this);
        this.moveRelease = this._moveRelease.bind(this);
        this.menu.observe('mouseover', this.move);
        this.menu.observe('mouseout', this.moveRelease);
    },
    
    _move: function(event) {
        if (this.timeId2) clearInterval(this.timeId2);
        if (this.timeId) clearInterval(this.timeId);
        var that = this;
        var x1 = that.mobj.positionedOffset().left;
        var x2 = event.element().positionedOffset().left;
        var width = event.element().getWidth();
        that.mobj.setStyle({width: width + 'px'});
        var dx = Math.abs(x2 - x1);
        this.timeId = setInterval(function() {
    	    x1 += (x2 - x1) / 10;
    	    var dmx = Math.abs(x2 - x1);
            if (dx / 2 < dmx) var w = (dx - dmx);
            else var w = dmx;
            that.mobj.setStyle({
                left: x1 + 'px'
            });
            if (dmx < 1) {
                clearInterval(that.timeId);
                that.mobj.setStyle({
                    left: x2 + 'px'
                });   
            }
    	}, 15);
    },
    
     _moveRelease: function(event) {
        if (this.timeId2) clearInterval(this.timeId2);
        if (this.timeId) clearInterval(this.timeId);
        var that = this;
        var x1 = event.element().positionedOffset().left;
        var x2 = that.initLeft;
        var width = that.initWidth;
        var dx = Math.abs(x2 - x1);
        this.timeId2 = setInterval(function() {
    	    x1 += (x2 - x1) / 10;
    	    var dmx = Math.abs(x2 - x1);
            if (dx / 2 < dmx) var w = (dx - dmx);
            else var w = dmx;
            that.mobj.setStyle({
                width: Math.max(width, width + w) + 'px',
                left: x1 + 'px'
            });
            if (dmx < 1) {
                clearInterval(that.timeId2);
                that.mobj.setStyle({
                    left: x2 + 'px',
                    width: width
                });   
            }
    	}, 15);
    }
});

var Slideshow = Class.create({
    initialize: function(container, imgPath, phpFile) {
        var that = this;
        this.container = container;
        this.imgPath = imgPath;
        this.imgs = [];
        this.idx = 0;
        var containerInit = function(img) {
            var dimensions = img.getDimensions();
            that.container.setStyle({
                position: 'relative',
                width: dimensions.width + 'px',
                height: dimensions.height + 'px',
                overflow: 'hidden'
            });
        };
        new Ajax.Request(phpFile, {
            method: 'get',
            onSuccess: function(req) {
                that.imgs = req.responseText.evalJSON();
                var img = new Element('img');
                img.src = that.imgPath + that.imgs[that.idx];
                that.container.insert(img);
                if (img.complete) containerInit(img);
                else img.observe('load', function(event) {containerInit(img);});
            }
        });
        this.play();
    },
    nextImg: function() {
        var that = this;
        that.idx++;
        if (that.idx == that.imgs.length) that.idx = 0;
        var img = new Element('img');
        img.setStyle({visibility: 'hidden'});
        img.src = that.imgPath + that.imgs[that.idx];
        that.container.insert(img);
        if (img.complete) that.fromLeftToRight(img);
        else {
            img.observe('load', function(event) {
                that.fromLeftToRight(img);
            }.bindAsEventListener(that));
        }
    },
    play: function() {
        clearInterval(this.timeId);
        var that = this;
        this.timeId = setInterval(function() {
            that.nextImg();
        }, 5000);
    },
    fromLeftToRight: function(img) {
        var opt = 0;
        var dimensions = img.getDimensions();
        var left = -dimensions.width;
        img.setStyle({visibility: 'visible', position: 'absolute', left: left + 'px', opacity: opt / 100});
        setTimeout(function() {
            left += 10;
            opt += 3;
            img.setStyle({left: left + 'px', opacity: opt / 100});
            if (left < 0) setTimeout(arguments.callee, 15);
            else if (img.previous()) img.previous().remove();
        }, 15);
    }
});

/*
function shuffle(list) {
  var i = list.length;

  while (--i) {
    var j = Math.floor(Math.random() * (i + 1));
    if (i == j) continue;
    var k = list[i];
    list[i] = list[j];
    list[j] = k;
  }

  return list;
}
*/

/*
document.observe('dom:loaded', function(event) {
    $$('#globalmenu ul li a').each(function(elem) { 
        var path = elem.toString().split('/');
        var cnt = path.length;
        var page = path[path.length - 1] ? path[path.length - 1] : path[path.length - 2];
        var page = page == 'ruf'? 'index' : page;
        if (document.body.id == page) {
            elem.addClassName('current');
        }
    });
});
*/

/*
Event.observe(window, 'load', function(event) {
    new ShadowApply('/ruf/img/', $('wrapper'), {
        marginTop: 0,
        marginBottom: 0,
        marginLeft: 'auto',
        marginRight: 'auto'
    });
});
*/
