/**
 * @author Jen Fowler [jennifer@reachfarther.com]
 */


/***********************************
* gets the embedded movie player
***********************************/
function getEmbed(embedID) {
if (document.getElementById) {
getEmbed = function(embedID) { return document.getElementById(embedID); }
} else if (document.all) {
getEmbed = function(embedID) { return document.all[embedID]; };
} else if (document.layers) {
getEmbed = function(embedID) { return document.layers[embedID]; };
} else {
getEmbed = function() { return null; }
}
// When we get here, the getEmbed function has been replaced.    
// So we return the result of the new function.
return getEmbed(embedID);
}

/**********************************
* BEGIN functions for FLASH video
***********************************/
// first detect DOM, then send string to the flash function
var flash;
function thisMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    //return (isIE) ? window[movieName] : document[movieName];
    return (isIE) ? getEmbed(movieName) : document[movieName];
}

function sendVid(newVid) {
    thisMovie("resVideo").changeVid("vids/" + newVid)
}

function playVideo(videoIndex) {
	
	// get values for video details
	var videoTitle = document.getElementById(videoIndex + '_Title').value;
	var videoDesc = document.getElementById(videoIndex + '_Desc').value;
	var videoPath = document.getElementById(videoIndex + '_Path').value;

	// first, try sending the video path to the flash player
	try {
		sendVid(videoPath);
	} catch(e) {
		return;
	}

	// replace the currently shown video title
	document.getElementById('videoTitle').childNodes[0].nodeValue = videoTitle;

	// replace the currently show video description
	/* SWITCHED TO INNERHTML TO ALLOW INLINE LINKS TO BE HANDLED PROPERLY: JLF - 6/7/2010 */
	//document.getElementById('videoDesc').childNodes[0].nodeValue = videoDesc;
	document.getElementById('videoDesc').innerHTML = videoDesc;
	
	// send the video title to the flash player
	thisMovie(videoTitle);
	
	
	//// now, remove the nowPlaying class from the last video played and add it to the currently selected video thumb
    //	var activeVideos = getElementsByClass('nowPlaying', document.getElementById('catalogPage'), 'a');	// gets a collection of all elements with this class
    //	for (var i = 0; i < activeVideos.length; i++) {
    //		if (hasClassName(activeVideos[i], "nowPlaying")) {
    //			removeClassName(activeVideos[i], "nowPlaying");
    //		}
    //	}
	
	//// add the 'nowPlaying' class to the current video thumb
	//addClassName(videoLink, "nowPlaying");
	
}
/**********************************
* END functions for FLASH video
***********************************/



/******************************************************************************
 * @author Dustin Diaz (http://www.dustindiaz.com/getelementsbyclass/)
 * 
 * @param 	{string} searchClass	The class name as a string; required
 * 
 * @param 	{string} node 			The DOM node; optional. Can be obtained 
 * 									with getElementById().  Mainly useful if 
 * 									you know your parent and you don’t want to 
 * 									loop through the entire DOM
 * 
 * @param	{string} tag 			Limit results by adding a tagName; optional
 * 
 * @return	{array}	 classElements
 *****************************************************************************/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	
	if (node == null)	node = document;
	if (tag == null)	tag = '*';
	
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\b|\\s)" + searchClass + "(\\s|\b|$)");
	
	for (i = 0, j = 0; i <  elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	
	return classElements;
}



/* ----------------------------------------------------------------------------
*  hasClassName
* 
*  Description : returns boolean indicating whether the object has the class name
*  built with the understanding that there may be multiple classes
*
*  Arguments:
*     objElement              - element to manipulate
*     strClass                - class name to add
*-----------------------------------------------------------------------------*/
function hasClassName(objElement, strClass) {

   // if there is a class
   if (objElement.className) {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for (var i = 0; i < arrList.length; i++) {

         // if class found
         if (arrList[i].toUpperCase() == strClassUpper) {
            // we found it
            return true;
         }
	  }
   }

   // if we got here then the class name is not there
   return false;
}



// ----------------------------------------------------------------------------
// addClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function addClassName(objElement, strClass, blnMayAlreadyExist)
   {

   // if there is a class
   if (objElement.className) {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // if the new class name may already exist in list
      if (blnMayAlreadyExist) {

         // get uppercase class for comparison purposes
         var strClassUpper = strClass.toUpperCase();

         // find all instances and remove them
         for (var i = 0; i < arrList.length; i++) {

            // if class found
            if (arrList[i].toUpperCase() == strClassUpper) {

               // remove array item
               arrList.splice(i, 1);

               // decrement loop counter as we have adjusted the array's contents
               i--;
			}
		 }
	  }

      // add the new class to end of list
      arrList[arrList.length] = strClass;

      // add the new class to beginning of list
      //arrList.splice(0, 0, strClass);
      
      // assign modified class name attribute
      objElement.className = arrList.join(' ');

   }
   // if there was no class
   else {
      // assign modified class name attribute      
      objElement.className = strClass;
   }
}



// ----------------------------------------------------------------------------
// removeClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
function removeClassName(objElement, strClass)
   {

   // if there is a class
   if (objElement.className) {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for (var i = 0; i < arrList.length; i++) {

         // if class found
         if (arrList[i].toUpperCase() == strClassUpper) {

            // remove array item
            arrList.splice(i, 1);

            // decrement loop counter as we have adjusted the array's contents
            i--;
		 }
	  }

      // assign modified class name attribute
      objElement.className = arrList.join(' ');

   }
   // if there was no class
   // there is nothing to remove
}

