// nav 
currnav="";
function getactivepage(){ 
	//gets the name of the current page, compares it to the navigation array below and sets the active nav button to down.
	//this is the list of pages and the button number they reference...
	//you can have multiple pages in the same section, ie in a subnav.
	//maybe add page title, meta descriptions and keywords in asp/php array so that the code is generated at runtime...
	//breadcrumbs could be generated via the page title index in the array
	pages=new Array( 
		new Array("default","0","Home","#666",""),
		new Array("about_dladvantage","1","#B69147","content_bg_sand.jpg"),
		new Array("about_liveyourdream","1","#B69147","content_bg_sand.jpg"),
		new Array("about_company","1","#B69147","content_bg_sand.jpg"),
		new Array("lifestyle_investment","2","#CD8222","content_bg_pebbles.jpg"),
		new Array("opportunities_cradle","3","#996666","content_bg_star.jpg"),
		new Array("opportunities_cradle_pilanesberg","3","#996666","content_bg_star.jpg"),
		new Array("membership","4","#8F718E","content_bg_pebbles.jpg"),
		new Array("membership_survey","4","#8F718E","content_bg_pebbles.jpg"),
		new Array("developers","5","#669933","content_bg_sand.jpg"),
		new Array("news_index","6","#6B90A0","content_bg_star.jpg"),
		new Array("news_article","6","#6B90A0","content_bg_star.jpg"),
		new Array("faqs","7","#80463E","content_bg_pebbles.jpg"),
		new Array("contact","8","#729E5D","content_bg_star.jpg"),
		new Array("userlogin","9","#80463E","content_bg_star.jpg"),
		new Array("admin","10","#80463E","content_bg_star.jpg"),
		new Array("destinations","11","#8F718E","content_bg_pebbles.jpg")
	);
	currpage=window.location.href.match(/[^\/]+$/);
	try{
		currpage=currpage[0].match(/^[^\.]+/);
	}catch(e){
		currpage="default" //if there's no page in the url - home page
	}
	
	for(k=0;k<pages.length;k++){
		if(currpage==pages[k][0]){
			currnav="topnav"+pages[k][1];
			themecol=pages[k][3];
			o=document.getElementById(currnav);
			try{ //the home page will give an error as it doesn't have a nav button...
				swapimage(o.parentNode,'down') // set the active page button down
			}catch(e){} 
			// set colour theme of the page
			h=document.getElementsByTagName("h4"); // todo: deprecate and remove
			for(i=0;i<h.length;i++){
				h[i].style.color=themecol;
			}
			h=document.getElementsByTagName("h2");
			for(i=0;i<h.length;i++){
				h[i].style.color=themecol;
			}
			try{
				document.getElementById("footer").style.borderTopColor=themecol;
				document.getElementById("pagename").style.borderBottomColor=themecol;
				document.getElementById("content").style.backgroundImage="url(images/"+pages[k][4]+")";
			}catch(e){}
			return true;
		}
	}
}

function hidesubnav(hideid){
	document.getElementById(hideid).style.visibility='hidden';
}

function subnav(id,vis){
	d=document.getElementById(id);
	if(vis=='show'){
		try{
			window.clearTimeout(timer);
			document.getElementById(hideid).style.visibility='hidden'; //hide the previously active subnav regardless
		}catch(e){}
		d.style.visibility="visible";
	}else{
		hideid=id;
		timer=window.setTimeout("hidesubnav('"+hideid+"')",100);
	}
}

function swapimage(o,state){
	try{
		if(state=='over' && o.firstChild.name!=currnav){ //if this page isn't active, change the image state
			o.firstChild.src="images/"+o.firstChild.name+"_o.jpg";
			return true;
		}
		if(state=='down'){
			o.removeAttribute('href');
			o.firstChild.src="images/"+o.firstChild.name+"_d.jpg";
			return true;
		}
		else if(o.firstChild.name!=currnav){ //if this page isn't active, change the image state
			o.firstChild.src="images/"+o.firstChild.name+".jpg";
		}
	}
	catch(ex){}
}
function moreless(o,pnum){
// show or hide a certain amount of paragraphs above the read more / less button
	b=o.parentNode;
	//alert(pindex);
	for(i=0;i<pnum;i++){
		if(!b.previousSibling.tagName){
			b=b.previousSibling.previousSibling;

		}
		else{
			b=b.previousSibling;
		}
		try{
			b.style.display=(b.style.display=='none' || b.style.display=="undefined")?"block":"none";
			if(i==0){ //only toggle the morelessbutton on the first loop
				o.value=(b.style.display=='none' || !b.style.display=="undefined")?"Read more »":"Read less «";
			}
		}catch(e){
			//alert(e);
			return false;
		}
	}
}
function morelessinit(){
	o=document.getElementsByName("morelessbutton");
	for(j=0;j<o.length;j++){
		o[j].click();
	}
}

$(function(){
	getactivepage();
	morelessinit();
});
