var NOTW = {};

NOTW.Cookie = Class.create({
    
    name: null,
    
    initialize: function(name, value){
        
        if(name){
            this.name = name;
        }
        
    },
    
    set: function(value, days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = this.name+"="+value+expires+"; path=/";
    },

    get: function() {
    	
    	var nameEQ = this.name + "=";
    	var ca = document.cookie.split(';');
    	
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    	}
    	
    	return null;
    },

    erase: function() {
    	this.set("", -1);
    }
    
});

NOTW.Nav = Class.create({
    
    prefCookie: null,
    state: null,
    
    initialize: function(){
        this.prefCookie = new NOTW.Cookie("NavState");
        this.state = this.getStatePreference();
        if(this.state == 'small'){
            $('small-header-contents').style.display = 'block';
            $('large-header-contents').style.display = 'none';
            $('header').style.height = '55px';
            $('svg-banner-container').style.height = '55px';
            $('svg-banner-container').fade({duration:0.35, from: 1, to: 0.3});
        }
    },
    
    getStatePreference: function(){
        if(v = this.prefCookie.get()){
            return v;
        }else{
            return 'large';
        }
    },
    
    setStatePreference: function(val){
        
        if(val == 'small'){
            this.showSmallNav();
        }else{
            this.showLargeNav();
        }
        
        this.prefCookie.set(val);
    },
    
    grow: function(){
        $('header').style.height = '95px';
        $('svg-banner-container').style.height = '95px';
    },
    
    shrink: function(){
        $('svg-banner-container').style.height = '55px';
        $('header').style.height = '55px';
    },
    
    showLargeNav: function(){
        $('small-header-contents').fade({duration:0.65});
        this.grow();
        $('svg-banner-container').appear({duration:0.35, from: 0.3, to: 1});
        $('large-header-contents').appear({duration:0.3});
        this.state = 'large';
    },
    
    showSmallNav: function(){
        $('large-header-contents').fade({duration:0.3});
        var nv = this;
        setTimeout(function(){nv.shrink()}, 350);
        $('svg-banner-container').fade({duration:0.35, from: 1, to: 0.3});
        $('small-header-contents').appear({duration:0.3});
        this.state = 'small';
    },
    
    temporaryStowForMovie: function(){
        if(this.state = 'large'){
            this.showSmallNav();
            var alreadyStowedCookie = new NOTW.Cookie("NavTemporarilyStowedMsg");
            if(alreadyStowedCookie.get() != 'true'){
                alreadyStowedCookie.set('true');
                // $('nav-message').update("<p class=\"sub-heading\">Did you see what happened? The navigation bar was tucked out of the way for you so that you can focus on the video. Just click the grey arrow in the nav and it'll come back again. <a href=\"javascript:nav.hideNavStowMessage();\">Ok, thanks.</a></p>");
                // $('nav-message').appear({delay: 1, duration: 0.5});
            }
        }
    },
    
    hideNavStowMessage: function(){
        $('nav-message').fade({duration: 0.5});
    }
    
});

NOTW.LogoBrowser = Class.create({
    
    defaultView: null,
    currentView: null,
    listElementPrefix: null,
    navListElementItemPrefix: null,
    
    initialize: function(options){
        
        this.listElementPrefix = options.prefix;
        this.defaultView = options.defaultView;
        this.navListElementItemPrefix = options.navListElementItemPrefix;
        
        if(window.location.hash && document.getElementById(this.listElementPrefix+'-'+this.removeHash(window.location.hash))){
            this.setView(window.location.hash);
        }else{
            this.setView(this.defaultView);
        }
        
        document.observe("hash:changed", this.setViewFromHashChangeEvent.bind(this));
        
    },
    
    hideCurrentView: function(){
        
        this.hideView(this.currentView);
        
    },
    
    hideView: function(view){
        if(document.getElementById(this.listElementPrefix+'-'+view)){
            $(this.listElementPrefix+'-'+view).fade({duration: 0.4});
            $(this.navListElementItemPrefix+'-'+view).removeClassName('current');
            // console.log(this.navListElementItemPrefix+'-'+view);
            $$('#'+this.listElementPrefix+'-'+view+' li').each(function(s) {
                setTimeout(function(){s.style.display = 'none'}, 1000);
            });
        }
    },
    
    showView: function(view){
        if(document.getElementById(this.listElementPrefix+'-'+view)){
            $(this.listElementPrefix+'-'+view).style.display='block';
            $(this.navListElementItemPrefix+'-'+view).addClassName('current');
            // console.log(this.navListElementItemPrefix+'-'+view);
            new VSC.Effect.DisplayProgressively('#'+this.listElementPrefix+'-'+view+' li', view, {interval: 180, duration: 0.9});
        }
    },
    
    setView: function(v){
        
        view = this.removeHash(v);
        
        if(this.currentView != view && document.getElementById(this.listElementPrefix+'-'+view)){
        
            var cv = this.currentView;
            
            if(this.currentView){
                this.hideCurrentView();
            }
    
            if(document.getElementById(this.listElementPrefix+'-'+view)){
                this.showView(view);
            }
    
            this.currentView = view;
    
        }
        
    },
    
    setViewFromHashChangeEvent: function(event){
        this.setView(event.memo.currentHash);
    },
    
    removeHash: function(hash){
        if(hash.charAt(0) == '#'){
            return hash.substring(1);
        }else{
            return hash;
        }
    }
    
});

NOTW.PhotoPageBrowser = Class.create({
    
    defaultView: null,
    currentView: null,
    pageElementPrefix: null,
    navListElementItemPrefix: null,
    
    initialize: function(options){
        
        this.pageElementPrefix = options.prefix;
        this.defaultView = options.defaultView;
        this.navListElementItemPrefix = options.navListElementItemPrefix;
        
        if(window.location.hash && document.getElementById(this.pageElementPrefix+'-'+this.removeHash(window.location.hash))){
            this.setView(window.location.hash);
        }else{
            this.setView(this.defaultView);
        }
        
        document.observe("hash:changed", this.setViewFromHashChangeEvent.bind(this));
        
    },
    
    setView: function(v){
        
        // console.log("set view: "+v);
        view = this.removeHash(v);
        
        if(this.currentView != view && document.getElementById(this.pageElementPrefix+'-'+view)){
        
            var cv = this.currentView;
            
            if(this.currentView){
                this.hideCurrentView();
            }
    
            if(document.getElementById(this.pageElementPrefix+'-'+view)){
                this.showView(view);
            }
    
            this.currentView = view;
    
        }
        
    },
    
    hideView: function(view){
        if(document.getElementById(this.pageElementPrefix+'-'+view)){
            $(this.pageElementPrefix+'-'+view).fade({duration: 0.4});
            $(this.navListElementItemPrefix+'-'+view).removeClassName('current');
        }
    },
    
    showView: function(view){
        if(document.getElementById(this.pageElementPrefix+'-'+view)){
            $(this.pageElementPrefix+'-'+view).appear({duration: 0.4});
            $(this.navListElementItemPrefix+'-'+view).addClassName('current');
        }
    },
    
    hideCurrentView: function(){
        
        this.hideView(this.currentView);
        
    },
    
    setViewFromHashChangeEvent: function(event){
        this.setView(event.memo.currentHash);
    },
    
    removeHash: function(hash){
        if(hash.charAt(0) == '#'){
            return hash.substring(1);
        }else{
            return hash;
        }
    }
    
});

NOTW.PiecesArray = Class.create({
    
    currentActivePieceId: false,
    
    initialize: function(){
        // console.log('Initialized');
    },
    
    setActivePiece: function(newPieceId){
        if(this.currentActivePieceId != newPieceId){
            if(this.currentActivePieceId){
                this.hidePiece(this.currentActivePieceId);
            }
            this.showPiece(newPieceId);
            this.currentActivePieceId = newPieceId;
        }
    },
    
    hidePiece: function(id){
        // console.log('Hide: '+id);
        var new_id = id+'-inner';
        // $(new_id).fade({duration: 0.3});
        $(new_id).style.display = 'none';
    },
    
    showPiece: function(id){
        // console.log('Show: '+id);
        var new_id = id+'-inner';
        // console.log('show: \''+new_id+'\'');
        $(new_id).appear({duration: 0.4});
        // $(new_id).style.display = 'block';
    }
    
});
