// JavaScript Document

	//slide show
	var currentShow;
	var i = 0; 
	var wait = 8000;
	var intID;
	var slideShows = new Hash();
	var siteTitle = 'Dos Jefes Uptown Cigar Bar';
	
	function Slideshow(name, theSlides){
		this.slides = theSlides;
		this.index = 0;
		addSlideshow(name, this);
	
		this.getIndex = function(){
			return this.index;
		}
		
		this.increment = function(){
			this.index++;
			if(this.index == this.slides.length){
				this.index = 0;
			}
		}
		
		this.getSlides = function(){
			return this.slides;
		}
	}
	
		
	
	
	function playSlideshow() { 
	var fade = new Spry.Effect.Fade(currentShow.getSlides()[currentShow.getIndex()],{from:100, to:0, duration:2000});
	fade.start();
		//Effect.Fade(currentShow.getSlides()[currentShow.getIndex()], { duration:3, from:1.0, to:0.0 }); 
		currentShow.increment();
		
		var appear = new Spry.Effect.Fade(currentShow.getSlides()[currentShow.getIndex()],{from:0, to:100, duration:2000});
		appear.start();
		
		//Effect.Appear(currentShow.getSlides()[currentShow.getIndex()], { duration:3, from:0.0, to:1.0 });
			
	 } 
	
	
	// the onload event handler that starts the fading. 
	function startSlideshow(name) {
		if(name != undefined){
			slides = slideShows.get(name);
			if(slides == undefined){
				//alert("The slideshow " + name + " can't be found");
			}else{
				currentShow = slides;
				intID = setInterval('playSlideshow()',wait);
			}
		}
		
	}
			
	
	function stopSlideshow() { 
		clearInterval(intID);
	}
	
	function addSlideshow(name,slides){
		slideShows.set(name, slides);
		if(currentShow == undefined){
			currentShow = slides;
		}
	}
	
	function getSlideshow(name){
		return slideShows.get(name);
	}
	
	//page
	var pages = new Hash();
	var currentPage;
	var activeMenuColor = '#EFBE00';
	var inactiveMenuColor = '#FFFFFF';
	
	function Page(pageName, menuName, slides){
		this.name = pageName;
		this.menu = menuName;
		pages.set(pageName, this);
		if(slides != undefined){
			var show = new Slideshow(this.name, slides);
			addSlideshow(this.name, show);
		}
	}
	
	Page.prototype.getName = function(){
		return this.name;
	}
	
	Page.prototype.getMenu = function(){
		return this.menu;
	}
	
	Page.prototype.getSlideshow = function(){
		return this.slideshow;
	}
	
	Page.prototype.activate = function(){
		if(currentPage != this.name){
			stopSlideshow();
			//handle menu
			pages.each(function(pair){
			    document.getElementById(pair.value.getMenu()).style.color = inactiveMenuColor;
			});
			
			document.getElementById(this.menu).style.color = activeMenuColor;
			
			//handle transition
			if(currentPage != undefined){
				Effect.SwitchOff(currentPage, {queue: { position: 'front', scope: 'open' } }); 	
			}
			Effect.Grow(this.name, {queue: { position: 'end', scope: 'open' } });
				
			currentPage = this.name;
			
			//handle slideshow if it exists.
			currentShow = getSlideshow(this.name);
			if(currentShow != undefined){
				startSlideshow();
			} 
			
			//set title
			if(this.name != 'home'){
				document.title = siteTitle + ' - ' + this.name.replace(this.name.charAt(0), this.name.charAt(0).toUpperCase());
			}else{
				document.title = siteTitle;
			}
			
		}
		
	}

	var thePage = null;
	
	function activate(page){
		thePage = pages.get(page);
		thePage.activate();
		currentPage = page;
	}
	
	function activateWithHistory(page){
		dhtmlHistory.add(page, "#");
		activate(page);
	}
	
	function getPageFromURL(){
		
		switch (window.location.hash){
			case null:
			case "":
			case "#home":
				activate("home");
				break;
			case "#cigars":
				activate("cigars");
				break;
			case "#music":
				activate("music");
				break;
			case "#libations":
				activate("libations");
				break;
			case "#location":
				Event.onReady(setTimeout("loadMap()", 2000));
				activate("location");
				break;
			case "#libationsMenuPage":
				activate("libationsMenuPage");
				break;
		}
		
	}
	
	
	
	function start(){
		pages.each(function(pair){
					var pageName = pair.value.getName();
			        document.getElementById(pageName).style.display = 'none';
				   }
				  );
		getPageFromURL();
	}
	
	

		
	
		
		
// Adapted from DOM Ready extension by Dan Webb
// http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
// which was based on work by Matthias Miller, Dean Edwards and John Resig
//
// Usage:
//
// Event.onReady(callbackFunction);
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (Event._timer)  clearInterval(Event._timer);
    
    Event._readyCallbacks.each(function(f) { f() });
    Event._readyCallbacks = null;
    
  },
  onReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady;
      
      if (domReady.done) return f();
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            var dummy = location.protocol == "https:" ?  "https://javascript:void(0)" : "javascript:void(0)";
            document.write("<script id=__ie_onload defer src='" + dummy + "'><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") { domReady(); }
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

function showInfoPane(contentPane){
	document.getElementById('info').style.visibility = 'visible';
	document.getElementById(contentPane).style.visibility = 'visible';
}

function closeInfoPane(contentPane){
	document.getElementById(contentPane).style.visibility = 'hidden';
	document.getElementById('info').style.visibility = 'hidden';
}

function changeContent(contentID, page){
	var ele = document.getElementById(contentID + "MI");
	if(ele != null){
		pages.each(function(pair){
			    document.getElementById(pair.value.getMenu()).style.color = inactiveMenuColor;
			});
		document.getElementById(contentID + "MI").style.color = activeMenuColor;
	}
	var openIt = new Spry.Effect.Grow("content",{from:'0%',to:'100%',duration:500});
	var closeIt = new Spry.Effect.Grow("content",{from:'100%',to:'0%',duration:500});
	var effectObserver = {onPostEffect:function(){panel.loadContent(page, {id:[contentID + "_blurb"]});}};
	var updateObserver = {onPostUpdate:function(){openIt.start();}};
	closeIt.start();
	closeIt.addObserver(effectObserver);
	panel.addObserver(updateObserver);
	stopSlideshow();
	startSlideshow(contentID);
}
