// JavaScript Document

$(document).ready(function(){
	mainmenu();
    doWatermark();
	contentTabs();
	rightnavbar();	
	LeftNavRelated();
	assignBodyID();
	addLeftNavSelectState();
	unHideContent();
});

//Top Nav
function mainmenu(){
$(" #topNav ul ").css({display: "none"}); // Opera Fix
$(" #topNav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
	},function(){
 		$(this).find('ul:first').css({visibility: "hidden"});
	});
}

//Search Watermark
function doWatermark() {
    $('form').submit(function() {
        $('.textWatermark').each(function() {
            if ($(this).attr('value') == $(this).attr('defaultvalue'))
                $(this).attr('value', '');
        });
    });

    $('.textWatermark').each(function() {
        if ($(this).attr('value') == '')
            $(this).attr('value', $(this).attr('defaultvalue'));

        if ($(this).attr('value') == $(this).attr('defaultvalue'))
            $(this).css('color', $(this).attr('watermarkcolor'));
    });

    $('.textWatermark').focus(function() {
        if ($(this).attr('defaultvalue') == $(this).attr('value')) {
            $(this).attr('value', '');
            $(this).css('color', 'black');
        }
    });

    $('.textWatermark').blur(function() {
        if ($(this).attr('value') == '') {
            $(this).attr('value', $(this).attr('defaultvalue'));
            $(this).css('color', $(this).attr('watermarkcolor'));
        }
    });
}

function contentTabs() {
	// Tabs
	$(function() {
		$("#flowtabs").tabs("#flowpanes > div").history();
	});
	$("#flowtabs a").click(function() {
			omnitureTrackClick(this,"flowtabs");
	});
}

function LeftNavRelated()
{
	// Related Info Drop Down
	// Show 3 items, show the more if remainding items is > 1
	var Count = 2;
	var minShow = Count;
    var liCount = $("#leftsidebar #relatedinfo li").size();
	
	//don't hide one item
	if ( (liCount > Count+1) && (liCount - (Count+1)) == 1 )		
    	Count=Count + 1;
		
    ShowItems(Count);
	
	if (liCount <= Count || liCount <= Count+1){
		return false;		
	}
	
	if ( $("#more").length )
		$("#more").text('More');
	else
		$('#leftsidebar #relatedinfo > ul').after('<p id="more" class="link">More</p>');
			
    $("#more").click(function(){
		
		if($(this).hasClass('less')){
			Count = minShow;
			ShowItems(Count);
			$("#more").text('More');
			$(this).removeClass('less');
		}
		
		else {
			Count = $("#leftsidebar #relatedinfo ul li").size();
			ShowItems(Count);	
			$("#more").text('Less');
			$("#more").addClass('less');
		}
    });
}


function ShowItems(Count) {
    if (Count > $("#relatedinfo li").size()) { Count = $("#list li").size() };
    $("#leftsidebar #relatedinfo ul li:lt(" + Count + ")").show();
	$("#leftsidebar #relatedinfo li:gt(" + Count + ")").hide();
	
}

function rightnavbar() {
	// Default slide  to display is 1 unless specific. You can set the default slide  
	// by setting the variable sideBoxDefault=slideId OR passing the parameter in the URL
	// ?sideBoxDefault=slideId
	// Order:  (1) Uses URL parameter first if passed, (2) explict defined variable, (3) default to
	// first slide.
	// Example: 
	       //<script> sideBoxDefault="sideNews" <\/script> OR
		   // in the URL: ?sideBoxDefault=sideNews
	var collateral = jQuery.url.param("sBD");	
	if (typeof(collateral) != 'undefined') {
		sideBoxDefault = collateral;
		if (!$("div.sideBoxes h3[id='"+sideBoxDefault+"']").length)
			sideBoxDefault = undefined;
	} 
	
	if (typeof(sideBoxDefault) != 'undefined')
		$('div.sideBoxes h3#'+sideBoxDefault).next('ul').show('fast');
	else 
		$('div.sideBoxes> ul:lt(1)').show('fast');
 	// $('div.sideBoxes> ul:gt(0)').hide();
	
 	$('div.sideBoxes> h3').click(function() {
		if ($(this).next('ul').is(':hidden') || $(this).parents(':hidden').length)
				omnitureTrackClick(this,"sideBoxes");
 		$(this).next('ul').slideToggle('fast')
 		.siblings('ul:visible').slideUp('fast');
 	});
	
}

// assign body id
// get base folder using url.segment(0)
// and match folder with navigation menu
// use following mapping (foldername<|> --> Body ID we need to set to)
// aboutus|careers|investors|news_events|partners --> aboutUs
// innovation --> innovation
// markets|portfolios|products|product_index --> products
// services|product_services|training --> services
// solutions --> solutions
// success --> success
function assignBodyID() {
	var map = new Object();  
	map['aboutus'] = 'aboutUs';  
	map['careers'] = 'aboutUs';  
	map['investors'] = 'aboutUs';
	map['news_events'] = 'aboutUs';
	map['partners'] = 'aboutUs';
	map['products'] = 'products';
	map['markets'] = 'products';
	map['portfolios'] = 'products';
	map['product_index'] = 'products';
	map['services'] = 'services';
	map['product_services'] = 'services';
	map['training'] = 'services';
	map['solutions'] = 'solutions';
	map['success'] = 'success';
	map['innovation'] = 'innovation';
   	var firstnode = jQuery.url.segment(0);
   	var bodyid = map[firstnode];
  
	// assign body id if one doesn't exist to highlight global navigation
	if ($('body').attr('id')) {
    		// do nothing;
	} else {
	    // id attribute does not exist, so go ahead and assign one dynamically
	 	if (typeof(bodyid) == undefined)
		   	return false;
		if (typeof(firstnode) == undefined) {
			if ( firstnode.match('product_index') && jQuery.url.attr("path").search('/services') > -1) {
				bodyid="services";
			}
  		}
	   
		$('body').attr("id",bodyid);
		//$('body').attr("id",jQuery.url.segment(0));   //assigns id to body element
	}
}

function addLeftNavSelectState(){
	//alert(jQuery.url.attr("file"));
	var currentPathOrig = jQuery.url.attr("path") ;
	var currentPathFixed = currentPathOrig;
	var fileName = jQuery.url.attr("file") ;
	var dirName = jQuery.url.attr("directory") ;
	
	if ( fileName == "index.html" ) {
		currentPathFixed = dirName;
	} else if ( fileName == null ) {
		currentPathFixed = dirName + "index.html" ;
	}
	// alert("current path: " + currentPathFixed);
	if ($('#pagenav li > a').is('.select'))
		return false;
	
	$('#pagenav li > a').each(
			function(){
				//alert($(this).attr('href'));
				if( ($(this).attr('href') == currentPathOrig) || ($(this).attr('href') == currentPathFixed) ){
						$(this).addClass('select');
						return false;
					
				}
			}
		);
}

function unHideContent(){
	//Show for ClickMap: All related items, show all collapsed sideboxes
	var showText = jQuery.url.param("unHide");
	if (typeof(showText) != 'undefined' && showText == 1) {
		$("#leftsidebar #relatedinfo ul li").each(function() {
		$(this).show();	});
		$('div.sideBoxes> ul').show();
	}		
}

function omnitureTrackClick(node, type) {
			var text = "";
			
			if(typeof(s) != 'undefined') {
				if (type.match("flowtabs")) {
						text = $(node).text() + " Tab";
						
								}
				else if (type.match("sideBoxes")) {
						text = $(node).find("span:first").text() + " Category";
								}
				
				var linkname = s.pageName + " -- " + text;
				//alert("a text ->" + linkname);
				s.linkTrackVars="None"
				s.linkTrackEvents="None"
				s.tl(this,'o',linkname);
			}
	
}


