// JavaScript Document

//Buttons
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//Add commas to numbers
function comma(number) {
	number = '' + number;
	if (number.length > 3) {
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length / 3); i++) {
			if ((mod == 0) && (i == 0))
				output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
			else
				output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
		}
		return (output);
	}
	else return number;
}

//Toggle the newsletter or RSS on/off
function toggle(type) {
	if (type == "rss" && document.getElementById('popup_rss').style.display=='none') {
		document.getElementById('popup_rss').style.display='block';
		document.getElementById('popup_newsletter').style.display='none';
	}
	else if (type == "newsletter" && document.getElementById('popup_newsletter').style.display=='none') {
		document.getElementById('popup_rss').style.display='none';
		document.getElementById('popup_newsletter').style.display='block';
	}
	else {
		document.getElementById('popup_rss').style.display='none';
		document.getElementById('popup_newsletter').style.display='none';
	}
}

//Subscribe or unsubscribe
function submitform(param)
{
  if (param == 1)
	document.inputform_newsletter.submit();
  else
  	document.inputform_newsletter.unsub.value = "1";
	document.inputform_newsletter.submit();
}

//Clears and re-populates the right column on the video page
function outputList() {
	var html = "";
	for (x=0; x < video.length; x++)
		html += "<div class=\"right_image\"><a href=\"javascript:void(0);\" onClick=\"swapVideo(" + video[x].key + ");\"><img src=\"" + video[x].thumb + "\" class=\"video_thumb\" style=\"width:130px;height:97px;\" border=\"0\"><br>" + video[x].title + "</a></div>";
	document.getElementById('scroller').innerHTML = html;
}

//Sorting Functions
function sortByTitle(a, b) {
	var x = a.title.toLowerCase();
	var y = b.title.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByPopular(a, b) {
	var x = a.views;
	var y = b.views;
	return ((y < x) ? -1 : ((y > x) ? 1 : 0));
}

function sortByDate(a, b) {
	var x = a.key;
	var y = b.key;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

//Sorts videos by title and re-populates
function sortTitle() {
	document.getElementById("sorttitle").className = "sort_on";
	document.getElementById("sortpopular").className = "sort_off";
	document.getElementById("sortdate").className = "sort_off";
	video.sort(sortByTitle);
	outputList();
}

//Sorts videos by popular and re-populates
function sortPopular() {
	document.getElementById("sorttitle").className = "sort_off";
	document.getElementById("sortpopular").className = "sort_on";
	document.getElementById("sortdate").className = "sort_off";
	video.sort(sortByPopular);
	outputList();
}

//Sorts videos by date and re-populates
function sortDate() {
	document.getElementById("sorttitle").className = "sort_off";
	document.getElementById("sortpopular").className = "sort_off";
	document.getElementById("sortdate").className = "sort_on";
	video.sort(sortByDate);
	outputList();
}

//Swaps in the video content in the left frame
function swapVideo(x) {
	var i=0;
	while (video[i].key != x) //we need the video where x = the key, not the video x
		i++
	document.getElementById("video_title").innerHTML = video[i].title.toUpperCase();
	document.getElementById("video_desc").innerHTML = video[i].desc;
	document.getElementById("video_time").innerHTML = video[i].time;
	document.getElementById("video_date").innerHTML = video[i].date;
	document.getElementById("video_views").innerHTML = comma(video[i].views);
	document.getElementById("video_player").innerHTML = "<object width=\"425\" height=\"355\"><param name=\"movie\" value=\"" + video[i].url + "\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"" + video[i].url + "\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object>";
}

//Loads the initial video content
function loadVideo(x) {
	if (x == 0)
		document.getElementById("video_title").innerHTML = "LATEST VIDEO: " + video[x].title.toUpperCase();
	else
		document.getElementById("video_title").innerHTML = video[x].title.toUpperCase();
	document.getElementById("video_desc").innerHTML = video[x].desc;
	document.getElementById("video_time").innerHTML = video[x].time;
	document.getElementById("video_date").innerHTML = video[x].date;
	document.getElementById("video_views").innerHTML = comma(video[x].views);
	document.getElementById("video_player").innerHTML = "<object width=\"425\" height=\"355\"><param name=\"movie\" value=\"" + video[x].url + "\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"" + video[x].url + "\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object>";
}


