// Magazine Manager service routines

// initialise page constants all new 7-1-10
var mmco="";
var mmcat="";
var mmptype="";
var mmcatadvertiser = new Array();
	mmcatadvertiser[1] = '';
	mmcatadvertiser[2] = '';
	mmcatadvertiser[3] = '';
	mmcatadvertiser[4] = '';
	mmcatadvertiser[5] = '';
	mmcatadvertiser[6] = '';
	mmcatadvertiser[7] = '';
	mmcatadvertiser[8] = '';
	mmcatadvertiser[9] = '';
	mmcatadvertiser[10] = 'M1286';
	mmcatadvertiser[11] = '';
	mmcatadvertiser[12] = '';
	mmcatadvertiser[13] = '';
	mmcatadvertiser[14] = '';
	mmcatadvertiser[15] = '';
	mmcatadvertiser[16] = '';
	mmcatadvertiser[17] = '';
	mmcatadvertiser[18] = '';
	mmcatadvertiser[19] = '';
	mmcatadvertiser[20] = '';
// refinement 16-1-10	
var mmlhbuttons = new Array();
var mmrhbuttons = new Array();
var mmtopbuttons = new Array();
var mmadvertisers = new Array();

//write a date in the format dd textmonth yyyy
function mm_date () {
	d = new Date();
	y = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	document.write (d.getDate()+" "+y[d.getMonth()]+" "+d.getFullYear());
	}
	
function mm_copyright() {
	d = new Date();
	var fy = d.getFullYear().toString();
	document.write ("<p>&copy; Copyright 2009-"+fy.substring(2,4)+" Industrial Communications Network Ltd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>");
	}
	
// place the left-hand premium ad
// this version of function does not open new window
//
function mm_top_ad () {
	adlink = "#";
	adimgsrc  = "images/trans.gif";
	adwidth = "468";
	adheight = "60";
	document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");
	}	
	
function mm_top_ad1 () {
	adlink = "#";
	adimgsrc  = "../images/trans.gif";
	adwidth = "468";
	adheight = "60";
	document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");
	}		
	
	
// place the left-hand premium ad
// this version of function does not open new window
//
function mm_lh_premium_ad () {
	adlink = "#";
	adimgsrc  = "images/defaultb90h.gif";
	adwidth = "120";
	adheight = "90";
	document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");
	}
	
// place the left-hand premium ad
// this version of function does not open new window
//
function mm_lh_premium_ad1 () {
	adlink = "#";
	adimgsrc  = "../images/defaultb90h.gif";
	adwidth = "120";
	adheight = "90";
	document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");
	}
	
// place the ad on an index page	
function mm_index_ad1() {
	//adlink = "#";
	//adimgsrc  = "../images/iv_fhbad.gif";
	//adwidth = "468";
	//adheight = "60";
	//document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");

	mm_premium_ads(1);

}
	
function mm_rhcol_ads(lev,co,cat) {
// new version 13-1-07 which incorporates the lower value text block at the bottom and 
// a maximum limit on the number of buttons listed
// newer version 5-1-10 with co and cat in the interface
	if (lev==0) {
		pfx = "";
	} else if (lev==1) {
		pfx = "../";
	}
	
	var rht = 0;
	
	imdir = "images/ad/";
	
	r = "http://www.internationalvacuum.com/service/mmgo.php"+"?http://";
	
	var banners = mmrhbuttons;
	var button_display_max = 8;
	//var texts   = mm_rhcol_textad_initialise();
	//var text_display_max = 5;
	
	// find and take out any fixed position buttons
	
	switch(mmptype) {
		case '10': // company page
		case '12': // intermediate index page
			if (rht) alert('RH: Company or Co Int Index');
			if ((mmco !="") && (in_array(mmadvertisers,mmco))) {
				if (rht) alert('RH find and use the ad');
				adselector = mmco; 
				// locate idx
				found = "";
				var i;
				for (i=0;i<banners.length;i++) {
					//alert("compare "+banners[i].co+" with "+adselector);
					if (banners[i].co == adselector) 
						found = i;
				}
				
				//alert("found = "+found+" length "+found.length);
				//alert("i = "+i);
				
				if (found.length == 0) { // should have been found but hasn't been
					//ridx = Math.floor(Math.random()*banners.length);
					if (rht) alert("null ad");
					document.write ("<table width=\"100%\" class=\"rhcolads\">\n");
					document.write ("<tr><td>&nbsp;</td></tr></table>");
					break;
				} else {
					if (rht) alert("found ad");
					ridx = found;
					if (banners[ridx].adlinktype) {
						adlink=r+banners[ridx].adlink; // a redirect, via mmgo
					} else {
						adlink = pfx+banners[ridx].adlink; // local link
					}
					adimgsrc  = pfx+imdir+banners[ridx].source;
					adcaption = banners[ridx].caption;
					adwidth = banners[ridx].width;
					adheight = banners[ridx].height;
					document.write ("<table width=\"100%\" class=\"rhcolads\">\n");
					document.write ("<tr><td><a href=\""+adlink+"|b"+i+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");
					document.write ("</td></tr>");
					document.write("</table>");
					break;
				}
		
			} else {
				alert('backstop: default');	
				// tacky programming. no break but it works
			}
			
		
		default:
			if (rht) alert('random-10');
			buttons = banners;
			var priv = new Array(); //these are the ranked ads
			var seq = new Array();
			for (i=0; i<buttons.length; i++) {
				if (buttons[i].rank > 0) {
					priv.push(i);
				} else {
					seq.push(i);
				}
			}
			
			// sort the privileged ads in ascending sequence
			priv.sort();
			// shuffle the non-privileged ones and then...
			// make a new array of indexes by concatenating priv and seq
			var finalseq = priv.concat(mm_shuffle(seq));
			
			//alert(finalseq.toString() + " final sequence");
			
			document.write ("<table width=\"100%\" class=\"rhcolads\">\n");
			
			for (i=0; i<buttons.length; i++) {
				if (i < button_display_max) {
					
					s = finalseq[i];
					if (buttons[s].adlinktype) {
						adlink=r+buttons[s].adlink; // a redirect, via mmgo
					} else {
						adlink = pfx+buttons[s].adlink; // local link
					}
					adimgsrc  = pfx+imdir+buttons[s].source;
					adcaption = buttons[s].caption;
					adwidth = buttons[s].width;
					adheight = buttons[s].height; 
					document.write ("<tr><td><a href=\""+adlink+"|b"+i+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\"></a>");	
					if (adcaption.length > 0) {
						document.write ("<br><a href=\""+adlink+"\">"+adcaption+"\</a></td></tr>");	
					}
					document.write ("</td></tr>");
				}
			}
			
			document.write("</table>");
	}
}

function mm_lhcol_ads(lev,co,cat) {
// new version 5-1-10 with co and cat in the interface
	if (lev==0) {
		pfx = "";
	} else if (lev==1) {
		pfx = "../";
	}
	
	var lht = 0;
	
	imdir = "images/ad/";
	
	r = "http://www.internationalvacuum.com/service/mmgo.php"+"?http://";
	
	var banners = mmlhbuttons;
	
	var ridx = -1;
	
	switch(mmptype) {
		case '10': // company page
		case '12': // intermediate index page
		
			if (lht) alert('LH: Company or Co Int Index');
				if ((mmco !="") && (in_array(mmadvertisers,mmco))) {
					if (lht) alert('LH find and use the ad');
					adselector = mmco; 
					// locate idx
					found = "";
					var i;
					for (i=0;i<banners.length;i++) {
						//alert("compare "+banners[i].co+" with "+adselector);
						if (banners[i].co == adselector) 
							found = i;
					}
					
					//alert("found = "+found+" length "+found.length);
					//alert("i = "+i);
					
					if (found.length == 0) { // should have been found but hasn't been
						if (lht) alert("LH null ad");
						// exit without altering ridx
					} else {
						if (lht) alert("LH found ad");
						ridx = found;
						
					}
			
				} else {
					if (lht) alert('LH backstop: default');	
					// exit without altering ridx
				
				}
				
				break;
		
		default:
			// which banner shall we use this time?
			var ridx = Math.floor(Math.random()*banners.length);
			if (lht) alert("random-20");
	}
	
	
	if (ridx != -1) {
	
		if (banners[ridx].adlinktype) {
				adlink=r+banners[ridx].adlink; // a redirect, via mmgo
			} else {
				adlink = pfx+banners[ridx].adlink; // local link
			}
		adimgsrc  = pfx+imdir+banners[ridx].source;
		adcaption = banners[ridx].caption;
		adwidth = banners[ridx].width;
		adheight = banners[ridx].height;
		if (adwidth == 120) {
			adstyle = " style=\"padding:5px 20px 5px 20px;\"";
		}else {
			adstyle = "";	
		}
	
		document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\""+adstyle+"></a>");
	} else {
		if (lht) alert("LH &nbsp;");
		document.write ("&nbsp;");
		
	}

}

function mm_premium_ads(lev,co,cat) {
// new version 5-1-10 with co and cat in the interface
	if (lev==0) {
		pfx = "";
	} else if (lev==1) {
		pfx = "../";
	} else if (lev<0) {
		pfx = "/";
	}
	
	var topt = 0;
	
	imdir = "images/ad/";
	
	r = "http://www.internationalvacuum.com/service/mmgo.php"+"?http://";
	
	var banners = mmtopbuttons;
	
	//alert("There are "+banners.length+" banners");
	
	var ridx=-1;
	
	switch (mmptype) {
		case '10': // company page
		case '12': // intermediate index page
			if (topt) alert('Company or Co Int Index');
			if (mmco != "") {
				// is the co an advertiser?
				if (in_array(mmadvertisers,mmco)) {
					if (topt) alert(mmco+' is an advertiser');
					adselector = mmco; 
					// locate idx
					found = "";
					var i;
					for (i=0;i<banners.length;i++) {
						//alert("compare "+banners[i].co+" with "+adselector);
						if (banners[i].co == adselector) 
							found = i;
					}
					
					//alert("found = "+found+" length "+found.length);
					//alert("i = "+i);
					
					if (found.length == 0) { // no appropriate ad
						if (topt) alert("Advertiser but no banner");
					} else {
						ridx = found;
						//alert("found, index = "+ridx);
					}
				} else {
					// if not, take default action
					ridx = Math.floor(Math.random()*banners.length);	
					if (topt) alert("random-5");
				}
			} else {
				ridx = Math.floor(Math.random()*banners.length);	
				if (topt) alert("random-4");
			}
			break;
		case '31': // category page
		case '20': // article page
			if (topt) alert('Category or article');
			if (mmcat != "") {
				if (topt) alert('Art has cat or cat has cat: '+mmcat);
				adselector = mmcatadvertiser[mmcat]; 
				// locate idx
				found = "";
				var i;
				for (i=0;i<banners.length;i++) {
					//alert("compare "+banners[i].co+" with "+adselector);
					if (banners[i].co == adselector) 
						found = i;
				}
				
				//alert("found = "+found+" length "+found.length);
				//alert("i = "+i);
				
				if (found.length == 0) { // should have been found but hasn't been
					ridx = Math.floor(Math.random()*banners.length);
					if (topt) alert("random-1");
				} else {
					ridx = found;
					//alert("found, index = "+ridx);
				}
						
			} else {
				
				ridx = Math.floor(Math.random()*banners.length);	
				if (topt) alert("random-2");
				
			}
			break;
		default:
			if (topt) alert('Take default action');
			ridx = Math.floor(Math.random()*banners.length);
			if (topt) alert("random-3, ridx= "+ridx);
	}
	
	if (ridx != -1) { // it's managed to get a proper value
		if (banners[ridx].adlinktype) {
			adlink=r+banners[ridx].adlink; // a redirect, via mmgo
		} else {
			adlink = pfx+banners[ridx].adlink; // local link
		}
		adimgsrc  = pfx+imdir+banners[ridx].source;
		adcaption = banners[ridx].caption;
		adwidth = banners[ridx].width;
		adheight = banners[ridx].height;
		if (adheight == 60) {
			adstyle = " style=\"padding-top:10px;\"";
		}else {
			adstyle = "";	
		}
		//alert(adimgsrc);
		
		document.write ("<a href=\""+adlink+"\"><img src=\""+adimgsrc+"\" width=\""+adwidth+"\" height=\""+adheight+"\""+adstyle+"></a>");
	} else {
		if (topt) alert('ridx unset - blank adspace');
		document.write ("<img src=\""+pfx+"images/trans.gif\" width=\"468\" height=\"60\" style=\"padding-top:10px;\">");
	}

}
	
//
//  An email address service routine (partly courtesy of Actinic)
//  <a href="mailto:chrishutchins@btinternet.com">chrishutchins@btinternet.com</a>

function mm_mto (mad) {
	_ad = mad.replace(/&/,"@");
	document.write("<a href=\"mailto:" + _ad + "\">" + _ad + "</a>");
	return;
}

//  A variant on the one above, which doesn't put in a mail link, but still hides what's
//  obviously an email address

function mm_ema (mad) {
	_ad = mad.replace(/&/,"@");
	document.write(_ad);
	return;
}

// This function initialises the array containing the button ads, used on the RH side of the
//  page. It uses a subfunction to convert ad info into an object.
//

function mm_rhcol_ad_initialise() {

	_b = new Array;
	
	//              co, target url, local or external, graphic, caption, width, height, rank
	_b.push(mm_cbo("M1010","www.pvl.co.uk", 1,"m1010_btn.jpg", "", 120, 60, 0));
	_b.push(mm_cbo("M1054","www.avs-vacuum.co.uk", 1,"m1054_btn.jpg", "", 120, 90, 0));
	_b.push(mm_cbo("M1097","www.varianinc.com", 1,"m1097_btn.jpg", "", 120, 90, 0));
	_b.push(mm_cbo("M1126","www.fpz.com", 1,"m1126_btn.jpg", "", 120, 120, 0));
	_b.push(mm_cbo("M1252","www.vacuumsys.com", 1,"m1252_btn.jpg", "", 120, 60, 0));
	_b.push(mm_cbo("M1253","www.ddtecserv.com", 1,"m1253_btn.jpg", "", 120, 90, 0));
	//_b.push(mm_cbo("www.internationalvacuum.com/about.htm%23industrialvacuum",1,"iv_btn.jpg","", 120, 90, 0));
	_b.push(mm_cbo("M1286","www.aerotech.com/litreq.cfm",1,"m1286_btn.gif","", 120, 120, 0));
	
	return _b;
	
}

// This function initialises the array containing the skyscraper ads, used on the LH side of the
//  page. It uses a subfunction to convert ad info into an object.
//

function mm_lhcol_ad_initialise() {

	_b = new Array;
	
	//              co, target url, local or external, graphic, caption, width, height
	_b[0] = mm_cbo("M1260","www.liquivac.co.uk", 1, "m1260_widesky.gif", "", 160, 600);
	_b[1] = mm_cbo("M1341","www.atlasuhv.com", 1, "m1341_atlas_bnr.gif", "", 120, 600);
			
	return _b;
	
}

// This function initialises the array containing the text ads, used on the RH side of the
//  page. It uses a subfunction to convert ad info into an object.
//
function mm_rhcol_textad_initialise() { // not currently used

	_b = new Array;
	
	//              target url, local or external, graphic, caption, width, height rank
	_b[0] = mm_cbo("www.norcaluk.com", 1,"", "Nor-Cal UK", 120, 90, 0);
	_b[1] = mm_cbo("www.creativevacuum.co.uk", 1, "", "Creative Vacuum", 120, 90, 0);
	_b[2] = mm_cbo("www.parker.com", 1, "", "Parker Pneumatics", 120, 60, 0);
	_b[3] = mm_cbo("www.nitto.co.uk", 1,"", "Nitto Kohki", 120, 90, 0);
	_b[4] = mm_cbo("www.ilmvac.co.uk", 1,"", "Ilmvac", 120, 60, 0);
	_b[5] = mm_cbo("www.pvl.co.uk", 1,"", "PVL", 120, 60, 0);
	_b[6] = mm_cbo("about.htm#iv", 1,"iv_btn.jpg", "", 120, 90, 0);
	//_b[7] = mm_cbo("www.knf.co.uk", 1,"m1064_knf_btn.jpg", "", 120, 60, 0);
	//_b[8] = mm_cbo("www.duerr-technik.de", 1,"m1065_durr_btn.jpg", "", 120, 60, 0);
	
	
	return _b;
	
}


function mm_premium_ad_initialise() {

	_b = new Array;
	
	//              co, target url, local or external, graphic, caption, width, height
	//_b[0] = mm_cbo("www.practicalvacuum.co.uk", 1,"aniban07.gif", "", 468, 60);
	//_b[1] = mm_cbo("www.charlesausten.com", 1, "m1005_char_btn.jpg", "", 468, 60);
	_b[0] = mm_cbo("M1093","www.creative-group.biz", 1, "creativeban.gif", "", 468, 60);
	_b[1] = mm_cbo("M1135","www.norcaluk.com", 1, "m1135_bnr.gif", "", 468, 60);
	_b[2] = mm_cbo("M1361","about.htm#register", 0, "intvacreg.jpg", "", 468, 60);
	_b[3] = mm_cbo("M1286","www.aerotech.com/litreq.cfm",1,"m1286_bnr.gif","", 468, 60);
		
	return _b;
	
}

// pack the ad info into an object

function mm_cbo (co, lnk, ltype, src, cap, w, h, r) {
	
	var ad = new Object();

	ad.co = co;
	ad.adlink = lnk;
	ad.adlinktype = ltype;
	ad.source = src;
	ad.caption = cap;
	ad.width = w;
	ad.height = h;
	ad.rank = r;
	
	return ad;
	
}

function mm_shuffle (o) {
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function mm_search_header() {
document.write ('<div style="float: left; width: 975px; padding: 0; margin: 0 auto; border: 0; background-image: url(/images/iv_banner_news.gif);">');
document.write ('<div style="float: left; width: 247px; padding: 0; margin: 0; border: 0;">');
document.write ('<a href="/index.htm"><img src="/images/trans.gif" width="247" height="90" style="border: 0;"></a>');
document.write ('</div>');
//document.write ('<div style="float: right; width: 728px; padding: 0; margin: 0; border: 0;">');
//document.write ('<img src="/images/trans.gif" width="1" height="8">');
//document.write ('Top margin');
//document.write ('</div>');
document.write ('<div style="float: right; width: 728px; text-align: center; padding: 0; margin: 0; border: 0;">');
//document.write ('The ad');
document.write ('<div style="float: right; padding: 11px 60px 11px 0; margin: 0; border: 0;">');
document.write ('<style>');
document.write ('img {border: 0;}');
document.write ('</style>');
document.write ('<script type="text/javascript">mm_premium_ads(-1)</script>');
//document.write ('<a  href="/service/mmgo.php?http://www.machinebuilding.net/pdf/MB_Media_Guide_2008.pdf"><img style="border: 0;"  src="/images/ad/m1273_ban_sizesample.gif" width="728" height="90"></a></td>');
document.write ('</div>');
document.write ('</div>');
//document.write ('<div style="float: right; width: 728px; padding: 0; margin: 0; border: 0;">');
//document.write ('Bottom margin');
//document.write ('<img src="/images/trans.gif" width="1" height="8">');
//document.write ('</div>');
//document.write ('<div style="float:right"><img src="/images/ad/m1273_ban_mediapack1.gif"  width="468" height="60"></div>');

document.write ("</div>  <!-- end of topbanner -->");

}
// useful service function
function in_array(haystack, straw) {
	var isin = false;
	for(var i=0;i<haystack.length;i++) {
		if (straw == haystack[i]) {
			isin = true;
			//alert('found');
			break;
		}
	}
	return isin;
}

function mm_initialise_ad_arrays() {
	
	mmlhbuttons = mm_lhcol_ad_initialise();
	
	mmrhbuttons = mm_rhcol_ad_initialise();
	
	mmtopbuttons = mm_premium_ad_initialise()
	
	//alert('INITIALISE\n'+mmlhbuttons.length+' lh buttons\n'+mmrhbuttons.length+' rh buttons\n'+mmtopbuttons.length+' top buttons');
	
}

function mm_initialise_advertisers() {

	//alert(mmadvertisers.length.toString()+" mmadvertisers length");
	
	if (mmlhbuttons.length) {
		for (var j=0;j<mmlhbuttons.length;j++) {
			if (!in_array(mmadvertisers,mmlhbuttons[j].co)) {
				mmadvertisers.push(mmlhbuttons[j].co);
			}
		}
	}
	
	//alert(mmadvertisers.join());
	
	if (mmrhbuttons.length) {
		for (var j=0;j<mmrhbuttons.length;j++) {
			if (!in_array(mmadvertisers,mmrhbuttons[j].co)) {
				mmadvertisers.push(mmrhbuttons[j].co);
			}
		}
	}
	
	//alert(mmadvertisers.join());
	
	if (mmtopbuttons.length) {
		for (var j=0;j<mmtopbuttons.length;j++) {
			if (!in_array(mmadvertisers,mmtopbuttons[j].co)) {
				mmadvertisers.push(mmtopbuttons[j].co);
			}
		}
	}
	
	//alert(mmadvertisers.join());

}

mm_initialise_ad_arrays();

mm_initialise_advertisers();

//alert(mmadvertisers.length.toString()+" advertisers total");
