

var Resizer = {
    debug: function(){
        if($.browser.mozilla || $.browser.webkit || $.browser.safari){
            try { 
                console.log.apply(console, arguments);
            } catch (e) {
            
            }
        }
    },
    Util: {
        
        scaleElement: function(percent){
            return $(this).each(function(){
                var $el = $(this);
                $el.height( $el.height() * percent );
                $el.width( $el.width() * percent );
                return;
                var h = $el.css('height') || $el.attr('height') || $el.height();
                var w = $el.css('width')  || $el.attr('width')  || $el.width();
                
                $el.height(h * percent);
                $el.width(w * percent);
            });
        },
        
        
        SequenceLoader: function(){
            var fnLoader = arguments.callee;
            var $el = $(this);
            var i = new Image();
            i.onload = function(){
                
                $el.find('img').attr('src', i.src);
                $el.next('li').each(fnLoader);
            };
            i.src = $el.find('img').attr('data-src');
        }
        
    }
};

var myJsp;

/* DOM Ready Functions */
jQuery(function($){

    var $imgs = $('#itemList img');
    
    var nextPhoto = function(){
        var x = ( myJsp.getContentPositionX() );
        var i=0, w=0;
        while(i < $imgs.length && x >= 0){
            w = ($imgs.eq(i).width());
            x -= w;
            i++;
            //Resizer.debug('w', w, 'x', x);
            if(x < 0){
                myJsp.scrollByX( Math.abs(x), true);
                return;
            }
            if(x == 0){
                myJsp.scrollByX( $imgs.eq(i).width(), true);
                return; 
            }
            
        }
        return;
    };
    
    var prevPhoto = function(){
        var x = (myJsp.getContentPositionX());
        var i=0, w=0;
        while(i < $imgs.length && x >= 0){
            w = $imgs.eq(i).width();
            x -= w;
            i++;
            
            if(x < 0){
                myJsp.scrollByX( -1 * (($imgs.eq(i).width()) - Math.abs(x)), true );
                return;
            }
            if(x == 0){
                myJsp.scrollByX( -1 * ($imgs.eq(i-1).width()), true );
                return;
            }
        }
    };
    
    var fnHandleKeyDown = function(e){
        if(!myJsp) return;
        
        var $el = $(this);
        $el.data('keydown', e.keyCode);
        switch(e.keyCode){
            case 37 :
                (function(){
                    if( $el.data('keydown') != 37 ){
                        return;
                    }
                    if(true || !$('#itemList:animated').length){
                        prevPhoto();
                    }
                    
                })();
            break; 
            case 39 :
                (function(){
                    if( $el.data('keydown') != 39 ){
                        return;
                    }
                    if(true || !$('#itemList:animated').length){
                        nextPhoto();
                    }
                    
                })();
            break; 
        }
    };
    
    var fnHandleKeyUp = function(e){
        $(this).data('keydown', false);
    };

    var setUlWidth = function(h){
        var w = 0;
        $('#itemList img').each(function(){
            var $el = $(this);
            
            var ow = $el.attr('img-width');
            var oh = $el.attr('img-height');
            
            var nh = h;
            var nw = ow * (nh/oh);
            w += nw + 10;
            $el.css({
                width: nw,
                height: nh
            });
            
        });
        
        // +1 for firefox to prevent last image from breaking to next line
        var shim = 1; // $.browser.mozilla ? 1 : 0; 
        $('#itemList').width(w+shim).height(h);
        
        try { 
            myJsp.reinitialise({
                contentWidth: w 
            });
        } catch(e) {}
    };
    
    
    var resizeContent = function(){
    	
        var sidenavWidth = 280;
        var minHeight = 250;
        var maxHeight = 900;
        // max height on iPad is 670
		if (isMobile || navigator.platform.indexOf('iPad') != -1) {
			maxHeight = 670;
		}
		
		

        $('#content').css('width', $(document).width() - sidenavWidth); 
        
        var h = $(window).height() -85 ; // adds 10px under the scrollbar 

        if(h > maxHeight) h = maxHeight;
        else if(h < minHeight) h = minHeight; 
        
        

        //$('.jspPane,#itemList').height(h);
        
        $('.jspContainer,.scroll-pane').height(h + 20);
        if(myJsp){
            var tmp = myJsp.getPercentScrolledX();
        }
        setUlWidth(h);
        positionFootnote();
        if(myJsp){
            myJsp.scrollToPercentX(tmp, false);
        }
    };
    
    
    var positionFootnote = function(){
    	var offset = 0;
    	if ($('body').is('#contact') || $('body').is('#home')) {
    		offset = 28;
    	} else {
    		offset = 48;
    	}
    	
        $('#footnote').css({
            position: 'absolute',
            top: $('#content').height() - offset
        });
    };
    
    // Initialize Scroll Panes
		myJsp = $('.scroll-pane').jScrollPane({
			//autoReinitialise: true,
            //autoReinitialiseDelay: 10,
            //mouseWheelSpeed: 1000
            //animateEase: 'linear',
            animateDuration: 50
            
		}).data('jsp');
	

    
    
    // bind to window resize event 
    var isMobile = typeof(isMobile) != 'undefined' ? isMobile : false;
    if(!isMobile) {
        $(window).unbind('resize', resizeContent).bind('resize', resizeContent).trigger('resize');
    } else if ($('body').is('#home')) {
        $(window).unbind('resize', resizeContent).bind('resize', resizeContent).trigger('resize');
    }

    //$(document).keydown(fnHandleKeyDown).keyup(fnHandleKeyUp);

    // Open Links in New Window via rel="external"
    $('a[rel="external"]').click(function(){this.target = "_blank";});
    
    $('#itemList li:eq(0)').each(Resizer.Util.SequenceLoader);
});
