/*
 * mmGallery JavaScript functions
 * by Brandon McKinney
 * requires mootools 1.11   
 */ 

var thePageDiv = document.getElementById('mmPLAYERPAGE'); 


function hidePopup(person){
	document.getElementById(person).style.display = 'none';
}


function drawPopup(person){
	var popdiv = document.getElementById(person).getElementsByTagName('div')[0];
	if (popdiv.style.display != 'none'){
		popdiv.style.display = 'none';
		return 1;
	}
	popdiv.innerHTML = '';
	popdiv.innerHTML = '<div style="margin:20px 15px 15px; float:none;">';

	var strRowLayout = '<div onmouseover="this.style.color=\'#eee\'" onmouseout="this.style.color=\'#fff\'" onclick="openPlayer(\'==FILE==\');" class="mmPLRow">==TITLE==</div>';

	// create/draw a playlist
	var pl = createPlaylist(person);
	if (pl.length > 0){
		for (i=0; i<pl.length; i++){
			var rowtxt = strRowLayout;
			var itm = pl[i];
			(itm['file']) ? rowtxt = rowtxt.replace('==FILE==', pl[i]['file']) : rowtxt = rowtxt.replace('==FILE==','');
			(itm['title']) ? rowtxt = rowtxt.replace('==TITLE==', pl[i]['title']) : rowtxt = rowtxt.replace('==TITLE==','');
			popdiv.innerHTML += rowtxt;
		}
	} else {
		popdiv.innerHTML = 'This category contains no media at this time.';
	}
	var alldivs = document.getElementsByTagName('div');
	for (i=0; i < alldivs.length; i++){
		if (alldivs[i].className == 'popup') alldivs[i].style.display = 'none';
	}
	popdiv.style.display = 'block';
	popdiv.innerHTML += "</div>";
}


// draws a player file loaded
function openPlayer(thefile){
	var so = new SWFObject('/tvspots/mediaplayer.swf','mpl','440','330','8','#000000');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','false');
	so.addVariable('height','330');
	so.addVariable('width','440');
	so.addVariable('file',thefile);
	so.addVariable('image','/tvspots/defaultimg.jpg');
	so.addVariable('backcolor','0x1fc4f4');
	so.addVariable('frontcolor','0x0000CC');
	so.addVariable('autostart', 'true');
	so.write('mmTHEPLAYER');
}


// returns an array of MediaItems
function createPlaylist(iType){
	var tmpary = [];
	for (i=0; i<mmAllMediaItems.length; i++){
		if (mmAllMediaItems[i]['person'] == iType) tmpary.push(mmAllMediaItems[i]);
	}
	tmpary.sort(function(a,b){ return b['date'] - a['date']; });
	return tmpary;
}






/*
 * UTILITY FUNCTIONS BELOW
 */

function toTitleCase(str){
    return str.replace(/\w+/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}


// this function is caught by the JavascriptView object of the player.
function sendEvent(typ,prm){
	thisMovie("playerID").sendEvent(typ,prm); 
}


// This is a javascript handler for the player and is always needed.
function thisMovie(movieName){
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}


// appends new functions/code to the onload event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
