$(function(){

	//Account box
	var logout = $('div#logged a').attr('href').indexOf("logout");

	if (logout < 0){
		
		$('#myaccount').remove();
 
	} else {
		
		$('#myaccount').show();
	}
	
	$('#TopMenu #customer div#myaccount span').click(function(e) {
		e.preventDefault();
		$('#TopMenu #customer div#myaccount ul').toggle();
		$(this).addClass("open");
	});
	
	$(document).mouseup(function(e) {
		if($(e.target).parent("#TopMenu #customer div#myaccount span").length==0) {
			$("#TopMenu #customer div#myaccount span").removeClass("open");
			$("#TopMenu #customer div#myaccount ul").hide();
		}
	}); 
	
	//newsletter
	var name = $('input#nl_first_name').attr('value');

	$('input#nl_first_name').focus (function() {
		if ($(this).attr('value') == name) {
				$(this).val("")
		}
	});
	
	$('input#nl_first_name').blur(function() {
		if ($(this).attr('value') == "") {
				$(this).val(name)
		}
	});
	
	var email = $('input#nl_email').attr('value');

	$('input#nl_email').focus (function() {
		if ($(this).attr('value') == email) {
				$(this).val("")
		}
	});
	
	$('input#nl_email').blur(function() {
		if ($(this).attr('value') == "") {
				$(this).val(email)
		}
	});
	
	//drop downs

	//every list level has class
	$('#FlameMenu .BlockContent > ul').addClass('parent');
	$('#FlameMenu .BlockContent > ul > li > ul').addClass('child');
	$('#FlameMenu .BlockContent > ul > li > ul > li > ul').addClass('grandchild');
	$('<li class="Break">').insertAfter('#FlameMenu .child li:nth-child(5n)');
	
	//wrap parent level in a div
	$('#FlameMenu ul.child').wrap('<div class="parentwrap">');
	$('#FlameMenu .parentwrap ul').show();
	
	//first/last
	$('#FlameMenu > ul > li:first-child').addClass('first');
	$('#FlameMenu > ul > li:last-child').addClass('last');
	
	$('.flamemenu').prepend('<li class="home"><a href="http://www.bpsalesdirect.com"><img src="http://www.bpsalesdirect.com/templates/Benefits/images/gray/home.png" alt="bp sales home"></a></li>');
	
	//drop downs
	$('#FlameMenu li:has(ul)').addClass('hasSub');
	$('#FlameMenu li').hover(function(){
		$(this).addClass('active').find('> div.parentwrap').stop(true, true).show();						  							  
	}, function(){
		$(this).removeClass('active').find('> div.parentwrap').stop(true, true).hide();	
	});
	
	//inex first level ul
	$(function(){
		$('#FlameMenu ul.parent > li').addClass(function(index) {return 'nav' + index;});
 	});
	
	//Take href and add as class
	$('li.hasSub.nav2 ul.child li a').each(function(){
		var aVal = $(this).html().toLowerCase(); //make lowercase
		var aValHyp = aVal.replace(/ /g,"-"); //repalce spaces with -
		$(this).parent().addClass(aValHyp); //pass in new var as class onto parent
	});

	//Side menu
	var mylocation = window.location.href;
	$('#SideCategoryList a[href=' + mylocation + ']').parent().addClass('active');
	$('#SideCategoryList a[href=' + mylocation + ']').parent().parent().addClass('active');
	$('#SideCategoryList a[href=' + mylocation + ']').parent().parent().parent().parent().addClass('active');
	
	var prodlocation = $('#ProductBreadcrumb li:nth-child(3) a').attr('href');
	$('#SideCategoryList a[href=' + prodlocation + ']').parent().addClass('active');
	$('#SideCategoryList a[href=' + prodlocation + ']').parent().parent().addClass('active');
	
	$('#SideCategoryList').find('ul:first').addClass('first');
	$('#SideCategoryList').find('ul:first > li > ul').addClass('second');
	$('#SideCategoryList').find('ul:first > li > ul > li > ul').addClass('third');
	$('#SideCategoryList').find('ul:first > li > ul > li > ul > li > ul').addClass('fourth');
	$('#SideCategoryList li:first-child').addClass('frst');
	$('#SideCategoryList li:last-child').addClass('lst');
	
	//Category lists, last item doesnt have class so target it and add inline style
	$('.SubCategoryListGrid li:last-child').css('border', 'none');
	
});




