window.addEvent('domready', function(){
	if(window.ie6)
	$('logo').fix();
});
function mouseOverL(which)
{
which.src ="arrowpix/arrowmouseover_left.png";

}
function mouseOutL(which)
{
which.src ="arrowpix/arrowup_left.png";

}
function mouseOverR(which)
{
which.src ="arrowpix/arrowmouseover_right.png";
}
function mouseOutR(which)
{

which.src ="arrowpix/arrowup_right.png";
}



var active_tab;
var active_item;
var error;
var active_id;
var saf_cache;


/*function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
*/

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function itemover(which) {
	if (which) {
		if (active_item) {
			if (which != active_item)
				which.className = 'cat_item itemover';
		} else
			which.className = 'cat_item itemover';
		//var x = document.getElementById(which.id+'_desc');
		var x = $(which.id+'_desc');
		if (x) {
			x.style.display = 'block';
		}
		//x = document.getElementById('r0_desc');
		x = $('r0_desc');
		if (x) {
			x.style.display = 'none';
		}
	}
}

function itemout(which) {
	if (which) {
		if (active_item) {
			if (which != active_item)
				which.className = 'cat_item itemup';
		} else
			which.className = 'cat_item itemup';
		//var x = document.getElementById(which.id+'_desc');
		var x = $(which.id+'_desc');
		if (x) {
			x.style.display = 'none';
		}
		//x = document.getElementById('r0_desc');
		x = $('r0_desc');
		if (x) {
			x.style.display = 'block';
		}
	}
}

function itemclick(which, id) {
	which = which.parentNode;
	
	if (which) {
		if (active_item) {
			if (which != active_item) {
			active_item.className = 'cat_item itemup';
			active_item = which;
			which.className = 'cat_item itemactive';
			which.blur();
			}
		}
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url="includes/getVideo.php";
		url=url+"?id="+id;
		//alert(url);
		xmlHttp.onreadystatechange=videoReady;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function itemclickd(which, id) {
	
	if (which) {
		if (active_item) {
			if (which != active_item) {
			active_item.className = 'cat_item itemup';
			active_item = which;
			which.className = 'cat_item itemactive';
			which.blur();
			}
		}
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url="includes/getVideo.php";
		url=url+"?id="+id;
		//alert(url);
		xmlHttp.onreadystatechange=videoReady;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function pageReady() {
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			//alert(xmlHttp.responseXML);
			var text = xmlHttp.responseText;
			//alert(text);
			if (text == 'Not Found') {
				alert('ERROR');
			}
			else {
				var xmldoc = xmlHttp.responseXML;
				var video = xmldoc.getElementsByTagName('video');
				//alert(video.length);
				var cat = xmldoc.getElementsByTagName('category')[0].childNodes[0].nodeValue;
				var items = document.getElementById('itemcontainer_'+cat);
				var total = items.getElementsByTagName('div').length;
				for (var i=1; i<=total; i++) {
					var x = $(cat+''+i);
					//alert("Removing "+cat+''+i);
					if (x) items.removeChild(x);
				}

				//alert('done');
				for (var i=0; i<video.length; i++) {
					var id = video[i].attributes.getNamedItem('id').value;
					var title = video[i].getElementsByTagName('title')[0].childNodes[0].nodeValue;
					var desc = video[i].getElementsByTagName('description')[0].childNodes[0].nodeValue;
					var thumb = video[i].getElementsByTagName('thumb')[0].childNodes[0].nodeValue;
					var newItem = document.createElement("DIV");
					var newTip = document.createElement("SPAN");
					var newID = i+1;
				    newItem.id = cat+''+newID;
					newTip.id = 'tip_'+cat+''+newID;
					if (active_id && id)
						if (active_id == id) {
							newItem.className = "cat_item itemactive";
							active_item = newItem;
							//alert(active_item.id);
						} else
							newItem.className = "cat_item itemup";
					else
					    newItem.className = "cat_item itemup";
					//var strFunctMO = 'itemover(this);showTooltip(event, "<h3>'+title+'</h3><p>'+desc+'</p>");';
					var strFunctMD = 'itemclickd(this, '+id+');';
					newItem.onmouseover = function(e) {itemover(this);TagToTip('tip_'+this.id);};
					//AttachEvent(newItem, 'mouseover', myMouseOver, false);
					//newItem.onmouseover = myMouseOver(window.event, "<h3>'+title+'</h3><p>'+desc+'</p>");
					//newItem.onmouseover = new Function('event', strFunctMO);// {itemover(this);showTooltip(event, '"<h3>"'+title+'"</h3><p>"'+desc+'"</p>"');};
					newItem.onmouseout = function() {itemout(this);};
					newItem.onmousedown = new Function('', strFunctMD);// {itemclick(this, id);};
					
					var tipTitle = document.createElement("H3");
					tipTitle.innerHTML = title;
					var tipDesc = document.createElement("P");
					tipDesc.innerHTML = desc;
					newTip.appendChild(tipTitle);
					newTip.appendChild(tipDesc);
					newTip.style.display = "none";
					
					newImg = document.createElement("IMG");
					newImg.src = "images/"+thumb;
					newImg.alt = "";
					
					newBR = document.createElement("BR");
					newText = document.createTextNode(title);
					
					newItem.appendChild(newImg);
					newItem.appendChild(newBR);
					newItem.appendChild(newText);
					newItem.appendChild(newTip);
					
					items.appendChild(newItem);
				}
				
				var pages = $("cat_"+cat);
				var limarks = pages.getElementsByTagName('li');
				var curr = parseInt(xmldoc.getElementsByTagName('page')[0].attributes.getNamedItem('current').value);
				var allpages = parseInt(xmldoc.getElementsByTagName('page')[0].attributes.getNamedItem('total').value);
				//tds[0].innerHTML = curr > 1 
				//		? '<a href="javascript:void(0);" onclick="goToPage(\'1\', \''+cat+'\');">|&lt;&lt; First</a> ' 
				//		: '<span class="inactive">|&lt;&lt; First</span> ';
				var prev = curr - 1;
				limarks[0].innerHTML = curr > 1
						? '<div class="leftarrow"><a href="javascript:void(0);" onclick="goToPage(\''+prev+'\', \''+cat+'\');"><img onmouseover="mouseOverL(this);" onmouseout="mouseOutL(this);" src="arrowpix/arrowup_left.png" id="b1" /></a> </div>' 
						: '<div class="leftarrow"><span class="inactive"><img src="arrowpix/arrowinactive_left.png" /></span> </div>';
				start = curr-5 < 1 ? 1 : curr-5;
				end = curr+5 > allpages ? allpages : curr+5;
				
				var next = curr+1;
				limarks[2].innerHTML = allpages > 1
					? curr < allpages
						? '<div class="rightarrow"><a href="javascript:void(0);" onclick="goToPage(\''+next+'\', \''+cat+'\');"><img onmouseover="mouseOverR(this);" onmouseout="mouseOutR(this);" src="arrowpix/arrowup_right.png" id="b2" /></a></div>'
						: '<div class="rightarrow"><span class="inactive"><img src="arrowpix/arrowinactive_right.png" /></span></div> '
					: '<div class="leftarrow"><span class="inactive"><img src="arrowpix/arrowinactive_right.png" /></span></div> ';
					
					
				/*for(var i=0; i<5; i++)
					alert(tds[i].innerHTML);*/
			}
		}
	}
}


function videoReady()
{
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			//alert(xmlHttp.responseXML);
			var text = xmlHttp.responseText;
			if (text == 'Not Found') {
				alert('ERROR');
			}
			else {
				//alert(xmlHttp.responseXML);
				var xmldoc = xmlHttp.responseXML;
				var title = xmldoc.getElementsByTagName('title')[0];
				//document.getElementById('ttube_title').innerHTML = title.childNodes[0].nodeValue;
				var category = xmldoc.getElementsByTagName('category')[0];
				category = category.childNodes[0].nodeValue.toLowerCase();
				$('ttube_title').innerHTML = title.childNodes[0].nodeValue;
				var video = xmldoc.getElementsByTagName('video')[0];
				//var id = video.attributes.getNamedItem('id').value;
				var width = video.attributes.getNamedItem('width').value;
				var height= video.attributes.getNamedItem('height').value;
				var desc = xmldoc.getElementsByTagName('description')[0].childNodes[0].nodeValue;
				//document.getElementById('ttube_desc').innerHTML = "<h3>Description:</h3><p>"+desc+"</p>";
				$('ttube_desc').innerHTML = '<h3 class="desctitle">Description:</h3><div class="desctext">'+desc+"</div></p>";
				var rel = xmldoc.getElementsByTagName('related');
				//var relDiv = document.getElementById('related_links');
				var relDiv = $('tagsdiv');
				relDiv.innerHTML = '';
			
				var count = 0;
				for (var i=0; i<rel.length; i++) {
					if (count > 0)
						relDiv.innerHTML += "<br/>";
					relDiv.innerHTML += "<a href=\""+rel[i].attributes.getNamedItem('href').value+"\" target=\"_blank\">"+rel[i].childNodes[0].nodeValue+"</a>";
					count++;
				}
				if (count == 0)
					relDiv.innerHTML += "<span class=\"desctext\">None</span>";
				//var id = xmldoc.getElementsByTagName('ROW')[0].attributes.getNamedItem('id').value;
				var id = video.attributes.getNamedItem('id').value;
				var filename = xmldoc.getElementsByTagName('filename')[0].childNodes[0].nodeValue;
				//frames['player_frame'+saf_cache].location.href="video.php?v="+id;
				$('shareURL').value = "http://www.techtube.mtu.edu/watch.php?v="+id;
				var url="<embed src=\"http://www.mtu.edu/mtu_resources/video/player.swf\" width=\"415\" height=\"354\" allowscriptaccess=\"always\" allowfullscreen=\"true\" flashvars=\"file=http://www.techtube.mtu.edu/videos/"+category+"/"+filename+".flv&fullscreen=true&image=http://www.techtube.mtu.edu/clicktoplay.jpg\" />";
				$('shareEmbed').value = url;
				$('fbshare').onclick = function() {return fbs_click("http://www.techtube.mtu.edu/watch.php?v="+id);};
				del_url = "http://www.techtube.mtu.edu/watch.php?v="+id;
				del_title = $("ttube_title").innerHTML;
				$('delshare').onclick = function() { window.open('http://del.icio.us/post?v=4&noui&jump=close&url='+encodeURIComponent(del_url)+'&title='+encodeURIComponent(del_title), 'delicious','toolbar=no,width=700,height=400'); return false;};
				$('diggshare').href = "http://digg.com/submit?phase=2&url=http%3A%2F%2Fwww.techtube.mtu.edu%2Fwatch.php%3Fv%3D"+id+"&title="+encodeURIComponent($('ttube_title').innerHTML)+"&bodytext="+encodeURIComponent(desc)+"&topic=videos_educational";
				var thetitle = encodeURIComponent($('ttube_title').innerHTML);
				$('twitter').href = "http://www.twitter.com/home?status=Check+out+this+video+from+Michigan+Tech+*"+(thetitle.replace(/%20+/g,"+"))+"*+http://www.techtube.mtu.edu/watch.php?v="+id;
				$('technorati').href = "http://www.technorati.com/faves?add=http%3A%2F%2Fwww.techtube.mtu.edu%2Fwatch.php%3Fv%3D"+id;
				$('emailshare').href = "mailto:?subject=You've been sent a Michigan Tech video!&body=http://www.techtube.mtu.edu/watch.php?v="+id;
				active_id = id;
				document.getElementById('changes').innerHTML = '';
				var s1 = new SWFObject("http://www.mtu.edu/mtu_resources/video/player.swf","ply","415","354","9","#FFFFFF");
				s1.addParam("allowfullscreen","true");
				s1.addParam("allowscriptaccess","always");
				s1.addParam("flashvars","file=http://www.techtube.mtu.edu/videos/"+category+"/"+filename+".flv&fullscreen=true&image=http://www.techtube.mtu.edu/clicktoplay.jpg");
				s1.write("changes");
				
				//document.getElementById('changes').innerHTML = ('<embed src="player.swf" width="415" height="354" allowscriptaccess="always" allowfullscreen="true" flashvars="file=http://www.techtube.mtu.edu/videos/'+category+'/'+filename+'.flv&fullscreen=true&image=http://www.techtube.mtu.edu/clicktoplay.jpg&skin=skin.swf" />');
				
				if ($('ttube_error'))
					$('ttube_error').style.display = 'none';
			}
		}
	}
}

var prev_color;
var prev_row;
function ttube_highlight(which, color) {
	if (which) {
		prev_color = which.style.backgroundColor;
		which.style.backgroundColor = color;
	}
}

function ttube_unhighlight(which) {
	if (which) {
		which.style.backgroundColor = prev_color;
	}
}

function ttube_admin_click(which) {
	if (which) {
		if (prev_row) {
			var pre = $(prev_row.id+"_image");
			pre.style.width='50px';
			pre.style.height='37px';
			pre = $(prev_row.id+"_short");
			pre.style.display = "block";
			pre = $(prev_row.id+"_long");
			pre.style.display = "none";
			pre = $(prev_row.id+"_action");
			pre.style.display = "none";
		}
		var img = $(which.id+"_image");
		img.style.width='100px';
		img.style.height='75px';
		img = $(which.id+"_short");
		img.style.display = "none";
		img = $(which.id+"_long");
		img.style.display = "block";
		img = $(which.id+"_action");
		img.style.display = "block";
		prev_row = which;
	}
}

function featReady() {
	//alert(xmlHttp.responseText);
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			if (xmlHttp.responseText == 'false') {
				alert('There was an error.  The video could not be featured.');
			}
			else {
				var id = xmlHttp.responseText;
				$("row"+id).style.fontWeight = 'bold';
			}
		}
	}
}

function featReady2() {
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			if (xmlHttp.responseText == 'false') {
				alert('There was an error.  Could not change feature status.');
			}
			else {
				var id = xmlHttp.responseText;
				$("row"+id).style.fontWeight = 'normal';
			}
		}
	}
}

function ttube_feature(which, id) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="includes/functions.php";
	url=url+"?t=feat";
	url=url+"&id="+id;
	if (which.checked == true) {
		url=url+"&v=true";
		xmlHttp.onreadystatechange=featReady;
	}
	else {
		url=url+"&v=false";
		xmlHttp.onreadystatechange=featReady2;
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function tabover(which) {
	if (which && active_tab) {
		if (which != active_tab) {
			which.className = 'ttube_tab tabover';
		}
	}
}

function tabout(which) {
	if (which && active_tab) {
		if (which != active_tab) {
			which.className = 'ttube_tab tabup';
		}
	}
}

function tabclick(which) {
	
	if (which && active_tab) {
		if (which != active_tab) {
			active_tab.className = 'ttube_tab tabup';
			var x = $('cat_'+active_tab.id);
			if (x) {
				x.style.display = 'none';
			}
			x = $('cat_pages_'+active_tab.id);
			if (x) {
				x.style.display = 'none';
			}
			active_tab = which;
			which.className = 'ttube_tab tabactive';
			x = null;
			x = $('cat_'+which.id);
			if (x) {
				x.style.display = 'block';
			}
			x = $('cat_pages_'+which.id);
			if (x) {
				x.style.display = 'block';
			}
		}
	}

			//alert(currpages);
}

function goToPage(page, categ) {
	//alert(page+" "+categ);
	var url="includes/getPage.php";
	url=url+"?c="+categ;
	url=url+"&p="+page;
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=pageReady;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	currpages = eval(active_tab.id+'_total');
	mypage = page;
		
		if (mypage > 1) {
			mypage = ((mypage-1) * perpage) + 1;
		}
		if ((parseFloat(mypage)+4) > currpages) {
			endmypage = currpages;
		} else {
			endmypage = (parseFloat(mypage))+4;
		}
	currdiv = ('pagenumbers_'+active_tab.id);
	document.getElementById(currdiv).innerHTML = mypage+"-"+(endmypage)+" of "+currpages;
	

}

Element.implement({
	fix: function(){
	if(!window.ie6)
	return this;
	var img;
	if(this.getTag()=='img'){
	//img = "'"+this.get('src')+"'";
	img = this.getProperty('src');
	this.setProperty('src', 'images/null.gif');
	}else{
	var bg = this.getStyle('background-image');
	if(bg && bg!='none')
	img = bg.match(/\(([^)]+)\)/)[1];
	}
	if(img){
	if(this.getStyle('display')=='inline' && !['input', 'textarea', 'button'].contains(this.getTag()))
	this.setStyles({
	'display': 'block',
	'width': this.getStyle('width'),
	'height': this.getStyle('height')
	});
	this.setStyles({
	'background': '',
	'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src="+img+", sizingMethod='crop')"
	});
	}
	return this;
	}
	});
