//make home page 

function sendarticle(pageURL){
	url = document.location.href;
	url = url.replace('&','xxxyyy');
	window.location	= '../COMMONmoduleASP/sendarticle.asp?url=' + url;
}

function makehome(makehome){
	if(!document.getElementById('makehome'))return false;
	var area='';
	area=document.getElementById('makehome');
	area.firstChild.onclick=function(){
		try {
				this.style.behavior='url(#default#homepage)';
				this.setHomePage('http://www.cgs.org.uk');
				throw "failure";
		}
			catch(oException) {
				alert('Set Home Page failed!\nWe were unable to change your homepage due to your browsers security settings.\nTry dragging the icon in your browsers address bar to the home icon to perform this action.');
				}
			return false;
	}
}


function is_iemac(){
	var SniffVer=navigator.appVersion;
	SniffVer=SniffVer.substring(0,4);
	var SniffClient=navigator.appName;
	var SniffOS=navigator.platform;
	if(SniffOS=='MacPPC' && SniffClient=='Microsoft Internet Explorer'){
			return true;
			}
			else{return false;}
}
//creates image popup
function imgPopup(url,alt){
	var openIpix='';
	var closeIpix='';
	var isIpix=false;
	urlExtension=url.split(".");
	if(urlExtension[urlExtension.length-1]=='ipx'){isIpix=true;}
	//else{alert('not an ipix, must be an image');}
                                        var imgPopup=window.open('','imgPopup','height=50,width=50,toolbar=no,scrollbars=no,resizable=yes,menubar=no,status=no,directories=no,location=no,50,50');
                                        imgPopup.document.open();
                                        imgPopup.document.writeln('<html><head><title>image</title> \n <style type="text/css"> \n a{color:#792549;} p#caption{background-color:#eeeeee;text-align: left;padding-left: 10px;padding-right: 10px;}  \n body {font-family: Tahoma, Arial, Helvetica, sans-serif;text-align: center;} \n p {font-size: 75%;margin-top: 0px;padding-top: 0.5em;padding-bottom: 1em;} \n img {display: block; margin-right: auto; margin-left: auto;} \n  p.close {text-align: center; margin-bottom: 0px;	width: 9em;	float: right;	color: #FFFFFF;	border: 2px solid #792549;	padding: 3px;	font-weight: bold;}  \n </style> \n </head> \n <body>');
										if(isIpix){
											imgPopup.document.writeln('<div id="lrgimage" style="width:450;height:360;border:solid;" > \n <embed src="' + url + '" width="450" height="360" border="0" ></embed> \n </div>');
											}else{
											imgPopup.document.writeln('<img src="' + url + '" alt="' + alt + '" id="lrgimage" />');
											}
										imgPopup.document.writeln(' \n <p id="caption" >' + alt + '</p><p class="close"><a href="javascript:window.close();">Close Window</a></p> \n </body> \n </html>');
										imgPopup.document.title=alt;
										var bigImage=imgPopup.document.getElementById('lrgimage');
                                        var captiontxt=imgPopup.document.getElementById('caption');
										//alert(bigImage.width);
                                        //captiontxt.style.color='#333333';
                                        //captiontxt.style.fontsize='70%';
                                        //1st resize for shitty explorer
                                                var newwidth=bigImage.width+60;
												if(!newwidth){newwidth=490;}
                                                var newheight=imgPopup.document.body.scrollHeight+40;
                                                imgPopup.resizeTo(newwidth,newheight);
                                                imgPopup.window.focus;
                                                
                                        bigImage.onload = function ()
                                        //for firefox
                                        {
                                                newwidth=bigImage.width+40;
                                                newheight=imgPopup.document.body.scrollHeight+60;
                                                imgPopup.resizeTo(newwidth,newheight);
                                                imgPopup.window.focus;
                                        }
										imgPopup.onblur=imgPopup.window.focus;
}

function docPopup(url){
						window.open(url,'popup');
}

function popupLinks(){
if(!document.getElementById)return false;
var aLinks=document.getElementsByTagName('a');
for (var i=0;i<aLinks.length;i++) {
	if (aLinks[i].className == 'popup')
			{
				aLinks[i].onclick = function ()
				{
					//alert(linktype);]
					//
					var linktype=this.getAttribute('href');
					var linktype_array=linktype.split(".");
					var linkex=linktype_array[linktype_array.length-1];
					//if(this.protocol=="file:"){
					if(linkex=='jpg' || linkex=='gif' || linkex=='ipx'){
						imgPopup(this.getAttribute('href'),this.getAttribute('title') );}
					else{docPopup(this.getAttribute('href'));}
				return false;
				}
				//return false;
			}
			aLinks[i].onkeypress = aLinks[i].onclick;
		}
}

function ReDraw(){
//needs attention
        var CurrentWidth=document.body.clientWidth;
        //alert(CurrentWidth);
        var NewWidth=CurrentWidth+1;
        //alert(NewWidth);
        document.body.style.width=NewWidth;
        document.body.style.width=CurrentWidth;
}
function setActiveStyleSheet(title){ 
var isiemac=is_iemac();
if(isiemac){return false;}
 var i, a, main;
  if (title) {
    for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
      if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title') ) {
       if(a.getAttribute('title')!='printstyle'){
		  // alert(a.getAttribute('title'));
	   		a.disabled = true;
	   	}
        if(a.getAttribute('title') == title && a.getAttribute('title')!='printstyle' ) a.disabled = false;
      }
    }
  }
ReDraw();
}

function getActiveStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
      if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title') && !a.disabled ) return a.getAttribute('title');
    }
    return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
    if(a.getAttribute('rel').indexOf('style') != -1
       && a.getAttribute('rel').indexOf('alt') == -1
       && a.getAttribute('title')
       )
       return a.getAttribute('title');
  }
  return null;
ReDraw();
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = '; expires='+date.toGMTString();
  }
  else expires = '';
  document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
  var nameEQ = name + '=';
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
        createCookie(name,'',-1);
}
//comment in to activate
	//window.onload = function(e) {
	//var cookie = readCookie('second-themeName');
  	//var title = cookie ? cookie : getPreferredStyleSheet();
  	//setActiveStyleSheet(title);
//}

window.onunload = function(e) {
	var title = getActiveStyleSheet();
  	createCookie('second-themeName', title, 365);
}

function changeTxtSize(direction){
		//alert('calling function correctly');
		var textIncrement=10;//percentage by which font size is increased
		if(document.body.style.fontSize==''){document.body.style.fontSize='100%';}
		if(direction=='dec'){textIncrement=-textIncrement;}
		//alert(textIncrement);
  		newFs=parseInt(document.body.style.fontSize)+textIncrement;
		createCookie('preferedFontSize', newFs,365);
		document.body.style.fontSize=newFs+'%';
		//alert(document.body.style.fontSize);
		
}


function swapTextTool(){
	
if(document.getElementById && !is_iemac()){
	var TC = document.getElementById('txtControls');
	//a.innerHTML="<a href=\"textsize.htm\">increase text size</a> | <a href=\"textsize.htm\">decrease text size</a>";
	var swappedtext='<h1>text controls</h1> ';
	swappedtext=swappedtext + '<div><h2><a href="javascript:setActiveStyleSheet(\'standard stylesheet\'),setDefaultFontSize()">restore default <em>text size and style</em></a></h2></div>';
	swappedtext=swappedtext + '<div id="tCsize">';
	swappedtext=swappedtext + '	<h2>Adjust font size:</h2>';
	swappedtext=swappedtext + '	<ul id="textsize">';
	swappedtext=swappedtext + '		<li><a href="javascript:changeTxtSize(\'dec\');" title="decrease font size">-<em>Decrease font size</em></a></li>';
	swappedtext=swappedtext + '		<li><a href="javascript:changeTxtSize(\'inc\');" title="increase font size" >+<em>Increase font size</em></a></li>';
	swappedtext=swappedtext + '	</ul>';
	swappedtext=swappedtext + '</div>';
	
	swappedtext=swappedtext + '<div id="tCstyle" >';	
	swappedtext=swappedtext + '	<h2>Adjust appearance:</h2>';
	swappedtext=swappedtext + '	<ul id="switchstyle">';
	swappedtext=swappedtext + '		<li><a href="javascript:setActiveStyleSheet(\'high contrast reversed\');" title="use high constrast stylesheet- dark text on light background" class="contrast1"  > T <em>high constrast (reversed)</em></a></li>';
	swappedtext=swappedtext + '		<li><a href="javascript:setActiveStyleSheet(\'high contrast\');" class="contrast2" title="use high constrast stylesheet- light text on dark background" > T <em>high constrast </em></a></li>';
	swappedtext=swappedtext + '		<li><a href="javascript:setActiveStyleSheet(\'null\');" class="null" title="remove site stylesheet - use browser default"> T <em>no site styles</em></a></li>';
	swappedtext=swappedtext + '		<li><a href="javascript:setActiveStyleSheet(\'standard stylesheet\');" class="default" title="use standard style sheet - view default appearance" > T <em>Default appearance</em></a></li>';
	swappedtext=swappedtext + '	</ul>';
	swappedtext=swappedtext + '</div>';
	//document.write(swappedtext);
	TC.innerHTML=swappedtext;
	}
}
 
function getFontSize(){
 	  var pFS = readCookie('preferedFontSize');
		if(pFS)	{document.body.style.fontSize=pFS +'%';	}
 }
 function setDefaultFontSize(){
	 	createCookie('preferedFontSize','',-365);
		{document.body.style.fontSize='100%';	}
 }
 function getstyle(){
	 var pSS = readCookie('second-themeName');
	 setActiveStyleSheet(pSS);
 }
function makesendtofriend(){
	if(!document.getElementById('sendtofriend'))return false;
	sl=document.getElementById('sendtofriend');
	sl.innerHTML="<a onclick='sendarticle()' href='#'>Send to Friend</a>";	
}
 /*<span id="top"><a href="#preheader" name="toplink">Back to top </a></span>  */

function getScroll() {
		
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    
  } else if( document.body && ( document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    
  } else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    
  }
  var scr0fY = document.documentElement.scrollTop; 
  //&& window.pageYOffset && /*document.body.scrollTop;*/
 // alert(scr0fY); alerts the value of amount scrolled
  {
	  displaylink();
	  sniffing();
  }
  function displaylink(){

	var mylink = document.getElementById("top");
	
	if (!document.getElementById("top")){
		return false
		}
	  var scr0fY = document.documentElement.scrollTop;
	  if (scr0fY > 20){
		  
		    var mylink = document.getElementById("top");
	  	 //alert("displaying the back to top link"); tells you the value is more than 20 and displays the link
		  mylink.style.display = 'block';
		  
	  } else
	  
	  var mylink = document.getElementById("top");
	  var scr0fY = document.documentElement.scrollTop;
	  	var position = 0;
	  if (scr0fY = 0){
	//alert("removing the back to top link"); tells you the value is 0 and removes the link
		  mylink.style.display = 'none';

	  }
  }
}
	  
	function sniffing(){
		
		if(navigator.appName == "Microsoft Internet Explorer"){
if (document.body.clientHeight > document.documentElement.clientHeight){
	if(!document.getElementById("top")){
		return false;
	}

				var accesstop = document.getElementById("top");
					accesstop.style.display = 'block';
			
			}
			else if(document.body.clientHeight < document.documentElement.clientHeight){
				
				var accesstop = document.getElementById("top");
				accesstop.style.display = 'none';
				
    
  }
				
				
		}
		
	}
	   
var totalwidth;


function is_iemac(){
        var SniffVer=navigator.appVersion;
        SniffVer=SniffVer.substring(0,4);
        var SniffClient=navigator.appName;
        var SniffOS=navigator.platform;
        if(SniffOS=='MacPPC' && SniffClient=='Microsoft Internet Explorer'){
                        return true;
                        }
                        else{return false;}
}



function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = '; expires='+date.toGMTString();
  }
  else expires = '';
  document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
  var nameEQ = name + '=';
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
        createCookie(name,'',-1);
}



function makescrollmenuwidth(scrollmenu){
	if(document.getElementById(scrollmenu)&& !is_iemac() ){
		var menu=document.getElementById(scrollmenu);
		var totalwidth=0;
		//alert("calling function");
		var aList=menu.getElementsByTagName('li');
		//var unOrlist=menu.getElementsByTagName('ul');
		var InstanceMargin=25;
		
		if(aList[0]){
		for (var i=0;i<aList.length;i++)
		{
			//alert(aList[i].width);
			//totalwidth=totalwidth+aList[i].idth;
			totalwidth=totalwidth+aList[i].clientWidth;
			totalwidth=totalwidth+InstanceMargin;
		}
		//alert(aList[1].parentNode);
		//alert(totalwidth);
		//alert(aList[1].parentNode.style.width);
		
			totalwidth=totalwidth+'px';
			aList[1].parentNode.style.width=totalwidth;
		
		//alert(aList[1].parentNode.style.Width);
		}
	}
}



function imgPopup(url,alt){
        var openIpix='';
        var closeIpix='';
        var isIpix=false;
        urlExtension=url.split(".");
        if(urlExtension[urlExtension.length-1]=='ipx'){isIpix=true;}
        //else{alert('not an ipix, must be an image');}
                                        var imgPopup=window.open('','imgPopup','height=50,width=50,toolbar=no,scrollbars=no,resizable=yes,menubar=no,status=no,directories=no,location=no,50,50');
                                        imgPopup.document.open();
                                        imgPopup.document.writeln('<html><head><title>image</title> \n <style type="text/css"> \n a{color:#3f3479;} p#caption{background-color:#eeeeee;text-align: left;padding-left: 10px;padding-right: 10px;}  \n body {font-family: Tahoma, Arial, Helvetica, sans-serif;text-align: center;} \n p {font-size: 70%;margin-top: 0px;padding-top: 0.5em;padding-bottom: 1em;} \n img {display: block; margin-right: auto; margin-left: auto;} \n  p.close {text-align: center; margin-bottom: 0px;        width: 9em;        float: right;        color: #FFFFFF;           padding: 3px;        font-weight: normal;}  \n </style> \n </head> \n <body>');
                                                                                if(isIpix){
                                                                                        imgPopup.document.writeln('<div id="lrgimage" style="width:450;height:360;border:solid;" > \n <embed src="' + url + '" width="450" height="360" border="0" ></embed> \n </div>');
                                                                                        }else{
                                                                                        imgPopup.document.writeln('<img src="' + url + '" alt="' + alt + '" id="lrgimage" />');
                                                                                        }
                                                                                imgPopup.document.writeln(' \n <p id="caption" >' + alt + '</p><p class="close"><a href="javascript:window.close();">Close Window</a></p> \n </body> \n </html>');
                                                                                imgPopup.document.title=alt;
                                                                                var bigImage=imgPopup.document.getElementById('lrgimage');
                                        var captiontxt=imgPopup.document.getElementById('caption');
                                                                                //alert(bigImage.width);
                                        //captiontxt.style.color='#333333';
                                        //captiontxt.style.fontsize='70%';
                                        //1st resize for shitty explorer
                                                var newwidth=bigImage.width+60;
                                                if(!newwidth){newwidth=490;}
                                                var newheight=imgPopup.document.body.scrollHeight+40;
                                                imgPopup.resizeTo(newwidth,newheight);
                                                imgPopup.window.focus;
                                                
                                        bigImage.onload = function ()
                                        //for firefox
                                        {
                                                newwidth=bigImage.width+40;
                                                newheight=imgPopup.document.body.scrollHeight+60;
                                                imgPopup.resizeTo(newwidth,newheight);
                                                imgPopup.window.focus;
                                        }
										window.onload = function ()
                                        //for firefox
                                        {
                                                newwidth=bigImage.width+40;
                                                newheight=imgPopup.document.body.scrollHeight+60;
                                                imgPopup.resizeTo(newwidth,newheight);
                                                imgPopup.window.focus;
                                        }
                                                                                imgPopup.onblur=imgPopup.window.focus;
}

function docPopup(url){
                                                window.open(url,'popup');
}

function popupLinks(){
if(!document.getElementById)return false;
var aLinks=document.getElementsByTagName('a');
//alert('running script');
for (var i=0;i<aLinks.length;i++) {
        if (aLinks[i].className == 'popup')
                        {
                                aLinks[i].onclick = function ()
                                {
                                        //alert(linktype);]
                                        //
                                        var linktype=this.getAttribute('href');
                                        var linktype_array=linktype.split(".");
                                        var linkex=linktype_array[linktype_array.length-1];
										linkex=linkex.toLowerCase();
                                        //if(this.protocol=="file:"){
                                        if(linkex=='jpg' || linkex=='gif' || linkex=='ipx'){
                                                imgPopup(this.getAttribute('href'),this.getAttribute('title') );}
                                        else{docPopup(this.getAttribute('href'));}
                                return false;
                                }
                                //return false;
                        }
                        aLinks[i].onkeypress = aLinks[i].onclick;
                }
}

function loadcookiefunctions(){
	//stops scripts running whilst in full bb frame
	if(self.parent.frames.length != 0 || (!document.getElementById('scrollmenu')) ){return false;}
	//alert("im being called");
	makescrollmenuwidth('scrollmenu');
	 if(!document.getElementById('bblitebar'))
	 {
		 popupLinks();
		 makescrollmenuwidth('scrollmenu');
	 }
	 var prevpagescroll;
	prevpagescroll=readCookie('scrollmenu');
	//var lasttotalwidth=readCookie('lasttotalwidth');
	//alert('last'+lasttotalwidth+'  totalwidth'+totalwidth);
	if((prevpagescroll!='')&&(document.body.getAttribute('class')=='level2page' || document.body.className=='level2page' )){
		var scrollinggallery=document.getElementById('scrollmenu');
		scrollinggallery.scrollLeft=prevpagescroll;
		//alert(prevpagescroll);
	}
	else{
		if(readCookie('scrollmenu')){
		eraseCookie('scrollmenu');
		}
	}
		
	 
}

function unloadfunctions(){
if((document.body.getAttribute('class')=='level2page' || document.body.className=='level2page')){
	if(!document.getElementById('scrollmenu')){
	var leftdistance=0;
	}
	else{
//alert('unload page');
	var scrollinggallery=document.getElementById('scrollmenu');
	var leftdistance=scrollinggallery.scrollLeft;
	
	}
}
if(leftdistance || leftdistance==0 ){
createCookie('scrollmenu',leftdistance,365);
//alert("cookie created");
}
}
window.onload=loadfunctions;
window.onunload=unloadfunctions;
	 
function loadfunctions(){
	getFontSize();
	swapTextTool();
	//popupLinks();
	loadcookiefunctions();
	makescrollmenuwidth('scrollmenu');
	sniffing();
	getstyle();
	makesendtofriend();
	makehome('makemyhomepage');
 }
