var isOn; // Checks if floating nav is still in state
function setState(state){
	isOn = state;
	destroyMenu();
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function isArray(obj) {
	var tmpArray = obj.toString().split(',');
	if(tmpArray.length > 1) return true;
	else return false;
}


function floatingMenu(imgId){
	var posX = (findPosX(document.getElementById(imgId)));// +20);
	var posY = (findPosY(document.getElementById(imgId))+18);// +10);
	
	
	var contents = '';
	var allLinks = new Array();

	// Get links
	switch(imgId){
		/* Example
		// Note. 3rd Parameter is optional, if set, target
		// is new window
		// ----------------------------------------
		case '':
			allLinks.push(['url','friendly link']);
			break;*/

		case 'contracts':
			allLinks.push(['/contracts/','Search for a Contract',0]);
			allLinks.push(['/jctcontracts/default.asp', 'JCT Contracts', 0]);
			allLinks.push(['/jct2011/', 'JCT Contracts 2011', 0]);
			allLinks.push(['/jctdisksubscription.asp', 'JCT Digital', 0]);
			allLinks.push(['/NEC-Contracts/default.asp', 'NEC Contracts', 0]);
			break;
			
		case 'ricsbooks':
			allLinks.push(['http://www.isurv.com','isurv - Building value from knowledge',1]);
			allLinks.push(['/cip/default.asp','Case in Point Guides',0]);
			allLinks.push(['/product_list.asp?pageTitle=Building+Surveying&list_id=9','Building Surveying',0]);
			allLinks.push(['/product_list.asp?pageTitle=Construction&list_id=10','Construction',0]);
			allLinks.push(['/product_list.asp?pageTitle=General+Practice&list_id=11','General Practice',0]);
			allLinks.push(['/hbr/default.asp', 'RICS Home Surveys', 0]);	
			allLinks.push(['/product_list.asp?pageTitle=Homebuyer+and+Home+Inspectors&list_id=12','Home Inspectors',0]);									
			allLinks.push(['/officialRicsProducts.asp','RICS Guidance Notes',0]);
      allLinks.push(['/product_list.asp?pageTitle=RICS+JCT+2005+Contract+Administration+Pads&list_id=17','RICS JCT 98 & 05 Contract Administration Pads',0]);	
			allLinks.push(['/redbook/default.asp','RICS Valuation Standards (The Red Book)',0]);
			allLinks.push(['/apc/guides.asp','APC - the guides',0]);
			break;
			
		case 'homeinspector':
			allLinks.push(['/home_dip.asp','For the Prospective Home Inspector',0]);
			allLinks.push(['/qualified_homeinspector.asp','For the Qualified Home Inspector',0]);			
			allLinks.push(['http://www.hcrwriter.com',"The RICS 'HCRwriter' Software",1]);						
			//allLinks.push(['',"isruv - home inspector",1]);									
			break;
			
		case 'apc_cpd':
			allLinks.push(['/apc/','APC Zone',0]);
			allLinks.push(['/apc/guides.asp','APC - the guides',0]);
			allLinks.push(['/cpd/','CPD Zone',0]);
			break;
			
		case 'professionalreference':
			allLinks.push(['/buildingregulations/','Building Regulations',0]);
			allLinks.push(['/searchResults.asp?searchMethod=category&pagetitle=Industry%20Best%20Practice,%20Standards%20and%20Regulations&subject_id=1119&bestpractice=1','SMM',0]);
			allLinks.push(['/searchResults.asp?searchMethod=category&pagetitle=Industry%20Best%20Practice,%20Standards%20and%20Regulations&subject_id=1120&bestpractice=1&intropage=1','Other Industry Reference',0]);						
			break;
			
		case 'other':
			allLinks.push('Products');
			//allLinks.push(['/equipment/default.asp', 'Equipment for Surveyors',0]);
			allLinks.push(['/giftware/','Giftware Collection',0]);
			//allLinks.push(['/giftware/basics.asp','Giftware Basics',0]);
			allLinks.push(['/bcis/','BCIS Products',0]);
			allLinks.push(['/product_list.asp?pageTitle=Special+Offers&list_id=7','Special Offers',0]); // Going to need asp in here ---REMOVE COMMENT AFTER
			allLinks.push(['/framingservice.asp','RICS Diploma Framing',0]);
			allLinks.push('Information');
			allLinks.push(['/new_account.asp','Credit Account',0]);
			allLinks.push(['/potential_authors.asp','Potential Authors',0]);
			allLinks.push(['/eNewsletter.asp','E-newsletter sign-up',0]);
			break;

		case 'scotland':
			allLinks.push(['http://www.rics.org','Royal Institution of Chartered Surveyors','target=_new']);
			allLinks.push(['http://hk.ricsbooks.com','RICS Hong Kong Bookshop','target=_new']);
			allLinks.push(['http://www.ricsbooks.com/scotland','RICS Scotland Bookshop','target=_new']);
			allLinks.push(['http://www.rics.org/bookshop', 'RICS London Bookshop', 'target=_new']);
			allLinks.push(['http://www.isurv.com','isurv','target=_new']);
			allLinks.push(['http://www.bcis.co.uk','BCIS','target=_new']);
			allLinks.push(['http://www.ricsrecruit.com','RICS Recruit','target=_new']);
			allLinks.push(['http://www.ricsfirms.com','RICS Firms ','target=_new']);
			allLinks.push(['http://www.rics.org/training','RICS Training','target=_new']);
			
			break;
			
		case 'specialOffers':
			allLinks.push(['/product_list.asp?pageTitle=Special offers&list_id=7', 'Special Offers',0]);
			allLinks.push(['/product_list.asp?pageTitle=Member offers&list_id=18','Member Offers',0]);
			allLinks.push(['http://www.ricsbooks.com/students/','Student Offers',0]);
			break;

			case 'standards':
			allLinks.push(['/product_list.asp?pageTitle=Special offers&list_id=7', 'Special Offers',0]);
			allLinks.push(['/product_list.asp?pageTitle=Member offers&list_id=18','Member Offers',0]);
			allLinks.push(['/product_list.asp?pageTitle=Student offers&list_id=19','Student Offers',0]);
			break;
			
		default:
			allLinks.push(['/index.asp','Home',0]);
			break;
	}

	// Set content
	contents = '<ul style="list-style: none;padding:0; margin:0;"">';
	for(var link=0; link < allLinks.length; link++){

		// Check for titles
		if(isArray(allLinks[link])){
			if(!link &&  link == (allLinks.length -1)) contents += '<li class="singleLink">';
			else if(!link) contents += '<li class="topLink">';
			else if(link == (allLinks.length -1)) contents += '<li class="bottomLink">';
			else contents += '<li>';

			contents += '<a href="'+ allLinks[link][0] +'"';
			if(allLinks[link][2]) contents += ' target="_blank"';
			contents += '>'+ allLinks[link][1] +'</a>';

			contents += '</li>';
		}
		else{
			contents += '<li  class="title">'+allLinks[link]+'</li>';
		}
	}
	contents += '</ul>';
	document.getElementById('floatingNavContents').innerHTML = contents;

	// Set menu left and right pos
	document.getElementById('floatingNav').style.display = 'block';
	document.getElementById('floatingNav').style.left = posX +'px';
	document.getElementById('floatingNav').style.top = posY +'px';

	setState(1);
}

function destroyMenu(clear){
	if(!isOn){
		if(parseInt(clear) > 0){
			document.getElementById('floatingNavContents').innerHTML = '';
			document.getElementById('floatingNav').style.display = 'none';
			document.getElementById('floatingNav').style.left = 0;
			document.getElementById('floatingNav').style.top = 0;
		}
		else setTimeout("destroyMenu(1)",500); // How long the nav menu stays open after mouseout
	}
}

