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

/* 
---------------------------------------------------------------------------------------------------
    ExtraInit
---------------------------------------------------------------------------------------------------
*/
var ExtraInit = new Class({
    Implements: Options,
    options: {
    },
    initialize: function(options) {
        this.setOptions(options);
        $$('h2.post-title').each(function(postTitle, index) {
            (function() {
                if (postTitle.hasClass('rotate') == false) {
                    postTitle.addClass('rotate');
                    postTitle.addClass('rotate-left');
                    (function() {
                        postTitle.removeClass('rotate');
                        postTitle.removeClass('rotate-left');
                    }).delay(2500);
                }
            }).delay(100 * index);
        });
        
        if (Browser.Platform.ipod) {
            window.addEventListener('orientationchange', function() { 
                $$('h2.post-title').each(function(postTitle, index) {
                    if (postTitle.hasClass('rotate') == false) {
                        postTitle.addClass('rotate');
                        postTitle.addClass('rotate-left');
                        (function() {
                            postTitle.removeClass('rotate');
                            postTitle.removeClass('rotate-left');
                        }).delay(2500);
                    }
                });
            }, false);
        }
    }
});

/*
---------------------------------------------------------------------------------------------------
    Extra Section Focus
---------------------------------------------------------------------------------------------------
*/
var ExtraSectionFocus = new Class({
    Implements: Options,
    options: {
    },
    initialize: function(options) {
        this.setOptions(options);
        
        $$('.page-section .post-title').each(function(postTitle) {
        
            postTitle.addEvents({
                mouseenter: function(mouse) {
                    if (Browser.safari || Browser.chrome) {
                        var width = postTitle.getSize().x;
                        if (mouse.event.layerX <= width / 2) {       // from left
                            if (postTitle.hasClass('rotate') == false) {
                                postTitle.addClass('rotate');
                                postTitle.addClass('rotate-left');
                                (function() {
                                    postTitle.removeClass('rotate');
                                    postTitle.removeClass('rotate-left');
                                }).delay(2500);
                            }
                        } else {                                    // from right
                            if (postTitle.hasClass('rotate') == false) {
                                postTitle.addClass('rotate');
                                postTitle.addClass('rotate-right');
                                (function() {
                                    postTitle.removeClass('rotate');
                                    postTitle.removeClass('rotate-right');
                                }).delay(2500);
                            }
                        }
                    } else {
                        postTitle.set('tween', {
                            property: 'margin-top',
                            duration: 'short',
                            transition: 'bounce:out',
                            link: 'cancel'
                        }).get('tween').start(20);
                    }
                    //this.focused.run([postTitle], this);
                }.bind(this),
                
                mouseleave: function() {
                    if (!Browser.safari && !Browser.chrome) {
                        postTitle.set('tween', {
                            property: 'margin-top',
                            duration: 'short',
                            transition: 'back:out',
                            link: 'cancel'
                        }).get('tween').start(0);
                    }
                
                    //this.unFocused.run([postTitle], this);
                }.bind(this)
            });
            
        }.bind(this));
    },
    
    focused: function(self) {
        var section = self.getParent('.category-section');

        $$('.category-section').each(function(curSection) {
            var curContent = curSection.getElement('.category-content');
            if (curSection == section) {
                curContent.get('tween', {
                    property: 'opacity',
                    duration: 'short',
                    transition: 'expo:out',
                    link: 'cancel'
                }).start(1);
            } else {
                curContent.get('tween', {
                    property: 'opacity',
                    duration: 'short',
                    transition: 'expo:out',
                    link: 'cancel'
                }).start(0.05);
            }
        });
    },
    
    unFocused: function(self) {
        var section = self.getParent('.category-section');
        
        $$('.category-section').each(function(curSection) {
            if (curSection != section) {
                curSection.getElement('.category-content').get('tween', {
                    property: 'opacity',
                    duration: 'short',
                    transition: 'expo:in',
                    link: 'cancel'
                }).start(1);
            }
        });
    }
});

/*
---------------------------------------------------------------------------------------------------
    Extra Mask
---------------------------------------------------------------------------------------------------
*/
var ExtraMask = new Class({
	Implements: [Options,Events],
    maskSrc: null,
    options: {
        maskCount: 3,
		onComplete: function(){}
    },
    initialize: function(options) {
        this.setOptions(options);
    },
    
    run: function() {
        var date = new Date();
        var hour = date.get('hr');
        var timeZone = null;
        if (0 <= hour && hour <= 4) {
            timeZone = 'evening';
        } else if (5 <= hour && hour <= 10) {
            timeZone = 'morning';
        } else if (11 <= hour && hour <= 17) {
            timeZone = 'daytime';
        } else if (18 <= hour && hour <= 24) {
            timeZone = 'evening';
        }
        var prevIndex = null;
        $$('.grid-image.grid-content').each(function(gridContent) {
            var gridContentSize = gridContent.getSize();
            var mask = new Element('div', {
                'class': 'mask png',
                styles: {
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: gridContentSize.x,
                    height: gridContentSize.y,
                    'background-position': '50% 50%',
                    'background-repeat': 'no-repeat'
                }
            });
            var index = 1;
            if (this.options.maskCount > 1) {
            	while(true){
            		var index = Number.random(1, this.options.maskCount);
            		if(prevIndex != index){
                        prevIndex = index;
            			break;
            		}
            	}
            }
            mask.addClass('mask-' + index);
            mask.addClass('mask-' + timeZone);
            gridContent.adopt(mask);

            this.maskSrc = mask.getStyle('background-image');
        }.bind(this));

        var src = (this.maskSrc.match(/url\((.+)\)/) || [])[1] || null;
        if (src) {
            new Asset.images([src], {
                onComplete: function() {
                    this.fireEvent('complete');
                }.bind(this)
            });
        } else {
            this.fireEvent('complete');
        }
        
    },
    
    fade: function() {
        (function() {
            $$('.mask').each(function(mask, index) {
                (function() {
                    mask.set('tween', {
                        property: 'opacity'
                    }).get('tween').start(0)
                }).delay(100 * index);
            });
        }).delay(3000);
    }
});

