/*
Copyright (c) 2011, STRAIGHTLINE All rights reserved.
*/

/* 
---------------------------------------------------------------------------------------------------
    Extra Init
---------------------------------------------------------------------------------------------------
*/
var ExtraInit = new Class({
	Implements: [Options,Events],
    scrollTimer: null,
    defaultHeaderInnerSize: null,
    options: {
    },
    initialize: function(options) {
        this.setOptions(options);

        if (Browser.ie6 || Browser.Platform.ios || Browser.Platform.android) {
            $(document.body).addClass('disable-fixed');
        }
        
        var header = $('header');
        if (header) {
            header.store('defaultMarginLeft', $('header').getStyle('margin-left').toInt());
        }
        var footer = $('footer');
        if (footer) {
            footer.store('defaultMarginLeft', $('footer').getStyle('margin-left').toInt());
        }
        var headerInner = $('header-inner');
        if (headerInner) {
            this.defaultHeaderInnerSize = headerInner.getSize();
        }
        
        $$('.lang-ja #global-nav a').each(function(anchor, index) {
            var spans = new Array();
            for (var i = 0; i < anchor.get('text').length; i++) {
                var span = new Element('span', {
                    text: anchor.get('text').slice(i, i + 1)
                });
                spans.push(span);
                anchor.addClass('no-' + Number.random(1, 3));
            }
            anchor.empty();
            anchor.adopt(new Element('span', {
                text: index + 1,
                'class': 'no'
            }), spans);
        });
        
        var reservationButton = $('reservation-button');
        if (reservationButton) {
            reservationButton.store('defaultMarginTop', reservationButton.getStyle('margin-top').toInt());
            reservationButton.setStyles({
                opacity: 0,
                'margin-top': reservationButton.retrieve('defaultMarginTop') * 3
            });
        }
        var pageTop = $('page-top');
        if (pageTop) {
            pageTop.setStyle('opacity', 0);
            pageTop.set('tween', {
                property: 'opacity',
                transition: 'expo:out',
                link: 'cancel'
            });
            pageTop.addEvents({
                mouseenter: function() {
                    if (pageTop.retrieve('visible') == true) {
                        pageTop.get('tween').start(0.7);
                    }
                },
                mouseleave: function() {
                    if (pageTop.retrieve('visible') == true) {
                        pageTop.get('tween').start(1);
                    }
                }
            });
        }
        
        window.addEvents({
            scroll: function() {
                clearTimeout(this.scrollTimer);
                this.scrollTimer = (function() {
                    this.adjust.apply(this);
                }.bind(this)).delay(200);
            }.bind(this),
            resize: function() {
                this.adjust.apply(this);
            }.bind(this)
        });
        this.adjust.apply(this);
    },
    
    run: function() {
        $$('.lang-ja .post-title .ja, #photo-gallery-button .ja').each(function(ja) {
            if (ja.retrieve('setting') == null) {
                ja.store('setting', true);
                var spans = new Array();
                for (var i = 0; i < ja.get('text').length; i++) {
                    var text = ja.get('text').slice(i, i + 1);
                    if (text == 'ー') {
                        text = '丨';
                    }
                    var span = new Element('span', {
                        text: text
                    });
                    spans.push(span);
                }
                ja.empty();
                ja.adopt(spans);
            }
        });
        
        $$('.travel-index').each(function(ul) {
            if (ul.retrieve('setting') == null) {
                ul.store('setting', true);
                var imgs = new Array();
                var postContent = ul.getParent('.post-content');
                var targetSections = postContent.getElements('.target-section');
                targetSections.each(function(targetSection) {
                    imgs.push(targetSection.getElement('.popup-group img'));
                    targetSection.store('defaultHeight', targetSection.getSize().y);
                    targetSection.setStyle('overflow', 'hidden');
                    targetSection.set('tween', {
                        property: 'height',
                        transition: 'expo:out',
                        link: 'cancel'
                    });
                });
                var no = 1;
                postContent.getElements('h3').each(function(h3, index) {
                    var img = imgs[index];
                    img.addClass('no-' + no);
                    if (no == 3) {
                        no = 1;
                    } else {
                        no++;
                    }
                    var targetSection = targetSections[index];
                    if (img) {
                        var src = img.get('index-thumb');
                        var text = h3.get('text').replace(/（.*?）/i, '');
                        var spot = new Element('span', {
                            'class': 'spot',
                            text: 'spot ' + (index + 1) + '.'
                        });
                        spot.inject(h3, 'top');
                        var li = new Element('li');
                        var anchor = new Element('a', {
                            href: '#' + h3.get('id'),
                            events: {
                                mouseenter: function() {
                                    li.set('tween', {
                                        property: 'opacity',
                                        duration: 300,
                                        transition: 'expo:out',
                                        link: 'cancel'
                                    }).get('tween').start(0.7);
                                },
                                mouseleave: function() {
                                    li.set('tween', {
                                        property: 'opacity',
                                        duration: 1000,
                                        transition: 'sine:in:out',
                                        link: 'cancel'
                                    }).get('tween').start(1);
                                }
                            },
                            styles: {
                                'background-image': 'url(' + src + ')'
                            }
                        });
                        /*
                        if (anchor.retrieve('anchorScrollEvent') == null) {
                            anchor.store('anchorScrollEvent', function(event) {
                                event.stop();
                                var fxScroll = new Fx.Scroll(window, {
                                    duration: 'long',
                                    transition: 'expo:out'
                                });
                                fxScroll.toElement(h3);

                            })
                            anchor.addEvent('click', anchor.retrieve('anchorScrollEvent'));
                        }
                        */
                        anchor.removeEvent('click', anchor.retrieve('anchorScrollEvent'));
                        anchor.store('anchorScrollEvent', true);
                        anchor.addEvent('click', function(event) {
                            event.stop();
                            targetSections.each(function(curTargetSection) {
                                if (curTargetSection == targetSection) {
                                    curTargetSection.get('tween').start(curTargetSection.retrieve('defaultHeight'));
                                } else {
                                    curTargetSection.get('tween').start(0);
                                }
                            });
                        });
                        var span = new Element('span', {
                            text: text
                        })
                        li.adopt(anchor, span);
                        ul.adopt(li);
                    }
                });
            }
        });
        
        var floorGuide = $('floor-guide');
        if (floorGuide) {
            if (floorGuide.retrieve('setting') == null) {
                floorGuide.store('setting', true);
                floorGuide.getElements('.floor-guide-point').each(function(point) {
                    point.addEvents({
                        mouseenter: function() {
                            if (point.retrieve('result') == null) {
                                var floorGuideSection = point.getParent('.floor-guide-section');
                                var id = point.get('id') + '-result';
                                var result = $(id);
                                if (result) {
                                    result = result.clone();
                                    result.addClass('floor-guide-result');
                                    result.setStyle('opacity', 0);
                                    floorGuideSection.adopt(result);
                                    result.setStyles({
                                        top: point.getCoordinates(floorGuideSection).top - result.getSize().y - 10,
                                        left: point.getCoordinates(floorGuideSection).left + point.getSize().x / 2 - result.getSize().x / 2
                                    });
                                    point.store('result', result);
                                }
                            }
                            point.retrieve('result').setStyle('display', 'block');
                            point.retrieve('result').set('tween', {
                                property: 'opacity',
                                transition: 'expo:out',
                                link: 'cancel'
                            }).get('tween').start(1);
                        },
                        mouseleave: function() {
                            if (point.retrieve('result')) {
                                point.retrieve('result').set('tween', {
                                    property: 'opacity',
                                    transition: 'expo:out',
                                    link: 'cancel'
                                }).get('tween').start(0).chain(
                                    function() {
                                        point.retrieve('result').setStyle('display', 'none');
                                    }
                                );
                            }
                        }
                    });
                });
            }
        }
        
        $$('.q-and-a').each(function(qa) {
            if (qa.retrieve('setting') == null) {
                qa.store('setting', true);
                var title = qa.getElement('h4');
                var content = qa.getElement('.note');
                new Element('span', { 'class': 'question', text: 'q.' }).inject(title, 'top');
                var contentWrapper = new Element('div');
                contentWrapper.wraps(content);
                var height = contentWrapper.getSize().y;
                contentWrapper.setStyles({
                    height: 0,
                    overflow: 'hidden'
                });
                contentWrapper.set('tween', {
                    property: 'height',
                    transition: 'expo:out',
                    link: 'cancel'
                });
                title.addEvents({
                    click: function() {
                        if (contentWrapper.getSize().y == 0) {
                            contentWrapper.get('tween').start(height);
                        } else {
                            contentWrapper.get('tween').start(0);
                        }
                    }
                });
            }
        });
        
        var wrapper = $('wrapper');
        if (wrapper && wrapper.getStyle('opacity').toInt() != 1) {
            wrapper.setStyle('opacity', 1);
            var reservationButton = $('reservation-button');
            if (reservationButton) {
                (function() {
                    reservationButton.set('morph', {
                        transition: 'back:out',
                        duration: 1000
                    }).get('morph').start({
                        opacity: 1,
                        'margin-top': reservationButton.retrieve('defaultMarginTop')
                    });
                }).delay(1500);
            }
        }
    },
    
    adjust: function() {
        var header = $('header');
        if (header) {
            header.setStyle('height', getWindowSize().y);
            if (header.getStyle('position') == 'fixed') {
                header.setStyle('margin-left', header.retrieve('defaultMarginLeft') - window.getScrollLeft());
            }
        }
        var footer = $('footer');
        if (footer) {
            var footerSize = footer.getSize();
            if (header.getStyle('position') == 'fixed') {
                footer.setStyle('margin-left', footer.retrieve('defaultMarginLeft') - window.getScrollLeft());
            }
            if (Browser.ie6) {
                footer.setStyles({
                    bottom: 'auto',
                    top: getWindowSize().y - footerSize.y
                });
            }
        }
        var pageTop = $('page-top');
        if (pageTop && this.defaultHeaderInnerSize) {
            if (window.getScrollTop() > 0) {
                pageTop.store('visible', true);
                pageTop.get('tween').start(1);
            } else {
                pageTop.store('visible', false);
                pageTop.get('tween').start(0);
            }
            if (getWindowSize().y < this.defaultHeaderInnerSize.y + footerSize.y + 50) {
                $(document.body).addClass('low');
            } else {
                $(document.body).removeClass('low');
            }
        }
    }
    
});

/* 
---------------------------------------------------------------------------------------------------
    Extra Global Nav
---------------------------------------------------------------------------------------------------
*/
var ExtraGlobalNav = new Class({
	Implements: [Options,Events],
    options: {
    },
    initialize: function(options) {
        this.setOptions(options);

        var globalNav = $(document.body).getElement('.lang-ja #global-nav');
        if (globalNav == null) {
            return;
        }
        
        var globalNavInner = $('global-nav-inner');
        var globalNavUl = globalNavInner.getElement('ul');
        var globalNavWidth = globalNav.getSize().x
        var globalNavUlWidth = globalNavUl.getSize().x
        globalNavInner.setStyles({
            width: globalNavUlWidth,
            overflow: 'hidden'
        });
        globalNavInner.set('morph', {
            transition: 'expo:in:out',
            link: 'cancel'
        });
        $$('#global-nav li').each(function(li) {
            var childUl = li.getElement('ul');
            if (childUl) {
                
                var childUlWidth = childUl.getDimensions().width;
                childUl.setStyles({
                    display: 'none',
                    opacity: 0,
                    'margin-left': - childUlWidth
                });
                childUl.set('tween', {
                    property: 'opacity',
                    transition: 'expo:in:out',
                    link: 'cancel'
                });
                li.set('tween', {
                    property: 'padding-left',
                    transition: 'expo:in:out',
                    link: 'cancel'
                });
                
                var paddingLeft = li.getStyle('padding-left').toInt();
                
                var timer = null;
                li.addEvents({
                    mouseenter: function() {
                        timer = (function() {
                            var globalNavInnerWidth = globalNavUlWidth + childUlWidth;
                            var over = (globalNavInnerWidth - globalNavWidth) / 2;
                            var marginLeft = - childUlWidth / 2;
                            if (over > 0) {
                                marginLeft = - globalNav.getStyle('padding-right').toInt() + 5;
                            }
                            globalNavInner.get('morph').start({
                                'margin-left': marginLeft,
                                width: globalNavInnerWidth
                            });

                            li.get('tween').start(childUlWidth);
                            childUl.setStyle('display', 'block');
                            childUl.get('tween').start(1);
                        }).delay(500);
                    }.bind(this),
                    mouseleave: function() {
                        clearTimeout(timer);
                        timer = null;
                        globalNavInner.get('morph').start({
                            'margin-left': 0,
                            width: globalNavUlWidth
                        });
                        li.get('tween').start(paddingLeft);
                        childUl.get('tween').start(0).chain(
                            function() {
                                childUl.setStyle('display', 'none');
                            }
                        );
                    }.bind(this)
                    
                });
            }
        }.bind(this));
    }
    
});

/* 
---------------------------------------------------------------------------------------------------
    Extra Random Gallery
---------------------------------------------------------------------------------------------------
*/
var ExtraRandomGallery = new Class({
	Implements: [Options,Events],
    srcs: new Array(),
    titles: new Array(),
    firstLoad: true,
    options: {
        randomGalleryBgSrc: null
    },
    initialize: function(options) {
        this.setOptions(options);
        
        /*
        if (
            Browser.ie6 == null && Browser.ie7 == null && Browser.ie8 == null && 
            Browser.Platform.ios == null &&
            Browser.Platform.android == null
        ) {
            window.addEvents({
                resize: function() {
                    this.adjust.apply(this);
                }.bind(this)
            });
        }
        */
    },
    
    run: function() {
        var randomGallery = $('random-gallery');
        var randomGalleryWrapper = $('random-gallery-wrapper');
        if (randomGallery == null || randomGalleryWrapper) {
            return;
        }
        randomGalleryWrapper = new Element('div', {
            id: 'random-gallery-wrapper',
            styles: {
                height: getWindowSize().y
            }
        });
        randomGalleryWrapper.wraps(randomGallery);
        
        randomGallery.getElements('li').each(function(li) {
            this.titles.push(li.get('title'));
            this.srcs.push(li.get('text'));
        }.bind(this));

        new Asset.images([this.options.randomGalleryBgSrc], {
            onComplete: function() {
                var content = $('content');
                var contentSize = {
                    x: content.getStyle('width').toInt()
                };
                
                randomGalleryWrapper.setStyle('height', getWindowSize().y);
                randomGallery.empty();
                randomGallery.setStyles({
                    display: 'block',
                    'margin-top': getWindowSize().y,
                    width: contentSize.x,
                    height: getWindowSize().y
                });
                contentSize = content.getSize();
                
                var totalWidth = 0;
                var totalHeight = 0;
                var randomGalleryImgs = new Array();
                var loopCount = 0;
                
                var min = 90;
                var max = 130;
                var curHeight = Number.random(min, max);
                while (totalHeight < getWindowSize().y && loopCount < 1000) {
                    var curWidth = Number.random(100, 250);
                    var randomGalleryImg = new Element('div', {
                        'class': 'random-gallery-img',
                        styles: {
                            width: curWidth,
                            height: curHeight
                        }
                    });
                    var img = new Element('img', {
                        src: this.options.randomGalleryBgSrc
                    });
                    randomGalleryImgs.push(randomGalleryImg);
                    randomGalleryImg.adopt(img);
                    randomGallery.adopt(randomGalleryImg);
                    
                    if (totalWidth + curWidth > contentSize.x) {
                        curWidth = contentSize.x - totalWidth;
                        if (Browser.ie6 && curWidth > 2) {
                            curWidth -= 2;
                        }
                        randomGalleryImg.setStyles({
                            width: curWidth,
                            height: curHeight
                        });
                        totalHeight = totalHeight + randomGalleryImg.getSize().y;
                        totalWidth = 0;
                        curHeight = Number.random(min, max);
                    } else {
                        totalWidth = totalWidth + curWidth;
                    }
                    loopCount++
                }
                
                var duration = 1000;
                if (this.firstLoad) {
                    this.firstLoad = false;
                } else {
                    duration = 0;
                }
                
                randomGallery.set('tween', {
                    property: 'margin-top',
                    duration: duration,
                    transition: 'circ:in:out'
                }).get('tween').start(0).chain(
                    function() {
                        new Asset.images(this.srcs, {
                            onComplete: function() {
                                this.adjust.apply(this, [true, randomGalleryImgs]);
                            }.bind(this)
                        });
                    }.bind(this)
                );
            }.bind(this)
        });
    },
    
    adjust: function(animation, randomGalleryImgs) {
        var randomGallery = $('random-gallery');
        var randomGalleryWrapper = $('random-gallery-wrapper');
        
        var imgs = new Array();
        var srcIndex = 0;
        randomGalleryImgs.each(function(randomGalleryImg, index) {
            var img = new Element('img', {
                src: this.srcs[srcIndex]
            });
            var title = new Element('div', {
                'class': 'random-gallery-title'
            });
            title.adopt(new Element('span', {
                html: this.titles[srcIndex]
            }));
            var borderRight = new Element('div', {
                'class': 'border-right',
                styles: {
                    height: 0
                }
            });
            var borderBottom = new Element('div', {
                'class': 'border-bottom',
                styles: {
                    width: 0
                }
            });
            img.store('borderRight', borderRight);
            img.store('borderBottom', borderBottom);
            img.store('title', title);
            randomGalleryImg.adopt(img);
            img.setStyle('opacity', 0);
            randomGalleryImg.adopt(title);
            randomGalleryImg.adopt(borderRight);
            randomGalleryImg.adopt(borderBottom);
            imgs.push(img);
            
            borderRight.store('defaultHeight', randomGalleryImg.getSize().y);
            borderBottom.store('defaultWidth', randomGalleryImg.getSize().x);

            img.setStyles({
                top: randomGalleryImg.getSize().y / 2,
                left: randomGalleryImg.getSize().x / 2
            });
            title.setStyles({
                width: randomGalleryImg.getSize().x,
                bottom: - title.getSize().y,
                opacity: 0
            });
            srcIndex = srcIndex + 1;
            if (srcIndex == this.srcs.length) {
                srcIndex = 0;
            }
        }.bind(this));
        
        var duration = 0;
        var delay = 0;
        if (animation) {
            duration = 1000;
            delay = 50;
        }
        imgs.shuffle();
        imgs.each(function(img, index) {
            (function() {
                var imgSize = img.getSize();
                var loopCount = 0;
                while (imgSize.x == 0 && imgSize.y == 0 && loopCount < 100) {
                    imgSize = img.getSize();
                    loopCount++;
                }
                var top = img.getStyle('top').toInt() - imgSize.y / 2;
                var left = img.getStyle('left').toInt() - imgSize.x / 2;
                var rate = 1.5;
                img.setStyles({
                    opacity: 0,
                    width: imgSize.x,
                    height: imgSize.y,
                    top: top,
                    left: 0
                });
                
                img.set('morph', {
                    duration: duration,
                    transition: 'sine:in:out',
                    link: 'cancel'
                }).get('morph').start({
                    opacity: 1,
                    left: left
                }).chain(
                    function() {
                        if (index + 1 == imgs.length) {
                            imgs.each(function(img) {
                                var borderRight = img.retrieve('borderRight');
                                var borderBottom = img.retrieve('borderBottom');
                                borderRight.set('tween', {
                                    property: 'height',
                                    duration: duration,
                                    transition: 'sine:in:out',
                                    link: 'cancel'
                                }).get('tween').start(borderRight.retrieve('defaultHeight'));
                                borderBottom.set('tween', {
                                    property: 'width',
                                    duration: duration,
                                    transition: 'sine:in:out',
                                    link: 'cancel'
                                }).get('tween').start(borderBottom.retrieve('defaultWidth'));
                            });
                        }
                        img.getParent('.random-gallery-img').addEvents({
                            mouseenter: function() {
                                img.retrieve('title').set('morph', {
                                    duration: 500,
                                    transition: 'expo:out',
                                    link: 'cancel'
                                }).get('morph').start({
                                    bottom: 0,
                                    opacity: 1
                                });
                            },
                            mouseleave: function() {
                                img.retrieve('title').set('morph', {
                                    duration: 500,
                                    transition: 'expo:out',
                                    link: 'cancel'
                                }).get('morph').start({
                                    bottom: - img.retrieve('title').getSize().y,
                                    opacity: 0
                                });
                            }
                        });
                    }
                );
            }).delay(index * delay);
        }.bind(this));
    }
    
});
