//Global variable		
var _WebcamVideoInputWindow = null;		

//The following enumeration describes the different webcam properties.
var
	ISDKWEBCAMPROPERTY_UNKNOWN               =  0, /* The property is not known. */
	ISDKWEBCAMPROPERTY_BACKLIGHTCOMPENSATION =  1, /* Backlight compensation, a feature that compensates for bright backgrounds so foreground objects aren't silhouetted. Possible values are 0 (off) and 1 (on). */
	ISDKWEBCAMPROPERTY_BRIGHTNESS            =  2, /* Brightness (also called the black level) determines the amount of light that is shown for the darkest areas of an image. Brightness should be adjusted so that black picture content displays as true black on your monitor. Possible values range from 0.0 to 1.0. */
	ISDKWEBCAMPROPERTY_COLORENABLE           =  3, /* Specifies the color enable setting. Possible values are 0 (off) and 1 (on). */
	ISDKWEBCAMPROPERTY_CONTRAST              =  4, /* Contrast determines the range of brightness between the darkest and lightest elements of an image. Possible values range from 0.0 to 1.0. */
	ISDKWEBCAMPROPERTY_GAIN                  =  5, /* Specifies the gain adjustment. Values from 0.0 upto 0.5 specify a darker image, a value of 0.5 is normal and values starting from 0.5 to 1.0 specify a brighter image. */
	ISDKWEBCAMPROPERTY_GAMMA                 =  6, /* Gamma is the brightness of mid-level tones in an image. Possible values range from 0.0 to 1.0. */
	ISDKWEBCAMPROPERTY_HUE                   =  7, /* Tint control. Possible values range from 0.0 to 1.0. */
	ISDKWEBCAMPROPERTY_SATURATION            =  8, /* Richness, intensity of the colors. A highly saturated hue has a vivid, intense color, while a less saturated hue appears more muted and grey. Possible values range from 0.0 to 1.0. */
	ISDKWEBCAMPROPERTY_SHARPNESS             =  9, /* Determines the apparent image resolution. Higher sharpness creates the effect of greater resolution by accentuating light to dark transitions. Setting the sharpness too high actually degrades the picture by exaggerating the transitions. Possible values range from 0.0 to 1.0. */
	ISDKWEBCAMPROPERTY_WHITEBALANCE          = 10; /* Specifies the color temperature. The white balance determines the adjustment for the type of lighting used. Possible values range from 0.0 to 1.0. */	

function isString()
{
	for (var i = 0; i < arguments.length; ++i )
	{  		
		if (arguments[i] == null || typeof(arguments[i]) != 'string') {
			return false;
		}
	}
	return true;	  	    	
}	

function GetVideoWindow()
{  	  	
	if (!window["Intellivic_Video_Window"]) {  		
		return null;
	}

	try
	{
	  var _windowId = Intellivic_Video_Window.WindowId();						
	}
	catch (err)
	{
	  //alert('Could not use videowindow. Please check browser settings and make sure all Intellivic addons are enabled.');
	  return false;
	}	 	
	 	
	return Intellivic_Video_Window;
}      
	
function CreateWebcamVideoInput()
{  	  	  	
  if (_WebcamVideoInputWindow) 
    DestroyWebcamVideoInput(_WebcamVideoInputWindow);  	  	
	
	var manager = GetManager();
	if (!manager) {
		return;
	}		  	  	  	  	  	
	  	  	  	  	  	  	
	//Get the id of the currently selected video input device.
	PrintToLogfile('VIDIVIC-LOG: function call: manager.CurrentVideoInput()');
	var currentWebCamVideoInput = manager.CurrentVideoInput();
	PrintToLogfile('VIDIVIC-LOG: function result: CurrentVideoInput = ' + currentWebCamVideoInput.toString());
  
  if (!isString(currentWebCamVideoInput) || currentWebCamVideoInput.length == 0) {
  	return;
  }
  	
  PrintToLogfile('VIDIVIC-LOG: function call: manager.CreateWebcamVideoInput(' + currentWebCamVideoInput.toString() +')');	
  _WebcamVideoInputWindow = manager.CreateWebcamVideoInput( currentWebCamVideoInput );  			  	  			                      
  PrintToLogfile('VIDIVIC-LOG: function result: CreateWebcamVideoInput = ' + _WebcamVideoInputWindow.toString());
  
  if(_WebcamVideoInputWindow == "")
  {
	  PrintToLogfile('VIDIVIC-LOG: function call: manager.LastError()');
		lastError = manager.LastError();
		PrintToLogfile('VIDIVIC-LOG: function result: manager.LastError() = ' + manager.LastError());
		if(lastError == ISDKERROR_INTERNAL_ERROR)
      return lastError;
    else
      return false;
  }
}	

function DestroyWebcamVideoInput()
{				
	var manager = GetManager();
	if (!manager) {
		_WebcamVideoInputWindow = null;
		return;
	}														
	if (_WebcamVideoInputWindow) 
  {
		PrintToLogfile('VIDIVIC-LOG: function call: manager.DestroyWebcamVideoInput(' + _WebcamVideoInputWindow.toString() +')');	
		error = manager.DestroyWebcamVideoInput(_WebcamVideoInputWindow);
		PrintToLogfile('VIDIVIC-LOG: function result: DestroyWebcamVideoInput = ' + error.toString());	
		_WebcamVideoInputWindow = null;
	}
}	

function InitVideoWindow()
{																			
	var videoWindow = GetVideoWindow();
	if (!videoWindow) {
		return "no_video_window";
	}								

	if (!_WebcamVideoInputWindow) {			
		return false;
	}												

	var manager = GetManager();
	if (!manager) {
		return false;
	}				
	
	var _windowId = videoWindow.WindowId();						

  PrintToLogfile('VIDIVIC-LOG: function call: manager.SetVideoWindowForWebcamVideoInput(' + _WebcamVideoInputWindow.toString() +',' + _windowId.toString() + ')');	
	error = manager.SetVideoWindowForWebcamVideoInput(_WebcamVideoInputWindow, _windowId);	  
	PrintToLogfile('VIDIVIC-LOG: function result: SetVideoWindowForWebcamVideoInput = ' + error.toString());	
  
  return true;
}	

function FillIntPropertyList(combobox, property)
{						
	_combo = myGetElementById(combobox);
	if (!_combo) {
		return;
	}
	
	if (!_WebcamVideoInputWindow) {
		return;
	}		
	
	_visible = IsPropertySupported(property);
	
	_row = 'row' + combobox.substr(6, combobox.length)		
	_row = myGetElementById(_row);
	if (_row) {
		if (!_visible) {
			_row.style.display = 'none';
		}
		else {
			_row.style.display = '';
		}
	}							
			
	if (!_visible) {
		_combo.style.display = 'none';
	}
	else {
		_combo.style.display = '';
	}					
	
	
  var firstChild = _combo.firstChild;
  while ( firstChild ) {
	  _combo.removeChild ( firstChild );
	  firstChild = _combo.firstChild;
  }		
  if (!_visible) {
  	return;
  }
  
  _default = GetWebcamPropertyValue(property);										
  
  for (i = 0; i <= 20; i++) {
  	var _option = document.createElement('option');
  	_option.value = i;    	
  	var value = (i / 20).toFixed(2);
  	_option.selected = value == _default;
  	_option.innerHTML = ' ' + value;
  	_combo.appendChild(_option);
  }    						
}	

function FillBoolPropertyList(combobox, property)
{
	_combo = myGetElementById(combobox);
	if (!_combo) {
		return;
	}
	
	_visible = IsPropertySupported(property);
	
	_row = 'row' + combobox.substr(6, combobox.length)		
	_row = myGetElementById(_row);
	if (_row) {
		if (!_visible) {
			_row.style.display = 'none';
		}
		else {
			_row.style.display = '';
		}
	}					
	
	if (!_visible) {
		_combo.style.display = 'none';
	}
	else {
		_combo.style.display = '';
	}				
	
	_default = (GetWebcamPropertyValue(property) / 1).toFixed(0);
	if (_default == 0) {
		_default = false;
	}
	else {
		_default = true;
	}		
	
	var firstChild = _combo.firstChild;
	while ( firstChild ) {
		_combo.removeChild( firstChild );
		firstChild = _combo.firstChild;
	}
	if (!_visible) {
		return;
	}
	
	var _option = document.createElement('option');
	_option.value = 0;
	_option.selected = !_default;
	_option.innerHTML = ' ' + 'Nee';
	_combo.appendChild(_option);
	
	var _option = document.createElement('option');
	_option.value = 1;
	_option.selected = _default;
	_option.innerHTML = ' ' + 'Ja';
	_combo.appendChild(_option);
}

function IsPropertySupported(property)
{				
	if (!_WebcamVideoInputWindow) {
		return;
	}
	
	manager = GetManager();
	if (!manager) {
		return;
	}
	
	PrintToLogfile('VIDIVIC-LOG: function call: manager.PropertyForVideoInput(' + _WebcamVideoInputWindow.toString() +',' + property.toString() + ')');	
	value = manager.PropertyForVideoInput(_WebcamVideoInputWindow, property);				
	PrintToLogfile('VIDIVIC-LOG: function result: PropertyForVideoInput = ' + value.toString());
	
	return value != -1;
}

function GetWebcamPropertyValue(property)
{
	if (!_WebcamVideoInputWindow) {
		return;
	}
	
	manager = GetManager();
	if (!manager) {
		return;
	}
	
	PrintToLogfile('VIDIVIC-LOG: function call: manager.PropertyForVideoInput(' + _WebcamVideoInputWindow.toString() +',' + property.toString() + ')');	
	value = manager.PropertyForVideoInput(_WebcamVideoInputWindow, property);
	PrintToLogfile('VIDIVIC-LOG: function result: PropertyForVideoInput = ' + value.toString());
	value = (value / 1).toFixed(2);
	
	return value;
}

function SetWebcamPropertyValue(property, value)
{							
	if (!_WebcamVideoInputWindow) {
		return;
	}
				
	manager = GetManager();						
	if (!manager) {						
		return;
	}										
					
	value = (value / 1).toFixed(2);												
	
	PrintToLogfile('VIDIVIC-LOG: function call: manager.SetPropertyForVideoInput(' + _WebcamVideoInputWindow.toString() +',' + property.toString() + ',' + value.toString() + ')');	
	var error = manager.SetPropertyForVideoInput(_WebcamVideoInputWindow, property, value);
	PrintToLogfile('VIDIVIC-LOG: function result: SetPropertyForVideoInput = ' + error.toString());
	if (error != ISDKERROR_NONE) {
		alert( showErrorMsg( error ) );
	}        	    			
}						

function SetWebcamPropertyFromCombobox(property, combobox)
{
	_combo = myGetElementById(combobox);
	if (!_combo) {
		return;
	}				
	
	var value = 0;
	for (i=0; i < _combo.options.length; i++) {
		if (_combo.options[i].selected) {
			value = _combo.options[i].value;
		}
	}						
	
	value = (value / (_combo.options.length - 1)).toFixed(2);								
			
	SetWebcamPropertyValue(property, value);
}	
