/**
 * @author Kevin Jundt
 */
/*
Copyright (c) 2009, Teske's Germania Restaurant. All rights reserved.
version: 1.0.0
*/
/**
 * The TESKES object is the single global object used by TUI Library.
 * @author kjundt
 * @module base
 * @title  TESKES Global
 */
(function() {

	if (typeof YAHOO != "undefined") {
		
		function Teskes() {
			
			var Dom = YAHOO.util.Dom;
			var Event = YAHOO.util.Event;
			var Widget = YAHOO.widget;
			
			var carousel, items = [], autoplay = false, numVis = 7;
			
			/**
			 * Disables the Open Table calendar because it's messed up!
			 *
			 * @method _disableOTCalendar
			 *
			 * @private
			 * @return {Void}
			 */
			function _disableOTCalendar(){
				var date = Dom.get("startDate");
				if (date) {
					date.onfocus = null;
					date.onchange = null;
				}
			}

			/**
			 * Updates the menu pages from JSON data objects!
			 *
			 * @method _disableOTCalendar
			 *
			 * @private
			 * @return {Void}
			 */
			function _updateMenus() {
				var data = null;
				if(TESKES.lunch != null) {
					data = TESKES.lunch;
				}
				if(data) {
					var items = Dom.get("menu-items");
					if(items) {
						var html = "", i;
						for(i in data) {
							var item = data[i];
							if (item.description == null || item.description == "") {
								html += '<dl class="nodesc"><dd class="price">$';
								html += item.price;
								html += '</dd><dd class="description"><span>';
								html += item.item;
								html += '</span></dd></dl>';
							}
							else {
								html += '<dl><dt>';
								html += item.item;
								html += '</dt><dd class="price">$';
								html += item.price;
								html += '</dd><dd class="description"><span>';
								html += item.description;
								html += '</span></dd></dl>';
							}
						}
						items.innerHTML = html;
					}
				}
			}

			function _updateEvents() {
				var data, table, event, html, el, html = "", i;
				if(TESKES.events != null) {
					data = TESKES.events;
					table = Dom.get("teskes-events");
					if(table) {
						el = table.getElementsByTagName("caption");
						if(el) {
							el[0].innerHTML = data["caption"];
						}
						el = table.getElementsByTagName("tbody");
						if (el) {
							data = data["events"]
							for (i in data) {
								event = data[i];
								
								html += '<tr><td>';
								html += event.event;
								html += '</td><td>';
								html += event.date;
								html += '</td><td>';
								html += event.time;
								html += '</td></tr>';
							}
							el[0].innerHTML = html;
						}
					}
				}
			}

			function _getImageTag(img) {
				return "<img src=\"" + img["url"] + "\" height=\"75\" width=\"75\" alt=\"" + img["desc"] + "\">";
			}
		
			function _getImages() {
				var carousel = this;
				var nDate = new Date();
				var url = "php/getImages.php?page=" + (carousel.get("currentPage") + 2) + "&dummy=" + nDate.getTime();
				YAHOO.util.Connect.asyncRequest("GET", url, {
						success: function (o) {
							var curpos, i, j = 0, photo, r = eval('(' + o.responseText + ')');
							curpos = r["photos"].length;

							for (i = 0; i < curpos; i++) {
								photo = r["photos"][i];
								if (photo) {
									carousel.addItem(_getImageTag(photo));
								} else {
									break;
								}
							}
							carousel.set("selectedItem", carousel.get("firstVisible"));
						},
		
						failure: function (o) {
							alert("Ajax request failed!");
						}
				});
			}
		                
			// Get the image link from within its (parent) container.
			function _getLargeImage(parent) {
				var el = parent.firstChild;
		                
				while (el) { // walk through till as long as there's an element
					if (el.nodeName.toUpperCase() == "IMG") { // found an image
						// flickr uses "_s" suffix for small, and "_m" for big
						// images respectively
						//return el.src.replace(/_s\.jpg$/, ".jpg");
						return "<img src=\"" + el.src.replace(/_s\.jpg$/, ".jpg") + "\"><p id=\"spotlight-desc\">" + el.alt + "</p>";
					}
					el = el.nextSibling;
				}
		                
				return "";
			}
			
			function _setUpCarousel(ev) {

				if (typeof Widget.Carousel != "undefined") {

					var i, spotlight;
					carousel = new Widget.Carousel("teskes-gallery", {
						numVisible: numVis,
						scrollIncrement: numVis
					});
					var url = "php/getImages.php?dummy=" + new Date().getTime();
					YAHOO.util.Connect.asyncRequest("GET", url, {
						success: function(o){
							var curpos, i, r = eval('(' + o.responseText + ')');
							carousel.set("numItems", r["total"]);
							curpos = r["photos"].length;
							
							for (i = 0; i < curpos; i++) {
								items.push(r["photos"][i]);
							}
							
							// check if the Carousel widget is available
							if (typeof carousel != "undefined") {
								for (i = 0; i < curpos; i++) {
									// if so, shove the elements into it
									carousel.addItem(_getImageTag(items[i]));
								}
								carousel.set("selectedItem", 0);
								items = [];
							}
						},
						
						failure: function(o){
							alert("Ajax request failed!");
						}
					});
					spotlight = Dom.get("spotlight");
					carousel.render();
					carousel.show();
					
					carousel.on("loadItems", function(o){
						// more items available?
						_getImages.call(this);
					});
					
					carousel.on("itemSelected", function(index){
						// item has the reference to the Carousel's item
						var el, item = carousel.getElementForItem(index);
						
						if (item) {
							spotlight.innerHTML = _getLargeImage(item);
						}
					});
				}
		    }
			
			function _init() {
				_disableOTCalendar();
				_updateMenus();
				_setUpCarousel();
				_updateEvents();
			}
			
			Event.onDOMReady(_init, this, true);
		}
		
		if ((typeof TESKES == "undefined" || !TESKES)) {
			/**
			 * The AKAMAI global AUI object.
			 * @class AKAMAI
			 */
	
			window.TESKES = new Teskes();
		}
	}
})();

		
		  


