// JavaScript Document

var bcVidObj = {
	vidPgURL : "videos",
	bcToken : "bjkrAJ08KJ3BUsaX_rx_J6tabLuy0NxF7umrKvBAbnI.",
	bcPubId : 4249780001,
	playerId : 30402252001,
	//defaultList : 6614406001,
	//featuredList : 6614406001,
	htmlTitleText : "RealAge Video"
}
	
	var playerObjTags = '<object id="myExperience" class="BrightcoveExperience"><param name="bgcolor" value="#FFFFFF" />\n<param name="width" value="798" />\n<param name="height" value="490" />\n<param name="wmode" value="transparent" />\n<param name="playerID" value="' + bcVidObj.playerId + '" />\n<param name="publisherID" value="' +  bcVidObj.bcPubId + '"/>\n<param name="isUI" value="true" />\n<param name="isVid" value="true" /></object>';
	
	function getQStringValue() {
		var qstring, params, param_name;
		qstring = window.location.search;
		params = qstring.split("&");
		
		for (i = 0; i < params.length; i++) {
			param_name = params[i];
			param_name = param_name.substring(0, param_name.indexOf('='));
			if (param_name === "?v" || param_name === "v") {
				if (param_name === "?v") {
					qstring = params[i].replace(/\?v=/, "");
				} else {
					qstring = params[i].replace(/v=/, "");
				}
				return qstring;
			}
		}
	}
	function clearQString(val) { // Clear the Query String
		window.location.href = "http://" + location.host + "/" + bcVidObj.vidPgURL + "/#v" + val;
	}
	function getHashValue() { // See if there is a video id in the URL
		var haveString = getQStringValue();
		if (!haveString) {
			var hash = window.location.hash.replace(/#v/, "");
			if (isNaN(hash)) {
				getQStringValue();
			} else {
				return hash;
			}
		} else {
			clearQString(haveString);
		}
	}
	urlVideo = getHashValue();
	function onContentLoad(evt) {
		if (urlVideo) {
			modCon.getVideoAsynch(urlVideo);
		} else {
			if (vidFirstEntry === 1 && vidCommand === "find_playlist_by_id") {
				showVid(vidId);
				if (!selectCatCount) {
					treeNavToggle();
				}
			} else {
				modCon.getPlaylistAsynch(bcVidObj.defaultList);
			}
			vidFirstEntry = 0;
		}
	}
	function onVideoLoad(evt) {
		modVP.loadVideo(evt.video.id);
		currentVidObj = modVP.getCurrentVideo();
		currentVidInfo();
	}
	function onPlaylistLoad(evt) {
		showVid(evt.playlist.videoIds[0]);
	}
	//function onExternalAd(evt) {
	//	playAd(evt.ad);
	//}
	//function onAdComplete(evt) {
	//	modAds.resumeAfterExternalAd(); 
	//}
	function onTemplateLoaded(experienceID) {
		bcExp = brightcove.getExperience(experienceID);
		// get modules
		modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
		modExp = bcExp.getModule(APIModules.EXPERIENCE);
		modCon = bcExp.getModule(APIModules.CONTENT);
		modSoc = bcExp.getModule(APIModules.SOCIAL);
		// turn on ADs
		//modAds = bcExp.getModule(APIModules.ADVERTISING);
		//modAds.enableExternalAds(true);
		// events
		modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
		modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
		modCon.addEventListener(BCContentEvent.PLAYLIST_LOAD, onPlaylistLoad);
		//modAds.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, onExternalAd);
		//modAds.addEventListener(BCAdvertisingEvent.AD_COMPLETE, onAdComplete);
	}
	
	function currentVidInfo() {
		vidId = currentVidObj.id;
		modSoc.setLink("http://" + window.location.host + "/" + bcVidObj.vidPgURL + "/#v" + currentVidObj.id + "");
		document.title = currentVidObj.displayName + " - " + bcVidObj.htmlTitleText;
	}