/* 
header.js
Version: 10.24.11 (2)
*/


$(window).load(function () {
	//Get country site: 
	cSite = $('#utilitynav-locationselect').text(); //United States(change Country/Language)
	cSite = cSite.substr(0,cSite.indexOf('(')); //Strip "(change Country/Language)"
	//Set Country Site Path (if nessessary - United States does not have a sub path
	if (cSite == "United States") {
		hasCountryPath = false;
	} else {
		hasCountryPath = true;	
	}

	subPathInt = currpath.indexOf('/', 1);
	if (hasCountryPath) {
		//If a country site find the SECOND slash to ignore the /uk/ or whatever
		subPathInt = currpath.indexOf('/', subPathInt + 1);
	} 
	
	subPath = currpath.substring(1, subPathInt);
	
	var currPath = location.pathname.substring(1);
	var targStr = currPath.split('/');
	
	//Main Nav
	if (targStr[2] != 'sap-bydesign') {
		var navMarked = false;
		$('#nav-main ul li').each(function(index) {
			theLink = $(this).find('a').attr('href');
			if (theLink == currpath) {
				//Highlight THIS menu item
				$(this).find('a').addClass('on');
				navMarked = true;
			}
		});
		
		if (!navMarked) {
			checkCount = 0; //This is to make sure we don't turn ALL of them on (if more than one is marked, we don't mark any.)
			$('#nav-main ul li').each(function(index) {
				theLink = $(this).find('a').attr('href');
				slashCount = theLink.match(/[\/]/g, "").length;
				subPathInt = theLink.indexOf('/', 1);
				if (hasCountryPath) {
					//If a country site find the SECOND slash to ignore the /uk/ or whatever
					subPathInt = theLink.indexOf('/', subPathInt + 1);
				} 
				linkPath = theLink.substring(1, subPathInt); 
				
				initialSlash = linkPath.indexOf('/');
				if (initialSlash > -1 && !hasCountryPath) {
					linkPath = theLink.substring(0); 
				} 
				//alert('linkPath: ' + linkPath + ' <-> subpath: ' +  subPath);
				
				if (linkPath === subPath) {
					//Highlight THIS menu item
					checkCount++;
					//$(this).find('a').addClass('on');
					tempVar = $(this).find('a');
				}
			});
			
			//alert(checkCount + ' ! - ' + $(tempVar).html());
			
			if (checkCount == 1) {
				$(tempVar).addClass('on');
			}
		}
	}
	
});


$(document).ready(function() {
	/*====== SITE SEARCH INIT  ======*/
    $('#searchsite-input').live("focus", function () {
        initInputs();
        $(this).autocomplete({ //header site search input
            minLength: 0,
            delay: 0,
            source: function (request, respond) {
                get_suggestions(request.term, function (response) {
                    respond(eval(response));
                });
            },
            minWidth: 146,
            width: '100%',
            zIndex: 9999,
            select: function (event, ui) {
                $('#searchsite-input').val(ui.item.value);
                $('#searchsite').submit();
            }
        });
    });


    $('#QueryText').autocomplete({
        minLength: 0,
        delay: 0,
        source: function (request, respond) {
            get_suggestions(request.term, function (response) {
                respond(eval(response))
            });
        },
        minWidth: 186,
        width: '100%',
        zIndex: 9999,
        select: function (event, ui) {
            $('#QueryText').val(ui.item.value);
            $('#searchresults').submit();
        }
    });

	
});


