var Pane, PaneThread;
var Thumbs, ThumbThread;
var ThumbsBusy = false;
var ShowThumbs = true;
var Scroll, ScrollDistance;
var SlideDuration = 250; // ms
var Content, ContentHeight;
var Preloader, PhotoStyle, PhotoCache;
var PhotoIndex = 0;
var PhotoTimer = 0;
var Map;

var Lat = null;
var Lng = null;

window.addEvent('domready', function()
{
	Pane = new Fx.Slide('pane',
	{
		mode: 'horizontal',
		fps: 30,
		duration: SlideDuration,
		transition: Fx.Transitions.Cubic.easeInOut
	});
	
	if($('thumbs'))
	{
		Thumbs = new Fx.Styles('thumbs',
		{
			fps: 30,
			duration: SlideDuration,
			transition: Fx.Transitions.Cubic.easeInOut,
			onStart: function () { ThumbsBusy = true; },
			onComplete: function () { ThumbsBusy = false; }
		});
	}
	
	PhotoStyle = new Fx.Styles('photo2',
	{
		fps: 15,
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeInOut,
		onComplete: function ()
		{
			$('background').src = $('background2').src;
			$('photo2').style.opacity = 0;
			$('photo2').style.filter = 'alpha(opacity=25)';
			
			PhotoTimer = setTimeout(AdvancePhoto, 5000);
		}
	});
	
	$('navwrap').getElements('div.top').each(function (Div, Index)
	{
		if (Sub = Div.getElement('div'))
		{
			Div.Fader = new Fx.Styles(Sub,
			{
				fps: 30,
				duration: SlideDuration,
				transition: Fx.Transitions.Cubic.easeInOut
			});
		}
		else { Div.Fader = false; }
		
		Div.Open = function () { if (this.Fader) { this.Fader.set({ 'opacity': 1 }); } }
		Div.Close = function () { if (this.Fader) { this.Fader.start({ 'opacity': 0 }); } }
		
		Div.addEvent('mouseenter', function () { this.Open(); });
		Div.addEvent('mouseleave', function () { this.Close(); });
	});
	
	$('navwrap').getElements('a').each(function (Link, Index)
	{
		Link.addEvent('click', function (e)
		{
			
			(new Event(e)).stop(); this.blur();
			PaneRequest(this, (Link.href.indexOf('events') != -1) ? false : true);

			
			if(Link.href.indexOf('events') == -1)
			{
				$('pane').style.width = '445px';		$('pane').style.height = '350px';
				$('contentwrap').style.width = '445px'; $('contentwrap').style.height = '350px';
				$('content').style.width = '445px';		$('content').style.height = '350px';	$('content').style.background = 'url("img/content.shadow.gif") center center no-repeat';
				$('track').style.height = '350px';
				$('photo').style.display = 'block';
				$('photo2').style.display = 'block';
				$('thumbs').style.display = 'block';
				$('track').style.display = 'block';
				$('handle').style.display = 'block';
			} 	else {
				
			}
			
			$('navwrap').getElements('a').each(function (LinkB, IndexB) { LinkB.removeClass('selected'); });
			Link.addClass('selected');
			if ((Top = Link.parentNode.parentNode) && Top.className.indexOf('top') > -1) { Top.getElement('a.top').addClass('selected'); }
			
		});
	});

	
	if($('thumbs'))
	{
		$('thumbs').addEvent('mouseenter', function (e)
		{
			if (!ThumbsBusy && ShowThumbs) { Thumbs.start({ 'opacity': 1 }); }
		});
		$('thumbs').addEvent('mouseleave', function (e)
		{
			if (!ThumbsBusy) { Thumbs.start({ 'opacity': .25 }); }
		});
	}
	
	Preloader = $('preloader');
	PhotoCache = [];
	
	PaneInit();
	if($('thumbs')) ThumbsInit();
	MapInit();
});

function PaneRequest(Link, Slide)
{
	var Slide = arguments[1] || false;
	
	var Vars = Link.href.split('?');
	Vars[0] = Vars[0].split('/');
	Vars[0] = Vars[0][Vars[0].length - 1]
	Vars = 'apath=' + Vars[0] + ((Vars[1]) ? '&' + Vars[1] : '');
	
	PaneThread = new Ajax('ajax.php?' + Vars,
	{
		method: 'get',
		update: 'pane',
		autoCancel: true,
		evalScripts: true,
		onComplete: function ()
		{
			PageId = PaneThread.getHeader('page_id');
			ShowThumbs = (PaneThread.getHeader('showthumbs') == 'show') ? true : false;
			
			ThumbThread = new Ajax('thumbs.php?page_id=' + PageId,
			{
				method: 'get',
				update: 'thumbs',
				autoCancel: true,
				evalScripts: false,
				onComplete: function ()
				{
					ThumbsInit();
					if (ShowThumbs) { Thumbs.start({ 'opacity': .25 }); }
				}
			});
			
			PaneInit();
			MapInit();
			
			Pane.slideIn();
			if($('thumbs'))
			{
				Thumbs.start({ 'opacity': 0 });
				setTimeout('ThumbThread.request();', SlideDuration);
			}

			if(Link.href.indexOf('events') != -1)
			{
				$('photo').style.display = 'none';
				$('photo2').style.display = 'none';
				if($('thumbs')) $('thumbs').style.display = 'none';
				$('pane').style.width = '890px';		$('pane').style.height = '600px';
				$('contentwrap').style.width = '890px'; $('contentwrap').style.height = '600px';
				$('content').style.width = '890px';		$('content').style.height = '600px';	$('content').style.background = 'none';
				$('track').style.height = '600px';
				$('track').style.display = 'none';
				$('handle').style.display = 'none';
				
				
			} else {

				$('pane').style.width = '445px';		$('pane').style.height = '350px';
				$('contentwrap').style.width = '445px'; $('contentwrap').style.height = '350px';
				$('content').style.width = '445px';		$('content').style.height = '350px';	$('content').style.background = 'url("img/content.shadow.gif") center center no-repeat';
				$('track').style.height = '350px';
				$('photo').style.display = 'block';
				$('photo2').style.display = 'block';
				$('thumbs').style.display = 'block';
				$('track').style.display = 'block';
				$('handle').style.display = 'block';

			}
			
			
		}
	});
	
	if (Slide) { Pane.slideOut(); }
	setTimeout('PaneThread.request();', Slide ? SlideDuration : 0);
}

function PaneInit()
{
	var height = ($('events')) ? 600 : 350;

	if (Content = $('page_content')) {}
	else if (Content = $('links_page')) {}
	else if (Content = $('news_page')) {}
	else if (Content = $('contact_page')) {}
	else if (Content = $('guestbook_page')) {}
	else if (Content = $('event_calendar')) {}
	else if (Content = $('events')) {}
	else if (Content = $('shop')) {}

	if (Content)
	{
		ContentHeight = (Content.getSize())['size']['y'] + 30;
		//alert(Content.id);
		if(Content.id=='shop') { ContentHeight = 800; }
		ScrollDistance = Math.max(ContentHeight - height, 0);
		$('handle').style.height = (height - (ScrollDistance / ContentHeight * height)) + 'px';
	
		Scroll = new Slider('track', 'handle',
		{
			mode: 'vertical',
			steps: 100,
			offset: 0,
			onChange: function (Pos)
			{
				if (Content) { Content.style.top = Math.round(Math.min(height - ContentHeight, 0) * Pos / 100) + 'px'; }

			}
		});
	}
	
	$('pane').getElements('a').each(function (Link, Index)
	{
		if
		(
			!Link.hasClass('exclude')
			&& Link.href.search(/^http:\/\/(www\.)?ceviche\./i) > -1
			&& Link.href.search(/^mailto:/i) == -1
			//&& Link.href.indexOf('events') == -1
			&& Link.target.indexOf('blank') == -1
		){
			Link.addEvent('click', function (e)
			{
				(new Event(e)).stop(); this.blur();
				PaneRequest(this);
			});
		}
	});
	
	$('pane').getElements('form').each(function (Form, Index)
	{
		var Action = (Form.action.match(/.*?([^\/=]+)$/i))[1]
			
		if(Action!='webscr') { 
			Form.action = Action;
			Form.id = Action;
			Form.addEvent('submit', function (e)
			{
				(new Event(e)).stop();
				
				var Err = '';
				Err += (!Form.form_name.value) ? "Enter your name.\r\n" : '';
				Err += (!Form.form_email.value) ? "Enter your email address.\r\n" : '';
				Err += (!Form.form_question.value) ? "Enter a message.\r\n" : '';
				Err += (!Form.form_val_word.value) ? "Please fill out the anti-spam field.\r\n" : '';
				
				if (Err.length) { alert(Err); }
				else
				{
					PaneThread = new Ajax('ajax.php?apath=' + this.id,
					{
						data: this,
						method: 'post',
						update: 'pane',
						autoCancel: true,
						evalScripts: false,
						onComplete: function ()
						{
							PageId = PaneThread.getHeader('page_id');
							
							PaneInit();
							MapInit();
						}
					});
					
					PaneThread.request();
				}
			});
		}
	});
	
	if (ShowMap = $('showmap'))
	{
		ShowMap.addEvent('click', function (e)
		{
			(new Event(e)).stop(); this.blur();
			$('hide').style.display = 'none';
			$('map_control').style.visibility = 'visible';
		});
	}
	
	if (HideMap = $('hidemap1'))
	{
		HideMap.addEvent('click', function (e)
		{
			(new Event(e)).stop();
			$('map_control').style.visibility = 'hidden';
			$('hide').style.display = 'block';
		});
		
		$('hidemap2').addEvent('click', function (e)
		{
			(new Event(e)).stop();
			$('map_control').style.visibility = 'hidden';
			$('hide').style.display = 'block';
		});
	}
}

function ThumbsInit()
{
	clearTimeout(PhotoTimer);
	PhotoTimer = 0;
	PhotoIndex = 0;
	
	if (Thumb = $('thumbs').getElement('a')) { PreloadPhoto(Thumb.id); }
	
	$('thumbs').getElements('a').each(function (Link, Index)
	{
		Link.Index = Index;
		Link.addEvent('click', function (e)
		{
			(new Event(e)).stop(); this.blur();
			clearTimeout(PhotoTimer);
			PhotoIndex = this.Index;
			PreloadPhoto(this.id);
		});
	});
}

function MapInit()
{
	if (Map = document.getElementById('map'))
	{
		Lat = PaneThread.getHeader('lat');
		Lng = PaneThread.getHeader('lng');
	}
	
	if (Map && Lat != null && Lng != null)
	{
		Map = new GMap2(Map);
		Map.addControl(new GSmallMapControl());
		Map.addControl(new GMapTypeControl());
		
		var Center = new GLatLng(Lat, Lng)
		Map.setCenter(Center, 15);
		
		var Marker = new GMarker(Center);
		Map.addOverlay(Marker);
	}
}

function PreloadPhoto(Img)
{
	if (PhotoCache[Img] == 1) { SwitchPhoto(Img); }
	else { Preloader.src = '/preloader.php?img=' + Img; }
}

function SwitchPhoto(Img)
{
	PhotoCache[Img] = 1;
	$('background2').src = Img;
	PhotoStyle.start({ 'opacity': 1 });
}

function AdvancePhoto()
{
	clearTimeout(PhotoTimer);
	var T = $('thumbs').getElements('a');
	if (T.length > 1)
	{
		if (++PhotoIndex >= T.length) { PhotoIndex = 0; }
		PreloadPhoto(T[PhotoIndex].id);
	}
}





