var ch, gh, vh, sutil;

var photoCreditText = "Photos by Yours Truly";

dojo.event.connect(window, "onload", function() {
	
	sutil = new SuphalaUtility();
	ch = new ContactHandler();
	vh = new ViewHandler();
	gh = new GalleryHandler();
	
	vh.constructNav();
	vh.showLeft('about');
	vh.showRight('albums');
});

function ContactHandler() {
	
	var lblCur = 'booking';
	var navContactEmail = dojo.byId("contact-email");
	var navContactBooking = dojo.byId("contact-booking");
	
	if (navContactBooking) {
		
		dojo.event.connect(navContactEmail, "onmouseover", function() {
			roll('email', 'on');
		});
		
		dojo.event.connect(navContactEmail, "onmouseout", function() {
			roll('email', 'off');
		});
		
		dojo.event.connect(navContactEmail, "onclick", function() {
			click('email');
		});
		
		dojo.event.connect(navContactBooking, "onmouseover", function() {
			roll('booking', 'on');
		});
		
		dojo.event.connect(navContactBooking, "onmouseout", function() {
			roll('booking', 'off');
		});
		
		dojo.event.connect(navContactBooking, "onclick", function() {
			click('booking');
		});
		
		/* Email list form */
		var btnSubmit1 = dojo.byId("btn-email-submit");
		var fh1 = new FormHandler(dojo.byId("form-email-list"), dojo.byId("container-form-contact-feedback"), dojo.byId("container-form-contact-feedback"), "/responders/subscribe.php", btnSubmit1);
		
		dojo.event.connect(btnSubmit1, "onclick", function() {
			var errorFlag = false;
			
			/* Required: email */
			var emailVal = dojo.byId("form_email").value;
			if (!emailVal || emailVal == "Email address is required") {
				fh1.errorMessage("You must enter an email address.");
				errorFlag = true;
			}
			
			if (!errorFlag) {
				fh1.showFeedback("Please wait while we process your request.");
				fh1.submitForm();
			}
		});
		
		/* Booking form */
		var btnSubmit2 = dojo.byId("btn-booking-submit");
		var fh2 = new FormHandler(dojo.byId("form-booking"), dojo.byId("container-form-contact-feedback"), dojo.byId("container-form-contact-feedback"), "/responders/booking.php", btnSubmit2);
		
		dojo.event.connect(btnSubmit2, "onclick", function() {
			var errorFlag = false;
			
			/* Required: name */
			var nameVal = dojo.byId("form_booking_name").value;
			if (!nameVal || nameVal == "Name is required") {
				fh2.errorMessage("You must enter a name.");
				errorFlag = true;
			}
			
			/* Required: email */
			if (!errorFlag) {
				var emailVal = dojo.byId("form_booking_email").value;
				if (!emailVal || emailVal == "Email address is required") {
					fh2.errorMessage("You must enter an email address.");
					errorFlag = true;
				}
			}
			
			/* Required: Message */
			if (!errorFlag) {
				var messageVal = dojo.byId("form_booking_message").value;
				if (!messageVal || messageVal == "Message is required") {
					fh2.errorMessage("You must enter a message.");
					errorFlag = true;
				}
			}
			
			if (!errorFlag) {
				fh2.showFeedback("Please wait while we process your request.");
				fh2.submitForm();
			}
		});
	}
	
	
	function roll(lbl, mode) {
		var lblRoll = 'contact-' + lbl;
		if ('on' == mode) {
			if (lblCur != lbl) {
				dojo.byId(lblRoll).className = 'nav-contact nav-contact-' + lbl + '-roll';
			}
		} else {
			if (lblCur != lbl) {
				dojo.byId(lblRoll).className = 'nav-contact nav-contact-' + lbl + '-off';
			}
		}
	}
	
	function click(lbl) {
		var lblClick = 'contact-' + lbl;
		
		/* Hide feedback layer */
		dojo.byId('container-form-contact-feedback').style.display = 'none';
		
		if (lblCur != lbl) {
			var oldLink = dojo.byId('contact-' + lblCur);
			oldLink.className = 'nav-contact nav-contact-' + lblCur + '-off';
			oldLink.title = 'Click to view';
			
			var newLink = dojo.byId(lblClick);
			newLink.className = 'nav-contact';
			newLink.title = '';
			
			dojo.byId('container-form-' + lblCur).className = 'form-contact hide';
			dojo.byId('container-form-' + lbl).className = 'form-contact';
			lblCur = lbl;
		}
	}
}

function ViewHandler() {
	
	this.constructNav = constructNav;
	this.getCurrent = getCurrent;
	this.showLeft = showLeft;
	this.showRight = showRight;
	this.stopVideo = stopVideo;
	
	var navL = [ 'about', 'concerts', 'news', 'press' ];
	var navR = [ 'albums', 'video', 'photo', 'quantum' ];
	
	var currentL = '';
	var currentR = '';
	
	var constructed = false;
	
	function constructNav() {
		if (dojo.byId('nav-' + navL[0])) {
			dojo.event.connect(dojo.byId('nav-' + navL[0]), 'onclick', function() { vh.showLeft(navL[0]); });
			dojo.event.connect(dojo.byId('nav-' + navL[1]), 'onclick', function() { vh.showLeft(navL[1]); });
			dojo.event.connect(dojo.byId('nav-' + navL[2]), 'onclick', function() { vh.showLeft(navL[2]); });
			dojo.event.connect(dojo.byId('nav-' + navL[3]), 'onclick', function() { vh.showLeft(navL[3]); });
			
			dojo.event.connect(dojo.byId('news-view-all'), 'onclick', function() { vh.showLeft('news'); });
			
			dojo.event.connect(dojo.byId('nav-' + navR[0]), 'onclick', function() { vh.showRight(navR[0]); });
			dojo.event.connect(dojo.byId('nav-' + navR[1]), 'onclick', function() { vh.showRight(navR[1]); });
			dojo.event.connect(dojo.byId('nav-' + navR[2]), 'onclick', function() { vh.showRight(navR[2]); });
			dojo.event.connect(dojo.byId('nav-' + navR[3]), 'onclick', function() { vh.showRight(navR[3]); });
			
			constructed = true;
		}
	}
	
	function getCurrent(side) {
		if ('l' == side) {
			return currentL;
		}
		return currentR;
	}
	
	function hide(side, avoid) {
		hideArr = ('left' == side) ? navL : navR;
		
		/* Hide the gallery */
		gh.hideAll();
		
		for (var i = 0; i < hideArr.length; i++) {
			if (avoid && avoid == hideArr[i]) {
				continue;
			}
			dojo.byId('container-' + hideArr[i]).style.display = 'none';
		}
	}
	
	function navOn(id) {
		if ('' != id) {
			dojo.byId('nav-' + id).className = 'nav nav-focus';
		}
	}
	
	function navOff(id) {
		if ('' != id) {
			dojo.byId('nav-' + id).className = 'nav';
		}
	}
	
	function show(panel) {
		if ('' != panel) {
			dojo.byId('container-' + panel).style.display = 'block';
		}
	}
	
	function showLeft(id) {
		if (!constructed) {
			return false;
		}
		
		if (currentL) navOff(currentL);
		hide('left', id);
		navOn(id);
		show(id);
		currentL = id;
	}
	
	function showRight(id) {
		if (!constructed) {
			return false;
		}
		
		if (currentR) navOff(currentR);
		
		if (currentR && currentR == 'video') stopVideo();
		
		hide('right', id);
		navOn(id);
		show(id);
		currentR = id;
	}
	
	function stopVideo() {
		/* Stop the video player, if we can see and control it */
		//if (document.videoplayer && document.videoplayer.Stop) {
		if (document.videoplayer) {
			try {
				if (document.videoplayer.Stop) {
					document.videoplayer.Stop();
				}
			} catch (e) {}
		}
	}
}

function SuphalaUtility() {
	this.openWindow = openWindow;
	
	/**
	 * Basic window opener with default sizes; pass w / h 
	 * as 2nd / 3rd args to override
	 */
	function openWindow(url) {
		var w = 600;
		var h = 400;
		
		if (openWindow.arguments.length == 3) {
			w = openWindow.arguments[1];
			h = openWindow.arguments[2];
		}
		
		window.open(url, '_blank', 'width=' + w + ',height=' + h + ',scrollbars=yes,status=yes,resizable=yes,toolbar=yes,location=yes,menubar=yes');
	}
}

function GalleryHandler() {
	
	this.show = show;
	this.showEmbed = showEmbed;
	this.showImage = showImage;
	this.showVideo = showVideo;
	this.showYouTube = showYouTube;
	this.hideAll = hideAll;
	
	var current = '';
	var pCredit = '';
	var swfo; // FlashObject video player
	
	var displayPane = dojo.byId("nav-gallery-display");
	
	function hideAll() {
		var di = dojo.byId("img-display");
		if (di) {
			try {
				displayPane.removeChild(di);
			} catch (e) {
				dojo.debug("Can't remove img-display from displayPane");
			}
		}
		
		var dv = dojo.byId("video-display");
		if (dv) {
			try {
				displayPane.removeChild(dv);
			} catch (e) {
				dojo.debug("Can't remove video-display from displayPane");
			}
		}
		
		displayPane.style.display = 'none';
	}
	
	function destroyCredit() {
		if (pCredit != '') {
			try {
				displayPane.removeChild(pCredit);
				pCredit = '';
			} catch (e) {
				dojo.debug("Can't remove pCredit");
			}
		}
	}
	
	function show(/* string */url, /* int */w, /* int */h) {
		hideAll();
		
		if (vh) {
			vh.stopVideo();
		
			/* Turn off video for Safari and IE, which try
			 * to show it over the lightbox layer */
			if (vh.getCurrent('r') == 'video') {
				vh.showRight('');
			}
		}
		
		var di = dojo.byId("img-display");
		if (!di) {
			di = document.createElement("img");
			di.id = "img-display";
			displayPane.appendChild(di);
		}
		di.src = url;
		di.width = w;
		di.height = h;
		
		destroyCredit();
		
		displayPane.style.display = 'block';
	}
	
	function showImage(/* string */url, /* int */w, /* int */h, caption) {
		
		show(url, w, h);
		if (pCredit == '') {
			pCredit = document.createElement("p");
			pCredit.appendChild(document.createTextNode(caption));
			pCredit.id = "photo-credit";
			displayPane.appendChild(pCredit);
		}
	}

	function showVideo(/* string */url, /* int */w, /* int */h) {
		
		destroyCredit();
		
		var di = dojo.byId("img-display");
		if (di) {
			try {
				displayPane.removeChild(di);
			} catch (e) {
				dojo.debug("Can't remove img-display from displayPane");
			}
		}
		
		var dv = dojo.byId("video-display");
		if (!dv) {
			dv = document.createElement("div");
			dv.id = "video-display";
			displayPane.appendChild(dv);
		}
		
		if (swfo) {
			swfo = null;
			dojo.byId("video-display").innerHTML = "";
		}
		swfo = new SWFObject("/media/player/mp3player.swf", "playlist", w, h, "7");
		swfo.addVariable("file", url);
		swfo.addVariable("backcolor","0x00000");
		swfo.addVariable("frontcolor","0xEECCDD");
		swfo.addVariable("lightcolor","0xCC0066");
		
		dv.style.display = "block";
		dv.style.width = w + "px";
		swfo.write("video-display");
		
		displayPane.style.display = 'block';
	}

	function showYouTube(/* string */code) {
		
		destroyCredit();
		
		var di = dojo.byId("img-display");
		if (di) {
			try {
				displayPane.removeChild(di);
			} catch (e) {
				dojo.debug("Can't remove img-display from displayPane");
			}
		}
		
		var dv = dojo.byId("video-display");
		if (!dv) {
			dv = document.createElement("div");
			dv.id = "video-display";
			displayPane.appendChild(dv);
		}
		
		if (swfo) {
			swfo = null;
			dojo.byId("video-display").innerHTML = "";
		}
		swfo = new SWFObject("http://www.youtube.com/v/" + code, "playlist", 423, 335, "7");
		swfo.addVariable("backcolor","0x00000");
		swfo.addVariable("frontcolor","0xEECCDD");
		swfo.addVariable("lightcolor","0xCC0066");
		
		dv.style.display = "block";
		dv.style.width = "425px";
		swfo.write("video-display");
		
		displayPane.style.display = 'block';
		
		dojo.debug(dv.innerHTML);
	}
	
	function showEmbed(/* string */src, width, height) {
		
		destroyCredit();
		
		var di = dojo.byId("img-display");
		if (di) {
			try {
				displayPane.removeChild(di);
			} catch (e) {
				dojo.debug("Can't remove img-display from displayPane");
			}
		}
		
		var dv = dojo.byId("video-display");
		if (!dv) {
			dv = document.createElement("div");
			dv.id = "video-display";
			displayPane.appendChild(dv);
		}
		
		if (swfo) {
			swfo = null;
			dojo.byId("video-display").innerHTML = "";
		}
		
		dv.innerHTML = '<embed src="' + src + '" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"></embed>';
		
		displayPane.style.display = 'block';
		var dvw = width + 2;
		dv.style.width = dvw + "px";
		dv.style.display = 'block';
		
		dojo.debug(dv.innerHTML);
	}
}



