window.addEvent('domready', function() {

	$$('.mainMenu li')[0].addClass('home');
	count = 0;
	$$('.mainMenu li').each( function(el) {
	  if(el.getParent().getProperty('class') == "mainMenu")
		{
			anc = el.getElement('a');
			anc.setProperty('id', 'anc'+count);
			span = anc.getElement('span');
			span.setProperty('id', 'span'+count);
			
		
			if (!isIE6)
			{
				if(el.getChildren('div').length == 1	)
				{
					document.getElementById(span.id).style.backgroundImage = "none";
					document.getElementById(span.id).style.width = "10px";
				}
			}
			if (isIE6)
			{
				if(el.getElement('.subMenuLeft') == null	)
				{
					document.getElementById(span.id).style.backgroundImage = "none";
					document.getElementById(span.id).style.width = "10px";
				}
				if( $chk(el.getElement('.ieHelper')))
				{
					el.getElement('.ieHelper').setProperty('id', 'sub'+count); 
				}
			}
			count++;
		}
	});
  if (isIE6)
	{
			$$('.mainMenu li a').each( function(a) {
	   	  a.addEvent('mouseover', function(e) {
						pid = a.getProperty('id').substring(3);
  					ShowMenu('sub'+pid);
			  });
			  a.addEvent('mouseout', function(e) {
						pid = a.getProperty('id').substring(3);
  					HideMenu('sub'+pid);
			 });
		});			
	}
  countElementsInPanel = 0;
	if (document.getElementById('panel') && $chk($$('.panelList ul li')) ){
			$$('.panelList ul li').each( function(el) {
				children = el.getChildren('p');
					countElementsInPanel +=children.length;
				children = el.getChildren('table');
					countElementsInPanel +=children.length;
			});	
			if(countElementsInPanel == 0){
				document.getElementById('panel').style.display = 'none' ;
			}
	}
	$$('.headerOptions ul li a').each(function(a) {
    if (window.location.hostname) {
			var hostname = window.location.hostname.replace("www.", "").toLowerCase();
			if (isIE7 && a.getProperty('class') == "lin"){
						 a.getParent().setProperties({
								'class': (a.getParent().getProperty('class')) ? a.getParent().getProperty('class') + ' businessDirectLIIE7' : 'businessDirectLIIE7'
						 })
			}
			if(a.getProperty('href').contains("sainsburys.co.uk") || a.getProperty('href').contains("sainsburysbusinessdirect") )
			{
				a.setProperties({
					'target': '_blank',
					'style':'margin-right:15px',
					'class': (a.getProperty('class')) ? a.getProperty('class') + ' blackOpen' : 'blackOpen'
				})
				
				if((a.getProperty('href').contains("sainsburysbusinessdirect")))
				{
					 a.setProperties({
							'class': (a.getProperty('class')) ? a.getProperty('class') + ' businessDirect' : 'businessDirect'
					 })
				}
			}
    }
    if(document.getElementById('cp'))document.getElementById('cp').style.display = "none"
  });
	$$('.option ul li a').each(function(a) {
    if (window.location.hostname) {
        var hostname = window.location.hostname.replace("www.", "").toLowerCase();
        if (a.getProperty('href').contains("http://")) {
						a.setProperties({
									'target': '_blank',
									'style':'padding-right:15px',
									'class': (a.getProperty('class')) ? a.getProperty('class') + ' blackOpen' : 'blackOpen'
  					})
			}
    }
  });
	
	$$('.footerBar a[href^=http]').each(function(a) {
    if (window.location.hostname) {
			var hostname = window.location.hostname.replace("www.", "").toLowerCase();
			if (!a.getProperty('href').contains(hostname)) {
			
					a.setProperties({
								'target': '_blank',
								'style':'margin-right:15px',
								'class': (a.getProperty('class')) ? a.getProperty('class') + ' whiteOpen' : 'whiteOpen'
					})
			}
    }
  });
	$$('.replace-with-image H1').each(function(h1) {
					h1.setProperties({
									'title': h1.innerHTML
 					})
  });
	$$('p.button a').each(function(a) {
					a.setProperties({
									'title': a.innerHTML
 					})
  });
	$$('img').each(function(img) {
					if (img.getProperty('alt') && img.getProperty('title') != img.getProperty('alt'))
					{
						img.setProperties({
										'title': img.getProperty('alt')
						})
					}
  });
});
function ShowMenu(menu) {
	var thisMenu = document.getElementById(menu);
	if (thisMenu != null) {
		thisMenu.style.display="block";
	}
}
function HideMenu(menu) {
	var thisMenu = document.getElementById(menu);
	if (thisMenu != null) {
	thisMenu.style.display="none";
	}
}
function checkIsIE(ver) {
	var userAgent = navigator.userAgent.toLowerCase();
	if (/msie[\/\s](\d+\.\d+)/.test(userAgent)) {
		var ieversion = new Number(RegExp.$1);
		return (ieversion < (ver+1));
	} else {
		return false;
	}
}
isIE6 = checkIsIE(6);	
isIE7 = checkIsIE(7);