// Track any PDFs that don't already have onclick event attached

$('a').each(function() {
    if($(this).attr('href').indexOf('.pdf') > -1 && !$(this).attr('onclick')) {
       // add the analytics
       onclick="javascript: pageTracker._trackPageview(this.href);"
    }
	
	    if($(this).attr('href').indexOf('.doc') > -1 && !$(this).attr('onclick')) {
       // add the analytics
       onclick="javascript: pageTracker._trackPageview(this.href);"
    }
	
	    if($(this).attr('href').indexOf('.zip') > -1 && !$(this).attr('onclick')) {
       // add the analytics
       onclick="javascript: pageTracker._trackPageview(this.href);"
    }
	
	    if($(this).attr('href').indexOf('.jpg') > -1 && !$(this).attr('onclick')) {
       // add the analytics
       onclick="javascript: pageTracker._trackPageview(this.href);"
    }
});

function accordionInit() {
	// This should be in sf_menu.js, but there's a clash with scriptaculous somewhere and this is a quick fix to prevent JS errors while I remove the file
}

function tab(tab) {
	// Note that specialist is a global set in PHP
	var wrapper = document.getElementById('wrapper');
		
	// wrapper.style.backgroundImage = 'url(img/layout/backgrounds/wrappers/wrapper_tall_black.gif)';
	var hr = document.getElementById('header_right');
	var pr = document.getElementById('product_range');
	var ps = document.getElementById('product_search');		
	if (tab == 'product_range') {
		hr.className = 'right';
		pr.style.display = 'block';
		ps.style.display = 'none';	
		wrapper.style.background = 'url(img/layout/backgrounds/wrappers/wrapper_tall_'+specialist+'.gif)';
	}
	else if (tab == 'product_search') {
		hr.className = 'right search';
		pr.style.display = 'none';
		ps.style.display = 'block';		
		wrapper.style.background = 'url(img/layout/backgrounds/wrappers/wrapper_tall_black.gif)';
	}
	moveBar();
}


function ajaxUpdate(v,t,s) {
	return false;// Dummy function defined in request.js but installed here while testing
}

function findPos(obj) {
	
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

	
function moveBar() {
//	var footer = document.getElementById('footer');
//	if (footer) {
//	var position = findPos(footer);
//		var bar = document.getElementById('bar');
//		if (bar) {
//			// alert ("Moving bar to " + (position[1] - 30) + " as footer is at "+position[1]);
//			bar.style.top = (position[1] - 30) +'px';
//			// window.status = bar.style.top;//
////
//		}		
//	}
}
		
function myReset() {
	document.getElementById('specialist').selectedIndex = 0;
	document.getElementById('group').selectedIndex = 0;
	document.getElementById('product').selectedIndex = 0;
	document.getElementById('keyword').value = ' Enter a Keyword...';	
	document.getElementById('type_product-information').checked = 'checked';	
	return false;
}

// window.onload = moveBar;

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

var menuItems = ['home', 'paving', 'block-paving', 'machine-lay', 'water-management', 'kerb', 'traffic-management', 'street-furniture', 'walling'];
var currentMenuItemIndex = -1;

function preloadMenu() {
	// Why on earth have the URLs been hardcoded into the clientside JavaScript? This is an extremely poor approach.
 if ((document.location.href == 'http://www.marshalls.co.uk/select/') || (document.location.href == 'http://www.marshalls.co.uk/select/index.php') || (document.location.href == 'http://www.marshalls.co.uk/select/') || (document.location.href == 'http://www.marshalls.co.uk/select/index.php') || (document.location.href == 'http://www.marshalls.co.uk/select/carboncalculator.php') || (document.location.href == 'http://www.marshalls.co.uk/select/street-furniture.php') ) {currentMenuItemIndex = 0;}
 if (document.location.href.indexOf('specialist=Paving') > -1) {currentMenuItemIndex = 1;}
 if (document.location.href.indexOf('specialist=Block+Paving') > -1) {currentMenuItemIndex = 2;}
 if (document.location.href.indexOf('specialist=Machine+Lay') > -1) {currentMenuItemIndex = 3;}
 if (document.location.href.indexOf('specialist=Water+Management') > -1) {currentMenuItemIndex = 4;}
 if (document.location.href.indexOf('specialist=Kerb') > -1) {currentMenuItemIndex = 5;}
 if (document.location.href.indexOf('specialist=Traffic+Management') > -1) {currentMenuItemIndex = 6;}
 if (document.location.href.indexOf('specialist=Street+Furniture') > -1) {currentMenuItemIndex = 7;}
 if (document.location.href.indexOf('specialist=Walling') > -1) {currentMenuItemIndex = 8;}
 for (menuItemIndex=0; menuItemIndex<menuItems.length; menuItemIndex++) {
  if (currentMenuItemIndex == 0) { // homepage: black menu
   MM_preloadImages('img/menu/' + menuItems[menuItemIndex] + '_black.gif');
   MM_preloadImages('img/menu/' + menuItems[menuItemIndex] + '_black_hover.gif');
  } else { // other page: white menu
   MM_preloadImages('img/menu/' + menuItems[menuItemIndex] + '_white.gif');
   MM_preloadImages('img/menu/' + menuItems[menuItemIndex] + '_white_hover.gif');
	 }
 }
}

function updateMenu(rollOverMenuItemIndex) {
 if (rollOverMenuItemIndex == undefined) {rollOverMenuItemIndex = -1;}
 for (menuItemIndex=0; menuItemIndex<menuItems.length; menuItemIndex++) {
  if ((menuItemIndex == rollOverMenuItemIndex) || (menuItemIndex == currentMenuItemIndex)) { // rolling over this menu item, or this is the current section of the site; use hover image
   if (currentMenuItemIndex == 0) { // homepage: black menu
    MM_swapImage('menu' + menuItemIndex,'','img/menu/' + menuItems[menuItemIndex] + '_black_hover.gif',1);
   } else { // other page: white menu
    MM_swapImage('menu' + menuItemIndex,'','img/menu/' + menuItems[menuItemIndex] + '_white_hover.gif',1);
			}
	 } else {
   if (currentMenuItemIndex == 0) { // homepage: black menu
    MM_swapImage('menu' + menuItemIndex,'','img/menu/' + menuItems[menuItemIndex] + '_black.gif',1);
   } else { // other page: white menu
    MM_swapImage('menu' + menuItemIndex,'','img/menu/' + menuItems[menuItemIndex] + '_white.gif',1);
			}
	 }
	}
}

function hideDropdowns() {
//	window.status = 'Hiding dropdowns';
	document.getElementById('product').style.visibility = 'hidden';
	
	if (document.getElementById('material')) {
		document.getElementById('material').style.display = 'none';
	}
	
	document.getElementById('group').style.visibility = 'hidden';	
}
function showDropdowns() {
//	window.status = 'Hiding dropdowns';
	document.getElementById('product').style.visibility = 'visible';	
	document.getElementById('group').style.visibility = 'visible';	
}

function hideMaterialDropdown() {
	if (document.getElementById('material')) {
		document.getElementById('material').style.display = 'none';
	}
}


function showMaterialDropdown() {
	if (document.getElementById('material')) {
		document.getElementById('material').style.display = 'inline';
	}
}


preloadMenu();
updateMenu();

