var Slideshow = {
	//CONFIGURATION SECTION
	_slide_delay: 7000,
	_slide_transition: 3.5,
	_caption_delay: 5000,
	_caption_transition: 1.5,

	// Shouldn't need to edit much else beyond here.
	
	_slides: null,
	_curSlide: null,
	_interval: null,
	
	Initialize: function(sPanel, bGenerateNav)
	{
		var oPanel = $(sPanel);
		if (!oPanel)
		{
			return;
		}
		
		var navPanel = new Element('div',{'id':'slideNav'});
		oPanel.insert(navPanel);
		
		Slideshow._slides = oPanel.select('div.slide');
		
		for (var i = 0; i < Slideshow._slides.length; i++)
		{
			var slide = Slideshow._slides[i];
			var caption = $(slide.id + "_caption");
			if (!caption)
			{
				var captions = oPanel.select('div.' + slide.id + '_caption');
				if (captions.length > 0)
				{
					caption = captions[0];
				}
			}
			slide._caption = caption;
			
			slide._slideIndex = i;
			slide.hide();
			
			slide._navButton = new Element('span').update(i+1);
			navPanel.insert(slide._navButton);
			Event.observe(slide._navButton, 'click', Slideshow.NavClick.bindAsEventListener(this, i));
			
			if (slide._caption)
			{
				slide._caption.hide();
			}
		}
		
		Slideshow.NextSlide();
	},	
	
	HideAll: function()
	{
		for (var i = 0; i < Slideshow._slides.length; i++)
		{
			var slide = Slideshow._slides[i];
			if (Slideshow._isVisible(slide))
			{
				Slideshow._transitionOut(slide);
			}
		}
	},
	
	_isVisible: function(slide)
	{
		return slide.visible();
	},
	
	_transitionOut: function(slide)
	{
		var targetTop = 0;
		var targetLeft = 0;

		if (slide._navButton)
		{
			slide._navButton.removeClassName('current');
		}

		new Effect.Parallel([
			new Effect.Opacity(slide, { sync: true, from: 1, to: 0 })
			], {duration: Slideshow._slide_transition});
	},
	
	_transitionIn: function(slide, delayL)
	{
		var targetTop = 0;
		var targetLeft = 0;
				
		slide.setStyle({opacity: '0'});
		slide.setStyle({left: (targetLeft - 0) + 'px', top: (targetTop + 0) + 'px', opacity: '0'});

		slide.show();

		new Effect.Parallel([
			new Effect.Move(slide, { sync: true, x: targetLeft, y: targetTop, mode: 'absolute' }),
			new Effect.Opacity(slide, { sync: true, from: 0, to: 1 })
			], {duration: Slideshow._slide_transition, delay: delayL, afterFinish: function(){if (slide._navButton) { slide._navButton.addClassName('current'); }}});
	},
	
	NavClick: function(e, index)
	{
		Slideshow._slide_transition = 0.5;
		Slideshow.ShowSlide(index, true);
	},
	
	ShowSlide: function(index, bStop)
	{
		if (Slideshow._curSlide && index == Slideshow._curSlide._slideIndex)
		{
			return;
		}
		
		if (Slideshow._curSlide)
		{
			if (Slideshow._curSlide._caption)
			{
				Slideshow._transitionOut(Slideshow._curSlide._caption);
			}
			Slideshow._transitionOut(Slideshow._curSlide);
		}
		
		Slideshow._transitionIn(Slideshow._slides[index], 0);
		
		Slideshow._curSlide = Slideshow._slides[index];
		
		if (bStop)
		{
			if (Slideshow._interval)
			{
				window.clearTimeout(Slideshow._interval);
			}
		}
		if (!bStop)
		{
			if (Slideshow._curSlide._caption)
			{
				Slideshow._interval = window.setTimeout('Slideshow._showCaption()', 5000);
			}
			else
			{
				Slideshow._interval = window.setTimeout('Slideshow.NextSlide()', 7000);
			}
		}
	},
	
	_showCaption: function()
	{
				var caption = Slideshow._curSlide._caption
				caption.show();
				caption.setStyle({left:'', top: '0px', opacity: '0'});
				new Effect.Parallel([
					new Effect.Move(caption, { sync: true, x: 0, y: 0, mode: 'absolute' }),
					new Effect.Opacity(caption, { sync: true, from: 0, to: 1 })
					], {duration: 1.5, delay: 0});

		Slideshow._interval = window.setTimeout('Slideshow.NextSlide()', 7000);
	},
	
	NextSlide: function()
	{
		var nextId = 0;
		if (Slideshow._curSlide)
		{
			nextId = Slideshow._curSlide._slideIndex + 1;
			if (nextId >= Slideshow._slides.length) { nextId = 0; }
		}
		
		if (Slideshow._slides.length > 0)
		{
			Slideshow.ShowSlide(nextId);
		}
	},
	
	doStuff: function()
	{
		var oCapt = $('slideCaption_0');
		
		new Effect.Move(oCapt, { x: 0, y: 0, mode: 'absolute' });
		new Effect.SwitchOff(oCapt);
	}
	
}

var LightBoxFlow = {
	_path: null,
	_current: null,
	
	Navigate: function(sDest,sSource,bEnableBack,sPath,iIndex)
	{
		LightBoxFlow._path = $(sSource);
		LightBoxFlow._path.hide();
		LightBoxFlow._current = $(sDest);
		LightBoxFlow._current.show();
		if (bEnableBack)
		{
			$('lbActionBack').show();
		}
		
		if (sPath)
		{
			new Ajax.Request(sPath, {
				method: 'get',
				onSuccess: function(transport) {
					LightBoxFlow._cb_UpdateOrderForm(transport, LightBoxFlow._current, iIndex);
				}
			});
		}
	},
	
	Back: function()
	{
		LightBoxFlow._current.hide();
		LightBoxFlow._path.show();
		$('lbActionBack').hide();
	},
	
	_cb_UpdateOrderForm: function(transport, contain, index)
	{
		var json = transport.responseText.evalJSON();
		var container = $(contain);
		var special = json.data[index];
		var subtext = json.reservations.ordersubtext;
		
		LightBoxFlow._buildForm(container);

		if (subtext != '')
		{
			container.removeClassName('hideSometimes');
			container._itemQuantity.value = '';
		}
		else
		{
			container.addClassName('hideSometimes');
			container._itemQuantity.value = '1';
		}
		
		container._special = special;
		container._subtext.update(subtext);
		container._itemTitle.update(special.title);
		container._itemPrice.update(special.price);
		container._itemTitleFm.value = special.title;
		container._itemPriceFm.value = special.price;
		container._itemUnits.update(special.unit);
		LightBoxFlow._updateTotal(null, container);
	},
	
	_updateTotal: function(e, container)
	{
		LightBoxFlow._buildForm(container);
		var special = container._special;

		var txtVal = container._itemQuantity.value;

		var inVal = 0;
		if (!isNaN(txtVal))
		{
			inVal = parseFloat("0" + container._itemQuantity.value);
		}
		
		var finalVal = inVal * special.unitprice
		
		if (finalVal > 0.001)
		{
			var txtUp = special.currency + ' ' + finalVal.toFixed(2);
			container._itemTotal.update(txtUp);
			container._itemDisplayedTotal.value = txtUp;
		}
		else
		{
			container._itemTotal.update('');
			container._itemDisplayedTotal.value = 'NaN';
		}
	},
	
	_buildForm: function(container)
	{
		if (container._formBuilt)
		{
			return;
		}
		container._subtext = container.select('p.special_subtext')[0];
		container._itemTitle = container.select('span.special_title')[0];
		container._itemPrice = container.select('span.special_price')[0];
		container._itemTitleFm = container.select('input.special_title')[0];
		container._itemPriceFm = container.select('input.special_price')[0];
		container._itemQuantity = container.select('input.special_quantity')[0];
		container._itemUnits = container.select('span.special_units')[0];
		container._itemTotal = container.select('span.special_total')[0];
		container._itemDisplayedTotal = container.select('input.special_displayedTotal')[0];
		container._updateButton = container.select('a.special_updatetotal')[0];
		Event.observe(container._updateButton, 'click', LightBoxFlow._updateTotal.bindAsEventListener(this, container));
		container._formBuilt = true;
	}
}

var AjaxStuff = {
	LoadSpecials: function(path, container)
	{
		if (!container)
		{
			return;
		}
		
		new Ajax.Request(path, {
		  method: 'get',
		  onSuccess: function(transport) {
			AjaxStuff._callBack(transport, container);
		  }
		});
	},
	
	_callBack: function(transport, contain)
	{
		var json = transport.responseText.evalJSON();
		var container = $(contain)
		
		var specials = json.data;
		
		if (json.data && json.data.length > 0)
		{
			for (var i = 0; i < json.data.length; i++)
			{
				var demoUnit = new Element("div", {"class":"demoUnit floatClear"});
				container.insert(demoUnit);
				
				demoUnit.insert(new Element("img", {"class":"demoImage", "src":json.data[i].image}));
				
				var demoContent = new Element("div", {"class": "demoContent"});
				demoUnit.insert(demoContent);
				
				demoContent.insert(new Element("h3").update(json.data[i].title));
				demoContent.insert(new Element("p", {"class": "pricepoint"}).update(json.data[i].price));
				demoContent.insert(new Element("p").update(json.data[i].text));
			}
		}
		else if (json.empty)
		{
			var demoUnit = new Element("div", {"class":"demoUnit floatClear"});
			container.insert(demoUnit);
			
			var demoContent = new Element("div", {"class": "demoContent demoContentEmpty"});
			demoUnit.insert(demoContent);
			
			demoContent.insert(new Element("h3").update(json.data[i].title));
			demoContent.insert(new Element("p").update(json.data[i].text));
		}
	}
}

var FrameHandler =
{
	Navigate: function(sUrl)
	{
	},
	
	AdjustFrame: function(sDiv)
	{
		var oDiv = $(sDiv);
		
		var wrapper = $('frameBox');
		var frame =  $('leightPop');

		wrapper.style.width = frame.style.width = oDiv.offsetWidth + "px";
		wrapper.style.height = frame.style.height = oDiv.offsetHeight + "px";
	}
}

Event.observe(window, 'load', function() { Slideshow.Initialize('slidePanel'); });
Event.observe(window, 'load', function() { Slideshow.Initialize('smSlidePanel'); });
Event.observe(window, 'load', function() { AjaxStuff.LoadSpecials("/spezielle/uploads/kuche.json", "demoKuche"); });
Event.observe(window, 'load', function() { AjaxStuff.LoadSpecials("/spezielle/uploads/bad.json", "demoBad"); });
Event.observe(window, 'load', function() { AjaxStuff.LoadSpecials("/spezielle/uploads/plattli.json", "demoPlattli"); });

