var array_categories = new Array(); // stores all the information to display the expand/collapse categories
var i, j;

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function getQueryVariable(variable) { 
var query = window.location.search.substring(1); 
var vars = query.split("&"); 
for (var i=0;i<vars.length;i++) { 
var pair = vars[i].split("="); 
if (pair[0] == variable) { 
return pair[1]; 
} 
} 
//Query Variable not found;
} 

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function removeItem(itemname) { 
    var itm = document.getElementById(itemname); 
    if (itm!=null) {
	itm.parentNode.removeChild(itm);
    }
} 

function getSiteArea(sitepath) {
if (sitepath=="") {
	return_value = window.location.search.substring(1);
} else {
	// set correct council specific and default site paths
	sitepath = sitepath.slice(1);
	var array_siteareas = sitepath.split('/');
	var current_level = array_siteareas.length;
	var bottom_sitearea = array_siteareas[current_level-1];
	var sitelibrary = array_siteareas[0];
	var council = getCookie('council');
	var library = getCookie('library');

	var path_cs=sitepath.replace(sitelibrary,council);
	var path_default=sitepath.replace(sitelibrary,library);
	
	if (bottom_sitearea.search(/sa-/i)!=-1) {
		// we're at the service area level so add overview to the query string to display by default
		var sitearea_abb=bottom_sitearea.replace('sa-','');
		return_value ='sitepathd='+path_default+'/'+ sitearea_abb+'-ov';
		return_value += '&sitepathcs='+path_cs+'/'+ sitearea_abb+'-ov';
	} else {
		if ((bottom_sitearea.search(/c_/i)!=-1) || (bottom_sitearea.search(/-tr/i)!=-1)) {
			// we're at the content or tools&resources level, return to the service area level only
			return_value ='sitepathd='+path_default.replace(bottom_sitearea,'');
			return_value +='&sitepathcs='+path_cs.replace(bottom_sitearea,'');
		} else {
			return_value ='sitepathd='+path_default;
			return_value += '&sitepathcs='+path_cs;
		}
	}
}
return return_value;
}

function getRelativeArea(target_sitearea) {
//just want to swap the highest level site area with a different value
var sitepath=getQueryVariable('sitepathd');
var array_sas=sitepath.split('/');
var level=array_sas.length;
var bot_sitearea = array_sas[level-1];
var target_sitepath=sitepath.replace(bot_sitearea,target_sitearea);
return target_sitepath;
}

function getMainSA(sitepath,strip_prefix) {
// Gets the site area component of the site path
// Depending on whether 'strip prefix is set', either returns the site area
// eg sa-ff = food premises - fixed
// Or the trimmed site area
// eg ff = food premises - fixed
var array_sas=sitepath.split('/')
for (var i=0;i<array_sas.length;i++) {
pos = array_sas[i].indexOf('sa-')
if (pos!=-1) {
if (strip_prefix!=null) {
return (array_sas[i]);
}else {
return (array_sas[i].slice(3));
}
}
}
}

function returnCorrectURL(link,file,body,description,video) {
//checks the content document and determines what has been populated - file, link or body
// added functinoality to open swf files in a new window
var srchstr='<a href="';
if (link!='') {
	urltype=1
} else if (file!='') {
	urltype=2
} else if (video!='' && video!=undefined) {
	urltype=3
} else {
	urltype=4
};
switch (urltype){
case 1: 
url='<a target="_blank" href="'+ link +'">'+ description +'</a>';
break;
case 2: 
url='<a target="_blank" href="'+ file +'">'+ description +'</a>';
break;
case 3:
url='<a href="'+ body +'" onclick="window.open(\'\', \'external\', \'toolbar=no, menubar=no, width=800, height=600, resizable=yes, menubar=no, status=no, scrollbars=no\')"  target="external">'+ description +'</a>';
break;
case 4:
url ='<a href="'+ body +'?'+getSiteArea('')+'">'+ description +'</a>';
break;
}
return(url);
}

function getNavURL(href,sitepath) {
	var querystring=getSiteArea(sitepath);
	window.location.href=href+'?'+querystring;
}

function gotoServices(council,library,base,path) {
setCookie("council", council, '',path);
setCookie("library", library, '',path);
return (base+path+'/'+council+'/services/c/eh?sitepathd='+library+'/services/c/eh&sitepathcs='+council+'/services/c/eh');
}

function processMenuItem(this_head,this_sub,this_bodid) {
	var hiddenfield=document.getElementById(this_bodid);
	if (hiddenfield != null) {
		removeBody (this_bodid); 	// remove from the array as well
		removeItem(this_bodid);	 	// remove hidden field
		removeItem(this_bodid+"Post");	 	// remove hidden field
	};
	groupCategories(this_head,this_sub,this_bodid);
}



function groupCategories(this_head,this_sub,this_bodid) {
	//checking to see if there is already a category setup for this header
	// we always have a header and body, subheader is optional
	if (checkForSubHeader (this_head, this_sub)) { // check for matching subheader first
		array_categories[i][j][array_categories[i][j].length] = this_bodid;
	 } else if (checkForHeader (this_head)) { // no subheader, check for matching header
		array_categories[i][array_categories[i].length] = new Array (this_sub,this_bodid);
	} else { // no header, create new category branch
		array_categories[array_categories.length] = new Array(this_head,new Array (this_sub,this_bodid));
	}
}

function checkForHeader (this_head) {
	for (var x=0;x<array_categories.length;x++) {
		if (this_head == array_categories[x][0]) {
			i=x; return true; break;
		}
	}
}

function checkForSubHeader (this_head, this_sub) {
	for (var x=0;x<array_categories.length;x++) {
		if (this_head == array_categories[x][0]) {
			for (var y=0;y<array_categories[x].length;y++) {
				if (this_sub == array_categories[x][y][0]) {
					i=x; j=y; return true; break;
				}
			}
		}
	}
}

function removeBody (this_bodyid) {
	for (var i=0;i<array_categories.length;i++) {
		for (var j=0;j<array_categories[i].length;j++) {
			if (isArray(array_categories[i][j])) {
				for (var k=0;k<array_categories[i][j].length;k++) {
					if (this_bodyid  == array_categories[i][j][k]){
						 array_categories[i][j].splice(k, 1);break;
					}
				} // k level - loops through the subheaders
				// check length of the subheader array - if there is only 1 element (the sub header) delete
				if (array_categories[i][j].length <=1) {
					array_categories[i].splice(j, 1);	break;		
				}
			}
		} // j level - loops through the headers
				if (array_categories[i].length <=1) {
					array_categories.splice(i, 1);break;		
				}
	} // i level - loops the highest level of the array
}

function writeCategories () {
	for (var i=0;i<array_categories.length;i++) {
		for (var j=0;j<array_categories[i].length;j++) {
			if (j==0) {
					var sectitle = 'hd'+(i);  // write the header
					document.write('<h5 class="categoryheader" onclick="expandcontent(this, \''+sectitle+'\')">');
							document.write('<span class="showstate"></span>'+array_categories[i][j] +'</h5>');
							document.write('<DIV class=switchcontent id='+sectitle+'>');
			} else {
				for (var k=0;k<array_categories[i][j].length;k++) {
					if (k==0) {
						if (array_categories[i][j][k] != '') {
							var sectitle = 'sb' + i.toString(10) + j.toString(10) + k.toString(10);  // write the subheader
							document.write('<h5 class="categorysubheader" onclick="expandcontent(this, \''+sectitle+'\')">');
							document.write('<span class="showstate"></span>'+array_categories[i][j][k] +'</h5>');
							document.write('<DIV class=switchcontent id='+sectitle+'>');
						}
					} else {
						var fi = document.getElementById(array_categories[i][j][k]);
						document.write(fi.value); // write the body content
					}
				} // k level - 1st element is subheader, rest are body ids
				if (array_categories[i][j][0] != '') document.write('</DIV>'); // closing div for header
			}
		} // j level - loops through the headers
		document.write('</DIV>'); // closing div for header
	} // i level - loops the highest level of the array
};
