/**
 * @author David E. Still
 */

var headPics = {
	rotation: new Array(
		{ picout: "head_left_1", picin: "head_left_2" },
		{ picout: "head_right_1", picin: "head_right_2" },
		{ picout: "head_left_2", picin: "head_left_3" },
		{ picout: "head_right_2", picin: "head_right_3" },
		{ picout: "head_left_3", picin: "head_left_1" },
		{ picout: "head_right_3", picin: "head_right_1" }
	),
	idx: 0,
	duration: 2,
	rotate: function () {
		thisPic = this.rotation[this.idx];
		new Effect.Fade(thisPic.picout, { duration: this.duration });
		new Effect.Appear(thisPic.picin, { duration: this.duration });
		this.idx++;
		if (this.idx >= this.rotation.length) this.idx = 0;
	}
}

/* demos */
var videoDemo = { title: "demo reel", src: "/media/demoreel.flv", width: 640, height: 480 };

var picTimer = null;
var picTimerInterval = 4 * 1000;

Event.observe(window, "load", function(){
	console.log("loading DOM scripts...");
	picTimer = setInterval("headPics.rotate();", picTimerInterval);
	/* audio clip players */
	/* Commercials */
	var so_com = new SWFObject("/media/mp3player.swf", "audio_commercial", 290, 24, 6, false, "high");
	so_com.addParam("menu", "false");
	so_com.addParam("wmode", "transparent");
	so_com.addVariable("soundFile","/media/VO-Commercial.mp3");
	so_com.write("audio_com");
	/* Promos */
	var so_pro = new SWFObject("/media/mp3player.swf", "audio_promo", 290, 24, 6, false, "high");
	so_pro.addParam("menu", "false");
	so_pro.addParam("wmode", "transparent");
	so_pro.addVariable("soundFile","/media/VO-Promo.mp3");
	so_pro.write("audio_pro");
	/* Animation */
	var so_ani = new SWFObject("/media/mp3player.swf", "audio_animation", 290, 24, 6, false, "high");
	so_ani.addParam("menu", "false");
	so_ani.addParam("wmode", "transparent");
	so_ani.addVariable("soundFile","/media/VO-Animation.mp3");
	so_ani.write("audio_ani");
    /* Narration */
	var so_ani = new SWFObject("/media/mp3player.swf", "audio_animation", 290, 24, 6, false, "high");
	so_ani.addParam("menu", "false");
	so_ani.addParam("wmode", "transparent");
	so_ani.addVariable("soundFile","/media/VO-Narration.mp3");
	so_ani.write("audio_nar");
});

function demo(clip) {
	/* Build the modal window if it doesn't exist */
	if (!$("modal_overlay")) {
		document.getElementsByTagName("body")[0].appendChild(
			Builder.node('div', { id: 'modal_overlay', style: 'display: none;' })
		);
		if (macfix) $("modal_overlay").addClassName("modal_maczilla");
	}
	/* Build the clip window if it doesn't exist */
	if (!$("clip_container")) {
		document.getElementsByTagName("body")[0].appendChild(
			Builder.node("div", { id: "clip_parent" }, [
				Builder.node('div', { id: 'clip_container', style: 'display: none;' })
			])
		);
	}
	var clipDiv = $("clip_container");
	var so = new SWFObject("/media/FlowPlayer.swf", "clip", clip.width, (clip.height+23), 8, false, "high");
	so.addVariable("config", "{ videoFile: '"+clip.src+"'}");
	so.write("clip_container");
	if (clip.title) clipDiv.insertBefore(Builder.node('h1', clip.title), clipDiv.firstDescendant());
	clipDiv.appendChild( Builder.node('a', { href: 'javascript:demoClose();', style: 'display: block;' }, "close"));
	if (!macfix) {
		new Effect.Appear("modal_overlay", { duration: .5, from: 0, to: .8, afterFinish: function(){
			new Effect.Appear("clip_container", { duration: .5 });
		}});
	} else {
		var queue = Effect.Queues.get('myscope');
		queue.each(function(e) { e.cancel() });
		$("header").getElementsBySelector("img.head_left, img.head_right").each(function(img) {
			img.setStyle({ opacity: 1 });
		});
		clearInterval(picTimer);
		$("modal_overlay").show();
		$("clip_container").show();
	}
}

function demoClose() {
	if (!macfix) {
		new Effect.Fade("modal_overlay", { duration: .5, from: .8 });
		new Effect.Fade("clip_container", { duration: .5 });
	} else {
		$("modal_overlay").hide();
		$("clip_container").hide();
		picTimer = setInterval("headPics.rotate();", picTimerInterval);
	}
}

/**
 * Mutes firebug console code and errors on browsers where firebug is not installed
 */
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

