/* 
    Extra Init
*/
var ExtraInit = new Class({
    Implements: Options,
    options: {
        url: null,
        duration: 400,
        color: '#bbb'
    },
    initialize: function(options) {
        this.setOptions(options);
        
        $$('a').each(function(a) {
            a.set('tween', {
                duration: this.options.duration,
                transition: 'expo:out',
                link: 'cancel'
            });
            a.store('defaultColor', a.getStyle('color'));
            a.addEvents({
                mouseenter: function() {
                    a.tween('color', this.options.color);
                }.bind(this),
                
                mouseleave: function() {
                    a.tween('color', a.retrieve('defaultColor'));
                }.bind(this)
            });
        }.bind(this));
        
        
        $('global-nav').getElements('ul[class!=children] > li').each(function(li, liIndex) {
            var a = li.getElement('a');
            var text = a.get('text');
            
            li.setStyles({
                width: a.getSize().x,
                height: a.getSize().y
            });
            a.set('html', a.get('text') + '<br />' + a.get('text'));
            a.set('morph', {
                duration: this.options.duration,
                transition: 'expo:out',
                link: 'chain'
            });
            a.addEvents({
                mouseenter: function() {
                    a.morph({
                        'margin-top': - li.getSize().y
                    });
                }.bind(this),
                mouseleave: function() {
                    a.morph({
                        'margin-top': 0
                    });
                }.bind(this)
            });
            /*
            if (text == 'Online Shop' || text == 'Shopping Guide') {
                a.addClass('attach-tip');
                a.set('title', 'Coming Soon');
                a.addEvent('click', function(event) { event.stop() });
            }
            */
        }.bind(this));
        
        if ($('fixed-global-nav')) {
            $('fixed-global-nav').getElements('ul[class!=children] > li').each(function(li, liIndex) {
                var a = li.getElement('a');
                var text = a.get('text');
                
                /*
                if (text == 'Online Shop' || text == 'Shopping Guide') {
                    a.addClass('attach-tip');
                    a.set('title', 'Coming Soon');
                    a.addEvent('click', function(event) { event.stop() });
                }
                */
            }.bind(this));
        }
        
        new ToolTips();
    }
});


/* 
    Adjust Header
*/
var AdjustHeader = new Class({
	Implements: [Options,Events],
    resizeTimer: null,
    isFirst: true,
    options: {
        footerBackgroundColor: '#000',
        duration: 3000,
		onComplete: $empty
    },
    initialize: function(options) {
        this.setOptions(options);
        
        window.addEvent('resize', function() {
            $clear(this.resizeTimer);
            this.resizeTimer = (function() {
                if (this.isFirst == false) {
                    this.resize.run([], this)
                }
            }.bind(this)).delay(200);
        }.bind(this));
    },
    
    run: function() {
        var header = $('header');
        header.store('defaultWidth', header.getSize().x);
        header.store('defaultHeight', header.getSize().y);
        
        var globalNav = $('global-nav');
        globalNav.store('defaultWidth', globalNav.getSize().x);
        globalNav.store('defaultHeight', globalNav.getSize().y);
        
        var information = $('information');
        information.setStyle('opacity', 0);
        information.store('defaultWidth', information.getSize().x);
        information.store('defaultHeight', information.getSize().y);
        
        var copyright = $('copyright');
        copyright.setStyle('opacity', 0);
        copyright.store('defaultWidth', copyright.getSize().x);
        copyright.store('defaultHeight', copyright.getSize().y);
        
        var footerBg = $('footer-bg');
        footerBg.setStyles({
            position: 'absolute',
            top: 0,
            left: 0,
            width: $('footer').getSize().x,
            height: $('footer').getSize().y,
            'background-color': this.options.footerBackgroundColor,
            'z-index': -1,
            opacity: 0
        });
        footerBg.set('tween', {
            duration: 'normal',
            link: 'cancel',
            transition: 'expo:out'
        });
        
        $('footer').addEvent('mouseenter', function() {
            footerBg.tween('opacity', 0.8);
        });
        $('footer').addEvent('mouseleave', function() {
            footerBg.tween('opacity', 0);
        });
        
        header.set('tween', { duration: this.options.duration, link: 'cancel', transition: 'expo:out' });
        globalNav.set('tween', {
            duration: this.options.duration,
            link: 'cancel',
            transition: 'expo:out',
            onComplete: function() {
                information.tween('opacity', 1);
                copyright.tween('opacity', 1);
                this.isFirst = false;
                this.fireEvent('complete');
            }.bind(this)
        });
        
        this.resize.run([], this);
        
        header.setStyle('width', 0);
        globalNav.setStyle('width', 0);
        
        $('wrapper').setStyle('opacity', 1);
        
        (function() { header.tween('width', header.retrieve('defaultWidth')); }).delay(300);
        (function() { globalNav.tween('width', globalNav.retrieve('defaultWidth')); }).delay(300);    
    },
    
    resize: function() {
        var w = window.getWidth();
        var h = window.getHeight();
        
        $('header').setStyles({
            top: (h - $('footer').getSize().y / 2) / 2 - ( $('header').retrieve('defaultHeight') / 2),
            left: 0
        });
        $('header').store('defaultTop', $('header').getStyle('top').toInt());
        $('header').store('defaultLeft', $('header').getStyle('left').toInt());
    },
    
    move: function() {
        var header = $('header');
        header.set('morph', {
            duration: 10000,
            transition: 'expo:in:out',
            link: 'cancel'
        });
        header.morph({
            top: header.retrieve('defaultTop') + Math.floor(Math.random() * 200 - 100),
            left: header.retrieve('defaultLeft') + Math.floor(Math.random() * 200 - 100)
        });
    }
    
});

/* 
    Background Image
*/
var BackgroundImage = new Class({
    curImage: null,
    curImageIndex: 0,
    rotateTimer: null,
    isImageSetting: false,
    isResizing: false,
    Implements: Options,
    options: {
        path: '',
        images: [],
        periodical: 2000,
        duration: 2000,
        minWidth: 1024,
        minHeight: 1
    },
    initialize: function(options) {
        this.setOptions(options);

        $('wrapper').setStyles({
            margin: 0,
            padding: 0,
            overflow: 'hidden',
            opacity: 1
        });
        
        var imageList = new Element('ul', {
            id: 'image-list',
            styles: {
                position: 'relative',
                top: 0,
                left: 0,
                margin: 0,
                padding: 0,
                overflow: 'hidden',
                'z-index': 1
            },
            'events': {
                'click': function() {
                    this.imageSet();
                }.bind(this)
            }
        });
        for (var i = 0; i < this.options.images.length; i++) {
            var imageListItem = new Element('li', {
                id: 'image-list-item-' + i,
                styles: {
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    opacity: 0,
                    margin: 0,
                    padding: 0,
                    'list-style': 'none',
                    'z-index': i
                }
            });
            imageList.grab(imageListItem);
        }
        $('wrapper').grab(imageList);
        
        this.curImageIndex = $random(0, this.options.images.length - 1);

        this.imageSet();
        
        window.addEvent('resize', function() {
            if (this.isResizing == false) {
                this.isResizing = true;
                (function() {
                    this.resize();
                    (function() { this.isResizing = false; }).delay(41, this);
                }).delay(40, this);
            }
        }.bind(this));
    },
        
    imageSet: function() {
        if (this.isImageSetting == true) {
            return;
        }
        
        this.isImageSetting = true;
        
        var imageListItem = $('image-list-item-' + this.curImageIndex);
        this.stopRotate();
        
        var imageFade = function() {
            var imageListItem = $('image-list-item-' + this.curImageIndex);
            if ($defined(imageListItem) == false) {
                return;
            }
            
            this.curImage = imageListItem.getElement('img');
            
            var chain = new Chain();
            chain.chain(
                function() { this.resize.run([], this); }.bind(this),
                
                function() {
                    imageListItem.set('tween', {
                        duration: this.options.duration,
                        link: 'cancel',
                        transition: 'expo:out',
                        onComplete: function() {
                            var prevImageListItemId = null;
                            if (this.curImageIndex == 0) {
                                imageListItem.setStyle('z-index', 0);
                                prevImageListItemId = this.options.images.length - 1;
                            } else {
                                prevImageListItemId = this.curImageIndex - 1;
                            }
                            $('image-list-item-' + prevImageListItemId).setStyle('opacity', 0);
                            
                            this.curImageIndex++;
                            if (this.options.images.length == this.curImageIndex) {
                                this.curImageIndex = 0;
                            }
                            this.isImageSetting = false;
                            this.startRotate();
                        }.bind(this)
                    });
                    if (this.curImageIndex == 0) {
                        imageListItem.setStyle('z-index', this.options.images.length);
                    }
                    imageListItem.tween('opacity', 1);
                }.bind(this)
            );
            
            chain.callChain();
            chain.callChain();
        }.bind(this);        
        
        if ($defined(imageListItem.getElement('img')) == false) {
            this.curImage = new Asset.image(
                this.options.path + this.options.images[this.curImageIndex].path, 
                {
                    onload: function() {
                        imageListItem.grab(this.curImage);
                        imageFade();
                    }.bind(this),
                    onerror: function() {
                        this.curImageIndex++;
                        if (this.options.images.length == this.curImageIndex) {
                            this.curImageIndex = 0;
                        }
                        this.isImageSetting = false;
                    }.bind(this)
                }
            );
        } else {
            imageFade();
        }
    },
    
    resize: function() {
        $(document.body).setStyle('overflow', 'hidden');
        
        var w = window.getWidth();
        var h = window.getHeight();
        
        if (w < this.options.minWidth) {
            w = this.options.minWidth;
        }
        if (h < this.options.minHeight) {
            h = this.options.minHeight;
        }

        var rate = w / this.curImage.getSize().x;
        var imgWidth = w;
        var imgHeight = Math.floor(this.curImage.getSize().y * rate);
        
        if (imgHeight < h) {
            rate = h / imgHeight;
            imgWidth = Math.floor(imgWidth * rate);
            imgHeight = h;        
        }
        
        /*
        $('wrapper').setStyles({
            width: w,
            height: h
        });
        */
        $('image-list').setStyles({
            width: w,
            height: h
        });
        this.curImage.set({
            width: imgWidth,
            height: imgHeight,
            'styles': {
                'margin-top': (h / 2) - (imgHeight / 2),
                'margin-left': (w / 2) - (imgWidth / 2)
            }
        });
        
        $(document.body).setStyle('overflow', 'visible');
    },
    
    startRotate: function() {
        if ($defined(this.rotateTimer) == false) {
            this.rotateTimer = (function() { this.imageSet(); }.bind(this)).periodical(this.options.periodical);
        }
    },
    
    stopRotate: function() {
        if ($defined(this.rotateTimer)) {
            $clear(this.rotateTimer);
            this.rotateTimer = null;
        }
    }

});


