	function betterEncodingForMobile(encoding1, encoding2){
		IDEAL_ENCODING_RATE = 256000; //bits per second; equivalent to 256 kbps
		
		diff1 = Math.abs(encoding1 - IDEAL_ENCODING_RATE);
		diff2 = Math.abs(encoding2 - IDEAL_ENCODING_RATE);
		
		return ((diff1 <= diff2) ? encoding1 : encoding2);
	}

	if ( ewBCVideoPlayerID != null ) { // there is a video id
		referenceid = ewBCVideoPlayerID;
		findvideo();
	} else if (document.location.href.indexOf('bctid=') > -1) { // there is a video id but in the query
		var url = document.location.href;
		url = url.split('bctid=');
		referenceid = url[1];
		findvideo();
	} else if ( ewBCVideoPlayListID != null ) { // there is a playlist id, go get video id
		jQuery.ajax({
			url: "http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id="+ewBCVideoPlayListID+"&playlist_fields=videoIds&token=GYnXA3COTIHoLwfSSOEH_rATS-cPa0lQCkaKT9oFASU.",
			dataType: "jsonp",
			success: function(data) {
				referenceid = data.videoIds[0];
				findvideo();
			}
		})
	} else { // there is a player id, go get playlist id then go get video id
		jQuery.ajax({
			url: "http://api.brightcove.com/services/library?command=find_playlists_for_player_id&player_id="+ewBCPlayerId+"&page_size=3&playlist_fields=id&token=GYnXA3COTIHoLwfSSOEH_rATS-cPa0lQCkaKT9oFASU.",
			dataType: "jsonp",
			success: function(data) {
				ewBCVideoPlayListID = data.items[0].id;
				jQuery.ajax({
					url: "http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id="+ewBCVideoPlayListID+"&playlist_fields=videoIds&token=GYnXA3COTIHoLwfSSOEH_rATS-cPa0lQCkaKT9oFASU.",
					dataType: "jsonp",
					success: function(data) {
						referenceid = data.videoIds[0];
						findvideo();
					}
				})
			}
		})
	};
	
	function findvideo() {
		jQuery.ajax({
			url: "http://api.brightcove.com/services/library?command=find_video_by_id&video_id="+referenceid+"&media_delivery=http&video_fields=name,renditions,videoStillURL&token=GYnXA3COTIHoLwfSSOEH_rATS-cPa0lQCkaKT9oFASU.",
			dataType: "jsonp",
			success: function(data) {
				
				bestRenditionIndex = -1;
				bestEncodingRateSoFar = -1;
				for (var i = 0; i < data.renditions.length; i = i+1){
		/*
					if (data.renditions[i].videoCodec != "H264"){
						continue;
					}
		*/
		
					if (bestRenditionIndex == -1){
						bestRenditionIndex = i;
						bestEncodingRateSoFar = data.renditions[i].encodingRate;
					}
					else if (betterEncodingForMobile(data.renditions[i].encodingRate, bestEncodingRateSoFar) == data.renditions[i].encodingRate){
						bestRenditionIndex = i;
						bestEncodingRateSoFar = data.renditions[i].encodingRate;
					}
				}
				bestRendition = data.renditions[bestRenditionIndex];
				bestRenditionURL = bestRendition.url;
														
				vidName = data.name;
				vidStillURL = data.videoStillURL;
		
				videoTag = document.createElement("video");
				videoTag.setAttribute("id", vidName);
				videoTag.setAttribute("poster", vidStillURL);
				videoTag.setAttribute("src", bestRenditionURL);
				videoTag.setAttribute("controls", "true");
				
				/* NEW for Android */
				if ( (navigator.userAgent.match('android')) || (navigator.userAgent.match('droid')) ) {
					videoTag.setAttribute("onclick","this.play();");
				}
												
				targetVideo = document.getElementById('bcPlayerV' + referenceid);
				if (targetVideo == null)
				{
					targetVideo = document.getElementById('bcPlayer' + ewBCPlayerID).parentNode;
				}
				targetVideo.innerHTML = '';
				targetVideo.appendChild(videoTag);
				
				if(typeof ewBCVideoPlayListID == "undefined") {
					jQuery('.bcPlayer').hide();
				}
				jQuery('.brightcoveHolder').hide();
				
			}
		});
	}

