//  +---------------------------------------------------------------------------+
//  | The Virtual Cutter
//  +---------------------------------------------------------------------------+
//  | Filename: virtualcutter.js
//  | Version: 5.0.1
//  | Date created: 01/10/2001
//  | Date last revision: 27/10/2009
//  +---------------------------------------------------------------------------+
//  | Purpose: Shared JavaScript module.
//  | Contains functions for DHTML, communication with players, etc.
//  +---------------------------------------------------------------------------+
//  | Copyright (c) 2001, 2008: André Rosendaal, University of Groningen, Netherlands
//  | e-mail: a.j.m.rosendaal@rug.nl
//  +---------------------------------------------------------------------------+
//  | This program is free software; you can redistribute it and/or
//  | modify it under the terms of the GNU General Public License
//  | as published by the Free Software Foundation; either version 2
//  | of the License, or (at your option) any later version.
//  | 
//  | This program is distributed in the hope that it will be useful,
//  | but WITHOUT ANY WARRANTY; without even the implied warranty of
//  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  | GNU General Public License for more details.
//  | 
//  | You should have received a copy of the GNU General Public License
//  | along with this program; if not, write to the Free Software
//  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//  |
//  | More information: http://www.gnu.org/licenses/licenses.html
//  +---------------------------------------------------------------------------+
//  | Version history:
//  | - 4.0.4: Added parameters to metadata-editor call in OpenMetadataEditor 
//  | - 5.0.0: Introduction of version 5.0. New GUI with CSS. 
//  |          Added OpenModalWindow for displaying secundary modal windows.			 
//  | - 5.0.1: The parameter URL in OpenMetadataEditor is now the complete
//  !          URL to start the fragment (instead of separate end, start parameters etc.)
//  +---------------------------------------------------------------------------+
  

  var vStartPosition, vEndPosition
  var pos_play_part, pos_save_part


  vStartPosition = 0
  vEndPosition = 0

  ns4 = (document.layers)? true:false
  ie4 = (document.all)? true:false
  dom = (document.getElementById)? true:false
  
  // LayerWrite
  // Write text to a DHTML-object
  // id      : id of the object
  // nestref : ???
  // text    : text that has to written to the object
  function LayerWrite(id,nestref,text) {
  
	if (ns4) {
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document
		lyr.open()
		lyr.write(text)
		lyr.close()
	}
	else {
	  if (ie4) {
	    document.all[id].innerHTML = text
	  } else {
	      // 'innerHTML' is not part of DOM, but is supported by IE and Netscape 6
	      if (dom) {
	        document.getElementById([id]).innerHTML = text
	      } else {
	        alert (STR_NO_DHTML_SUPPORT)
	      }
	  } 
	}    
  }
 

  // ConvertMSecs
  // Converts time in milliseconds to text. Format: "xx min. xx.yy sec."
  // length: time in milliseconds
  function ConvertMSecs(length) {
  
  var vTime, vMin, vSec
  
  	// Round first, because otherwise problems occur, e.g. 
  	// 1 min. 4.1 sec becomes 64099.999999999. Javascript bug?
  	length = Math.round(length)
  	
  	vTime = length/1000
  	vMin = Math.floor(vTime/60)
  	vSec = Math.floor(vTime % 60)
  	vMsec = (length % 1000)
  	if (vMsec < 10) {
  	  vMsec = "00" + vMsec
  	}  
  	else {  	
    	  if (vMsec < 100) {
  	    vMsec = "0" + vMsec
  	  }
  	}
  	return  vMin + " min. " + vSec + "." +  vMsec + " sec."
  }
  
  // UpdateHREF
  // Adjust the 'href' value of the hyperlink for playing and saving the clip.
  // IE can do that if you use the 'name' parameter of the <a> tag, but to support Netscape 4,
  // the order number of the link wa sestablished first. For Netscape 6 the 'name' paramter could 
  // also be used, but only in a loop. Therefore, for NS6 the function also uses the order number.
  // link_name : name of the hyperlink
  // new_href  : new value of the href parameter
  function UpdateHREF (link_name, new_href) {
  	if (ie4) {
    	  document.links(link_name).href=new_href
    	} else {
//          if (ns4) {
      	    switch (link_name) {
      	      // update hyperlink for playing the clip
      	      case 'play_part_ref': 
      	        document.links[pos_play_part].href = new_href
      	        break
      	      // update hyperlink for saving the clip
      	      case 'save_part_ref': 
      	        document.links[pos_save_part].href = new_href
      	        break
      	    }
//      	  } else {
//       	    for (i=0;i<document.links.length;i++) {
//              if (document.links[i].name.indexOf(link_name) >= 0 ) {
//                document.links[link_name].href=new_href
//              }  
//            }    
//          }
        }  
    }

  
  // UpdateAfterMarksHaveChanged
  // Perform steps after the user has modified the clip settings
  function UpdateAfterMarksHaveChanged () {
    	
    	// display an icon (scissors) to indicate visualy that the clip was changed
    	document.playlist_icon.src = "./images/cutout_changed.jpg"

    	// Adjust href-value of the buttons the play and save the clip.
    	UpdateLinkForPlayback ()
    	UpdateLinkForSave () 
  } 
  
  
  // InitPositionOfDynamicLinks
  // A tric to get the order number of a hyperlink in Netscape
  // The name of the link cannot be used when its href value is to be changed.
  function InitPositionOfDynamicLinks () {

    pos_play_part = 9999;

    for(num=0;num<document.links.length;num++) {
    if (document.links[num].href.indexOf("play_part") != -1) {
      pos_play_part = num;
      num = 5000;
      }
    }  
    pos_save_part = 9998;
    for(num=0;num<document.links.length;num++) {
      if (document.links[num].href.indexOf("save_part") != -1) {
        pos_save_part = num;
        num = 5000;
      }
    }
  }  

 	
  // UpdateLinkForPlayback
  // Update the value of href for the button to play the current clip  
  function UpdateLinkForPlayback () {
  	
  	UpdateHREF ('play_part_ref', ShowURL() )
  }


  // UpdateLinkForSave
  // Update the value of href for the button to save the current clip  
  // For this, an empty hyperlink in edit_item.php was added
  function UpdateLinkForSave () {
    	if (document.links[pos_save_part]) {
        UpdateHREF ('save_part_ref', SaveCutOut() )
      }  

  }	

  // defaultErrorHandler
  // Restore the default JavaScript error handler

  function defaultErrorHandler() {
    return false
  }
  
  // PositionErrorHandler
  // Display a custom JavaScript error if the current position of the player could not be retrieved from the plugin
  // e.g. if it does not support JavaScript
  function PositionErrorHandler(desc,page,line,chr)  {
    alert(STR_CURRENT_POSITION_NOT_READ)
    window.onerror=defaultErrorHandler
    return true
  }
  
  
  // JumpErrorHandler
  // Display a custom JavaScript error if the currentposition of the player could not be set
  // e.g. because it does not support JavaScript
  function JumpErrorHandler(desc,page,line,chr)  {
    alert(STR_CURRENT_POSITION_NOT_SET)
    window.onerror=defaultErrorHandler
    return true
  }
  
    
  
  // GetInputByHand
  // Ask for manual input of the start or end point
  // Time    : current start or end point in milliseconds
  // Message : text of the prompt
  function GetInputByHand (Time, Message) {
  
    a = prompt (Message, ConvertMSecs(Time))
    if (a == null) {
      // "Cancel"
      return -1
    } else {  
      a = a.split(" ")
      mm = Math.abs(parseInt(a[0]))
      ss = Math.abs(parseFloat(a[2]))
      if (isNaN((mm)) | isNaN(ss)) {
        alert(STR_WRONG_TIME_FORMAT)
        return -1
      } else {
        return (mm*60 + ss) * 1000
      }
    }
  }  
 
  
  // GetStartByHand
  // User wants to enter start point manually
  function GetStartByHand () {
    
    OldStartPosition = vStartPosition	
    GetStartTime("ByHand")
    if (OldStartPosition != vStartPosition) {
      UpdateAfterMarksHaveChanged ()
    }
  }

  // GetEndByHand
  // User wants to enter end point manually
  function GetEndByHand () {
    
    OldEndPosition = vEndPosition	
    GetEndTime("ByHand")
    if (OldEndPosition != vEndPosition) {
      UpdateAfterMarksHaveChanged ()
    }
  }


  // GetStartTime
  // Get the start point, either by reading the current position of the player or
  // by opening an input window
  // Mode : "ByHand" for manual input, else NULL
  function GetStartTime  (Mode) {
  var NewPosition

    OldPosition = vStartPosition	
    if (Mode == "ByHand") {
      NewPosition = GetInputByHand (vStartPosition, STR_ENTER_START_TIME)	
      if (NewPosition == -1) {
      	<!-- input not recognized -->
      	return -1
      } else {
        vStartPosition = NewPosition
      }
    } else {	
        vStartPosition = GetPosition ()
    }  
    if (vStartPosition > vEndPosition) {
      vEndPosition = vStartPosition
      ShowEndTime()
    }
    if (OldPosition != NewPosition) {
      UpdateAfterMarksHaveChanged ()
    }
    <!-- Display start Time -->
    ShowStartTime()
  }
  
  
  // GetEndTime
  // Get the end point, either by reading the current position of the player or
  // by opening an input window
  // Mode : "ByHand" for manual input, else NULL
  function GetEndTime  (Mode) {
  var NewPosition

    OldPosition = vStartPosition	
    if (Mode == "ByHand") {
      NewPosition = GetInputByHand (vEndPosition, STR_ENTER_END_TIME)
      if (NewPosition == -1) {
      	<!-- input not recognized -->
      	return -1
      } else {
        vEndPosition = NewPosition
      }  
    } else {	
        vEndPosition = GetPosition ()
    } 
    if (vStartPosition > vEndPosition) {
      vStartPosition = vEndPosition
      ShowStartTime()
    }  
    if (OldPosition != NewPosition) {
      UpdateAfterMarksHaveChanged ()
    }
    <!-- Display end Time -->
    ShowEndTime()
  }

  // Anchor
  // returns the txt of the hyperlink (the anchor), either default or set by user.
  function Anchor () {
    if (TITLE == '') {
      return DEFAULT_ANCHOR
    } else {
      return TITLE	
    }  	  	
  }	
  
  // ShowCurrentSelection
  // Display current start and end time
  // start  : start time in milliseconds
  // end    : end time in milliseconds
  function ShowCurrentSelection (start, end) {
  
  vStartPosition = start*1000
  vEndPosition = end*1000
  ShowStartTime()
  ShowEndTime()
  }	  
  
  // DecreaseStartTime
  // Decrease the starttime by 0.1 sec
  function DecreaseStartTime () {
    vStartPosition = Math.max (0, vStartPosition - 100)
    UpdateAfterMarksHaveChanged ()
    ShowStartTime()
  }
  
  
  // IncreaseStartTime
  // Increase the starttime by 0.1 sec
  function IncreaseStartTime () {
    vStartPosition = vStartPosition + 100
    UpdateAfterMarksHaveChanged ()
    ShowStartTime()
    if (vStartPosition > vEndPosition) {
      vEndPosition = vStartPosition
      ShowEndTime()
    }
  }
  

  // DecreaseEndTime
  // Decrease the endtime by 0.1 sec
  function DecreaseEndTime () {
    vEndPosition = Math.max (0, vEndPosition - 100)
    UpdateAfterMarksHaveChanged ()
    if (vStartPosition > vEndPosition) {
      vStartPosition = vEndPosition
      ShowStartTime()
    }
    ShowEndTime()
  }


  // IncreaseEndTime
  // Increase the endtime by 0.1 sec
  function IncreaseEndTime () {
    vEndPosition = vEndPosition + 100
    UpdateAfterMarksHaveChanged ()
    ShowEndTime()
  }
  
  // mod
  // modulo function
  function mod(divisee,base) {
  	// Created 1997 by Brian Risk.  http://members.aol.com/brianrisk

  	return Math.round(divisee - (Math.floor(divisee/base)*base));
  }
  
  
  // GetPlaylistCount
  // returns the number of items in the playlist
  function GetPlaylistCount () {
    
      return count($playlist);
    }
    

  // OpenWindow
  // Open a secondary window without buttonbar
  // URL: url that is to be displayed in the window
  function OpenWindow(URL) {
   vWidth = 700;
   vHeight = 550; 
   vOptions = 'resizable,status,scrollbars,width=' + vWidth + ',height=' + vHeight + ',left=100,top=50'
   popupWin = window.open(URL, 'second', vOptions)
  } 
   
  // OpenModalWindow
  // Open a secondary modal window without buttonbar
  // URL: url that is to be displayed in the window
  // Due to a problem with the options page not operational at the moment
  // IE requested if a temporary page (save_options.php) should be cloded, but this page
  // should never become visible
  function OpenModalWindow(URL) {
//   if (window.showModalDialog) {
//     window.showModalDialog(URL,"name", "dialogWidth:800px;dialogHeight:550px");
//   } else {
     window.open(URL,'name','width=800,height=550,toolbar=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=yes ,modal=yes');
//   }
  }
  
  
  // GenerateOutput
  // Open the window that contains the different flavours of the clip
  function GenerateOutput() {
  	
    OpenModalWindow("./generate_output.php?player="+ PLAYERTYPE + "&lang=" + LANG + "&url=" + GetURL() + 
        "&assettype=" + ASSETTYPE + "&start=" + vStartPosition + "&end=" + vEndPosition + _SID +
        "&cur_title=" + document.mainform.item_title.value)
  }
    
    
  // OpenMetadataEditor
  // Open the webpage that contains the input form for entering the clip to an audiovisual database/CMS.
  // Parameters are added to the call to the URL, because ideally this would point to a script that collects the parameters
  // and re-creates the url to playback the clip, so users do not have to copy-and-paste them. 
  // Obviously, this would be CMS-specific.
  // type : if set to 'no_param', no parameters are added to the url. This is the case of OpenMetadataEditor
  //        is called from the playlist, because adding parameters only has meaning for a single clip.
  function OpenMetadataEditor (type) {
    
      if (METADATA_EDITOR == ""){
        // No address defined in virtualcutter.ini, thus option not supported on the current server
        
        var editor=window.open('','name','height=300,width=300');
    
        editor.document.write('<html><head><title>Metadata</title>');
        editor.document.write('<style type="text/css">');
        editor.document.write('<!--');
        editor.document.write('p {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }');
        editor.document.write('-->');
        editor.document.write('</style>');
        editor.document.write('</head><body>');
        editor.document.write('<p>' + STR_NO_METADATA_EDITOR + '</p>');
        editor.document.write('<p><a href="javascript:self.close()">' + STR_CLOSE_WINDOW + '</a></p>');
        
        editor.document.write('<p>' + type + '</p>');
        
        editor.document.write('</body></html>');
        editor.document.close();
      } else {	
      	if (type == 'no_param') {
      		OpenWindow (METADATA_EDITOR + "?lang=" + LANG )
      	} else {	
          /*OpenWindow (METADATA_EDITOR + "?url=" + GetURL() + 
          "&player=" + PLAYERTYPE + "&version=" + PLAYERVERSION + "&start=" + vStartPosition + "&end=" + vEndPosition + 
          "&script=" + escape(URLBase) + "&title=" + escape(document.mainform.item_title.value) + "&lang=" + LANG )*/
          enc_type=urldecode(type)
          OpenWindow (METADATA_EDITOR + "?url=" + urlencode(enc_type)+ "&title=" + escape(document.mainform.item_title.value) + "&lang=" + LANG )
        }  
      }  
  }
  
  
  // NextAsset
  // Open the input screen
  // parlist  : list of parameters
  function NextAsset(parlist) {
    _href = "./select_file.php"
    if (parlist != "") {
       //parlist is at least the SID
       _href = _href + "?" + parlist
    } 
    parent.location.href = _href
  } 
  
  // GetDocumentLocation
  // Return the url of the current webpage
  function GetDocumentLocation() {
    	
    return document.location
  }
    
  
  // DoNothing
  // Dummy function. Used to prevent an unexpected action if the user presses the Enter key
  // when the focus is on an input field. Is set as form action parameter. 
  // 
  function DoNothing () {
    
  }
  
  
  
  // ValidateNumber
  // Checks if an object contains a number
  // objectname : name of form lement
  // message    : error message if not a number
  function ValidateNumber (objectname, message) {
    TestVar = isNumberString (objectname.value)
    if (TestVar != 1) {
      alert (message);
    }  
    return TestVar  
  }
  
  // isNumberString
  // Checks if alle characters in a string are digits
  // Instring  : string that is to be checked
  function isNumberString (InString)  {
  	if(InString.length==0) return (false);
  	var RefString="1234567890";
  	for (Count=0; Count < InString.length; Count++)  {
  		TempChar= InString.substring (Count, Count+1);
  		if (RefString.indexOf (TempChar, 0)==-1)  
  			return (false);
  	}
  	return (true);
  }
  
  
  // getImageDir
  // returns the language specific directory with images
  // Lang     : selected language
  // StartDir : parent directory of language specific directory
  function getImageDir(Lang, StartDir) {
        return "./" + StartDir + "images/" + Lang + "/"
  }
  
/* hover buttons - added in version 5 */
function getElementsByClassName(className, tag, elm){
				var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
				var tag = tag || "*";
				var elm = elm || document;
				var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
				var returnElements = [];
				var current;
				var length = elements.length;
				for(var i=0; i<length; i++){
					current = elements[i];
					if(testClass.test(current.className)){
						returnElements.push(current);
					}
				}
				return returnElements;
			}

/* 
	Sets the onMouseOver and onMouseOut events for all button elements 
	The events set the classname of the elements to create a hover effect.
*/

function applyHoverState(classname) {
	elements = getElementsByClassName(classname,"*",document);

	var mouseoverfunction = new Function("this.className='" + classname + "_hover" + "'");
	var mouseoutfunction = new Function("this.className='" + classname + "'");
	if(elements && elements.length > 0) {
		for(n = 0; n < elements.length; n++) {
			
			
			e = elements[n];
			e.onmouseover = mouseoverfunction;
			e.onmouseout = mouseoutfunction;
		}
	}
}
function applyHoverStates() {
	/* set events for button_type_1 */
	applyHoverState("button_type_1");

	/* set events for button_type_2 */
	applyHoverState("button_type_2");

	/* set events for button_type_2_special */
	applyHoverState("button_type_2_special");
}
