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

/* 
---------------------------------------------------------------------------------------------------
    Extra Init
---------------------------------------------------------------------------------------------------
*/
var ExtraInit = new Class({
	Implements: [Options,Events],
    timer: null,
    options: {
    },
    initialize: function(options) {
        this.setOptions(options);
        if (Browser.Platform.ios) {
            $(document.body).addClass('ios');
        }
        
        var toggle = $('toggle-bg');
        if (toggle) {
            var toggleState = Cookie.read('akino-toggle-onoff');
            if (toggleState) {
                toggle.addClass('on');         
            } else {
                toggle.addClass('off');         
            }
            toggle.addEvent('click', function(event) {
                if (toggle.hasClass('on')) {
                    toggle.removeClass('on');
                    toggle.addClass('off');
                    Cookie.dispose('akino-toggle-onoff');
                    clearTimeout(this.timer);
                    $$('.flower').each(function(flower) {
                        flower.get('tween').start(0);
                    });
                } else {
                    toggle.removeClass('off');
                    toggle.addClass('on');
                    Cookie.write('akino-toggle-onoff', 1);
                    this.timer = this.addFlower.periodical(6000);
                    this.addFlower.apply(this);
                }
            }.bind(this));
            if (toggle.hasClass('on')) {
                this.timer = this.addFlower.periodical(6000);
                this.addFlower.apply(this);
            }
        }
        
        $$('.post-content h2, .post-content h3, post-content h4, .post-content h5, .post-content h6').each(function(heading) {
            var html = heading.get('html');
            heading.set('html', '');
            heading.adopt(new Element('span', {
                html: html
            }));
        });
        
        if ($('header').getStyle('position') == 'fixed') {
            window.addEvents({
                resize: this.adjust,
                scroll: this.adjust
            });
        }
        
        $$('h1.post-title').each(function(postTitle) {
            if (postTitle.retrieve('setting') == null) {
                postTitle.store('setting', true);
                var en = postTitle.getElement('.en');
                var ja = postTitle.getElement('.ja');
                if (en && ja) {
                    if (!Browser.ie6 && !Browser.ie7) {
                        var spans = new Array();
                        for (var i = 0; i < en.get('text').length; i++) {
                            var span = new Element('span', {
                                text: en.get('text').slice(i, i + 1),
                                styles: { opacity: 0 }
                            });
                            spans.push(span);
                            en.store('spans', spans);
                        }
                        en.set('html', '');
                        en.adopt(spans);
                        
                        ja.setStyle('opacity', 0);
                    }
                }
            }
        });
        
        $$('input[type=submit]').each(function(input) {
            input.addClass('submit');
            if (input.get('value') == '商品に関するお問い合わせ') {
                input.addClass('mail');
            }
        });
    },
    
    run: function() {
    
        if (!Browser.ie6 && !Browser.ie7) {
            $$('h1.post-title').each(function(postTitle) {
                var en = postTitle.getElement('.en');
                var ja = postTitle.getElement('.ja');
                if (en && ja) {
                    if (en.retrieve('visible') == null) {
                        var spans = en.retrieve('spans');
                        en.store('visible', true) ;
                        spans = spans.shuffle();
                        spans.each(function(span, index) {
                            var duration = 50;
                            (function() {
                                span.tween('opacity', 1);
                                if (index + 1 == spans.length) {
                                    (function() {
                                        ja.tween('opacity', 1);
                                    }).delay(500);
                                }
                            }).delay(duration * index);
                        });
                    }
                }
            });
        }
    },
    
    adjust: function() {
        if (window.getScrollTop() > $('content').getStyle('padding-top').toInt()) {
            $(document.body).addClass('scrolled');
        } else {
            $(document.body).removeClass('scrolled');
        }
        if (getWindowSize().y < 800) {
            $(document.body).addClass('low');
        } else {
            $(document.body).removeClass('low');
        }
    },
    
    addFlower: function() {
        var windowSize = getWindowSize();
        for (var i = 0; i < 5; i++) {
            (function() {
                var flower = new Element('span', {
                    'class': 'flower',
                    styles: {
                        opacity: 0,
                        top: Number.random(0, windowSize.y / 2),
                        left: Number.random(windowSize.x * 0.1, windowSize.x * 0.9)
                    }
                });
                flower.addClass('no-' + Number.random(1,2));
                $(document.body).adopt(flower);
                flower.set('tween', {
                    property: 'opacity',
                    duration: 1000
                }).get('tween').start(1);
                
                flower.set('morph', {
                    duration: 8000,
                    transition: 'sine:in'
                }).get('morph').start({
                    top: flower.getStyle('top').toInt() + (flower.hasClass('no-2') ? Number.random(windowSize.y / 10, windowSize.y / 7) : Number.random(windowSize.y / 4, windowSize.y / 2))
                });
                (function() {
                    flower.set('tween', {
                        property: 'opacity',
                        duration: 1000
                    }).get('tween').start(0).chain(
                        function() {
                            if (Browser.ie == null) {
                                flower.destroy();
                            }
                        }
                    );
                }).delay(7000);
            }).delay(i * 10);
        }
    }
    
});

/* 
---------------------------------------------------------------------------------------------------
    Extra Load CSS
---------------------------------------------------------------------------------------------------
*/
var ExtraLoadCSS = new Class({
	Implements: [Options,Events],
    options: {
        css: new Array()
    },
    initialize: function(options) {
        this.setOptions(options);

        var cssContainer = new Element('div', {
            styles: {
                position: 'fixed',
                top: 0,
                left: 0,
                'z-index': 9999
            }
        });
        this.options.css.each(function(css, index) {
            var cssNo = new Element('span', {
                text: index + 1,
                styles: {
                    display: 'inline-block',
                    cursor: 'pointer',
                    padding: '5px'
                },
                events: {
                    click: function() {
                        Asset.css(css + '?' + (new Date()).getTime());
                    }
                }
            });
            cssContainer.adopt(cssNo);
        });
        
        $(document.body).adopt(cssContainer);
    }
});

