/*
Copyright (c) 2011, STRAIGHTLINE All rights reserved.
*/

/* 
---------------------------------------------------------------------------------------------------
    Extra Init
---------------------------------------------------------------------------------------------------
*/
var ExtraInit = new Class({
	Implements: [Options,Events],
    resizeTimer: null,
    scrollTimer: null,
    options: {
    },
    initialize: function(options) {
        this.setOptions(options);

        if (Browser.ie6 || Browser.Platform.ios) {
            $(document.body).addClass('disable-fixed');
        }
        if (Browser.Platform.ios) {
            $(document.body).addClass('ios');
        }

        $(document.body).setStyle('overflow', 'hidden');
        
        $('global-nav').setStyle('opacity', 0);
        $('content').setStyle('opacity', 0);
        $('footer').setStyle('opacity', 0);
        
        window.scrollTo(0, 0);
        var title = $('title');
        title.setStyle('padding-top', getWindowSize().y - title.getSize().y - (300 + getWindowSize().y / 12));
        window.addEvents({
            resize: function() {
                clearTimeout(this.resizeTimer);
                this.resizeTimer = (function() {
                    this.adjust.apply(this);
                    this.showPortfolioTitle.apply(this);
                }.bind(this)).delay(200);
            }.bind(this),
            scroll: function() {
                clearTimeout(this.resizeTimer);
                this.scrollTimer = (function() {
                    this.showPortfolioTitle.apply(this);
                }.bind(this)).delay(400);
            }.bind(this)
        });
        this.adjust.apply(this);
    },
    
    firstLoad: function() {
        var wrapperFx = new Fx.Tween($('wrapper'), {
            property: 'opacity',
            onComplete: function() {
                (function() {
                    $('title').set('tween', {
                        property: 'padding-top',
                        duration: 1000,
                        transition: 'expo:out'
                    }).get('tween').start(0).chain(
                        function() {
                            $('global-nav').tween('opacity', 1);
                            $('footer').tween('opacity', 1);
                            $(document.body).setStyle('overflow', 'visible');
                            this.fireEvent('complete');
                        }.bind(this)
                    );
                }.bind(this)).delay(500);
            }.bind(this)
        });
        wrapperFx.start(1);
    },
    
    run: function() {
    
        var pageNav = $(document.body).getElement('.page-nav');
        if (pageNav) {
            pageNav.addEvents({
                mouseenter: function() {
                    pageNav.set('tween', {
                        property: 'opacity',
                        duration: 500,
                        transition: 'expo:out',
                        link: 'cancel'
                    }).get('tween').start(0.3);
                },
                mouseleave: function() {
                    pageNav.set('tween', {
                        property: 'opacity',
                        duration: 1000,
                        transition: 'sine:in:out',
                        link: 'cancel'
                    }).get('tween').start(1);
                }
            });
        }
    
        $$('.post-portfolio').each(function(postPortfolio) {
            if (postPortfolio.retrieve('setting') == null) {
                postPortfolio.store('setting', true);
                if (!Browser.ie6 && !Browser.ie7) {
                    var title = postPortfolio.getElement('.post-title-text');
                    var spans = new Array();
                    for (var i = 0; i < title.get('text').length; i++) {
                        var span = new Element('span', {
                            text: title.get('text').slice(i, i + 1),
                            styles: { opacity: 0 }
                        });
                        spans.push(span);
                        postPortfolio.store('spans', spans);
                    }
                }
                
                var anchor = postPortfolio.getElement('.post-title .url');
                if (anchor) {
                    var href = anchor.get('href');
                    var postContent = postPortfolio.getElement('.post-content');
                    var div = new Element('div', {
                        text: 'Website : '
                    });
                    div.adopt(new Element('a', {
                        'class': 'post-visit',
                        href: href,
                        text: href
                    }));
                    postContent.adopt(div);
                    if (!Browser.ie6 && !Browser.ie7) {
                        anchor.set('html', '');
                        anchor.adopt(spans);
                    }
                } else {
                    if (!Browser.ie6 && !Browser.ie7) {
                        title.set('html', '');
                        title.adopt(spans);
                    }
                }
                
                var date = postPortfolio.getElement('.post-date');
                if (date) {
                    var postContent = postPortfolio.getElement('.post-content');
                    var div = new Element('div', {
                        html: 'Date : ' + date.get('html')
                    });
                    postContent.adopt(div);
                    date.destroy();
                }
            }
        });

        this.showPortfolioTitle.apply(this);
        this.adjust.apply(this);
    },
    
    adjust: function() {
        var body = $(document.body);
        if (getWindowSize().x < 1024) {
            body.addClass('narrow');
        } else {
            body.removeClass('narrow');
        }
    },
    
    showPortfolioTitle: function() {
        if (!Browser.ie6 && !Browser.ie7) {
            var scrollBottom = window.getScrollTop() + window.getSize().y - 50;
            $$('.post-portfolio').each(function(postPortfolio) {
                if (postPortfolio.retrieve('visible') == null) {
                    var spans = postPortfolio.retrieve('spans');
                    if (spans && scrollBottom > spans[0].getCoordinates($('wrapper')).top) {
                        postPortfolio.store('visible', true) ;
                        spans = spans.shuffle();
                        spans.each(function(span, index) {
                            var duration = 50;
                            (function() {
                                span.tween('opacity', 1);
                            }).delay(duration * index);
                        });
                    }
                }
            });
        }
    }
});




