/*
$.animate([
    {obj: obj1, params:{height:10,width:32,margin:30,left:5}},
    {obj: obj2, params:{height:20,width:62,margin:20,left:45}},
    {obj: obj3, params:{height:30,width:92,margin:10,left:25}},
    {obj: obj4, params:{height:40,width:122,margin:00,left:23}}
]);
*/
/**
 * fishnav Javascript
 * 
 * @author: Chanel Munezero <chanel.munezero@escapemg.com>
 */
(function(){
    if(!window.gs) {
        return;
    }
    
    var fishnav = window.gs.fishnav = {
        // variables
        container: null,
        items: [],
        origNumKids: 0,
        itemWidth: null,
        nextObj: null,
        prevObj: null,
        titles: null,
        
        animDuration: 500,
        animTimeout: null,
        animInterval: null,
        animLock: false,
        animCount: 0,
        
        endOpacity: .3,
        endSize: '70%',
        endMargin: '15px 0px',
        fourthMargin: '15px 0px 15px 70px',
        innerOpacity: .6,
        centerOpacity: 1,
        centerSize: '100%',
        centerMargin: '0px',
        
        empty: null
    };
    
    fishnav.init = function(opts)
    {
        // default options/settings
        var options = {
            container: '.fishnav',
            nxt: '.next',
            prev: '.prev',
            titles: '.titles',
            
            empty: null
        }
        $.extend(options, opts);
        
        fishnav.getElements(options);
        fishnav.addEvents();
    };
    
    fishnav.getElements = function(opts)
    {
        //debug(opts);
        fishnav.container = $(opts.container).get(0);
        var validPos = ['absolute','relative','fixed']
        var pos = $(fishnav.container).css('position');
        var parPos = $(fishnav.container).parent().css('position');
        // make sure parent of container has valid position, if not, set to relative
        if(!validPos.inArray(parPos)) {
            $(fishnav.container).parent().css('position', 'relative');
        }
        // do same for container
        if(!validPos.inArray(pos)) {
            $(fishnav.container).css('position', 'relative');
        }
        // grab kids, its num and clone them
        fishnav.origWidth = $(fishnav.container).width();
        fishnav.items = $(fishnav.container).children().get();
        fishnav.origNumKids = fishnav.items.length;
        fishnav.itemWidth = $(fishnav.items).width() + parseInt($(fishnav.items).css('margin-left')) + parseInt($(fishnav.items).css('margin-right'));
        
        // try to grab valid nxt/prev buttons
        var nxt = $(opts.nxt, opts.container);
        fishnav.nextObj = (nxt.length) ? nxt : $(opts.nxt);
        
        var prev = $(opts.prev, opts.container);
        fishnav.prevObj = (prev.length) ? prev : $(opts.prev);
        
        var titles = $(opts.titles, opts.container);
        fishnav.titles = (titles.length) ? titles : $(opts.titles);
    }
    
    fishnav.addEvents = function()
    {
        $(fishnav.nextObj).click(function(event) {
            //debug('next');
            gs.fishnav.nxt(event, this);
            return false;
        });
        $(fishnav.prevObj).click(function(event) {
            //debug('prev');
            gs.fishnav.prev(event, this);
            return false;
        });
        //$(fishnav.items[3]).css('margin-left','70px');
        $(fishnav.items).each(function(i) {
            $(this).click(function(event) {
                //debug('click');
                gs.fishnav.itemClick(event, this);
                return false;
            });
        });
    }
    
    fishnav.nxt = function(event, obj)
    {
        event = event || window.event;
        fishnav.animate(1);
    }
    fishnav.prev = function(event, obj)
    {
        event = event || window.event;
        fishnav.animate(-1);
    }
    
    fishnav.itemClick = function(event, obj)
    {
        event = event || window.event;
        fishnav.animate(null, obj);
    }
    
    fishnav.proxyItemClick = function(obj)
    {
        obj = obj.parentNode;
        fishnav.itemClick(null, fishnav.items[$(obj).prevAll().length]);
    }
    
    fishnav.initTheme = function()
    {
        $('#initTheme').click();
    }
    
    fishnav.animateToType = function(type)
    {
        $('#fishnav li').each(function() {
            //debug(this);
            //debug($(this).attr('value'));
            if(type == $(this).attr('value')) {
                fishnav.proxyItemClick(this);
                return false;   // break out of each
            }
        });
    }
    
    fishnav.animate = function(direction, obj)
    {
        //debug('start animate: '+fishnav.animCount);
        fishnav.animCount++;
        if(fishnav.animCount>10) {
            //debug('anim count too big',direction,obj,fishnav.items);
            return;
        }
        // don't let more than one animation in progress
        if(fishnav.animLock) {
            return;
        }
        
        // can't handle both being null
        if(obj==null && direction==null) {
            return;
        }
        // you can't move 0 spaces, so return
        if(direction==0) {
            return;
        }
        fishnav.items = $(fishnav.container).children().get();
        if(direction==null) {
            $(fishnav.items).each(function(i) {
                if(this==obj) {
                    direction = i-2;
                    return false;   // break out of each
                }
            });
            return fishnav.animate(direction, obj);
        }
        // helper variable to provide single reference to which direction to move in
        var isRight = (direction>0) ? true : false;
        if(obj==null) {
            obj = fishnav.items[2-direction];
            return fishnav.animate(direction, obj);
        }
        //debug('final direction and obj: '+direction, obj);
        // lock the animation down
        fishnav.animLock = true;
        var curLeft = parseInt($(fishnav.container).css('left')) + (Math.abs(direction) * fishnav.itemWidth);
        //var newLeft = (isRight) ? curLeft - fishnav.itemWidth : curLeft + fishnav.itemWidth;
        
        var typeIndex = 2 + ((isRight)?1:-1);
        var type = fishnav.items[typeIndex].attributes.value.nodeValue;
        if(!$('#theme-'+type+':visible').length) {
            $('#themes').children().hide('fast');
        }
        
        if(isRight) {
            // clone first node to the end
            for(var i=0; i<Math.abs(direction); i++) {
                $(fishnav.container).append(fishnav.items[i].cloneNode(true));
            }
            
            // move titles around
            $(fishnav.titles).fadeOut('fast', function() {
                for(var i=0; i<Math.abs(direction); i++) {
                    var item = $($(fishnav.titles).children().get(0)).remove();
                    $(fishnav.titles).append(item);
                }
                $(fishnav.titles).fadeIn('fast');
            });
            
            $(fishnav.container).animate({left:'-'+curLeft+'px'},'slow', function() {
                // delete first node
                //$(fishnav.items[0]).remove();
                for(var i=0; i<Math.abs(direction); i++) {
                    $(fishnav.items[i]).remove();
                }
                $(fishnav.container).css('left', 0);
                
                fishnav.animLock = false;
                fishnav.animCount = 0;
                fishnav.items = null;
                fishnav.items = $(fishnav.container).children().get();
                gs.widget.showFormByType(fishnav.items[2].attributes.value.nodeValue);
            });
        } else {
            // clone last node and move it first
            $(fishnav.container).css('left', -curLeft);
            for(var i=0; i<Math.abs(direction); i++) {
                $(fishnav.container).prepend(fishnav.items[fishnav.items.length - i - 1].cloneNode(true));
            }
            
            // move titles around
            $(fishnav.titles).fadeOut('fast', function() {
                for(var i=0; i<Math.abs(direction); i++) {
                    var item = $($(fishnav.titles).children().get($(fishnav.titles).children().length - 1 )).remove();
                    $(fishnav.titles).prepend(item);
                }
                $(fishnav.titles).fadeIn('fast');
            });
            
            $(fishnav.container).animate({left:'0px'},'slow', function() {
                // delete last node
                for(var i=0; i<Math.abs(direction); i++) {
                    var last = fishnav.items[fishnav.items.length - i - 1];
                    last.parentNode.removeChild(last);
                }
                
                fishnav.animLock = false;
                fishnav.animCount = 0;
                fishnav.items = null;
                fishnav.items = $(fishnav.container).children().get();
                gs.widget.showFormByType(fishnav.items[2].attributes.value.nodeValue);
            });
        }
        /**
         * 1) depending on direction, clone nodes
         *  -left: first cloned to the end
         *  -right: last cloned to the front
         * 
         * 2) run multiple animations synchronously
         *  -move entire container (depends on container's parent width)
         */
    }
})();