/**
 * Horizontal Scroller
 * Code modfied from gallery-scroll-nav (http://yuilibrary.com/gallery/show/scroll-nav)
 **/
YUI.add('horz-scroller', function(Y) {
    var ScrollNav;
    
    ScrollNav = function(config) {
        ScrollNav.superclass.constructor.apply(this, arguments);
    };
    
    Y.extend(ScrollNav, Y.Plugin.Base, {
        initializer : function(config){
            Y.delegate('click', function(e){

                var href = e.currentTarget.getAttribute('href'),
                    targetY, hash, anim;

                // make sure the link we clicked has a hash and is for this page, not a link elsewhere
                if('#' !== href.substring(0,1)){
                    return;
                }
                
                e.preventDefault(); // if we made it this far let's prevent the link from firing
                hash = href.substring(1); // get the hash from the clicked href
                
                // find the target with the hash
                if(hash === '') {
                    targetX = 0;
                }else{
                    targetX = Y.one('#' + hash).getX();
                    targetX = targetX - 30; // margin of scroll from left so we have a little padding
                }

                if(targetX !== null) {
                    // pause the animation if it's running,
                    // stopping causes the scroll bar to jump
                    try {
                        if(this.anim.get('running')) {
                            this.anim.pause();
                        }
                    }catch(e){
                        // no animation to pause
                    }
        
                    // record current window conditions/widths
                    var winW = Y.DOM.winWidth(),
                        docW = Y.DOM.docWidth();
                    
                    // create the animation and run it
                    this.anim = new Y.Anim({
                        node: this.get('scroller'),
                        to: { // can't scoll to target if it's beyond the doc height - window height
                            //scroll : [Y.DOM.docScrollX(), Math.min(docW - winW, targetX)] // for vertical scrolling
                            scroll: [Math.min(docW - winW, targetX), Y.DOM.docScrollY()]
                        },
                        duration: this.get('duration'),
                        easing: this.get('easing'),
                        on : {
                            end : function() { location.hash = hash; }
                        }
                    }).run();
                }
            }, this.get('host'), this.get('selector'), this);
        }
    },{
        NAME : 'scroll-nav',
        NS : 'scrollNav',
        ATTRS : {
            easing : {
                value : Y.Easing.easeOutStrong
            },
            duration : {
                value : 1.5
            },
            selector : {
                value : 'a'
            },
            scroller : {
                value : 'window'
            }
        }
    });
    Y.namespace('Plugin').ScrollNav = ScrollNav;
}, 'indorg' ,{requires:['node','event','anim','plugin','base']});

/**
 * Implement horizontal scrolling on page
 **/
YUI().use('horz-scroller', 'node', 'anim', function(Y) {
    
    Y.one('header').plug(Y.Plugin.ScrollNav);
    
    // change scrollwheel to scroll horizontally
    var anim;
    Y.on('mousewheel', function(e){
        e.stopImmediatePropagation();
        /*
        try {
            if(anim.get('running')) {
                anim.pause();
            }
        }catch(e){
            // no animation to pause
        }

        var pixelsToMove = 100 * e.wheelDelta;
        var moveTo = Y.DOM.docScrollX()-pixelsToMove;
        
        // we need to prepare the x locations of the actual divs
        // to make sure mousewheel scrolling kind of "clicks" into place
        var infoDiv = Math.ceil(Y.one('#info').getX())-30;
        var workDiv = Math.ceil(Y.one('#work').getX())-30;
        var writingDiv = Math.ceil(Y.one('#writing').getX())-30;
        var websitesDiv = Math.ceil(Y.one('#websites').getX())-30;
        //var contactDiv = Math.ceil(Y.one('#contact').getX())-30;

        // within 100 pixels of a content div, scroll the content div into place
        if( moveTo > workDiv - 100 && moveTo < workDiv + 100 ) {
            moveTo = workDiv;
        } else if( moveTo > writingDiv - 100 && moveTo < writingDiv + 100 ) {
            moveTo = writingDiv;
        } else if( moveTo > websitesDiv - 100 && moveTo < websitesDiv + 100 ) {
            moveTo = websitesDiv;
        //} else if( moveTo > contactDiv - 100 && moveTo < contactDiv + 100 ) {
        //    moveTo = contactDiv;
        }
        
        // yay animation finally
        anim = new Y.Anim({
            node : window,
            to: {
                scroll: [moveTo, Y.DOM.docScrollY()]
            },
            duration: 1,
            easing: Y.Easing.easeOutStrong
        }).run();
        */
    }, 'body', this );
    Y.on( 'scroll', function(e){
        e.stopImmediatePropagation();
    }, 'body', this );

});

/**
 * Work stuff!
 **/
YUI().use('node', 'anim', function(Y) {

    Y.on( 'click', function(e){
        e.preventDefault();
        var liTarget = e.target.ancestor(function(o){
            var id = o.getAttribute('id');
            if( id == 'work-code-trigger' ||
                id == 'work-design-trigger' ||
                id == 'work-tools-trigger' )
                return o;
        });
        var type = liTarget.getAttribute('id').replace('-trigger','');
        
        var hideNodeAnim = new Y.Anim({
            node: '#work-intro',
            to: { 'height' : 0 },
            duration: 0.5,
            easing: Y.Easing.easeOutStrong
        });
        var showNodeAnim = new Y.Anim({
            node: '#'+type,
            to: { 'height' : 290 },
            duration: 0.5,
            easing: Y.Easing.easeOutStrong
        });
        showNodeAnim.on( 'start', function(e) {
            hideNodeAnim.run();
        }, this);
        showNodeAnim.run();
    
    }, '#work-triggers li' );

    Y.on( 'click', function(e){
        e.preventDefault();
        var liTarget = e.target.ancestor(function(o){
            var id = o.getAttribute('id');
            if( id == 'work-code' ||
                id == 'work-design' ||
                id == 'work-tools' )
                return o;
        });
        var type = liTarget.getAttribute('id');
        
        var hideNodeAnim = new Y.Anim({
            node: '#'+type,
            to: { 'height' : 0 },
            duration: 0.5,
            easing: Y.Easing.easeOutStrong
        });
        var showNodeAnim = new Y.Anim({
            node: '#work-intro',
            to: { 'height' : 290 },
            duration: 0.5,
            easing: Y.Easing.easeOutStrong
        });
        showNodeAnim.on( 'start', function(e) {
            hideNodeAnim.run();
        }, this);
        showNodeAnim.run();
    
    }, '#work .bd h2 a' );
    
    // detail view
    var detailWidth = 560;
    Y.on( 'click', function(e){
        var liTarget = e.target.ancestor(function(o){
            var id = o.get('id').split('-');
            if( id[0] == 'work' )
                return o;
        });
        var id = liTarget.get('id').split('-');
        var pos = id[id.length-1];
        var sectionId = id[0] + '-' + id[1];
        var to = detailWidth * pos;
        
        var showNodeAnim = new Y.Anim({
            node: '#'+sectionId+' .work-container',
            to: { 'left' : -to },
            duration: 0.5,
            easing: Y.Easing.easeOutStrong
        }).run();
    }, '.work-detail-nav li' );

    Y.on( 'click', function(e){
        e.preventDefault();
        var sectionTarget = e.target.ancestor(function(o){
            var id = o.getAttribute('id');
            if( id == 'work-code' ||
                id == 'work-design' )
                return o;
        });
        var id = sectionTarget.get('id');

        var showNodeAnim = new Y.Anim({
            node: '#'+id+' .work-container',
            to: { 'left' : 0 },
            duration: 0.5,
            easing: Y.Easing.easeOutStrong
        }).run();
    }, '.work-detail-back' );
        
});

/**
 * Misc stuff
 **/
YUI().use('node', function(Y) {
    // add page style enhancing stuff
    Y.on( 'domready', function(e){
        // pretty borders on images
        Y.all('section .bd img').each(function(o){
            var size = 6;
            if( Y.one( '#info' ).contains(o) || Y.one( '#work-triggers' ).contains(o) )
                size = 10;
            
            // get width and height of element for border "size"
            var w = o.get('width') - size,
                h = o.get('height') - size;
                
            // wrap the object so that we can have relative positioning for the fake border
            var wrapper = Y.Node.create('<div class="border-wrapper"></div>');
            wrapper.append( o.replace(wrapper) );
            
            // actually add the border
            o.insert('<div class="nice-border" style="width:'+w+'px;height:'+h+'px;"> </div>', 'after');
        });
    });
});

/*** FOR SOME REASON this makes my JS choke. wtf is it doing to my JS waaaah T_T ***/
YUI({
    //Last Gallery Build of this module
    gallery: 'gallery-2010.04.08-12-35'
}).use('gallery-lightbox', function(Y) {
    Y.Lightbox.init();
});
// **/
