jQuery.noConflict();

function getLanguage(){
	var l = window.location.pathname.split('/')[1].length == 2 ? window.location.pathname.split('/')[1]: 'en';
	document.cookie ='lang='+l+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/';
	return l;
}


/* DETECT COOKIES*/
var region_cookie = document.cookie.match('(^|;) ?region=([^;]*)(;|$)');
var region = region_cookie ? unescape(region_cookie[2]) : '';
var curr = document.cookie.match('(^|;) ?region=([^;]*)(;|$)') ? unescape((document.cookie.match('(^|;) ?region=([^;]*)(;|$)'))[2]):'';
var lang = getLanguage();
var authFlag;

if(lang == "de"){
	var headerLabels = {	
		//global nav labels
		investors:		"Investors",
		investorsUrl:	"http://investor.arrow.com",
		careers: 		"Karriere",
		careersUrl:		"http://www.arroweurope.com/de/das-unternehmen/jobs-karriere.html",
		offices: 		"Niederlassungen",
		officesUrl: 	"http://www.arroweurope.com/de/das-unternehmen/niederlassungen.html",
		help: 			"Hilfe",
		helpUrl: 		"http://www.arroweurope.com/de/",
		contact:		"Kontakt",
		contactUrl:		"http://www.arroweurope.com/de/kontakt.html",
		about:			"Unternehmen",
		aboutUrl:		"http://www.arroweurope.com/de/das-unternehmen.html",
		changeLang:		"Die Sprache wird ge&#228;ndert",
		changeCurr:		"Die W&#228;hrung wird ge&#228;ndert",
	
		//cart labels
		cart:			"Warenkorb",
	
		//part search labels
		partSearch:		"Artikel Suchen",
		myarrowPartSearch: "MyArrow&trade; Artikel Suchen",
		searchHint:		"Artikelnummer, Hersteller oder Suchbegriff",
		search:			"Suchen",
		instock:		"Vorr&auml;tig",
		chinarohs:		"China RoHS",
		eurohs:			"EU RoHS",
	
		//nav labels
		home:			"Home",
		homeUrl:		"http://www.arroweurope.com/de/",
		markets:		"M&#228;rkte & L&#246;sungen",
		marketsUrl:		"http://www.arroweurope.com/de/maerkte-loesungen.html",
		products:		"Produkte & Technologien",
		suppliers:		"Hersteller",
		services:		"Services",
		servicesUrl:	"http://www.arroweurope.com/de/services.html",
		events:			"News & Events",
		eventsUrl:		"http://www.arroweurope.com/de/news-events.html",
		
		//myarrow labels
		login:			"Login bei MyArrow",
		logout:			"Abmelden",
		learnMore:		"Weiterlesen",
		learnMoreUrl:	"http://www.arroweurope.com/de/services/myarrow.html",
		register:		"Registrieren",
		forgotPw:		"Passwort vergessen",
		myarrowTag:		"Kundenspezifische Einkaufsbedingungen",
		provideBom:		"St&#252;ckliste hochladen",
		welcome:		"Herzlich Willkommen",
		loadingNav:		"Die Navigation wird geladen",
		navError:		"Fehler beim Laden der MyArrow Navigation",
		hello:			"Hallo",
		manageBom:		"St&#252;cklisten verwalten",
		tryAgain:		"Noch einmal versuchen ",
		uploadNewBom:	"St&#252;ckliste hochladen",
		workingBom:		"Materialst&#252;ckliste &#246;ffnen",
		orders:			"Auftr&auml;ge",
		
		//region select labels
		regionTitle: 	"Um Ihren Besuch zu personalisieren, w&auml;hlen Sie bitte Ihre Region",
		choose:			"Ausw&auml;hlen",
		close:			"Schlie&szlig;en",
		otherRegions:	"Andere Regionen/L&auml;nder",
		ecomHandled:	"Die E-Commerce Abwicklung erfolgt in USA",
		customersIn:	"Angemeldete MyArrow Kunden in den",
		theseCountries:	"folgenden L&auml;ndern "
	}
}else if(lang == "it"){
	var headerLabels = {	
		//global nav labels
		investors:		"Investitori",
		investorsUrl:	"http://investor.arrow.com",
		careers: 		"Carriere",
		careersUrl:		"http://www.arroweurope.com/it/chi-siamo/careers.html",
		offices: 		"Uffici",
		officesUrl: 	"http://www.arroweurope.com/it/chi-siamo/sedi.html",
		help: 			"Guida",
		helpUrl: 		"http://www.arroweurope.com/it/",
		contact:		"Contattaci",
		contactUrl:		"http://www.arroweurope.com/it/contatti.html",
		about:			"Informazioni",
		aboutUrl:		"http://www.arroweurope.com/it/about-arrow.html",
		changeLang:		"Cambia lingua",
		changeCurr:		"Cambia valuta",
	
		//cart labels
		cart:			"Carrello",
	
		//part search labels
		partSearch:		"Ricerca",
		myarrowPartSearch: "MyArrow&trade; Ricerca",
		searchHint:		"N. particolare, produttore o parola chiave",
		search:			"Ricerca",
		instock:		"Disponibile",
		chinarohs:		"RoHS Cina",
		eurohs:			"RoHS UE",
	
		//nav labels
		home:			"Home",
		homeUrl:		"http://www.arroweurope.com/de/",
		markets:		"M&#228;rkte & L&#246;sungen",
		marketsUrl:		"http://www.arroweurope.com/it/maerkte-loesungen.html",
		products:		"Produkte & Technologien",
		suppliers:		"Hersteller",
		services:		"Services",
		servicesUrl:	"http://www.arroweurope.com/it/services.html",
		events:			"News & Events",
		eventsUrl:		"http://www.arroweurope.com/it/news-events.html",
		
		//myarrow labels
		login:			"Accesso a MyArrow",
		logout:			"Uscita",
		learnMore:		"Per saperne di piu",
		learnMoreUrl:	"http://www.arroweurope.com/it/servizi/myarrow.html",
		register:		"Registrazione",
		forgotPw:		"Password dimenticata",
		myarrowTag:		"Prezzi al cliente e condizioni online",
		provideBom:		"Carica il tuo BOM",
		welcome:		"Benvenuto",
		loadingNav:		"Carica sistema di navigazione",
		navError:		"Errore nel recupero del sistema di navigazione MyArrow",
		hello:			"Salve",
		manageBom:		"Crea, salva e gestisci le distine base",
		tryAgain:		"Riprova",
		uploadNewBom:	"Carica il tuo BOM",
		workingBom:		"Apri la distinta base",
		orders:			"Ordini",
		
		//region select labels
		regionTitle: 	"Per personalizzare la navigazione, selezionare la Regione geografica",
		choose:			"Seleziona",
		close:			"Chiudi",
		otherRegions:	"Altre Regioni / Paesi",
		ecomHandled:	"E-commerce gestito in USA",
		customersIn:	"Entra in My Arrow in",
		theseCountries:	"questi Paesi"
	}
}else if(lang == "fr"){
	var headerLabels = {	
		//global nav labels
		investors:		"Investisseurs",
		investorsUrl:	"http://investor.arrow.com",
		careers: 		"Carri&egrave;res",
		careersUrl:		"http://www.arroweurope.com/fr/a-propos-darrow/carrieres.html",
		offices: 		"Bureaux",
		officesUrl: 	"http://www.arroweurope.com/fr/a-propos-darrow/lieu.html",
		help: 			"Aide",
		helpUrl: 		"http://www.arroweurope.com/fr/",
		contact:		"Contact",
		contactUrl:		"http://www.arroweurope.com/fr/contactez-nous.html",
		about:			"A propos d'Arrow",
		aboutUrl:		"http://www.arroweurope.com/fr/a-propos-darrow.html",
		changeLang:		"Changer de langue",
		changeCurr:		"Changer de devise",
	
		//cart labels
		cart:			"Panier",
	
		//part search labels
		partSearch:		"Rechercher",
		myarrowPartSearch: "Rechercher dans MyArrow&trade;",
		searchHint:		"N&deg; r&eacute;f., fabricant ou mot-cl&eacute;",
		search:			"Recherche",
		instock:		"En Stock",
		chinarohs:		"RoHS Chine",
		eurohs:			"RoHS UE",
	
		//nav labels
		home:			"Home",
		homeUrl:		"http://www.arroweurope.com/fr/",
		markets:		"M&#228;rkte & L&#246;sungen",
		marketsUrl:		"http://www.arroweurope.com/fr/maerkte-loesungen.html",
		products:		"Produkte & Technologien",
		suppliers:		"Hersteller",
		services:		"Services",
		servicesUrl:	"http://www.arroweurope.com/fr/services.html",
		events:			"News & Events",
		eventsUrl:		"http://www.arroweurope.com/fr/news-events.html",
		
		//myarrow labels
		login:			"Connexion",
		logout:			"D&eacute;connexion",
		learnMore:		"En savoir plus",
		learnMoreUrl:	"http://www.arroweurope.com/fr/services/myarrow.html",
		register:		"Inscription",
		forgotPw:		"Mot de passe oubli&eacute;",
		myarrowTag:		"Devis et conditions personnalis&eacute;s en ligne",
		provideBom:		"Fournir votre nomenclatures",
		welcome:		"Bienvenue",
		loadingNav:		"Navigation en cours de chargement",
		navError:		"Erreur d'acc&egrave;s &agrave; votre navigation MyArrow",
		hello:			"Bonjour",
		manageBom:		"Gestion des nomenclatures",
		tryAgain:		"R&eacute;essayer",
		uploadNewBom:	"Envoyer une nomenclature",
		workingBom:		"Ouvrir la nomenclature active",
		orders:			"Ordres",
		
		//region select labels
		regionTitle: 	"Pour personnaliser votre visite, veuillez choisir votre r&eacutegion",
		choose:			"Choisissez",
		close:			"Fermer",
		otherRegions:	"Autres r&eacutegions,pays",
		ecomHandled:	"E-Commerce g&eacuter&eacute par les US",
		customersIn:	"Clients connect&eacutes &agrave MyArrow dans",
		theseCountries:	"ces pays"
	}
}else if(lang == "es"){
	var headerLabels = {	
		//global nav labels
		investors:		"Inversores",
		investorsUrl:	"http://investor.arrow.com",
		careers: 		"Carreras",
		careersUrl:		"http://www.arroweurope.com/es/acerca-de-arrow/trabajo.html",
		offices: 		"Oficinas",
		officesUrl: 	"http://www.arroweurope.com/es/acerca-de-arrow/oficinas.html",
		help: 			"Ayuda",
		helpUrl: 		"http://www.arroweurope.com/es/",
		contact:		"Contacto",
		contactUrl:		"http://www.arroweurope.com/es/contacto.html",
		about:			"Qui&eacute;nes somos",
		aboutUrl:		"http://www.arroweurope.com/es/acerca-de-arrow.html",
		changeLang:		"Cambiar idioma",
		changeCurr:		"Cambiar moneda",
	
		//cart labels
		cart:			"Caja",
	
		//part search labels
		partSearch:		"Buscar pieza",
		myarrowPartSearch: "Buscar piezas en MyArrow&trade;",
		searchHint:		"C&oacute;digo art&iacute;culo, fabricante o clave",
		search:			"Buscar",
		instock:		"En Stock",
		chinarohs:		"RoHS (China)",
		eurohs:			"RoHS (UE)",
	
		//nav labels
		home:			"Home",
		homeUrl:		"http://www.arroweurope.com/es/",
		markets:		"M&#228;rkte & L&#246;sungen",
		marketsUrl:		"http://www.arroweurope.com/es/maerkte-loesungen.html",
		products:		"Produkte & Technologien",
		suppliers:		"Hersteller",
		services:		"Services",
		servicesUrl:	"http://www.arroweurope.com/es/services.html",
		events:			"News & Events",
		eventsUrl:		"http://www.arroweurope.com/es/news-events.html",
		
		//myarrow labels
		login:			"Acceder a MyArrow",
		logout:			"Salir",
		learnMore:		"M&aacute;s informaci&oacute;n",
		learnMoreUrl:	"http://www.arroweurope.com/es/servicio/myarrow.html ",
		register:		"Registrarse",
		forgotPw:		"&iquest;Contrase&ntilde;a olvidada?",
		myarrowTag:		"Precios y t&eacute;rminos en l&iacute;nea",
		provideBom:		"Enviar lista de materiales",
		welcome:		"Bienvenido",
		loadingNav:		"Se est&aacute; cargando la navegaci&oacute;n",
		navError:		"Error al recuperar la navegaci&oacute;n para MyArrow",
		hello:			"Hola",
		manageBom:		"Administrar listas de materiales",
		tryAgain:		"Vuelva a intentarlo",
		uploadNewBom:	"Cargar una lista de materiales",
		workingBom:		"Abrir lista de materiales actual",
		orders:			"&Oacute;rdenes",
		
		//region select labels
		regionTitle: 	"Para personalizar su visita, elija su regi&oacute;n.",
		choose:			"Elegir",
		close:			"Cerrar",
		otherRegions:	"Otras regiones/pa&iacute;ses",
		ecomHandled:	"Comercio electr&oacute;nico gestionado en las Am&eacute;ricas ",
		customersIn:	"Clientes dados de alta en MyArrow en",
		theseCountries:	"estos pa&iacute;ses"
	}
}else{
	var headerLabels = {
		//global nav labels
		investors:		"Investors",
		investorsUrl:	"http://investor.arrow.com",
		careers: 		"Careers",
		careersUrl:		"http://www.arroweurope.com/about-arrow/careers.html",
		offices: 		"Offices",
		officesUrl: 	"http://www.arroweurope.com/about-arrow/locations.html",
		help: 			"Help",
		helpUrl: 		"http://www.arroweurope.com/",
		contact:		"Contact Us",
		contactUrl:		"http://www.arroweurope.com/contact-us.html",
		about:			"About Us",
		aboutUrl:		"http://www.arroweurope.com/about-arrow.html",
		changeLang:		"Changing Language",
		changeCurr:		"Changing Currency",
		
		//cart labels
		cart:			"Cart",
	
		//part search labels
		partSearch:		"Part Search",
		myarrowPartSearch: "MyArrow&trade; Part Search",
		searchHint:		"Part #, Manufacturer, or Keyword",
		search:			"Search",
		instock:		"In Stock",
		chinarohs:		"China RoHS",
		eurohs:			"EU RoHS",
	
		//nav labels
		home:			"Home",
		homeUrl:		"http://www.arroweurope.com/",
		markets:		"Markets & Solutions",
		marketsUrl:		"http://www.arroweurope.com/markets-solutions.html",
		products:		"Products & Technologies",
		suppliers:		"Suppliers",
		services:		"Services",
		servicesUrl:	"http://www.arroweurope.com/services.html",
		events:			"News & Events",
		eventsUrl:		"http://www.arroweurope.com/news-events.html",
		
		//myarrow labels
		login:			"Login to MyArrow",
		logout:			"Log Out",
		learnMore:		"Learn More",
		learnMoreUrl:	"http://www.arroweurope.com/services/myarrow.html",
		register:		"Register",
		forgotPw:		"Forgot Password",
		myarrowTag:		"Custom Pricing and Terms Online",
		provideBom:		"Provide your BOM to see inventory and pricing",
		welcome:		"Welcome",
		loadingNav:		"Loading Navigation",
		navError:		"Error retrieving your MyArrow Navigation",
		hello:			"Hello",
		manageBom:		"Create, Save, and Manage BOMs",
		tryAgain:		"Try again",
		uploadNewBom:	"Upload a new BOM",
		workingBom:		"Open working BOM",
		orders:			"Orders",
		
		//region select labels
		regionTitle: 	"To personalize your visit, please choose your region",
		choose:			"Choose",
		close:			"Close",
		otherRegions:	"Other Regions/Countries",
		ecomHandled:	"E-Commerce handled by The Americas",
		customersIn:	"Logged in MyArrow customers in",
		theseCountries:	"these countries"
	}
}


/* SET HEADER VARIABLES */
var data,loginInterval;

document.write(''+
	'<table id="global_header" class="base_header">\n'+
		'<tr>\n'+
			'<td id="header_top" colspan="2">\n'+
				'<div id="header_top_left">\n'+
					'<a href="http://www.arroweurope.com"><div id="logo"></div></a>\n'+
					'<div id="region_business">\n'+
						'<span id="region">Europe</span><br />\n'+
						'<h2 id="business_group">Components</h2>\n'+
					'</div>\n'+
				'</div>\n'+
				'<div id="header_top_right">\n'+
					'<ul id="global_nav">\n'+
						'<li><a href="http://www.arrow.com">arrow.com</a></li>\n'+
						'<li><a href="'+headerLabels.investorsUrl+'">'+headerLabels.investors+'</a></li>\n'+
						'<li><a href="'+headerLabels.careersUrl+'">'+headerLabels.careers+'</a></li>\n'+
						'<li><a href="'+headerLabels.officesUrl+'">'+headerLabels.offices+'</a></li>\n'+
						'<li><a href="'+headerLabels.helpUrl+'">'+headerLabels.help+'</a></li>\n'+
						'<li><a href="'+headerLabels.contactUrl+'">'+headerLabels.contact+'</a></li>\n'+
						'<li><a href="'+headerLabels.aboutUrl+'">'+headerLabels.about+'</a></li>\n'+
						'<li> | </li>\n'+
						'<li id="language_selector">\n'+
							'<a id="language_selected" class="dropdown"><span>...</span></a>\n'+
							'<div id="language_menu" class="dropdown_menu right_menu">\n'+
								'<ul>\n'+
									'<li><a href="#" class="en" id="en">English</a></li>\n'+
									'<li><a href="#" class="de" id="de">Deutsch</a></li>\n'+
									'<li><a href="#" class="fr" id="fr">Francais</a></li>\n'+
									'<li><a href="#" class="es" id="es">Espanol</a></li>\n'+
									'<li><a href="#" class="it" id="it">Italiano</a></li>\n'+
								'</ul>\n'+
							'</div>\n'+
						'</li>\n'+
						'<li id="currency_selector">\n'+
							'<a id="currency_selected" class="dropdown"><span>...</span></a>\n'+
							'<div id="currency_menu" class="dropdown_menu right_menu">\n'+
								'<ul>\n'+
								'</ul>\n'+
							'</div>\n'+
						'</li>\n'+
					'</ul>\n'+
					'<a href="#" id="cart">'+headerLabels.cart+' <span id="cart_items"></span></a>\n'+
				'</div>\n'+
			'</td>\n'+
		'</tr>\n'+
		'<tr>\n'+
			'<td id="search_wrapper">\n'+
				'<div id="search_box">\n'+
					'<h3>'+headerLabels.partSearch+'</h3>\n'+
					'<div id="search_selector">\n'+
						'<a id="search_selected" class="dropdown"><span></span></a>\n'+
						'<div id="search_menu" class="dropdown_menu left_menu">\n'+
							'<ul>\n'+
								'<li>\n'+
									'<a href="#" id="arrowce">\n'+
										'<h6>Central Europe</h6>\n'+
										'<span>Austria, Belgium, Germany, Netherlands, Switzerland</span>\n'+
									'</a>\n'+
								'</li>\n'+
								'<li>\n'+
									'<a href="#" id="arrowne">\n'+
										'<h6>Northern Europe</h6>\n'+
										'<span>Denmark, Estonia, Finland, Latvia, Lithuania, Norway, Sweden, United Kingdom</span>\n'+
									'</a>\n'+
								'</li>\n'+
								'<li>\n'+
									'<a href="#" id="na">\n'+
										'<h6>All Other Regions/Countries</h6>\n'+
										'<span>E-Commerce handled by The Americas</span>\n'+
									'</a>\n'+
								'</li>\n'+
							'</ul>\n'+
						'</div>\n'+
					'</div>\n'+	
					'<form action="http://components.arrow.com/part/search/" method="post" id="frm_search" name="frm_search" base="http://components.arrow.com/part/search/">\n'+
						'<input type="text" name="search_token" id="search_token" size="32" placeholder="'+headerLabels.searchHint+'" />\n'+
						'<input id="search_region" type="hidden" name="region" value="'+region+'" />\n'+
						'<input id="search_lang" type="hidden" name="lang" value="'+lang+'" />\n'+
						'<input id="search_curr" type="hidden" name="tocurr" value="'+curr+'" />\n'+
						'<a id="submit_search" class="button" href="#">'+headerLabels.search+'</a>\n'+
						'<div class="options">\n'+
							'<input type="checkbox" id="in_stock" name="in_stock" /><label for="in_stock">'+headerLabels.instock+'</label>\n'+
							'<input type="checkbox" id="eu_rohs" name="eu_rohs" /><label for="eu_rohs">'+headerLabels.eurohs+'</label>\n'+
							'<input type="checkbox" id="china_rohs" name="china_rohs" /><label for="china_rohs">'+headerLabels.chinarohs+'</label>\n'+
						'</div>\n'+
					'</form>\n'+
				'</div>\n'+
			'</td>\n'+
			'<td id="nav_wrapper">\n'+
				'<div id="nav"></div>\n'+
				'<div id="sub_nav">\n'+
					'<div id="bom_content">\n'+
						'<h5>BOM Upload</h5>\n'+
						'<a href="http://components.arrow.com/bom/bom_home.jsp" class="hdr_new_bom">'+headerLabels.provideBom+'</a>\n'+
					'</div>\n'+
					'<div id="myarrow_nav">\n'+
						'<div id="login_content">\n'+
							'<h5>MyArrow&trade;<span> - '+headerLabels.myarrowTag+' (<a href="'+headerLabels.learnMoreUrl+'">'+headerLabels.learnMore+'</a>)</span></h5>\n'+
							'<div id="login_options">\n'+
								'<a id="login_submit" class="button login_myarrow" href="http://components.arrow.com/globalauthentication/remote_login.html?region='+region+'&loadAction=login&remoteUrl='+encodeURIComponent(window.location.href)+'">'+headerLabels.login+'</a>\n'+
								'<a href="http://components.arrow.com/globalauthentication/sh_regpage.jsp" class="register_myarrow">'+headerLabels.register+'</a> | <a class="forgot_password" href="#">'+headerLabels.forgotPw+'?</a>\n'+
							'</div>\n'+
						'</div>\n'+
					'</div>\n'+
					'<div id="bkg_sub_nav">&nbsp;</div>\n'+
				'</div>\n'+
			'</td>\n'+
		'</tr>\n'+
	'</table>\n'+
	'<div id="region_popup">\n'+
		'<h4>'+headerLabels.regionTitle+':</h4>\n'+
		'<table>\n'+
			'<tbody>\n'+
				'<tr>\n'+
					'<td><a href="#" id="arrowce" class="choose_region button blue" />'+headerLabels.choose+'</a></td>\n'+
					'<td>\n'+
						'<strong>Central Europe</strong><br />\n'+
						'<span>Austria, Belgium, Germany, Netherlands, Switzerland</span><br />\n'+
						'<span>('+headerLabels.customersIn+' <a href="#" id="myarrow_countries">'+headerLabels.theseCountries+'</a>.)</span><br />\n'+
						'<div id="country_list"><a href="#" class="country_close">'+headerLabels.close+'</a><ul><li>Armenia</li><li>Austria</li><li>Azerbaijan</li><li>Belarus</li><li>Belgium</li><li>Bosnia and Herzegowina</li><li>Bulgaria</li><li>Croatia</li><li>Czech Republic</li><li>Germany</li><li>Hungary</li><li>Kazakhstan</li><li>Kyrgyzstan</li><li>Macedonia</li><li>Republic of Moldova</li><li>Netherlands</li><li>Poland</li><li>Romania</li><li>Russian Federation</li><li>Serbia and Montenegro</li><li>Slovakia</li><li>Slovenia</li><li>Switzerland</li><li>Tajikistan</li><li>Turkey</li><li>Turkmenistan</li><li>Ukraine</li><li>Uzbekistan</li></ul><a href="#" class="country_close">Close</a></div>\n'+ 
					'</td>\n'+
				'</tr>\n'+
				'<tr>\n'+
					'<td><a href="#" id="arrowne" class="choose_region button blue" />'+headerLabels.choose+'</a></td>\n'+
					'<td>\n'+
						'<strong>Northern Europe</strong><br />\n'+
						'<span>Denmark, Estonia, Finland, Latvia, Lithuania, Norway, Sweden, United Kingdom</span>\n'+
					'</td>\n'+
				'</tr>\n'+
				'<tr>\n'+
					'<td><a href="#" id="na" class="choose_region button blue" />'+headerLabels.choose+'</a></td>\n'+
					'<td>\n'+
						'<strong>'+headerLabels.otherRegions+'</strong><br />\n'+
						'<span>'+headerLabels.ecomHandled+'</span><br />\n'+
					'</td>\n'+
				'</tr>\n'+
			'</tbody>\n'+
		'</table>\n'+
	'</div>\n'+
'');


(function($){

$(document).ready(function(){
	
	// if they don't have a region set
	!loadCookies('region') ? showRegionSelector() : checkAuthentication();
	
	$('#language_menu a').live('click', function() {
		var langVal = $(this).attr('id').toLowerCase();
		window.location = "/"+langVal+'/home.html'
		return false;
	}); 
	
	$('#currency_menu a').live('click', function() {
		if(region != 'na'){
			var curVal = $(this).attr('id');
			$('#currency_selected span').html(curVal);
			$('#search_curr').val(curVal);
			$.ajax({
				type:'GET',
				dataType:'jsonp',
				jsonp: "callbackfn",
				cache: false,
				data:{
					fmt: 'json',
					region: region,
					tocurr: curVal
				},
				url:'http://components.arrow.com/gcart/getCartInfo',
				success: function(){
					
					document.cookie ='curr='+curVal+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/';
				}
			});
		}
		return false;
	}); 
	
	/* CHANGE SEARCH REGION */
	$('#search_menu a').live('click', function() {
		var sel_region = $(this).attr('id').toLowerCase();
		$('#search_region').val(sel_region);
		region = sel_region;
		document.cookie ='region='+sel_region+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/';
		$('#search_selected span').html($(this).children('h6').html());
		return false;
	}); 
	
	
	/*HANDLE PART SEARCH*/
	$('#search_token').keyup(function(){
		var o = $(this);
		o.parent('form').addSearchToken();
	});
	
	$('#submit_search').click(function(){
		if($('#search_region').val() == 'na'){
			$('#frm_search').attr('target', '_blank').submit();
		}else{
			$('#frm_search').submit();
		}
		return false;
	});
	
	$('#frm_search').bind('submit', function(e){
		if($('#search_token').val() != ""){
			$(this).addSearchToken();
			//force english on apps
			if($('#search_lang').val() != 'en' && $('#search_lang').val() != 'de'){
				$('#search_lang').val('en');
			}
			if($('#search_region').val() == 'na'){
				$('#frm_search').attr('target', '_blank');
				return true;
			}else{
				$('#frm_search').removeAttr('target');
				return true;
			}
		}else{
			return false;
		}		
	});
	
	$('#retry_profile').live('click',function(){
		checkAuthentication();
		return false;
	});
	
	$('#myarrow_countries').live('click',function(){
		$('#country_list').show();
		return false;
	});
	
	$('.country_close').live('click',function(){
		$('#country_list').hide();
		return false;
	});
	
	/* SET UP HEADER MODALS */
	$.extend($.modal.defaults, {
		escClose: false,
		containerId: 'modal_container',
		overlayClose:false,
		onClose: function (dialog) {
			dialog.container.fadeOut(100, function () {
				dialog.overlay.fadeOut(200, function(){
					$.modal.close(); // must call this!								   
				});
			});
		}
	});
	
	
	
	/* HANDLE CART MODAL */
	$('#cart').live('click', function(){
		if(region != 'na'){
			var src = 'http://components.arrow.com/gcart/getCart?region='+region+'&tocurr=' + $('#currency_selected span').text();
			$.modal('<iframe id="modal_iframe" src="about:blank" width="940" height="500" frameBorder="0">', {
				containerId: 'cart_modal',
				onClose: function(dialog){
					checkAuthentication();
					dialog.container.fadeOut(100, function () {
						dialog.overlay.fadeOut(200, function(){
							$.modal.close(); // must call this!								   
						});
					});
				},
				onOpen: function(dialog){
					dialog.overlay.show();
					dialog.container.show();
					dialog.data.show();
					$('#modal_iframe').attr('src', src);
				}
			});
		}else{
			var answer = confirm('This feature is not available yet. Would you like to change your region?');
			if(answer){
				showRegionSelector();
			}
		}
		return false;		
	});
	
	
	/* HANDLE FORGOT PASSWORD MODAL */
	$('.forgot_password').live('click', function(){
		if(region != 'na'){
			var src = 'http://components.arrow.com/globalauthentication/myarrow_password.jsp?region='+region;
			$.modal('<iframe id="modal_iframe" src="about:blank" width="940" height="500" frameBorder="0">', {
				containerId: 'cart_modal',
				onClose: function(dialog){
					dialog.container.fadeOut(100, function () {
						dialog.overlay.fadeOut(200, function(){
							$.modal.close(); // must call this!								   
						});
					});
				},
				onOpen: function(dialog){
					dialog.overlay.show();
					dialog.container.show();
					dialog.data.show();
					$('#modal_iframe').attr('src', src);
				}
			});
		}else{
			var answer = confirm('This feature is not available yet. Would you like to change your region?');
			if(answer){
				showRegionSelector();
			}
		}
		return false;		
	});
	
	$('.hdr_new_bom,.register_myarrow').live('click', function(){
		if(region != 'na'){
			return true;
		}else{
			var answer = confirm('This feature is not available yet. Would you like to change your region?');
			if(answer){
				showRegionSelector();
			}
			return false;
		}
	});	
	
	$('.choose_region').live('click',function(){
		region = $(this).attr('id');
		document.cookie ='region='+region+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/';
		checkAuthentication();
		$.modal.close();
		return false
	});
	
	
	//DROPDOWN MENU 
	var activeClass = 'dropdown_active', showingDropdown, showingMenu, showingParent;
	//Hide the open menu
	function hideMenu() {
		if(showingDropdown) {
			showingDropdown.removeClass(activeClass);
			showingMenu.hide();
		}
	}
  
	//set up all drop down menus
	$('.dropdown').each(function() {
		var dropdown = $(this);
		var menu = dropdown.next('div.dropdown_menu'), parent = dropdown.parent();
		
		/* function that shows THIS menu */
		function showMenu() {
			hideMenu();
			showingDropdown = dropdown.addClass('dropdown_active');
			menu.css('min-width', dropdown.outerWidth()+10);
			showingMenu = menu.show();
			showingParent = parent;
		}
		
		/* function to show menu when clicked */
		dropdown.bind('click',function(e) {
			if(e){
				e.stopPropagation();
				e.preventDefault();
			};
			if(dropdown.hasClass('dropdown_active')){ 
				hideMenu(); 
			}else{
				showMenu();
			}
		});
	
		/* function to show menu when someone tabs to the box */
		dropdown.bind('focus',function() {
			showMenu();
		});
	});
  
	/* hide when clicked outside */
	$(document.body).bind('click',function(e) {
		if (showingParent) {
			var parentElement = showingParent[0];
			if (!$.contains(parentElement,e.target) || parentElement == e.target || $.contains(parentElement,e.target)) {
				hideMenu();
			}
		}
	});
	

});


//////////////////////
/* HEADER FUNCTIONS */
//////////////////////


function showRegionSelector(){
	$('#region_popup').modal({
		closeHTML:'',
		containerId:'region_modal',
		appendTo: '#header_top',
		opacity: 80,
		onClose: function(dialog){
			dialog.container.fadeOut(100, function () {
				dialog.overlay.fadeOut(200, function(){
					$.modal.close(); // must call this!								   
				});
			});
		},
		onOpen: function(dialog){
			dialog.overlay.show();
			dialog.container.show();
			dialog.data.show();
		}
	});
}


function getCartInfo(){
	if(region != 'na'){
		$.ajax({
			type:'GET',
			dataType:'jsonp',
			jsonp: "callbackfn",
			cache: false,
			url:'http://components.arrow.com/gcart/getCartInfo?fmt=json&region='+region,
			error:function(){
				//alert('Error retrieving cart info');
			},
			success:function(data){
				var cart_items = data.shoppingcartsummaryresponseresult.data[0].size;
				$('#cart_items').html("(" + cart_items + ")");
			}
		});
	}
}


/*HANDLE MYARROW CONTENT*/
function buildMyArrow(authFlag){
	var login_content,bom_content,currency_list,lang_list;

	if(authFlag){ //user is logged in
		var ordersNav = '<li><a href="http://components.arrow.com/ordermanagement/order_listings.jsp">'+headerLabels.orders+'</a></li>\n';
		
		login_content =  ''+
		'<h5>MyArrow&trade; - '+headerLabels.hello+'!<span> (<a href="http://components.arrow.com/globalauthentication/remote_login.html?region='+region+'&loadAction=logout&remoteUrl='+encodeURIComponent(window.location.href)+'" id="btn_logout" class="logout_myarrow">'+headerLabels.logout+'</a>)</span></h5>\n'+
		'<ul id="user_nav">\n' + 
			'<li>'+headerLabels.loadingNav+'...</li>\n' +
		'</ul>\n';
		
		bom_content = ''+ 
        '<h5>BOM Upload</h5>\n'+
		'<a id="myBoms_link" href="http://components.arrow.com/bom/private/my_boms.jsp">'+headerLabels.manageBom+'</a>\n';
		
		$('#bom_content').html(bom_content);
		$('#login_content').html(login_content);
		$('#search_box h3').html(headerLabels.myarrowPartSearch);
		
		$.ajax({
			type:'GET',
			dataType:'jsonp',
			jsonp: 'callbackfn',
			url:'http://components.arrow.com/globalauthentication/getUserProfile?fmt=json&region='+region,
			error:function(){
				$('#user_nav').html(''+headerLabels.navError+'. <a href="#" style="text-decoration:underline;" id="retry_profile">'+headerLabels.tryAgain+'</a>');
			},
			success:function(data){
				var firstName,lastName,supportedCurrencyList,defCurrency,selectedCurrency,userLevel,supportedLangList,defLang,selectedLang,selectedRegion;

				firstName = data.userprofileresponseresult.data[0].userProfile.firstName;
				lastName = data.userprofileresponseresult.data[0].userProfile.lastName;
				supportedCurrencyList = data.userprofileresponseresult.data[0].userProfile.supportedCurrencyList;
				defCurrency = data.userprofileresponseresult.data[0].userProfile.defCurrency;
				selectedCurrency = data.userprofileresponseresult.data[0].selectedCurrency;
				userLevel = data.userprofileresponseresult.data[0].userProfile.userLevel;
				selectedRegion = data.userprofileresponseresult.data[0].selectedRegion;
				
				ordersNav = userLevel < 3 ? '' : ordersNav;
				selectedCurrency = selectedCurrency == '' ? defCurrency : selectedCurrency;
				supportedCurrencyList = supportedCurrencyList.split('#');
				currency_list = '';
				
				for(var i=0;i<(supportedCurrencyList.length -1);i++){
					currency_list = currency_list + '<li><a id="'+ supportedCurrencyList[i] +'" class="' + supportedCurrencyList[i] + '" href="#">' + supportedCurrencyList[i] + '</a></li>';
				}
				
				login_content =  ''+
				'<h5>MyArrow&trade; - '+headerLabels.hello+' ' + firstName + ' ' + lastName + '!<span> (<a href="http://components.arrow.com/globalauthentication/remote_login.html?region='+region+'&loadAction=logout&remoteUrl='+encodeURIComponent(window.location.href)+'" id="btn_logout" class="logout_myarrow">'+headerLabels.logout+'</a>)</span></h5>\n'+
				'<ul>\n' + 
					ordersNav + '\n'+
				'</ul>\n';
				
				$('#login_content').html(login_content);
				$('#currency_selected span').html(selectedCurrency);
				$('#search_curr').val(selectedCurrency);
				$('#currency_menu ul').html(currency_list);		
				$('#search_selected span').html($('#'+selectedRegion).children('h6').html());
				$('#language_selected span').html($('#'+lang).text());
			},
			complete:function(){
				//do nothing
			}
		});
		getCartInfo();
		
	}else{ //user is logged out
		
		login_content =  '' +
		'<h5>MyArrow&trade;<span> - '+headerLabels.myarrowTag+' (<a href="'+headerLabels.learnMoreUrl+'">'+headerLabels.learnMore+'</a>)</span></h5>\n'+
		'<div id="login_options">\n'+
			'<a id="login_submit" class="button login_myarrow" href="http://components.arrow.com/globalauthentication/remote_login.html?region='+region+'&loadAction=login&remoteUrl='+encodeURIComponent(window.location.href)+'">'+headerLabels.login+'</a>\n'+
			'<a href="http://components.arrow.com/globalauthentication/sh_regpage.jsp" class="register_myarrow">'+headerLabels.register+'</a> | <a class="forgot_password" href="#">'+headerLabels.forgotPw+'?</a>\n'+
		'</div>\n';

		$.ajax({
			type:'GET',
			dataType:'jsonp',
			jsonp: 'callbackfn',
			url:'http://components.arrow.com/globalauthentication/getDomainProfile?fmt=json&region='+region,
			success:function(data){
				var supportedCurrencyList,defCurrency,selectedCurrency,supportedLangList,defLang,selectedLang;
				
				supportedCurrencyList = data.domainprofileresponseresult.data[0].domainProfile.supportedCurrencyList;
				defCurrency = data.domainprofileresponseresult.data[0].domainProfile.defCurrency;
				selectedRegion = data.domainprofileresponseresult.data[0].selectedRegion;
				
				
				selectedCurrency = loadCookies('curr') ? loadCookies('curr') : defCurrency;
				selectedRegion = loadCookies('region') ? loadCookies('region') : selectedRegion;
				
				supportedCurrencyList = supportedCurrencyList.split('#');

				currency_list = '';
				
				for(var i=0;i<(supportedCurrencyList.length -1);i++){
					currency_list = currency_list + '<li><a id="'+ supportedCurrencyList[i] +'" class="' + supportedCurrencyList[i] + '" href="#">' + supportedCurrencyList[i] + '</a></li>';
				}
				
				$('#currency_selected span').html(selectedCurrency);
				$('#search_curr').val(selectedCurrency);
				$('#currency_menu ul').html(currency_list);
				$('#language_selected span').html($('#'+lang).text());
				$('#search_selected span').html($('#'+selectedRegion).children('h6').html());
				
			},
			complete:function(){
				//do nothing
			}
		});
		
		
		var workingBom = document.cookie.match ( '(^|;) ?' + 'p' + '=([^;]*)(;|$)'); //check for workingBom cookie
		
		if(workingBom){
			var wBomId = unescape(workingBom[2]);
			bom_content = ''+ 
			'<h5>BOM Upload</h5>\n'+
			'<div id="workingBOM">\n'+
			'<a class="left" href="http://components.arrow.com/bom/bom_summary.jsp?bomId=' + wBomId +'">'+headerLabels.workingBom+'</a><a class="right" href="http://components.arrow.com/bom/bom_home.jsp">'+headerLabels.uploadNewBom+'</a>\n'+
			'</div>\n';
		}else{
			bom_content = ''+ 
			'<h5>BOM Upload</h5>\n'+
			'<a href="http://components.arrow.com/bom/bom_home.jsp">'+headerLabels.provideBom+'</a>\n';
		}
		
		$('#bom_content').html(bom_content);
    	$('#login_content').html(login_content);
    	$('#search_box h3').html(headerLabels.partSearch);
    	
    	getCartInfo();
	}
	
}



function loadCookies(c) {
	var a = document.cookie.match('(^|;) ?' + c + '=([^;]*)(;|$)');
	return a ? (a[2].split(','))[0] : false;
}



function checkAuthentication(){
	if(region != 'na'){
		$.ajax({
			type:'GET',
			async: false,
			dataType:'jsonp',
			jsonp:'callbackfn',
			url:'http://components.arrow.com/globalauthentication/getUserProfile?fmt=json&region='+region,
			error:function(){
				$('#user_nav').html('Error retrieving your MyArrow Navigation. <a href="#" style="text-decoration:underline;" id="retry_profile">Try Again</a>');
			},
			success:function(data){
				var temp = data.userprofileresponseresult.transactionArea[0].success == true ? true : false;
				$('#currency_selector').show();
				buildMyArrow(temp);
			}
		});
	}else{
		$('#language_selected span').html($('#'+lang).text());
		$('#search_selected span').html($('#'+region).children('h6').html());
		$('#currency_selector').hide();
	}
}


(function($) {
	$.fn.extend({
		addSearchToken:function() {			
			var o = $(this);
			o.attr('action', 'http://components.arrow.com/part/search/' + fixSearchToken(o.find('input:text', o).val()));
		}
	});
})(jQuery);


function fixSearchToken(input) {
	try {
		input = decodeURIComponent(input);
	}catch (err){}
	var fs = '';
	$.each(input.split('%'), function(i,o){
		if(i>0){
			if(i.toString().charAt(0)==" "){
				fs += "%";
			}else{
				fs += "% ";
			}
		}
		fs += o;
	});
	var s = '';
	$.each(fs.split('/'), function(i, o) {
		s += (i > 0 ? '/' : '') + encodeURIComponent(o);
	});
	return s;
}



})(jQuery);
