/*---------------------------------------------------------
videoPodController_v2.0
---------------------------------------------------------
Controls passing of autoPlay and playList information
to video player swfobject.
---------------------------------------------------------
Used in conjuction with videoPodController_Object_v2.0.js
and var playList = [];
---------------------------------------------------------*/

// Method used to return new array with specified item removed
// Used in updateVideoPlaylist
Array.prototype.remove = function (from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};

// FUNCTION: updateVideoPlayList
// If videoOneOverride matches an item from playList,
// remove the matched item and place videoOneOverride at the front of the list
var updateVideoPlaylist = function (videoOneOverride) {
	for (i = 0; i < playList.length; i += 1) {
		if (videoOneOverride === playList[i]) {
			playList.remove(i);
			playList.unshift(videoOneOverride);
		}
	}
};

// FUNCTION: videoController
// Called from video swf
// Prepare autoPlay and playList and send to video swf
var videoController = function () {
	var autoPlay = false;
	var videoOneOverride = null;
	var urlParam = [];
	urlParam.video = null;
	// FUNCTION: getQueryString
	// Check url for query
	var getQueryString = function () {
		var query = window.location.search.substring(1);
		var params = query.split('&');
		for (var i = 0; i < params.length; i += 1) {
			var pos = params[i].indexOf('=');
			if (pos > 0) {
				var key = params[i].substring(0, pos);
				var val = params[i].substring(pos + 1);
				urlParam[key] = val;
			}
		}
	};
	getQueryString();
	// If "video=SOMENAME" exists,
	// set autoPlay to true and videoOneOverride to video = value
	if (urlParam.video) {
		autoPlay = true;
		videoOneOverride = urlParam.video;
	}
	// Jump to Section_Videos if autoPlay is true
	if (autoPlay === true) {
		window.location.hash = '#Section_Videos';
	}
	// Send videoOneOverride value to updateVideoPlaylist
	updateVideoPlaylist(videoOneOverride);
	// Get a handle on the swfobject
	var swfId = document.getElementById('videoFlash');
	// AS Callback - send autoPlay and playList information to swf
	swfId.setMenu(autoPlay, playList);
};

// FUNCTION: inlineVideoJump
// Pass video information from inline links
var inlineVideoJump = function (inlinePass) {
	window.location.hash = '#Section_Videos';
    updateVideoPlaylist(inlinePass);
	// Get a handle on the swfobject
	var swfId = document.getElementById('videoFlash');
	swfId.setMenu(true, playList);
};

// Called from video swf when a video is loaded
var updateShareInfo = function (videoName, videoTitle) {
	// Update Share Information
	addthis_share.url = location.protocol + location.host + location.pathname + "?video=" + videoName;
	addthis_share.title = videoTitle;
	addthis.button('.addthis_button');
	addthis_share.emailPath = location.pathname + "?video=" + videoName;
};

// FUNCTION: initializeVideoController
// Called from video swf on load - send data object to swf
// Once data is 100% loaded to swf - videoController will be called from the video swf
var initializeVideoController = function () {
	// Get a handle on the swfobject
	var swfId = document.getElementById('videoFlash');
	swfId.init(VPC.videoPodController);
};
