// regional mouse over; 
(function(){
var regional =  [
    { name: 'North East', link: 'Region.aspx?rgid=4'}, 
    { name: 'North West', link: 'Region.aspx?rgid=5'}, 
    { name: 'Yorkshire and Humber', link: 'Region.aspx?rgid=9'}, 
    { name: 'East Midlands', link: 'Region.aspx?rgid=2'}, 
    { name: 'West Midlands', link: 'Region.aspx?rgid=8'}, 
    { name: 'South West of England', link: 'Region.aspx?rgid=7'}, 
    { name: 'South East of England', link: 'Region.aspx?rgid=6'}, 
    { name: 'East of England', link: 'Region.aspx?rgid=1'}, 
    { name: 'London', link: 'Region.aspx?rgid=3'},
    { name: 'Scotland', link: 'Region.aspx?rgid=10'}, 
    { name: 'Wales', link: 'Region.aspx?rgid=11'}
    ];

var getOffset = function (e) { 
	var x = 0, y = 0; 
	do { 
		y += e.offsetTop || 0;
		x += e.offsetLeft || 0;
		e = e.offsetParent;
	} while (e); 
	return [x,y]; 
}


    
event_install(window,"onload", function() { 
    
    var regions = document.getElementById('regions'); 
    
    event_install(regions, "onmouseout", function(){ 
        if (!this._timer) 
        {
            this._timer = setTimeout('hideMenu()', 300); 
        }
    }); 
    
    event_install(regions, "onmouseover", function(){ 
        if (this.visible) 
            return false; 
        
        clearTimeout(this._timer);
        this._timer = 0; 
        
        if (!this._menu) 
        {
            var res = []; 
            for (var i = 0; i < regional.length; i++) 
            {
                var item  = regional[i]; 
                res.push('<li><a href="' + (item.link || 'javascript:void(0)') + '" title="' + item.name + '">' + item.name + '</a></li>'); 
            }

            this._menu = document.createElement('div'); 
            this._menu.className = 'menu'; 
            this._menu.innerHTML = '<ul>\n' + res.join('\n') + '\n</ul>'
            
            var self = this; 
            event_install(this._menu, "onmouseover", function(){ 
                clearTimeout(self._timer);
                self._timer = 0;    
            }); 
            event_install(this._menu, "onmouseout", function(){ 
                  if (!self._timer) {
                    self._timer = setTimeout('hideMenu()', 300); }
            }); 
            
            this.visible = false; 
        }
        
        var offset = getOffset(this); 
        
        document.body.appendChild(this._menu); 
        
        this._menu.style.position = 'absolute'; 
            
        this._menu.style.left = offset[0] + 'px';
        this._menu.style.top = (offset[1] + this.clientHeight) + 'px';
        
        this.visible = true;        
    }); 
    

    var anchors = document.getElementsByTagName("a"); 
    
    for (var i = 0; i < anchors.length; i++)
    {
        var anchor = anchors[i]; 
        
        if (anchor.className && anchor
            .className.match(/img-roller/)) 
        {
            var image = anchors[i].getElementsByTagName('img')[0];
            image.i_src = image.src; 
            image.o_src = image.src.replace(/\/(\w+)\.(.\w+)$/, '/$1_over.$2'); 
            
            event_install(anchors[i], "onmouseover", function(){ 
                var image = this.getElementsByTagName('img')[0];
                image.src = image.o_src;                 
            }); 


            event_install(anchors[i], 'onmouseout', function(){     
                var image = this.getElementsByTagName('img')[0]; 
                image.src = image.i_src;
            }); 
          
        }
    }
}); 

})();

    var hideMenu = function() 
    {
        var regions = document.getElementById('regions'); 
        
        if (regions.visible) 
        {
            regions.visible = false; 
            document.body.removeChild(regions._menu); 
            
            clearTimeout(regions._timer); 
            
            regions._timer = 0; 
        }   
    }
    
    

var win; 
function newwindow(mypage,myname,w,h,scroll,top){

	var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
if(win && !win.closed && win.location){win.location.href=mypage;}

	settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=no,location=no,status=no,menubar=no,resizable=yes,dependent=no'
	win=window.open(mypage,myname,settings)
	if (window.focus) {win.focus()}
	
	return win;
}

function event_install(obj,event_name,f){
	if (obj.addEventListener){	//W3C
		obj.addEventListener(event_name.substr(2), f,false);
	}else if (obj.attachEvent){	 //IE 
		obj.attachEvent(event_name,function(){f.apply(obj); });
	}
	
	
}
//#####################################
// cookie functions
//#####################################

/**
detect if cookies are available
*/
function cookies_available(){

	var use_cookies=false;

	//use browser defined value if available
	if (typeof navigator.cookieEnabled=="undefined"){ 
		if (!document.cookie){
			//try a test cookie
			document.cookie="testcookie";
			use_cookies=(document.cookie=="testcookie");
			
			//erase dummy value
			document.cookie=""; 
		}else{
			//if there is a cookie then cookies work
			use_cookies=true;
		}
	}else{
		use_cookies =navigator.cookieEnabled;
	}

	return (use_cookies);

}

/**
determine whether to use cookies based on availability and 
persist_force_frameset setting
*/
function cookies_use(){
	return (cookies_available() && !CTAD_SETTINGS["persist_force_frameset"]);
}


/**
Cookie class from rhino book doctored by jh to use cookies more efficiently
a single key/value pairs string is created	which gets split up into cookie-sized chunks
All the predefined properties of this object begin with '$'
to distinguish them from other properties which are the values to
be stored in the cookie.

@argument document:		Required Document object value.
@argument prefix:		Required string which all the cookies made by this object 
				will start with.
@argument days:			Optional number that specifies the days from now
				that the cookie should expire.
@argument path:			Optional string that specifies the cookie path attribute.
@argument domain:		Optional string that specifies the cookie domain attribute.
@argument secure:		Optional Boolean value, if true, requests a secure cookie.
@argument max_size:		Optional max size of cookie
@argument max_cookies:	Optional max number of cookies

*/
function Cookie(document, prefix, days, path, domain, secure, max_size, max_cookies) {


  this.$document = document;
  this.$prefix = prefix;
	if (days){
		this.$expiration =  new Date((new Date()).getTime() + days * 86400000);
	}else{
		this.$expiration = null;
	}
  if (path){   this.$path   = path;   }else{ this.$path   = null;}
  if (domain){ this.$domain = domain; }else{ this.$domain = null;}
  if (secure){ this.$secure = true;   }else{ this.$secure = false;}
  if (max_size){ this.$max_size = max_size;   }else{ this.$max_size = 4000;}
  if (max_cookies){ this.$max_cookies = max_cookies;   }else{ this.$max_cookies = 20;}

//now doctor the max size based on the other stuff we are puting into each cookie
	
	var metadata= this.metadata();
	this.$max_size-=metadata.length;

}

/** 
Store method of cookie object

Loop through the properties of the Cookie object and
put together an array of cookie strings.

then create the cookie(s)
*/
Cookie.prototype.store=function() {  

this.$ar_cookie_strings=Array(); 
this.$cookie_index=0;


  for(var prop in this) {  // Ignore "$" properties, and methods
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) {
		    continue;
		}

		var pair=prop + ":" + escape(this[prop]);
		this.fill_cookie(pair);
 
   }
   

//for each string in the array we create a new cookie
	
	for (var i=0;i<this.$cookie_index+1;i++){
		this.create_one(this.$prefix+i,this.$ar_cookie_strings[i]);
	}

};

/**
Recursive function to fill up cookies as much as possible and then move on to next one.
creates $ar_cookie_strings
@argument pair key-value pair
*/
Cookie.prototype.fill_cookie=function(pair){


	//initialise array if needed
	if (!this.$ar_cookie_strings[this.$cookie_index]){
		this.$ar_cookie_strings[this.$cookie_index]="";
	}
	
	var current_cookie_length=this.$ar_cookie_strings[this.$cookie_index].length;
	
	//set up pair with ampersand if needed
	if (current_cookie_length!=0){
		pair="&"+pair;
	}
	
	// check to see that the key-value pair is not too long
		if (pair.length>this.$max_size){
			ctad_error('cookie: string not saved, it was too long: ' + pair.length + '/' + this.$max_size + ' bytes');
			return;
		}
		
	//if cookie prop and val would push us into the next cookie
       if ((current_cookie_length+pair.length)>this.$max_size){
		
			//check to see that we aren't out of cookies
			if (this.$cookie_index+2>this.$max_cookies){
				ctad_error('cookie: string not saved, we ran out of cookies');
				return;
			}
			
			//try the next cookie
			this.$cookie_index++;
			this.fill_cookie(pair);
		
     }else{
	
			//otherwise now add to the cookie array
			this.$ar_cookie_strings[this.$cookie_index]+=pair;
	}
};

/**
returns the cookie metadata
*/
Cookie.prototype.metadata=function(){
	var metadata = "";

	if (this.$expiration)
      metadata += '; expires=' + this.$expiration.toGMTString();
	if (this.$path) metadata += '; path=' + this.$path;
	if (this.$domain) metadata += '; domain=' + this.$domain;
	if (this.$secure) metadata += '; secure';
  
	return(metadata);
};

/**
creates one cookie
@argument name name of the cookie to make
@argument val value to store
*/
Cookie.prototype.create_one=function (name,val){

	var cookie = name + '=' + val;
	cookie+=this.metadata();

  this.$document.cookie = cookie;  // store with magic property
};

/**
get a list of all cookies that pertain to this document.
We do this by reading the magic Document.cookie property. 
*/
Cookie.prototype.load=function () {  
	var allcookies = this.$document.cookie;
	  if (allcookies == "") return false;
		
	var ar_cookies_all = allcookies.split(/;\s*/);
	var cookieval="";
	var i=0;
	var ar_pairs = Array();
	var ar_crumb = Array();

	for(i = 0; i < ar_cookies_all.length; i++) {
		var ar_cookie = ar_cookies_all[i].split("=");
		if (ar_cookie[0].substring(0,this.$prefix.length)==this.$prefix){
			cookieval=ar_cookie[1];
			ar_crumb=cookieval.split("&");
			ar_pairs=ar_pairs.concat(ar_crumb);
		}
	}


	/* Now that we've parsed the cookie value, set all the names and values
	of the state variables in this Cookie object. Note that we unescape()
	the property value, because we called escape() when we stored it.
	*/
  for(var i = 0; i < ar_pairs.length; i++) {
	var ar_pair=ar_pairs[i].split(":");
    this[ar_pair[0]] = unescape(ar_pair[1]);
  }

  // We're done, so return the success code.
  return true;
};

/**
remove a cookie
*/
Cookie.prototype.remove =function () {  
	var cookie;
	cookie = this.$name + '=';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

	this.$document.cookie = cookie;  // magic store
};






/** 
 * SWITCHER
 * manages the alternative style sheets. Sets, loads.. (inso faksto) 
 * requires: functions.js (Cookie object) 
 * 
 * @author ivarsv feat Badgers Inc. 
 * 
 * $Id: switcher.js,v 1.0 2006/08/27 15:38:29 ivarsv Exp $
 */ 
 


function switcher() 
{ 
}

switcher.init = function () 
{
	switcher.$style_sheet = new Cookie(document,"styleswitcher",null,"/");
	switcher.$style_sheet.load(); 
	switcher.load('textsize');
	
}
switcher.load = function () { 
    var stylesheet = switcher.$style_sheet['textsize_current'] || 'textsize_normal'; 
    var collection = document.getElementsByTagName("LINK"); 

    for (var i = 0; i < collection.length; i++) { 

        var title = collection[i].title || collection[i].getAttribute('title'); 
        if (title && title.match(/text size/i)) 
        {   
            collection[i].disabled = true; 
            var href = collection[i].getAttribute("href"); 
            if (href.match(new RegExp(stylesheet + ".css"))) {
                collection[i].disabled = false; 
            } else { 
                collection[i].disabled = true; 
            }
        }
    }

}

switcher.set = function (id, sheet, color) {
	if (typeof switcher.$style_sheet == "undefined") switcher.init(); 
	
	switcher.$style_sheet[id + '_current'] = sheet; 
	if (!cookies_available())		
		alert("StyleSwitcher works only with cookies"); 

	switcher.$style_sheet.store(); 
	switcher.load()
}

// install onload event 
event_install(window,"onload",switcher.init); 

