
function updateResourceHighlight(sourceElement, targetElementID)
{
	sourceElement = $(sourceElement);
	var targetElement = $(targetElementID);

	if(sourceElement && targetElement)
	{
		var children = sourceElement.childElements();
		var img = null;

		var notFound = true;
		for(var i = 0; notFound &&  i < children.length; i++)
		{
			if(children[i].nodeName.toLowerCase() == "img")
			{
				img = children[i];
				notFound = false;
			}
		}

		if(img)
		{
			var newImg = document.createElement("img");

			if(img.src)
				newImg.setAttribute("src", img.src);

			if(img.width)
				newImg.setAttribute("width", img.width);

			if(img.height)
				newImg.setAttribute("width", img.height);

			if(img.alt)
				newImg.setAttribute("width", img.alt);

			targetElement.innerHTML = "";
			targetElement.appendChild(newImg);
		}

		sourceElement.siblings().each
		(
			function(sibling)
			{
				sibling.removeClassName("hover");
			}
		);

		if(!sourceElement.hasClassName("hover"))
			sourceElement.addClassName("hover");
	}
}

function uncheckChildCheckBoxes(parentID)
{
    var parent = $(parentID);
    
    if(parent)
    {
        parent.descendants().each
        (
            function(descendant)
            {
                if(descendant.nodeName.toLowerCase() == "input" && 
                        descendant.getAttribute("type").toLowerCase() == "checkbox")
                    descendant.checked = false;
            }
        );
    }
}
 
function displayControlBlock(controlID)
{
	toggleControlDisplay(controlID, "block");
}

function displayControlInline(controlID)
{
	toggleControlDisplay(controlID, "inline");
}

function hideControl(controlID)
{
	toggleControlDisplay(controlID, "none");
}

function toggleControlDisplay(controlID, displayCss)
{
	var cntrl = $(controlID);
			
	if(cntrl && displayCss && typeof(displayCss) == "string")
		cntrl.style.display = displayCss;
}

function hasFlashElement(element)
{
    var innerHTML = "";
    
    if(element)
        innerHTML = element.innerHTML.toLowerCase();
    
    return containsSubstring(innerHTML, "object") || containsSubstring(innerHTML, "embed");
}

function wasEnterKeyPressed(e)
{
    if(!e)
        e = event;
        
    var keyCode = null
    
    if(e.which)
        keyCode = e.which;
    else if(e.keyCode)
        keyCode = e.keyCode;
        
    var wasPressed = false;
    
    if(keyCode == 13)
        wasPressed = true;
        
    return wasPressed;
}

function containsSubstring(strSource, strSubString)
{
    return strSource.indexOf(strSubString) > -1;
}

function trim(str)
{
	if(str && typeof(str) == "string")
	{
		str = str.replace(/^\s+/, "");
		str = str.replace(/\s+$/, "");	
	}
	
	return str;
}

function setupGALinks() {
  var investors = $$('a[href="http://ir.statschippac.com/"]');
  if(investors.length) {
    $(investors[0]).observe('click', function() {
      _gaq.push(['_trackEvent', 'External Links', 'Click', 'Investors']);
    });
  }

  var myStats = $$('a[href="http://my.statschippac.com"]');
  if(myStats.length) {
    $(myStats[0]).observe('click', function() {
      _gaq.push(['_trackEvent', 'External Links', 'Click', 'mySTATSChipPAC']);
    });
  }

  var twitter = $$('a[href="http://www.twitter.com/STATSChipPACLtd"]');
  if(twitter.length) {
    $(twitter[0]).observe('click', function() {
      _gaq.push(['_trackEvent', 'External Links', 'Click', 'Twitter']);
    });
  }

	$$('a[href*="~/media/"]').invoke('observe', 'click', function(e) {
		var link = $(e.target);
		var href = link.readAttribute('href');
		_gaq.push(['_trackEvent', 'Media', 'Click', href]);
	});


}
document.observe("dom:loaded", setupGALinks);
