window.addEvent('domready',function()
{
	var whichPage = document.body.className;
	switch(whichPage)
	{
		case 'page_location':
			gMapObj.init();
			break;
		case 'page_menu':
			menu.init($$('#menuNav a'),$$('.menu_page'));
			break;
		case 'page_gallery':
			// Preload Gallery Images to prevent glitches
			window.slideshowImages = new Array;
			window.slideshowImages[0] = new Image(); 
			window.slideshowImages[0].src="assets/images/gallery/full/1.jpg";
			window.slideshowImages[1] = new Image(); 
			window.slideshowImages[1].src="assets/images/gallery/full/2.jpg";
			window.slideshowImages[2] = new Image(); 
			window.slideshowImages[2].src="assets/images/gallery/full/3.jpg";
			window.slideshowImages[3] = new Image(); 
			window.slideshowImages[3].src="assets/images/gallery/full/4.jpg";
			window.slideshowImages[4] = new Image(); 
			window.slideshowImages[4].src="assets/images/gallery/full/5.jpg";
			window.slideshowImages[5] = new Image(); 
			window.slideshowImages[5].src="assets/images/gallery/full/6.jpg";
			window.slideshowImages[6] = new Image(); 
			window.slideshowImages[6].src="assets/images/gallery/full/7.jpg";
			window.slideshowImages[7] = new Image(); 
			window.slideshowImages[7].src="assets/images/gallery/full/8.jpg";
			window.slideshowImages[8] = new Image(); 
			window.slideshowImages[8].src="assets/images/gallery/full/9.jpg";
			window.slideshowImages[9] = new Image(); 
			window.slideshowImages[9].src="assets/images/gallery/full/10.jpg";
			window.slideshowImages[10] = new Image(); 
			window.slideshowImages[10].src="assets/images/gallery/full/11.jpg";
			window.slideshowImages[11] = new Image(); 
			window.slideshowImages[11].src="assets/images/gallery/full/12.jpg";
			// Setup Gallery Functionality
			slideshow.init($('galleryThumbs'),$$('#galleryThumbs a'));
			break;
	}
});

gMapObj =
{
	map: null,
	geocoder: null,
	
	init: function()
	{
		if (GBrowserIsCompatible())
		{
			this.map = new GMap2(document.getElementById("snowbunny_loc"));
			this.geocoder = new GClientGeocoder();
			
			this.showAddress('312 Center Street, Healdsburg, CA 95448');
		}
	
		window.addEvent('unload',function()
		{
			GUnload();
		});
	},
	
	showAddress: function(address)
	{
		this.geocoder.getLatLng(
			address,
			
			function(point)
			{
				if (!point) { alert(address + " not found"); }
				else
				{
					gMapObj.map.setCenter(point, 13);
					var marker = new GMarker(point);
					gMapObj.map.addOverlay(marker);
					marker.openInfoWindowHtml('<h1 style="font-size:14px;margin-bottom:0;"><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Snow+Bunny&sll=38.610768,-122.86944&sspn=0.011435,0.018582&g=312+Center+Street,+Healdsburg,+CA+95448&ie=UTF8&ll=38.610851,-122.869442&spn=0.011435,0.018582&z=16&iwloc=A" target="_blank">Snowbunny</a></h1><p style="font-size:12px;">312 Center Street,<br />Healdsburg, CA 95448</p><p style="font-size:12px;"><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Snow+Bunny&sll=38.610768,-122.86944&sspn=0.011435,0.018582&g=312+Center+Street,+Healdsburg,+CA+95448&ie=UTF8&ll=38.610851,-122.869442&spn=0.011435,0.018582&z=16&iwloc=A" target="_blank">Get Directions</a></p>');
				}
			}
		);
	}
};

var menu =
{
	pages: {},
	links: {},
	currentPage: {},
	content: null,
	hash: null,
	
	init: function(elsA,elsP)
	{
		this.content = $('content');
		if(window.location.hash && (window.location.hash=='#frappes' || window.location.hash=='#shakes' || window.location.hash=='#coffee'))
		{
			this.hash = window.location.hash;
		}
		else
		{
			window.location.hash = 'yogurt';
			this.hash = window.location.hash;
		}
		
		for(var x=0;x<elsA.length;x++)
		{
			this.pages[x] = elsP[x].clone(true,true);
			this.links[x] = elsA[x];
			
			var tempLoc = this.rHash(elsA[x].href,true);
			
			elsA[x].addEvent('click',function(e){
				if(e.preventDefault) e.preventDefault();
				else e.returnValue = false;
				menu.change(this);
			});
			
			if(tempLoc == this.hash)
			{
				elsA[x].className = 'selected';
				elsP[x].style.zIndex = 2;
				this.currentPage = elsP[x];
			}
			else elsP[x].destroy();
		}
	},
	
	change: function(el)
	{
		if(el.className!='selected')
		{
			var newLoc = this.rHash(el.href);
			window.location.hash = newLoc;
			
			var oldPage = this.currentPage;
			
			this.links[0].className=null;
			this.links[1].className=null;
			this.links[2].className=null;
			this.links[3].className=null;
			
			switch(newLoc)
			{
				case 'shakes':
					this.currentPage = this.pages[1].clone(true,true);
					this.links[1].className='selected';
					break;
				case 'frappes':
					this.currentPage = this.pages[2].clone(true,true);
					this.links[2].className='selected';
					break;
				case 'coffee':
					this.currentPage = this.pages[3].clone(true,true);
					this.links[3].className='selected';
					break;
				default:
					this.currentPage = this.pages[0].clone(true,true);
					this.links[0].className='selected';
			}
			
			this.currentPage.style.zIndex=2;
			
			var tempAnim = new Fx.Elements([this.currentPage,oldPage],{
				duration:500,
				transition: 'quad:in:out',
				chain: 'ignore',
				onComplete: function(){ this.elements[1].destroy(); }
			});
			
			tempAnim.set({
				'0':{'opacity':0,'top':'50px'}
			})
			
			this.currentPage.inject(this.content);
			
			tempAnim.start({
				'0':{
					'opacity':1,
					'top':0
				},
				'1':{
					'opacity':0,
					'top':'100px'
				}
			})
			//oldPage.destroy();
		}
	},
	
	// Removes the has from a string, used for testing and writing new hashes
	rHash: function(string,bool)
	{
		var tempString = string.split('#');
		if(bool) return '#'+tempString[tempString.length-1];
		else return tempString[tempString.length-1];
	}
}

var slideshow =
{
	canvas: null,
	thumbGroups: new Array,
	currentGroup: null,
	currentLocation: null,
	next: null,
	prev: null,
	animating: true,
	currentImage: null,
	
	init: function(thumbContainer,thumbs)
	{
		thumbContainer.className = 'interactive';
		
		this.next = new Element('div',{
			'id':'thumbs_next',
			'class':'disabled'
		});
		
		this.prev = new Element('div',{
			'id':'thumbs_prev',
			'class':'disabled'
		});
		
		this.next.addEvent('click',function(){ if(this.className!='disabled' && slideshow.animating==false) slideshow.clickThumbNav('next'); });
		this.prev.addEvent('click',function(){ if(this.className!='disabled' && slideshow.animating==false) slideshow.clickThumbNav('prev'); });
		
		thumbContainer.adopt(this.next,this.prev);
		
		this.canvas = new Element('div',{ 'id':'slideshow_canvas' });
		
		this.canvas.inject($('content'),'top');
		
		for(var x=0,count=0,count2=0;x-thumbs.length;x++)
		{			
			var y = this.thumbGroups.length-1;
			if(count==0)
			{
				y++;
				this.thumbGroups[y] = new Element('div',{'class': 'thumb_group'});
				if(count2>0) this.next.className=null;
			}
			if(count<7)
			{
				thumbs[x].inject(this.thumbGroups[y])
				count++;
			}
			if(count==7)
			{
				count = 0;
				if(count2==0)
				{
					this.currentGroup = this.thumbGroups[y].clone(true,true)
					this.currentGroup.inject(thumbContainer);
				}
				count2++;
			}
		}
		
		this.currentLocation = 0;
		
		this.addThumbClick();
		
		var temp = $$('.thumb_group a');
		
		this.currentImage = new Element('img',{ 'src':temp[0].href });
		
		this.currentImage.style.marginTop = '-212px';
		this.currentImage.style.marginLeft = '-304px';
		
		this.currentImage.inject(this.canvas);
		
		$('galleryThumbs').getFirst('.thumb_group').getFirst('a').className='selected';
		
		this.animating=false;
	},
	
	addThumbClick: function()
	{
		var els = $$('#galleryThumbs a');
		
		for(var x=0;x<els.length;x++)
		{
			els[x].addEvent('click',function(e)
			{
				slideshow.clickThumb(e,this.href,this);
			});
		}
	},
	
	clearThumbSelection: function()
	{
		var els = $$('#galleryThumbs a');
		
		for(var x=0;x<els.length;x++) els[x].className=null;		
	},
	
	clickThumb: function(e,loc,el)
	{
		if(e && e.preventDefault) e.preventDefault();
		else if(e && e.returnValue) e.returnValue = false;
		
		var oldImg = this.currentImage;
		this.currentImage = new Element('img',{ 'src':loc });
		
		this.currentImage.style.visibility = 'hidden';
		
		var tempAnim = new Fx.Elements([this.currentImage,oldImg],{
			duration:500,
			transition:'quad:out',
			fps:1000,
			chain:'ignore',
			onComplete: function()
			{
				this.elements[1].destroy();
			}
		});
		
		tempAnim.set({
			'0':{ 'opacity':0 }
		});
		
		this.currentImage.inject(this.canvas);
		
		this.currentImage.style.marginTop = -(this.currentImage.height/2+12)+'px';
		this.currentImage.style.marginLeft = -(this.currentImage.width/2+4)+'px';
		
		this.currentImage.style.visibility = 'visible';
		
		tempAnim.start({
			'0':{'opacity':1},
			'1':{'opacity':0}
		});
		
		this.clearThumbSelection();
		
		el.className='selected';
	},
	
	clickThumbNav: function(type)
	{
		this.animating=true;
		var oldGroup = this.currentGroup;
		
		if(type=='prev')
		{
			this.currentLocation--;
			this.currentGroup = this.thumbGroups[this.currentLocation].clone(true,true);
		}
		else
		{
			this.currentLocation++;
			this.currentGroup = this.thumbGroups[this.currentLocation].clone(true,true);
		}
		
		var tempAnim = new Fx.Elements([this.currentGroup,oldGroup],{
			duration:500,
			transition: 'quad:out',
			chain: 'ignore',
			onComplete: function()
			{
				this.elements[1].destroy();
				slideshow.checkLocation();
			}
		});
		
		
		if(type=='prev')
		{
			tempAnim.set({
				'0':{
					'opacity':0,
					'left':'0'
				}
			});
			
			this.currentGroup.inject($('galleryThumbs'));
			
			tempAnim.start({
				'0':{
					'opacity':1,
					'left':'100px'
				},
				'1':{
					'opacity':0,
					'left':'200px'
				}
			});
		}
		else
		{
			tempAnim.set({
				'0':{
					'opacity':0,
					'left':'200px'
				}
			});
			
			this.currentGroup.inject($('galleryThumbs'));
			
			tempAnim.start({
				'0':{
					'opacity':1,
					'left':'100px'
				},
				'1':{
					'opacity':0,
					'left':'0'
				}
			});
		}
		
		this.currentGroup.inject($('galleryThumbs'));
	},
	
	checkLocation: function()
	{
		if(this.thumbGroups.length>1)
		{
			if(this.currentLocation==0)
			{
				this.prev.className='disabled';
				this.next.className=null;
			}
			else if(this.currentLocation==this.thumbGroups.length-1)
			{
				this.next.className='disabled';
				this.prev.className=null;
			}
			else
			{
				this.next.className=null;
				this.prev.className=null;
			}
		}
		
		this.addThumbClick();
		
		this.animating = false;
	}
}
