/*
 * mmGallery JavaScript functions
 * by Brandon McKinney
 * requires mootools 1.11   
 */ 

var thePageDiv = $('mmPLAYERPAGE'); 
// opacity controller for viewer page
thePageDiv.fx = new Fx.Style('mmPLAYERPAGE','opacity').set(0);


// draws a viewer page
function mmOpenPage(iType){
	// close the landing page
	$('mmLandingPage').style.display = "none";

	// constants for the pg titles
	var pgTitles = [];
//		pgTitles["surgery"] = "Surgery WebCasts";
//		pgTitles["tour"] = "Virtual Tours";
		pgTitles["tv"] = "TV Coverage";
		pgTitles["radio"] = "Radio";
		pgTitles["newsletters"] = "Newsletters   (requires Adobe Acrobat Reader)";
		pgTitles["pevideos"] = "Patient Education Videos";

	var pgTiles = [];
//		pgTiles["surgery"] = "/images/mmGallery/surgery_webcasts.jpg";
//		pgTiles["tour"] = "/images/mmGallery/virtual_tours.jpg";
		pgTiles["tv"] = "/images/mmGallery/tv_coverage.jpg";
		pgTiles["radio"] = "/images/mmGallery/radio.jpg";
		pgTiles["newsletters"] = "/images/mmGallery/newsletters.jpg";
		pgTiles["pevideos"] = "/images/mmGallery/PEV.jpg";

	thePageDiv.fx.set(0); // hide the pg

	// show the new page & load player after delay
	drawViewer({
		'title': pgTitles[iType],
		'type': iType,
		'bgimg': pgTiles[iType]
	});
}


function drawViewer(args){
	$$('#mmPLAYERPAGE .mmPLTitle')[0].innerHTML = args['title'];
	$('mmTHEPLAYER').style.backgroundImage = 'url(' + args['bgimg'] + ')';

	var plDiv = $$('#mmPLAYERPAGE .mmPLContents')[0];
	// clear the playlist
	plDiv.innerHTML = '';
	// and clear the mediaplayer
	$('mmTHEPLAYER').innerHTML = '';
	// clear the summary
	$('mmItemSummary').innerHTML = '';


	var strRowLayout = '\
		<div class="mmPLRow" mmstate="" mmt="==PLTYPE==" mmf="==PLFILE==">\
			<img src="/images/mmGallery/btnPlay.gif" alt="play"/> \
			<div>==PLTITLE==</div>\
			<span class="mmSummary">==PLSUMMARY==</span>\
		</div>\
	';

	// create/draw a playlist
	var pl = createPlaylist(args['type']);
	if (pl.length > 0){
		for (i=0; i<pl.length; i++){
			var rowtxt = strRowLayout;
			var itm = pl[i];
			(itm['type']) ? rowtxt = rowtxt.replace('==PLTYPE==', pl[i]['type']) : rowtxt = rowtxt.replace('==PLTYPE==','');
			(itm['file']) ? rowtxt = rowtxt.replace('==PLFILE==', pl[i]['file']) : rowtxt = rowtxt.replace('==PLFILE==','');
			(itm['title']) ? rowtxt = rowtxt.replace('==PLTITLE==', pl[i]['title']) : rowtxt = rowtxt.replace('==PLTITLE==','');
			(itm['summary']) ? rowtxt = rowtxt.replace('==PLSUMMARY==', pl[i]['summary']) : rowtxt = rowtxt.replace('==PLSUMMARY==','');
			plDiv.innerHTML += rowtxt;
		}

		// wire-up rollovers/events for the rows
		$$('.mmPLRow').each(function(obj){
			obj.addEvent('mouseover',function(){
				this.style.backgroundColor = "#EEE";
			});
			obj.addEvent('mouseout',function(){
				this.style.backgroundColor = "#FFF";
			});
			obj.addEvent('click',function(){
				var mystate = this.getProperty('mmstate');
				if (mystate == 'on'){
					this.setProperty('mmstate','');
					this.getElement('img').src = '/images/mmGallery/btnPlay.gif';
					$('mmTHEPLAYER').innerHTML = ' ';
				} else {
					$$('.mmPLRow').each(function(itm){
						if (itm != this) itm.getElement('img').src = '/images/mmGallery/btnPlay.gif';
					});
					openPlayer(this);
					this.getElement('img').src = '/images/mmGallery/btnStop.gif';
					this.setProperty('mmstate','on');
				}
			});
		});
	} else {
		plDiv.innerHTML = 'This category contains no media at this time.';
	}

	// fade the page in
	setTimeout(function(){
		thePageDiv.style.display = "block";
		thePageDiv.fx.start(1);
	},750);
}


// draws a player file loaded
function openPlayer(objCaller){
	var mmType = objCaller.getProperty('mmt');
	if (mmType != 'newsletters'){
		var so = new SWFObject('/flash/mediaplayer.swf','mpl','460','365','8','#000000');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addVariable('height','265');
		so.addVariable('width','460');
		so.addVariable('file',objCaller.getProperty('mmf'));
		so.addVariable('displayheight','346');
		so.addVariable('displaywidth','460');
		so.addVariable('backcolor','0x0098EA');
		so.addVariable('frontcolor','0xFFFFFF');
		so.addVariable('lightcolor','0x66C8FF');
		so.addVariable('usefullscreen','false');
		so.addVariable('autostart','true');
		if (mmType == 'radio') so.addVariable('showeq','true');
		so.write('mmTHEPLAYER');
		$('mmGrayFootbar').innerHTML = '';

	} else 
{
   window.open(objCaller.getProperty('mmf'));
//		$('mmTHEPLAYER').innerHTML = '<iframe height="100%" width="100%" src="' + objCaller.getProperty('mmf') + '"></iframe>';
//		$('mmGrayFootbar').innerHTML = '<div><a href="' + objCaller.getProperty('mmf') + '" target="ocaMMDocView">+ ENLARGE DOCUMENT</a></div>';
//		$('mmGrayFootbar').innerHTML = '<div><a href="http://www.adobe.com/products/acrobat/readstep2.html"><img src="/images/get_adobe_reader.gif">    Newsletters require Adobe Acrobat Reader.</a></div>';
		$('mmGrayFootbar').innerHTML = '';

	}
	
	// now write the description
	var sumry = $('mmItemSummary');
	sumry.innerHTML = '';
	sumry.innerHTML += '<div class="title">' + objCaller.getElement('div').innerHTML + '</div>';
	sumry.innerHTML += objCaller.getElement('span').innerHTML;
}


// returns an array of MediaItems
function createPlaylist(iType){
	var tmpary = [];
	for (i=0; i<mmAllMediaItems.length; i++){
		if (mmAllMediaItems[i]['type'] == iType) tmpary.push(mmAllMediaItems[i]);
	}
	/*mmAllMediaItems.each(function(obj){
		if (obj.type == iType) tmpary.push(obj);
	});*/
	tmpary.sort(function(a,b){ return b['date'] - a['date']; });
	return tmpary;
}


// moves the tick mark above the category bar
var mmTickFx;
addLoadEvent(function(){
	mmTickFx = new Fx.Style('mmCategoryTick','margin-left',{
		duration:1000,
		transition:Fx.Transitions.Cubic.easeOut
	});

	$$('#mmCategories div').each(function(obj){
		obj.addEvent('click',function(e){
			var newleft = (this.style.marginLeft.replace("px","") * 1);
			mmTickFx.start(newleft);
		});
	});
});






/*
 * 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();
    }
  }
}

