$(document).ready(function(){
	var activeItem;
	$('#sub-nav ul').css('opacity', 0.99);
	$('li.active').addClass('original_active');
	changeHoveredItem($('li.active'));
	$('#top-nav > li').hover(function(){
		changeHoveredItem($(this));
	}, function(){
		
	});
	$('#nav').hover(function(){
		
	}, function(){
		changeHoveredItem($('li.original_active'));
	});
	function changeHoveredItem(list) {
		if(list == undefined) {
			return;
		}
		if(activeItem != $('a', list).html()) {
			var left = list.position().left;
			$('#top-nav > li').removeClass('active');
			list.addClass('active');
			var content = $('ul', list).html();
			if(content == null) {
				content = '';
			}
			var offsetMargin = $('ul', list).css('margin-left');
			if(offsetMargin == undefined) {
				offsetMargin = 0;
			} else {
				offsetMargin = offsetMargin.replace(/px/, '');
			}
			$('#sub-nav').html('<ul style="margin-left: ' + (left - offsetMargin) + 'px">' + content + '</ul>');
			
			$('#sub-nav ul').css('opacity', 0).animate({opacity: 1}, 200);
			
			activeItem = $('a', list).html();
			
		}
		
	}
	var color = { 'default': '#DB0000', healthcare: '#0066CB', mobility: '#c7a900' };
	if (typeof sIFR == "function") {
		sIFR.replaceElement(named({
			sSelector: "h2",
			sFlashSrc: baseDir + "sifr/avantegardemdbt.swf",
			sColor: color[layout],
			sBgColor: "#FFFFFF"
		}));
		sIFR.replaceElement(named({
			sSelector: "#casestudyblock h3, .resource-search h3",
			sFlashSrc: baseDir + "sifr/avantegardemdbt.swf",
			sColor: "#FFFFFF",
			sBgColor: color[layout]
		}));				
	}
});

// bigTarget.js - A jQuery Plugin
// Version 1.0.1
// Written by Leevi Graham - Technical Director - Newism Web Design & Development
// http://newism.com.au
// Notes: Tooltip code from fitted.js - http://www.trovster.com/lab/plugins/fitted/

// create closure
(function($) {
  // plugin definition
  $.fn.bigTarget = function(options) {
    debug(this);
    // build main options before element iteration
    var opts = $.extend({}, $.fn.bigTarget.defaults, options);
    // iterate and reformat each matched element
    return this.each(function() {
      // set the anchor attributes
      var $a = $(this);
      var href = $a.attr('href');
      var title = $a.attr('title');
      // build element specific options
      var o = $.meta ? $.extend({}, opts, $a.data()) : opts;
      // update element styles
      $a.parents(o.clickZone)
        .hover(function() {
          $h = $(this);
          $h.addClass(o.hoverClass);
          if(typeof o.title != 'undefined' && o.title === true && title != '') {
            $h.attr('title',title);
          }
        }, function() {
          
          $h.removeClass(o.hoverClass);
          if(typeof o.title != 'undefined' && o.title === true && title != '') {
            $h.removeAttr('title');
          }
        })
        // click
        .click(function() {
          if(getSelectedText() == "")
          {
            if($a.is('[rel*=external]')){
              window.open(href);
              return false;
            }
            else {
              //$a.click(); $a.trigger('click');
              window.location = href;
            }
          }
        });
    });
  };
  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log)
    window.console.log('bigTarget selection count: ' + $obj.size());
  };
  // get selected text
  function getSelectedText(){
    if(window.getSelection){
      return window.getSelection().toString();
    }
    else if(document.getSelection){
      return document.getSelection();
    }
    else if(document.selection){
      return document.selection.createRange().text;
    }
  };
  // plugin defaults
  $.fn.bigTarget.defaults = {
    hoverClass  : 'hover',
    clickZone : 'li:eq(0)',
    title   : true
  };
// end of closure
})(jQuery);
