// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

/* begin - Navigation Primary */
var NavigationPrimary = {
  init: function() {    
		var c = this;
		var n = $('#NavigationPrimary');
		
		// Use bgiframe to fix dropdowns over selects if it's available.
		$.fn.bgiframe && $('li', n).bgiframe();

		// Mark parent elements so that we can style them with CSS
		$('> li', n).each(function() { c.markParents(this); });
  	
  	// Use the hoverintent plugin if it's available
  	if ($.fn.hoverIntent)
    {
  	  $('li', n).hoverIntent( {
  	    sensitivity: 1,
  	    over: this.mouseenter,
  	    out: this.mouseleave,
  	    timeout: 100
  	  });
    }
  	else
  	  $('li', n).hover(this.mouseenter, this.mouseleave);
  },
  markParents: function(li) {
    var c = this;
    if ($('> ul', li).size() > 0)
	    $(li).addClass('parent').find('> a').addClass('parent').end().find('> ul li').each( function() { c.markParents(this); });
	},
	mouseenter: function() { $(this).addClass('over').children('a').addClass('over'); },
	mouseleave: function() { $(this).removeClass('over').children('a').removeClass('over'); }
	
};
$(function() { NavigationPrimary.init(); });
/* end - Primary Navigation */

function getElementsByClass(searchClass,node,tag) {
  // This function gets the elements that all share a class name
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function an_toggleDisplay(theid) {
	// This function toggles between the tabs and the corresponding content for the tabs.  The tabs <li> must correspond 1-1 with the order of the content UL content elements.
	// if your 3rd tab is "tab for content z" then ensure that your UL for the content is placed as the 3rd element.
	// we could give the tabs an id in order to match the id of the actual content, but it was kept this way in order to save on logic processing time and added code to the HTML (yet more id's to keep track of).
  
	var contentelmt = getElementsByClass('tabContents');
	var tabelmt = getElementsByClass('litab');
	var match = false;
		for (i = 0; i < contentelmt.length; i++) {
			if (theid == contentelmt[i].id) {
				match = true;
				contentelmt[i].style.display = "block";
				tabelmt[i].className ="litab active";
			}
			else {
				contentelmt[i].style.display = "none";
				tabelmt[i].className = "litab";			
			}		
		}
		if (!match) {
			contentelmt[0].style.display = "block";
			tabelmt[0].className="litab active";
		}
}

function toggledivs() {
  var inc, endInc=arguments.length;
  for (inc=0; inc<endInc; inc+=2) {
    var id = arguments[inc];
    if (arguments[inc+1] == 'none') param = "none";
    else if(arguments[inc+1]=='block') param = "block";
    if (document.layers) document.layers['container'].layers[id].visibility = param;
    else if (document.all) eval("document.all." + id + ".style.display = \"" + param + "\"");
    else if (document.getElementById) eval("document.getElementById(id).style.display = \"" + param + "\"");
  }
}

// Hide content with JS so that users w/out JS can still see it.
document.write('<style type="text/css">ul.expandableContent div.content { display: none; }</style>');

var ExpandableContent = {
  expandAllLinks: null,
  collapseAllLinks: null,
  titleLinks: null,
  init: function() {
    var ep = this;
    this.titleLinks = $('ul.expandableContent > li > a.title').click(
      function() {
        if ($(this).is('.open'))
          ep.close($(this));
        else
          ep.open($(this));
        return false;
      }
    );
    this.expandAllLinks = $('ul.expandableContentControls a.expandAll').click( ep.openAll );
    this.collapseAllLinks = $('ul.expandableContentControls a.collapseAll').click( ep.closeAll );
    ExpandableContent.checkState();
  },
  open: function($item) {
    if ($item.is('.open'))
      return;
    $item.blur().addClass('open').next().show();
    ExpandableContent.checkState();
    return false;
  },
  close: function($item) {
    if (! $item.is('.open'))
      return;
    $item.blur().removeClass('open').next().hide();
    ExpandableContent.checkState();
    return false;
  },
  openAll: function() {
    if ($(this).blur().is('.disabled'))
      return false;
    ExpandableContent.titleLinks.each(
      function() { ExpandableContent.open($(this)); }
    );
    return false;
  },
  closeAll: function() {
    if ($(this).blur().is('.disabled'))
      return false;
    ExpandableContent.titleLinks.each(
      function() { ExpandableContent.close($(this)); }
    );
    return false;
  },
  checkState: function() {
    if (this.titleLinks.filter( function() { return ! $(this).is('.open'); }).length == 0) {
      // All Open
      this.expandAllLinks.addClass('disabled');
    } else if (this.titleLinks.filter('.open').length == 0) {
      // All Closed
      this.collapseAllLinks.addClass('disabled');
    } else {
      // Some open, some closed
      this.expandAllLinks.removeClass('disabled');
      this.collapseAllLinks.removeClass('disabled');
    }
  }
};
$(function() { 
	ExpandableContent.init(); 
      $('a[rel*=facebox]').facebox({
        loading_image : '../pics/common/facebox/loading.gif',
        close_image   : '../pics/common/facebox/closelabel.gif'
      }) 

	$('a.tooltip')
		.showTooltip( { title: $('a.tooltip').attr('title') } );

});

/* Tooltips */
(function($) {
	$.fn.showTooltip = function(options){
		var settings 	= $.extend({
				title: 	'',
				xOffset: 10,
				yOffset: 20,
				id: 'ToolTip'
		}, options);

		return this
			.attr('title', settings.title)
			.hover(
				function(e) {
					this.t = this.title;
					if (this.t == "undefined")
							this.t = settings.title;
					this.title = "";
					$("body").append("<p id='" + settings.id + "'>"+ this.t +"</p>");
					$("#" + settings.id)
						.css("top",(e.pageY - settings.xOffset) + "px")
						.css("left",(e.pageX + settings.yOffset) + "px")
						.fadeIn("fast");
				},
				function(e) {
					this.title = this.t;
					$("#" + settings.id).remove();
				}
			)
			.mousemove(function(e) {
				$("#" + settings.id)
					.css("top",(e.pageY - settings.xOffset) + "px")
					.css("left",(e.pageX + settings.yOffset) + "px");
			});
	}
})(jQuery);


/* Preload images function */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/* Preload homepage images */
MM_preloadImages("/pics/home/crm-on.gif", "/pics/home/marketing-on.gif", "/pics/home/retail-on.gif", "/pics/home/web-on.gif");