﻿$(document).ready(function() {
	initNavBar();
	writeBreadcrumb();
	setNavBarRollovers();
	//------------------------------------------------------------------------
	//--- these remove unwanted MS classes from the rendered DOM
	//--- and may replace them with custom classes
	//------------------------------------------------------------------------
	$('td.ms-sbcell').removeClass('ms-sbcell').addClass('searchBox');
	$('td.ms-sbgo').removeClass('ms-sbgo');
	//------------------------------------------------------------------------
	// set default search mode to ingredient
	$("#btnIngredient").attr('selected', true);
	btnIngredient_onclick();
	// initialize supplier category selection to "all"
	$("#cbManufacturer").attr("checked", "true");
	$("#cbDistributor").attr("checked", "true");
	$("#cbExporter").attr("checked", "true");
	// disclaimer on leaving site
	doInterstitial();
	//grab enter key when focus is on search words box
	$('input[id$="InputKeywords"]').bind("focus", grabEnterKey);
	$('input[id$="InputKeywords"]').bind("blur", releaseEnterKey);
});

function grabEnterKey(){
	$(document).bind("keypress",function(e){
		if ((e.keyCode == 13 || e.charCode == 13)) {
			$(this).keypress();
		}
	});
}
function releaseEnterKey(){
	$(document).unbind("keypress");
}

// disclaimer popup before navigating away from site
function doInterstitial() {
	$('a[href^="http"]').bind("click", function(){
		if($(this).attr("href").search(/innovatewithdairy\.com/i)==-1){
			return confirm("You are leaving innovatewithdairy.com.  Content and information viewed on external links is the sole responsibility of the site provider and not of Dairy Management Inc.");
		}
	});
//
	$('a[href^="mailto:"]').bind("click", function(){
		return confirm("You are leaving innovatewithdairy.com.  Content and information viewed on external links is the sole responsibility of the site provider and not of Dairy Management Inc.");
	});
}

//  MASTER.master PRIMARY NAVIGATION 
function initNavBar(){
	$('#image1').attr('src', '/images/nav_ProductResearch_off.gif');
	$('#image2').attr('src', '/images/nav_Ingredients_off.gif');
	$('#image3').attr('src', '/images/nav_Nutrition_off.gif');
	$('#image4').attr('src', '/images/nav_MarketInsights_off.gif');
	$('#image5').attr('src', '/images/nav_Resources_off.gif');
	$('#image6').attr('src', '/images/nav_FindIngredients_off.gif');
}

// End 
// -----------------------------------------------------------------
// -----------------------------------------------------------------
function writeBreadcrumb(){
	var strCategory = Url.decode($.jqURL.get('Category'));
	var strFilter = Url.decode($.jqURL.get('Filter'));
	var strBC = "Home";
	var strFilterTitle = document.title;	//div[id="FilterTitle"]
	
	//replace "+" with " "
	strCategory = strCategory.replace(/\+/g," ");
	strFilter = strFilter.replace(/\+/g," ");
	
	if(strCategory != "undefined") strBC += " > " + strCategory; 
	if(strFilter != "undefined") strBC += " > " + strFilter;
	 
	if (strFilterTitle.search(/filterpage/i)==-1){
		strBC += " > " + strFilterTitle; 
	}
	
	$('#pageBreadcrumb').html(strBC);
}
// -----------------------------------------------------------------
// -----------------------------------------------------------------

function bindAllRollovers(){
	$('#link1').bind('mouseover', function(){
		$('#image1').attr('src', '/images/nav_ProductResearch_on.gif');
	});		
	$('#link1').bind('mouseout', function(){
		$('#image1').attr('src', '/images/nav_ProductResearch_off.gif');
	});		
	$('#link2').bind('mouseover', function(){
		$('#image2').attr('src', '/images/nav_Ingredients_on.gif');
	});		
	$('#link2').bind('mouseout', function(){
		$('#image2').attr('src', '/images/nav_Ingredients_off.gif');
	});		
	$('#link3').bind('mouseover', function(){
		$('#image3').attr('src', '/images/nav_Nutrition_on.gif');
	});		
	$('#link3').bind('mouseout', function(){
		$('#image3').attr('src', '/images/nav_Nutrition_off.gif');
	});		
	$('#link4').bind('mouseover', function(){
		$('#image4').attr('src', '/images/nav_MarketInsights_on.gif');
	});		
	$('#link4').bind('mouseout', function(){
		$('#image4').attr('src', '/images/nav_MarketInsights_off.gif');
	});		
	$('#link5').bind('mouseover', function(){
		$('#image5').attr('src', '/images/nav_Resources_on.gif');
	});		
	$('#link5').bind('mouseout', function(){
		$('#image5').attr('src', '/images/nav_Resources_off.gif');
	});		
	$('#findSupplier').bind('mouseover', function(){
		$('#image6').attr('src', '/images/nav_FindIngredients_on.gif');
	});		
	$('#findSupplier').bind('mouseout', function(){
		$('#image6').attr('src', '/images/nav_FindIngredients_off.gif');
	});		
}

function fixPageNavItem(thisID, thisImg, thisSrc){
	$(eval("'"+thisID+"'")).unbind('mouseover');		
	$(eval("'"+thisID+"'")).unbind('mouseout');		
	$(eval("'"+thisImg+"'")).attr('src', eval("'/images/nav_"+thisSrc+"_on.gif'"));
}

function setNavBarRollovers(){
	var strCategory = Url.decode($.jqURL.get('Category'));
	var strFilter = Url.decode($.jqURL.get('Filter'));
	var strPage = Url.decode($.jqURL.url());

	bindAllRollovers();
		
	if (strPage.search(/Product-Research\.aspx/i)!=-1){
		fixPageNavItem('#link1','#image1','ProductResearch');
	}
	else if (strPage.search(/Applications\.aspx/i)!=-1){ //this is "Ingredients-Applications.aspx"
		fixPageNavItem('#link2','#image2','Ingredients');
	}
	else if (strPage.search(/Nutrition\.aspx/i)!=-1){
		fixPageNavItem('#link3','#image3','Nutrition');
	}
	else if (strPage.search(/Market-Insights\.aspx/i)!=-1){
		fixPageNavItem('#link4','#image4','MarketInsights');
	}
	else if (strPage.search(/Resources\.aspx/i)!=-1){
		fixPageNavItem('#link5','#image5','Resources');
	}
	else if (strCategory != "undefined"){
		if (strCategory.search(/^Pro/i)!=-1){
			fixPageNavItem('#link1','#image1','ProductResearch');
		}
		else if (strCategory.search(/^Ing/i)!=-1){
			fixPageNavItem('#link2','#image2','Ingredients');
		}
		else if (strCategory.search(/^App/i)!=-1){
			fixPageNavItem('#link2','#image2','Ingredients');
		}
		else if (strCategory.search(/^Nut/i)!=-1){
			fixPageNavItem('#link3','#image3','Nutrition');
		}
		else if (strCategory.search(/^Mar/i)!=-1){
			fixPageNavItem('#link4','#image4','MarketInsights');
		}
		else if (strCategory.search(/^Pub/i)!=-1){
			fixPageNavItem('#link5','#image5','Resources');
		}
		else if (strCategory.search(/^Res/i)!=-1){
			fixPageNavItem('#link5','#image5','Resources');
		}
	}



}