// main mootools animation and sIFR functions
var theFair = {
	makeShowHides: function() {
		/* Loop through animating elements and popuplate array with height style effect for each */
		this.showHides = $$('.extendable').map(function(el){
			el.open = false;
		// Check for any lists, given formatting issues occur w/in IE
		    var childLists = el.getElements('ul');
		    if(childLists.length > 0) {
		        for(var k = 0; k < childLists.length; k++) {
		            childLists[k].style.visibility = "hidden";
		        }
		    }
			return new Fx.Style(el, 'height', {
				duration: 300
			}).hide();
		});
		/* Get toggle buttons array, loop though and add event handlers */
		$$('img.toggle').each(function(el, index){
			/* Get associated showHide element */
			var target = this.showHides[index];
			// Define regular expression literal for image swap
			// Tests for "buttons/$1.gif" and uses value to grab on state image
			// The main match matches all characters except forward slash ("/") and dot (".")
			// Therefore, the image name will be the set of characters following the last forward slash ("/")
			// and before the first dot (".")
			var pattern = /(.*)\/([^\/|\.]*)(.*)\.gif/;
			var imgName = el.src.replace(pattern, "$2");
			el.imageOpen = el.src.replace(pattern, "$1") + '/' + el.src.replace(pattern, "$2") + '.open.gif';
			el.imageClose = el.src.replace(pattern, "$1") + '/' + el.src.replace(pattern, "$2") + '.close.gif';
			/* Add event handler to toggle button and manipulate associated showHide element */
			el.addEvent('click', function() {
				if (!target.element.open) {
					target.stop();
					target.start(target.element.scrollHeight);
        		// Check for any lists, given formatting issues occur w/in IE
        		    var childLists = target.element.getElements('ul');
        		    if(childLists.length > 0) {
        		        for(var k = 0; k < childLists.length; k++) {
        		            childLists[k].style.visibility = "visible";
        		        }
        		    }
					el.src = el.imageClose;
					target.element.open = true;
				} else {
					target.stop();
					target.start(0);
				// Check for any lists, given formatting issues occur w/in IE
        		    var childLists = target.element.getElements('ul');
        		    if(childLists.length > 0) {
        		        for(var k = 0; k < childLists.length; k++) {
        		            childLists[k].style.visibility = "hidden";
        		        }
        		    }
					el.src = el.imageOpen;
					target.element.open = false;
				}
			}.bind(this));
		}, this);
	},
	growShowHide: function(element) {
		/* Loop through animating elements and grow the appropriate one */
		$$('.extendable').each(function(el, index){
			if (element == el) {
				this.showHides[index].start(this.showHides[index].element.scrollHeight);
			}
		}, this);
	},
	initSIFR: function() {
		if(typeof sIFR == "function") {
			sIFR.replaceElement(named({sSelector:"body.blue h1", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#133551", sBgColor:"#FFFFFF", sCase:"upper", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#000000", sBgColor:"#FFFFFF", sCase:"upper", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"body.blue #newsletter h2", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#FFFFFF", sBgColor:"#FFFFFF", sCase:"upper", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"ul.gallerylist dt span", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#000000", sBgColor:"#FFFFFF", sCase:"mixed", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"ul.activities table td.leftAlign h2", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#000000", sBgColor:"#FFFFFF", sCase:"upper", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"table#specialDays td h2", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#000000", sBgColor:"#FFFFFF", sCase:"upper", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"table#specialDays td h2", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#000000", sBgColor:"#FFFFFF", sCase:"upper", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"table#ridesCallout td div.title", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#000000", sBgColor:"#FFFFFF", sCase:"mixed", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"table#ridesCallout td div.sponsor", sFlashSrc:"/_sifr/rockwell.swf", sColor:"#989898", sBgColor:"#FFFFFF", sCase:"mixed", sWmode:"transparent"}));
            sIFR.replaceElement(named({
                sSelector   : 'h2.homeFootSIFR',
                sFlashSrc   : '/_sifr/rockwell.swf',
                sColor      : '#000',
                sWmode      : 'transparent'
            }));
            sIFR.replaceElement(named({
               sSelector    : 'label.sIFR',
               sFlashSrc    : '/_sifr/rockwell.swf',
               sColor       : '#000',
               sWmode       : 'transparent'
            }));
            var _linkSIFRTemplate = named({
				sFlashSrc   : '/_sifr/rockwell.swf',
				sColor 	    : '#000000',
                sLinkColor  : '#000000',
                sHoverColor : '#701510',
				sBgColor    : '#FFFFFF',
				sCase	    : 'upper',
				sWmode	    : 'transparent'
			});
            sIFR.replaceElement('h2.sIFR', _linkSIFRTemplate);
            sIFR.replaceElement('div#siteinfo table.sIFR tr td', _linkSIFRTemplate);
        }
    },
	makeGalleries: function() {
		var myGallery = new gallery($('myGallery'), {
			timed: false,
			showArrows: true,
			showCarousel: true,
			showInfopane: true,
			embedLinks: false
		});
		var homeGallery = new gallery($('homeGallery'), {
			timed: false,
			showArrows: true,
			showCarousel: false,
			showInfopane: false,
			embedLinks: false
		});
	},
	initEcardPreview: function() {
		if($('ecardPreview')) {
			this.ecardPreview = new Fx.Style($('ecardPreview'), 'height', {
				duration: 300
			}).hide();
			this.ecardPreview.open = false;

			var theForm = $('ecardForm');
			for (var i = 0; i < theForm.ecard.length; i++) {
				if (theForm.ecard[i].checked) {
					var ecardCall = theForm.ecard[i].getAttribute('onclick');
					eval(ecardCall);
				}
			}
		}
	},
	showEcardPreview: function(imgSrc) {
		var ecardImageEl = $('ecardImage');
		var ecardImageLoadingEl = $('ecardImageLoader');

		// Hide preview element and show loading animation
		ecardImageEl.style.display = 'none';
		ecardImageLoadingEl.style.display = 'block';

		if (!this.ecardPreview.open) {
			this.ecardPreview.start(this.ecardPreview.element.scrollHeight)
			this.ecardPreview.open = true;
		}
		var ecardImage = new Image();
		ecardImage.src = imgSrc;
		ecardImageEl.setAttribute('src', ecardImage.src);

		// Show image when it loads
		ecardImageEl.addEvent('load', function() {
			ecardImageLoadingEl.style.display = 'none';
			this.style.display = 'block';
		});
	},
	init: function() {
		this.initSIFR();
		this.makeShowHides();
		this.initEcardPreview();
		this.makeGalleries();
	}
};

//use this function to control add/removes from my fair planner.
function modifyMyFairPlanner(element, schedName, schedObjName, date)
{
	//grab the current state & toggle
	var currVal = element.getAttribute("alt");
	var newVal = (currVal == "Add") ? "Remove" : "Add";
	var currAction = (currVal == "Add") ? "add" : "remove";
	element.setAttribute("alt", newVal);

	//print out the new output
	element.src = (newVal == "Add") ? "/_images/buttons/add.gif" : "/_images/buttons/remove.gif";
	element.setAttribute("width",((newVal == "Add") ? 41 : 62));

	//find any other add/remove buttons referencing the same schedule, and change them too.
	var imgList = document.getElementsByName("schedimg"+schedName+"_"+date);
	for(i = 0; i < imgList.length; i++)
	{
		if (imgList.item(i) != element)
		{
			imgList.item(i).setAttribute("alt", newVal);
			imgList.item(i).src = (newVal == "Add") ? "/_images/buttons/add.gif" : "/_images/buttons/remove.gif";
			imgList.item(i).setAttribute("width",((newVal == "Add") ? 41 : 62));
		}
	}

	//do the ajax to make the changes.
	var async = new Ajax("/_cms/Services/changeMyFairPlanner.code.php", {
		method: 'post',
		data: 'scheduleName='+schedName+'&scheduleObjectName='+schedObjName+"&date="+date+"&action="+currAction,
		onComplete: function(count) {
			var itemTxt = (count == 1) ? "item" : "items";
			document.getElementById("myfairplannercount").innerHTML = "("+count+" "+itemTxt+" added)";
		}
	}).request();

	return false;
}

/**
 * Function for handling switching tab content on the "rides" page.
 */
	function switchTabs() {
	// Grab the target tab content
		var tabContentID = this.id + 'Content';
		tabContentElement = $(tabContentID);
	// Figure out what was set to "active"
		var activeTabElement = $$('span.tabs span.active')[0];
		var activeTabContentElement = $(activeTabElement.id + 'Content');
	// Hide the tab content that was active
		activeTabContentElement.style.display = "none";
		activeTabElement.removeClass('active');
	// Show the new tab content
		if(tabContentElement.style.display == "none")
			tabContentElement.style.display = "block";
	// Lastly, make the currently active tab "active"
		this.addClass('active');
	}
/**
 * Function which sets the bindings for the "rides" page tab navigation
 * elements
 */
	function initTabs() {
		$each($$('.tabs span'), function(element) {
			element.addEvent('click', switchTabs.bindWithEvent(element));
		});
	}
