// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function fireColorManagement()
{
  try
  {
  	var event_fireCMM = document.createEvent("Events");
  	event_fireCMM.initEvent("fireCMM", true, true);
  	document.dispatchEvent(event_fireCMM);
	}
	catch(err)
	{
	  // Ignore errors
	}
}

function selectFormAFieldB(form_number, field_number)
{
	document.forms[form_number].elements[field_number].focus();
	document.forms[form_number].elements[field_number].select();
}

/* Adapted from: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow */
function getWindowHeight() {
  var myHeight = 0;
  if(typeof(window.innerHeight) == 'number') {
    // Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    // IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    // IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getElementHeight(elementName)
{
  element = document.getElementById(elementName);
  if (element) {
    /* style.height? */
    return parseInt(element.offsetHeight);
  }
  
  return 0;
}

function findAndResizeElements()
{
	fixedElementHeight = 0;
	fixedElementHeight += getElementHeight("header");
	fixedElementHeight += getElementHeight("top_menu_bar");
	fixedElementHeight += getElementHeight("first_panel_separator");
	fixedElementHeight += getElementHeight("my_palette_container");
	fixedElementHeight += getElementHeight("second_panel_separator");
	fixedElementHeight += getElementHeight("palettes_panel_rounded_corners");
	fixedElementHeight += getElementHeight("tab_container");
	fixedElementHeight += getElementHeight("footer_container");
	
	element_to_resize = document.getElementById("content_container");
	if(!element_to_resize)
	{
		element_to_resize = document.getElementById("content_container_main");
	}

	window_height = getWindowHeight();
	height = (window_height - fixedElementHeight) + "px";
	element_to_resize.style.height = height;
}

// Used to return to the palette that the user was looking at before the page refreshed
function scrollElementIntoView(elementToScrollToId)
{
  if (elementToScrollToId)
	{
    var elementToScrollTo = document.getElementById(elementToScrollToId);
    if (elementToScrollTo.scrollIntoView)
    {
      elementToScrollTo.scrollIntoView();
    }
  }
}

function installPlugIn()
{
  if (typeof InstallTrigger == "object")
  {
  	xpi = { 'xrcolormanager' : '/xrcolormanager_10.xpi' };
  	InstallTrigger.install(xpi, installPlugInCallback);
  }
  else 
  {
      alert("The X-Rite Color Management Firefox plug-in can only be installed in Firefox or other Mozilla browsers.");
  }
}

function installPlugInCallback(packageUrl, resultCode)
{
	if (resultCode != 0)
	{
		alert('Failed to install with error code: ' + resultCode + '.');
	}
}

// fixes bug in IE7 which does not support css hover
function addOverStateForMenu()
{
	if (document.all&&document.getElementById) 
	{
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) 
		{
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") 
			{
				node.onmouseover=function() 
				{
					this.className+=" over";
	  		}
	  		node.onmouseout=function() {
	  			this.className=this.className.replace(" over", "");
	   		}
	   	}
	  }
	}
}