var p1playing;
var p2playing;

function sendEvent(thePlayer,typ,prm) { thisMovie(thePlayer).sendEvent(typ,prm); };
function getUpdate(typ,pr1,pr2,pid) {
	if (pid == "mpl" && typ == "state" && pr1 == 3) { vid_done(); }
	if ((pid != "null") && (typ=="state") && (pr1==2)) {
		if (pid=="mplaudio") { 
			p2playing=true;
			if (p1playing) { sendEvent("mpl", 'stop');  p1playing=false; }
		} else { 
			p1playing=true;
			if (p2playing) { sendEvent("mplaudio", 'stop');  p2playing=false; }
		}
	}
}

function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}


function qn5_onload() {
	$('about-more').onclick = function() { return about_show(); }
	$('mapsUsSaddr').onfocus = function() { directions_text_toggle(this); }
	$('mapsUsSaddr').onblur = function() { directions_text_toggle(this); }

	window.videoplayer = $('videoplayer');
	window.picFeatured = $('pic-featured');
	window.picGallery = $('pic-gallery').getElements('a');

	// set onclick for gallery images:
	for (x=0; x<picGallery.length; x++) {
		picGallery[x].onclick = function() { return pic_swap(this); }
	}

	audio_show();

}

function directions_text_toggle(field) {
	if ((field.value) == "Your Street, City, State") { field.value = ""; }
	else if ((field.value) == "") { field.value = "Your Street, City, State"; }
}


function audio_show() {
	var so_audio = new SWFObject('_assets/mediaplayer.swf','mplaudio','245','158','8');
	so_audio.addParam("allowfullscreen","false");
	so_audio.addVariable("file", "_assets/playlist.xml");
	so_audio.addVariable("enablejs", "true");
	so_audio.addVariable("javascriptid", "mplaudio");
	so_audio.addVariable('displayheight','0');
	so_audio.addVariable("autostart", "false");
	so_audio.addVariable("shuffle","false");
	so_audio.addVariable("repeat", "list");
	so_audio.addVariable('backcolor','0x000000');
	so_audio.addVariable('frontcolor','0xD0E2FF');
	so_audio.addVariable('lightcolor','0xA2C5FE');
	so_audio.write('audioplayer');
}

// fixme: opera doesn't fade in/out photo gallery pics
// fixme: opera dies when firing the sendEvent() function... and most of the other interactive media player stuff.

function vid_show(showvid_link, filename) {
	videoplayer_temp = videoplayer.cloneNode(false);
	videoplayer.parentNode.removeChild(videoplayer);
	videoplayer_temp.injectAfter(showvid_link.parentNode);

	videoplayer = document.getElementById("videoplayer");
	videoplayer.style.height = 0;
	videoplayer.style.display = "block";

	var vpHeight = new Fx.Style('videoplayer', 'height',{duration: 850});
	vpHeight.start(0,365).chain(function() {

		var so = new SWFObject('_assets/mediaplayer.swf','mpl','400','320','8');
		so.addParam("allowfullscreen","false");
		so.addVariable("file", "http://megashow2007.qn5.com/_assets/video/" + filename);
		so.addVariable("enablejs", "true");
		so.addVariable("javascriptid", "mpl");
		so.addVariable("autostart", "true");
		so.addVariable('backcolor','0x000000');
		so.addVariable('frontcolor','0xD0E2FF');
		so.write('videoplayer');

	});
}

function vid_play(showvid_link, filename) {
	// hide the videoplayer if it's visible:
	if ($('mpl')) {
		if (!window.opera) sendEvent("mpl", "stop");
		$('videoplayer').removeChild($('mpl'));
		var vpHeight = new Fx.Style('videoplayer', 'height',{duration:650});
		vpHeight.start(365,0).chain(function() { $('videoplayer').style.display = "none"; vid_show(showvid_link, filename); });
	} else {
		// if it's not visible already, just show the video
		vid_show(showvid_link, filename);
	}
	return false;
}

function vid_done() {
	// re-hide video now that it's done playing:
	$('videoplayer').removeChild($('mpl'));
	var vpHeight = new Fx.Style('videoplayer', 'height',{duration:650});
	vpHeight.start(365,0).chain(function() { $('videoplayer').style.display = "none"; });
}


function pic_swap(clicked) {

	if ((clicked.className) == "active" || (clicked.className) == "last active") return false;

	// clear all active states first:
	for (x=0; x<picGallery.length; x++) {
		if ((picGallery[x].className) == "last") {
			continue;
		}
		else if ((picGallery[x].className) == "last active" ) {
			picGallery[x].className = "last";
		}
		else {
			picGallery[x].className = "";
		}
	}

	// set the clicked link to active state (if it's the last link, set both classes):
	if ((clicked.className) == "last") {
		clicked.className = "last active";
	}
	else {
		clicked.className = "active";
	}

	// star the animation:
	var picFadeOut = new Fx.Style('pic-featured', 'opacity',{duration:700}).start(1, 0).chain(function() {
		// show loading...
		picFeatured.parentNode.className = "loading";
		new Asset.images([clicked], {
			onComplete: function() {
				picFeatured.parentNode.className = "";
				picFeatured.src = clicked;
				var picFadeIn = new Fx.Style('pic-featured', 'opacity',{duration:700}).start(1);
			}
		});

	})
	return false;
}


function about_show() {
	var about_more_clone = $('about-more-text').clone();
	about_more_clone.className = "active";
	about_more_clone.setStyles({
			width: 537,
			position: 'absolute',
			top: 0,
			left: 0,
			height: 'auto',
			visibility: 'hidden'
	});
	about_more_clone.injectBefore('video');
	var about_more_height = about_more_clone.getSize().size.y;
	about_more_clone.remove();

	var aboutHeight = new Fx.Style('about-more-text', 'height',{duration: 850});
	aboutHeight.start(0,about_more_height).chain(function(){
		$('about-more').style.display = "none";

		$('about-more-text').className = "active";

		var aboutFade = new Fx.Style('about-more-text', 'opacity',{duration:700});
		aboutFade.set(0);
		aboutFade.start(1);
	});

	return false;
}


window.onload = function() { qn5_onload(); }
