﻿///-------------------------------------------------
/// Determing DOM or IE4
var DOM = document.getElementById;
var IE4 = document.all;
var LAYERS = document.layers;

// Ticker Variables
var tickerIV, tickerID;
var tickerItems = new Array();

// Compatible Get Element By ID
function compatibleGetObjById(id) {
  if(DOM) return document.getElementById(id);
  else if(IE4) return document.all[id];
  //else if(LAYERS) return document.layers[id];
  else return false;
}

function jumpURL(strURL) 
{
	if( strURL != undefined && strURL != "" )
	{
		location.href=strURL;
	}
}

function toggleShowDiv(divId, openClass, closedClass)
{
	var div = compatibleGetObjById(divId);
	
	if( div )
	{
		div.className = ( div.className == closedClass ) ? openClass : closedClass;
	}
}

function showClickTab(divNumber, baseTabId)
{
	var tabIndex = 1;
	var tabDivId = baseTabId + tabIndex;
	
	var tabDiv = compatibleGetObjById(tabDivId);
	
	while( tabDiv )
	{
		if( tabIndex == divNumber )
		{
			tabDiv.style.display = "block"
		}
		else
		{
			tabDiv.style.display = "none";
		}
		
		tabIndex++;
		
		tabDivId = baseTabId + tabIndex;
		tabDiv = compatibleGetObjById(tabDivId);
	}
}

function selectClickedTab(divNumber, baseLiId)
{
	var tabIndex = 1;
	var tabDivId = baseLiId + tabIndex;
	
	var tabDiv = compatibleGetObjById(tabDivId);
	
	while( tabDiv )
	{
		if( tabIndex == divNumber )
		{
			tabDiv.className = "selected";
		}
		else
		{
			tabDiv.className = "";
		}
		
		tabIndex++;
		
		tabDivId = baseLiId + tabIndex;
		tabDiv = compatibleGetObjById(tabDivId);
	}
}

function doSearch(language, searchText, startIndex) {
    //$('#startNum').val(startIndex);
   // doSearch(language, searchText);
//}


//function doSearch(language, searchText)
//{
    //alert("doSearch(" + language + ", " + searchText + ", " + startIndex + " );");
    
    var search = '';
    var searchTextQueryStringName = "searchtext"; ;
    var startIndexQueryStringName = "startindex"; ;


    if( searchText.length > 0 )
    {
        search = encodeURIComponent(searchText);
        var searchUrl = '';
        
        //alert(language);
        
        if( language == "en_us")
        {
            searchUrl = applicationSubDirectory + 'gsearch.aspx';
        }
        else
        {
			searchUrl = applicationSubDirectory + 'publicsearchresults.aspx';
        }
        
        var searchUrlParams = '?' + searchTextQueryStringName + '=' + search + '&' + startIndexQueryStringName + '=' + startIndex;
        //alert(searchUrl + searchUrlParams);
        window.location.href = searchUrl + searchUrlParams;
    }
}

function checkEnterForSearch(e, language, searchText, startIndex)
{ 
    //  e is event object passed from function invocation
    // var characterCode literal character code will be stored in this variable
    
    if(e && e.which)
    { //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else
    {
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    
    //alert(characterCode);

    if(characterCode == 13)
    { //if generated character code is equal to ascii 13 (if enter key)
        doSearch(language, searchText, startIndex); //submit the form
        return false;
    }
    else
    {
        return true;
    }
}

function tickerObject(id) {
  this.elem = compatibleGetObjById(id);
  this.width = this.elem.offsetWidth;
  this.x = tickerWidth;
  this.css = this.elem.style;
  this.css.width = this.width + 'px';
  this.css.left = this.x + 'px';
  this.move = false;
  return this;
}

function tickerNext() {
  if(!DOM && !IE4) return;
  var obj = tickerItems[tickerID];
  if(!obj.move) {
    obj.x = tickerWidth;
    obj.css.left = tickerWidth + 'px';
    obj.move = true;
  }
}

function tickerMove() {
  if(!DOM && !IE4) return;
  for(var i = 0; i < tickerItems.length; i++) {
    if(tickerItems[i].move) {
      if(tickerItems[i].x > -tickerItems[i].width) {
        tickerItems[i].x -= 2;
        tickerItems[i].css.left = tickerItems[i].x + 'px';
      }
      else tickerItems[i].move = false;
    }
  }
  if(tickerItems[tickerID].x + tickerItems[tickerID].width <= tickerWidth) {
    tickerID++;
    if(tickerID >= tickerItems.length) tickerID = 0;
    tickerNext();
  }
}

function tickerStart(init) {
  if(!DOM && !IE4) return;
  
  var obj = compatibleGetObjById('divTicker');
  obj.className = 'tickerNormalState';
   
  if(init) {
    tickerID = 0;
    tickerNext();
  }
  tickerIV = setInterval('tickerMove()', tickerDelay);
}

/*******************************************************************************/
/* TICKER CODE																   */
/*******************************************************************************/
function tickerStop() {
  if(!DOM && !IE4) return;
  clearInterval(tickerIV);
  
  var obj = compatibleGetObjById('divTicker');
  obj.className = 'tickerHoverState';
}

function tickerInit() {
  if(!DOM && !IE4) return;
  
  var entryIdBase = 'divTickerEntry';
  var id = 1;
  var entryId = entryIdBase + id;
  var entryDiv = compatibleGetObjById(entryId);
  
  while( entryDiv )
  {
		tickerItems[id - 1] = new tickerObject(entryId);
		id++;
		entryId = entryIdBase + id;
		entryDiv = compatibleGetObjById(entryId);
  }

  /*for(var i = 0; i < tickerEntries.length; i++) {
    tickerItems[i] = new tickerObject('divTickerEntry' + (i+1));
  }*/
  
  var obj = compatibleGetObjById('divTicker');
  obj.style.width = tickerWidth + 'px';
  obj.style.visibility = 'visible';
  tickerStart(true);
}

function tickerReload() {
  if(!DOM && !IE4) return;
  document.location.reload();
}

//Handle the onClick event for the taxonomy and search resutls
function AnalyticsOnClick(category, action, label) {
   _gaq.push(['_trackEvent', category, action, label]);
}

jQuery.noConflict();
jQuery(document).ready(function () {
    //alert("document.ready");
    // Do link check for shadowbox definitions in the accesskey attribute
    var jq1 = jQuery('a[accesskey^="lightwindow"]');
    if (jq1 != null) {
        // jQuery('a[accesskey^="lightwindow"]').each(function () {
        jq1.each(function () {
            var $a = jQuery(this);

            //alert( $a.attr('accesskey') );

            /* add the new attribute with the title value */
            //$a.attr("rel", $a.attr('accesskey'));
            doLightwindowParse($a, "accesskey");

            /* remove the old attribute */
            $a.removeAttr('accesskey');
        });
    }

    var jq2 = jQuery('a[target^="lightwindow"]');
    if (jq2 != null) {
        //jQuery('a[target^="lightwindow"]').each(function () {
        jq2.each(function () {
            var $a = jQuery(this);

            //alert( $a.attr('target') );

            /* add the new attribute with the title value */
            //$a.attr("rel", $a.attr('target'));
            doLightwindowParse($a, "target");

            /* remove the old attribute */
            $a.removeAttr('target');
        });
    }

    var linkCtr = 0; var assetMatch = 0; var externalMatch = 0; var mailMatch = 0; var specialsMatch = 0;
    var reFtypes = new RegExp("\\.(" + fTypes + ")$", "i");
    var baseHref = '';
    if (jQuery('base').attr('href') != undefined)
        baseHref = jQuery('base').attr('href');
    jQuery('a').each(function () {
        var href = jQuery(this).attr('href');
        var title = jQuery(this).text();
        linkCtr++;
        if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
            externalMatch++;
            jQuery(this).click(function () {
                var extLink = href.replace(/^https?\:\/\//i, '');
                //alert(extLink);
                //alert(href + "\n" + title);   
                _gaq.push(['_trackEvent', 'External', title, extLink]);
                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                    setTimeout(function () { location.href = href; }, 200);
                    return false;
                }
            });
        } else if (href && href.match(/^\/specials\//i)) {
            specialsMatch++;
            jQuery(this).click(function () {
                //alert(href + "\n" + title);                    
                _gaq.push(['_trackEvent', 'Specials', title, href]);
                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                    setTimeout(function () { location.href = href; }, 200);
                    return false;
                }
            });
        } else if (href && href.match(/^mailto\:/i)) {
            mailMatch++;
            jQuery(this).click(function () {
                var mailLink = href.replace(/^mailto\:/i, '');
                _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
            });
        }
        else if (href && href.match(reFtypes)) {
            assetMatch++;
            jQuery(this).click(function () {
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                var filePath = href;
                //alert(filePath);
                _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                    setTimeout(function () { location.href = baseHref + href; }, 200);
                    return false;
                }
            });
        }
    });

    //alert("linkCtr=" + linkCtr);
    //alert("assetMatch=" + assetMatch);
    //alert("externalMatch=" + externalMatch);
    //alert("mailMatch=" + mailMatch);
    //alert("specialsMatch=" + specialsMatch);

    //AutoSuggest setup        
    var cache = {};
    jQuery('#q').autocomplete({
        minLength: 2,
        source: function (request, response) {
            if (request.term in cache) {
                response(cache[request.term]);
                return;
            }

            jQuery.ajax({
                url: "gsuggest.aspx",
                dataType: "json",
                data: request,
                success: function (data) {
                    cache[request.term] = data;
                    response(data);
                }
            });
        }
    });
});

function doLightwindowParse(a, attributename) {

    var t = a.attr(attributename).split(";");

    if (t != null && t.length > 0) {
        if (a.attr("class") != "") {
            a.attr("class", a.attr('class') + " lightwindow page-options");
        }
        else {
            a.attr("class", "lightwindow page-options");
        }

        if (t.length > 1) {
            for (var i = 1; i < t.length; i++) {
                if (t[i].indexOf("lightwindow_") > -1) {
                    a.attr("params", t[i]);
                }
                else if (t[i].indexOf("desc") > -1) {
                    a.attr("caption", unescape(t[i].replace(/^desc=/gi, "")));
                }
                else if (t[i].indexOf("author") > -1) {
                    a.attr("author", unescape(t[i].replace(/^author=/gi, "")));
                }
            }
        }
    }

    //alert(a.attr(attributename));
} 
