// Requires Mootools Core for the iWOW video: http://wowivideo.com/dnn/ShowCase/tabid/78/ctl/Login/Default.aspx

// Custom for each page:
if ( !$chk(flv) ) {
	var flv = 'http://wowevideo.net/Spokesperson/BethelCollege/BethalCollegeAustinSpringHomePage.flv';
}
if ( !$chk(linkURL) ) {
	var linkURL = 'http://www.bethelcollege.edu/about/maps/360/';
}
if ( !$chk(playTimes) ) {
	var playTimes = '';// null/empty means no limit, else a number
}

if ( !$chk(WOW_id) ) {
	var WOW_id = '9E3A5BF33A214A6BA05E695688C755D9';
}

// this is the design/layout height or where the bottom line for the video should be
if ( !$chk(layout_height) ) {
	var layout_height = 650;
}
if ( !$chk(layout_width) ) {
	var layout_width = 800;
}
// true plays the video, false does not
if ( !$chk(advertising) ) {
	var advertising=true;
}
var wowFW = 400;// flash video width
var wowFH = 300;// flash video height
var buttonW = 104; // button width
var buttonH = 50;
var closeH = 20;// the height of the close element
var displayType = 'video';// replay or none
var resizeTimer;
var delayCount = 0;
var delayTimer = new Array();

function playWOW() {
	displayType = 'video';
	// create the swf object:
	var obj = new Swiff( 'http://wowevideo.net/Spokesperson/WOWSWF/WOWSWF.swf', {
		id: 'onlinespokesperson',
		width: wowFW,
		height: wowFH,
		params: {
			allowScriptAccess: 'always',
			wmode: 'transparent',
			quality: 'high',
			scale: 'noscale'
		},
		vars: {
			path: flv,// this is the flv video file
			pathSize: '400,300', 
			gotoPath: linkURL,
			APPV: 'SHUTDOWN',// or PA - Playagain
			PPV: playTimes, // This is the number of times to play
			SPA: 'T',// Playagian T/F (true, flase),
			// PB is the onhover close & ad
			PBPath: 'http://www.bethelcollege.edu/about/maps/360/', // 'http://OnlineSpokesperson.com/',
			PBTURL: '',//'http://wowevideo.net/spokesperson/PoweredByTags/BlueVertical1.swf',
			PBTTop: '0',
			PBTLeft: '-120',
			VidID: WOW_id // This is the campagin ID
		},
		callBacks: {
			load: PreShowAd
		}/**/
	});
	var wrapper  = new Element('div', {id: 'WOWWrapper'});
	if ( $chk($('WOWHolder')) && $chk($('WOWReplay')) ) {
		// 1. remove replay
		$('WOWReplay').dispose();
		// 2. put in the video
		obj.inject(wrapper);
		wrapper.inject('WOWHolder', 'bottom');
	} else {
		// 1. create the holder/container
		var holder  = new Element('div', {id: 'WOWHolder'});
		// 2. inject it
		holder.inject(document.body, 'top');
		// 3. create the close link:
		var text = '<a href="#close" id="WOWClose" style="visibility:hidden;" ><img src="/video/flowplayer/plugin/images/close_box.png" style="border:none;" alt="Close" /></a>';
		$('WOWHolder').set('html', text);
		// 4. put in the video
		obj.inject(wrapper);
		wrapper.inject(holder, 'bottom');
	}
	// add event to the holder div
	$('WOWHolder').addEvent('mouseenter', function(){
		showClose();
	});
	$('WOWHolder').addEvent('mouseleave', function(){
		hideClose();
	});
	$('WOWClose').addEvent('click', function(){
		closeWOW();
	});
	window.addEvent('resize', function(){
		$clear(resizeTimer);
		resizeTimer = (function(){
			positionHolder();
		}).delay(50);
	});
	// set the starting styles:
	$('WOWHolder').setStyles({
		'position': 'fixed', // 'absolute',
		'top': '0',
		'left': '0',
		'width': wowFW+'px',
		'height': (wowFW+closeH)+'px',
		'z-index': '10000',
		'display': 'none'
	});
	$('WOWClose').setStyles({
		'display': 'block',
		'visibility': 'hidden',
		'width': '400px',
		'height': closeH+'px',
		'line-height': '20px',
		'text-align': 'right',
		'padding': '0',
		'color': '#000'
	});
	// now position:
	PreShowAd();
}
function showClose() {
	$('WOWClose').setStyle('visibility', 'visible');
}
function hideClose() {
	$('WOWClose').setStyle('visibility', 'hidden');
}
function showReplay() {
	// console.log('Replay()');
	displayType = 'replay';
	// 1. remove swf/flv
	if ( $chk($('onlinespokesperson') ) ) {
		//$('onlinespokesperson')
		$('WOWWrapper').dispose();
	}
	// 2. change the size of the holder & close
	positionHolder();
	
	// 3. create replay button
	var re = new Element('div', {id: 'WOWReplay'});
	re.inject('WOWHolder', 'bottom');
	$('WOWReplay').setStyle('margin-top','10px');
	var text = '<a href="#replay" id="WOWReplayA"><img src="/video/flowplayer/plugin/images/play_text.png" style="border:none;" alt="Play" /></a>';
	$('WOWReplay').set('html', text);
	
	$('WOWReplayA').addEvent('click', function(){
		playWOW();
	});
}
function delayedWOW(time, video, URL, replay){
	++delayCount;
	$clear(delayTimer[delayCount]);
	delayTimer[delayCount] = (function(){
		 playDelayedWOW(video, URL, replay, delayCount);
	}).delay(time);
}
function playDelayedWOW(video, URL, replay, x){
	// this function is called from JS and it of course replays the video
	if ( displayType == 'video') {
		// there is currently a video playing
		delayTimer[x] = (function(){
			playDelayedWOW(video, URL, replay, x);
		}).delay(2000);// wait 2 seconds and try again
	//} else if ( displayType == 'replay') {
	} else { // play even if they closed the last video
		// the replay button is showing, go ahead and play the next video
		$clear(delayTimer[x]);
		flv = video;
		linkURL = URL;
		playWOW();
	}
}
function closeWOW() {
	// this will just close the div from my JS - redunant of the ORG function: wowivideoDone()
	if ( $chk($('onlinespokesperson') ) ) {
		wowivideoDone();
	}
	$('WOWHolder').dispose();
}
function positionHolder() {
	//left:
	var left =  ( window.getSize().x + layout_width )/ 2;
	if ( (left + wowFW) > window.getSize().x ) {
		left = window.getSize().x - wowFW;
		if ( left < 0 ) {
			left = 0;
		}
	}
	// top:
	var top =  layout_height - (wowFH + closeH);
	if ( ( top + wowFH + closeH) > window.getSize().y ) {
		top = window.getSize().y - ( wowFH + closeH );
		if ( top < 0 ) {
			top = 0;
		}
	}
	if ( displayType == 'video' ) {
		var tmpW = wowFW;
		var tmpH = wowFH + closeH;
	} else if ( displayType == 'replay' ) {
		var tmpW = buttonW;
		var tmpH = buttonH + closeH;
		left = (wowFW - buttonW)/2 + left;
		top = (wowFH - buttonH)/2 + top;// the h
	} else {
		return;
	}
	$('WOWHolder').setStyles({
		//position: 'fixed', // 'absolute',
		'top': top+'px',
		'left': left+'px',
		'width': tmpW+'px',
		'height': tmpH+'px',
		'display': 'block'
	});
	$('WOWClose').setStyle('width',tmpW+'px');
}
// ORG:
function PreShowAd(){
	try{Onload2();}
	catch(e){}
	ShowAd();
}
function ShowAd(){
	if(advertising==false){
		return;
	}
	// this function replaced their code
	positionHolder();
}
function wowivideoResize(w,h){
	//This is called after the video is done! - by the flv 
	showReplay();
}
function wowivideoDone(){
	advertising=false;
	document.getElementById('onlinespokesperson').style.display='none';
	try{
		document.getElementById('onlinespokesperson').parentNode.removeChild(document.getElementById('onlinespokesperson'));
	} catch(e){}
}
function wowivideoKillIT(){
	document.getElementById('onlinespokesperson').KillIT();
}
window.addEvent('domready', function() {
	playWOW();
});
