function getParamValue(url, param) {
  var nameStartIndex=url.indexOf("?"+param);
  if (nameStartIndex == -1) {
    nameStartIndex=url.indexOf("&"+param);
    if (nameStartIndex == -1) {
      return "";
    }
  }
  var startIndex=nameStartIndex+param.length+2;
  var remainder = url.substring(startIndex);
  var endIndex=remainder.indexOf("&");
  if (endIndex == -1) {
    var value = remainder;
  }
  else {
   var value= url.substr(startIndex, endIndex);
  }
  return value;
}

function includeJS(url) {
document.write('<script src="', url, '"><\/script>');
}


function writeVideoLinks(video_id, title)  {
	document.write('<table><tr><td style="font-size: 10pt" valign="middle">'+title+'</td><td>');
	document.write('<iframe scrolling="no" marginheight="0" marginwidth="0" frameborder="0" width="220" height="23" src="http://raphaelpungin.com/videos/links.html?id='+video_id+'"></iframe>');
	document.write('</td></tr></table>');
}

function showphotostext(path, button_label) {
  document.write('<form target="_blank" action="http://quadrille.fotki.com/members_only/">');
  document.write('<input type=hidden name=cmd value="authenticate">');
  document.write('<input type=hidden name="return_url" value="'+path+'">');
  document.write('<input type=hidden name="password" value="dotheq">');
  document.write('<input type=submit value="'+button_label+'"></form>');
}

function showphotos(path) {
  showphotostext(path, "Show Photos");
}

function showupdates() {
  document.write('<form target="_blank" action="http://quadrille.fotki.com/members_only/">');
  document.write('<input type=hidden name=cmd value="authenticate">');
  document.write('<input type=hidden name="return_url" value="http://new.quadrille.fotki.com/">');
  document.write('<input type=hidden name="password" value="dotheq">');
  document.write('<input type=submit value="10 Most Recently Updated Albums"></form>');
}

/* Calendar Data Functions */

function getDate(fields) {
  return fields[0];
}

function getTitle(fields) {
  return fields[1];
}

function getURL(fields) {
  return fields[2];
}

function getImgURL(fields) {
	if (fields.length > 3) 
		return fields[3];
	else 
		return "";
}

function getImg(fields) {
  var url = getImgURL(fields);
  if (url != "") {
    var html = '<img src="'+url+'" border="0">';
  } else {
    var html = '';
  }
  return html;
}

function createXMLHttpRequest() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function ytVideoId2Name(videoId) {
	var n = videoId.replace(/-|0|1|2|3|4|5|6|7|8|9/g,"");
	return n;
}

var ytFullScreenWindow;

function embedYouTubeVideo(videoId, width, height, autostart) {
	if (videoId == "") return "";
	if (videoId.substr(0, 4) == "&ms&") return embedMySpaceVideo(videoId.substring(4, videoId.length), width, height, autostart);
	if (videoId.substr(0, 4) == "&lv&") return embedLiveVideo(videoId.substring(4, videoId.length), width, height, autostart);
	
	var url = 'http://www.youtube.com/v/'+videoId;
	if (autostart) {
		url += '&autoplay=1';
	}
	var id=ytVideoId2Name(videoId);
	
	var html ='<div id="v'+id+'">' + getYtVideo(width, height, url)+'</div>'
	+'<div id="l'+id+'" style="font-size:8pt">'+getYtLinks(videoId, width, height, url)+'</div>';
	return html;
}

function embedMySpaceVideo(videoId, width, height, autostart) {
	if (videoId == "") return "";
	var html ='<div id="v'+videoId+'">'+getMySpaceVideo(videoId, width, height, autostart)+'</div>';
	return html;
}

function embedLiveVideo(videoId, width, height, autostart) {
	if (videoId == "") return "";
	var html ='<div id="v'+videoId+'">'+getLiveVideo(videoId, width, height, autostart)+'</div>';
	return html;
}
	
function getMySpaceVideo(videoId, width, height, autostart) {
	if (videoId == "") return "";
	var html = '<embed src="http://lads.myspace.com/videos/vplayer.swf" flashvars="m='+videoId+'&type=video'+(autostart ? '&a=1':'')+'" '
	+'type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed>';
	return html;
}

function getYtVideo(width, height, videoUrl) {
	//var html='<object width="'+width+'" height="'+height+'"><param name="movie" value="'+url+'"></param>'
	var html ='<embed src="'+videoUrl+'" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" wmode="transparent"></embed>';
	//html += '</object>';
	return html;
}

function getLiveVideo(videoId, width, height, autostart) {
	var html = '<embed src="http://www.livevideo.com/flvplayer/embed/'+videoId+(autostart ? '&autostart=1':'')+
	'" type="application/x-shockwave-flash" quality="high" WIDTH="'+width+'" HEIGHT="'+height+'" wmode="transparent"></embed>';
	return html;
}
	
function getYtLinks(videoId, width, height, videoUrl) {
	var html = '<a href="http://keepvid.com?url=http://www.youtube.com/v/'+videoId+'" target="_blank">Download</a>';
	html += '&nbsp;&nbsp;<a href="javascript:ytFullScreen(\''+videoId+'\','+width+','+height+',\''+videoUrl+'\')">Full Screen</a>';
	return html;
}


function ytFullScreen(videoId, width, height, videoUrl) {
	var url='http://www.youtube.com/v/'+videoId+'&autoplay=1&fs=1';
	var id = ytVideoId2Name(videoId);
	document.getElementById("v"+id).innerHTML = '<table bgcolor="black" width="'+width+'" height="'+height+'"><tr><td style="color:white;font-weight:bold">'
	+'Full Screen Mode Is On<br><br>'
	+'<a href="javascript:ytCloseFullScreen(\''+videoId+'\','+width+','+height+',\''+videoUrl+'\')">Restore Video</a>';
	+'</td></tr></table>';
	document.getElementById("l"+id).innerHTML = '';
	ytFullScreenWindow = window.open(url,"ytfullscreen",'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height='+screen.height+',width='+screen.width);
	if (ytFullScreenWindow == null) {
		document.getElementById("v"+id).innerHTML = '<br><b>Please disable popup blocker to enable Full Screen mode.</b><br><br>';
	} else {
		try {
			ytFullScreenWindow.moveTo(0,0);
		} catch (error) {
			//IE causes Access denied error
		}
	}
}

function ytCloseFullScreen(videoId, width, height, videoUrl) {
	try {
		ytFullScreenWindow.close();
	} catch (error) {
		//IE causes Access denied error
	}
	var id = ytVideoId2Name(videoId);
	document.getElementById("v"+id).innerHTML = getYtVideo(width, height, videoUrl)
	document.getElementById("l"+id).innerHTML = getYtLinks(videoId, width, height, videoUrl);
}		
	
 
function embedRealVideo(host, videoPath, width, height, autostart, standaloneLinks) {
var ramPath = host+videoPath+".ram";
var rmPath = host+videoPath+".rm";

var html = '<div align="center">'
+'<OBJECT id="rvocx" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'+width+'" height="'+height+'">'
+'<param name="src" value="'+ramPath+'">'
+'<param name="autostart" value="'+autostart+'"><param name="controls" value="imagewindow">'
+'<param name="console" value="video"><param name="loop" value="false">'
+'<EMBED src="'+ramPath+'" width="'+width+'" height="'+height+'" '
+'loop="false" type="audio/x-pn-realaudio-plugin" controls="imagewindow" console="video" autostart="'+autostart+'">'
+'</EMBED></OBJECT></td></tr><tr><td>'
+'<OBJECT id="rvocx" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'+width+'" height="30">'
+'<param name="src" value="'+ramPath+'">'
+'<param name="autostart" value="'+autostart+'"><param name="controls" value="ControlPanel">'
+'<param name="console" value="video">'
+'<EMBED src="'+ramPath+'" width="'+width+'" height="30" '
+'controls="ControlPanel" type="audio/x-pn-realaudio-plugin" console="video" autostart="'+autostart+'">'
+'</EMBED></OBJECT>';
if (standaloneLinks) {
  html += '<br><span style="font-size: 85%;"><a href="'+ramPath+'">Stream with standalone player</a>';
  html += ' | <a href="'+rmPath+'">Download</a></span>';
} 
html += '</div>'; 
return html;
}

function embedWindowsMedia(videoUrl, width, height, autostart) {

var html='<OBJECT id="mediaPlayer" width="'+width+'"320" height="'+height+'"285"' 
+'classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"' 
+'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"'
+'standby="Loading..." type="application/x-oleobject">'
+'<param name="fileName" value="'+videoUrl+'">'
+'<param name="animationatStart" value="true">'
+'<param name="transparentatStart" value="true">'
+'<param name="autoStart" value="'+autostart+'">'
+'<param name="showControls" value="true">'
+'<param name="loop" value="false">'
+'<EMBED type="application/x-mplayer2 "'
+'pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" '
+'id="mediaPlayer" name="mediaPlayer" displaysize="4" autosize="-1" ' 
+'bgcolor="darkblue" showcontrols="true" showtracker="-1" '
+'showdisplay="0" showstatusbar="-1" videoborder3d="-1" width="'+width+'" height="'+height+'" '
+'src="'+videoUrl+'" autostart="'+autostart+'" designtimesp="5311" loop="false">'
+'</EMBED>'
+'</OBJECT>'
return html;

}

function ytPlayList(videos, width, height, uniqueId, borderWidth) {
	if (borderWidth == null) borderWidth="1";
	
	var html='<table cellpadding="3" border="'+borderWidth+'" bordercolor="black" style="border-collapse:collapse"><tr><td align="center">'
	+'<select id="sel'+uniqueId+'" onchange="selectYtPlsVideo(\''+uniqueId+'\',this.options[this.selectedIndex].value, true, '+width+','+height+')">';
	for (var i=0; i<videos.length; i++) {
		var array = videos[i].split('|');
		if (array.length == 1) {
			var videoId = "";
			var videoTitle = array[0];
		} else {
			var videoId = array[0];
			var videoTitle=array[1];
		}
		html +='<option value="'+videoId+'"' ;
		if (i==0) html+= ' selected ';
		html +='>'+videoTitle+'</option>';
	}
	html += '</select>';
	html += '&nbsp;<input id="prev'+uniqueId+'" style="font-size:10pt" type="button" value="Previous" onclick="ytPlsSkip(\''+uniqueId+'\',-1,'+width+','+height+')">';
	html += '&nbsp;<input id="next'+uniqueId+'" style="font-size:10pt" type="button" value="   Next   " onclick="ytPlsSkip(\''+uniqueId+'\',1,'+width+','+height+')">';
	
	html += '<div id="vid'+uniqueId+'" style="width:'+width+'px; height:'+height+'px;"></div></td></tr></table>';
	document.write(html);
	selectYtPlsVideo(uniqueId, document.getElementById("sel"+uniqueId).options[0].value, false, width, height);
}

function selectYtPlsVideo(uniqueId, videoId, autoStart, width, height) {
	if (videoId != "") {
  	var html = embedYouTubeVideo(videoId, width, height, autoStart);
  	document.getElementById("vid"+uniqueId).innerHTML=html;
  }	
  var selector = document.getElementById("sel"+uniqueId);
  document.getElementById("prev"+uniqueId).disabled = (selector.selectedIndex == 0);
  document.getElementById("next"+uniqueId).disabled = (selector.selectedIndex >= selector.options.length-1);
}	

function ytPlsSkip(uniqueId, dir, width, height) {
	var selector = document.getElementById("sel"+uniqueId);
	selector.selectedIndex += dir;
	var videoId = selector.options[selector.selectedIndex].value;
	while (videoId == "" && selector.selectedIndex > 0 && selector.selectedIndex < selector.length-1) {
		selector.selectedIndex += dir;
	  videoId = selector.options[selector.selectedIndex].value;
	}
	selectYtPlsVideo(uniqueId, videoId, true, width, height);
}

function yearInReview(year) {
	var count =0;
	for (var i=0; i<data.length;i++) {
	  var entry=data[i].split("|");
	  var date=entry[0];
	  var title=entry[1];
	  var url=entry[2];
	  var image=entry[3];
	  if (date.indexOf(year) == -1) {
	    continue;
	  }
	  count++;
	  document.write('<a href="'+url+'"><img border="0" alt="'+date+' - '+title+'" src="'+image+'"></a>');
	  if (count % 7 == 0) document.write('<br>');
	} 
}

function indexOf(array, element) {
	for (var i = 0; i<array.length; i++) {
		if (array[i] == element) {
			return i;
		}
	}
	return -1;
}

function setSelection(selector, item) {
	for (var i=0; i<selector.length; i++) {
		var option = selector.options[i];
		if (option.value == item) {
			selector.selectedIndex = i;
			return;
		}
	}
}

function addToArray(array, element) {
	if (element == "") return;
	array[array.length]=element;
}
	
function addToSet(setArray, element) {
	if (element == "") return;
	var added = false;
	if (indexOf(setArray, element) == -1) {
		setArray[setArray.length]=element;
		added = true;
	}
	return added;
}

function getMapSize(array) {
	var size = 0;
	for (key in array) size++;
	return size;
}

function getNumberSuffix(n) {
	if (n == 1) return "1st";
	if (n == 2) return "2nd";
	if (n == 3) return "3rd";
	return n+"th";
}

//for string sample123, returns 123
function getRightMostNumber(s) {
	var leftIndex = 0;
	for (var i = s.length-1; i>=0; i--) {
		var c = s.charAt(i);
		if (c < '0' || c > '9') {
			leftIndex = i;
			break;
		}
	}
	if (leftIndex == s.length-1) 
		return "";
	else
		return s.substring(leftIndex+1, s.length);
}

function flv(file, image, w, h) {
var html = '<embed src="http://raphaelpungin.com/jw_flv_player/flvplayer.swf" width="'+w+'" height="'+h+'"' 
+' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"'
+' flashvars="file='+file+'&image='+image+'&displayheight='+h+'" />';
document.write(html);
}