/* direct home js MJF 13/03/2009 */


/* author: Simon Willisons - http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
function addLoadEvent(f){var o=window.onload;if(typeof window.onload!='function'){window.onload=f;}else{window.onload=function(){o();f();};}}

// jsCSS(action,object,class1,class2) -  className: swap, add, remove, check or toggle - author: Christian Heilmann - http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
function jsCSS(a,o,c1,c2){switch(a){case'swap':o.className=!jsCSS('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);break;case'add':if(!jsCSS('check',o,c1)){o.className+=o.className?' '+c1:c1;}break;case'remove':var rep=o.className.match(' '+c1)?' '+c1:c1;o.className=o.className.replace(rep,'');break;case'check':return new RegExp('\\b'+c1+'\\b').test(o.className);case'toggle':if(jsCSS('check',o,c1)){jsCSS('remove',o,c1);}else{jsCSS('add',o,c1);}break;}return false;}

/* author: mike foskett - http://webSemantics.co.uk/ */
function $id(id){return(document.getElementById(id)?document.getElementById(id):false);}
function idExists(id){return($id(id)?true:false);}
function replaceContent(id,content){if(idExists(id)){$id(id).innerHTML=content;}}
function isDom(){return (document.getElementById && document.getElementsByTagName)?true:false;}
function setOpacity(id,op){var o=$id(id).style;o.opacity=op/100;o.MozOpacity=op/100;o.KhtmlOpacity=op/100;o.filter="alpha(opacity="+op+")";}

/* author: Steffen Rusitschka - http://www.ruzee.com/blog/2006/07/retrieving-css-styles-via-javascript/ */
function hyphenToCamel(s){for(var exp=/-([a-z])/;exp.test(s);s=s.replace(exp,RegExp.$1.toUpperCase())){}return s;}
function getStyleProperty(id,property){
  // This function is not 100% generic for all css properties
  var obj=$id(id),value='';
  if(window.getComputedStyle){
    value=window.getComputedStyle(obj,null).getPropertyValue(property);
  }else{
    if(obj.currentStyle){
      value=obj.currentStyle[hyphenToCamel(property)];
    }
  }
  return value;
}

/*		Disjointed Cross-fading image rollover 
Parameters:
	id: id of disjointed image object,
	linkContainer: id of object containing the links,
	overlayID: id of the overlay to be created,
	spriteExt: extension of disjointed image sprite)
*/
function disjointedRollover(id,linkContainer,overlayID,spriteExt){
  if (idExists(id)){
    // get original bg image name, remove "url()" and for IE remove ""
    var newImg=getStyleProperty(id,'background-image').replace('url(','').replace(')','').replace(/"/g,'');

    // replace extension with .sprite.extension
    var ext=newImg.substring(newImg.lastIndexOf("."));
    newImg=newImg.replace(ext,spriteExt+ext);

    // preload rollover image and attach variables
    var img=new Image();
    img.overlayID=overlayID;
    img.spriteExt=spriteExt;
    img.disjointID=id;
    img.onload=function(){

      // create an overlay span for fading
      var sp=document.createElement('span');
      sp.id=img.overlayID;
      sp.className=$id(id).className;
      sp.style.backgroundImage="url("+img.src+")";

      // add overlay span
      $id(id).appendChild(sp);

      // image loaded so replace background image discreetly
	  $id('overlay').style.display="block";
	  setOpacity(img.overlayID,100);
      $id(id).style.backgroundImage="url("+img.src+")";
	  setOpacity(img.overlayID,0);

      var rollover=function(){
            var obj=$id(img.overlayID)

			// add link onclick to overlay
			if (this.onclick) {
				obj.onclick=this.onclick;
			// link has variables attached:
			// idName= id of div to create for popup (filename+anchor)
			// incName= url of page to ajax without anchor
			// contentDivId= id of div in ajax page to copy, default is "content"
				if (this.idName)
				{	
					obj.idName = this.idName;
				}
				if (this.incName)
				{	
					obj.incName = this.incName;
				}
				if (this.contentDivId)
				{	
					obj.contentDivId = this.contentDivId;
				}
			}
			

            // set overlay initial opacity and position (via class)
			setOpacity(img.overlayID,0);
            obj.className=this.id;

            // fade in overlay: adjust 101 for fade steps, currently there's 4.
            for(var i=25;i<101;i+=25){
              setTimeout("setOpacity('"+img.overlayID+"',"+i+")",i*2);
            }

            // set bg img position
            setTimeout("$id('"+img.disjointID+"').className='"+this.id+"'",i*2);

            // switch off overlay
            setTimeout("setOpacity('"+img.overlayID+"',0)",i*2);
      }

	  // add mouseover actions to links
      var liAs=$id(linkContainer).getElementsByTagName('a');
      for (var i=0;i<liAs.length;i++){
		// add onclick if one not present
		if (!liAs[i].onclick){
			liAs[i].onclick=function(){
				// action depends on whether link or banner clicked
				window.location=(this.id.substr(0,3)=="top")?this.href:$id(this.className).href;				
				return false;
			}
		}
        liAs[i].onmouseover=rollover;
        liAs[i].onfocus=rollover;
      }

      // main graphic acts as link. Initialise as first link.
      sp.onclick=$id(linkContainer).getElementsByTagName('a')[0].onclick;
    }
    img.src = newImg;
  }
}


/* Links with rel="external" get launched into new window */
function externalLinks(id){
if (document.getElementsByTagName){
	var anchors=(idExists(id))?$id(id).getElementsByTagName("a"):document.getElementsByTagName("a");
	for (var i=0;i<anchors.length;i++){
		var anchor=anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel")=="external"){
			anchor.target="_blank";
			anchor.title="Launches in new window";
}	}	}	}



function setup(){
/*
// Note: Pop-up, if used as part of disjointedrollover, must precede disjointedRollover call
	// pop-up for t&c's
	if (idExists('blue')){
		var mypopup1 = new popup("blue","top1","show","close","/p/i/directhome1/close.gif");
	}

	if (idExists('tc2')){
		var mypopup2 = new popup("tc2","tcLink2","show","close","/p/i/directhome1/close.gif");
	}
*/
	disjointedRollover('sectionTop','sectionTop','overlay','.sprite');

	externalLinks();
}

// required to prevent unstyled content flashing up as JS kicks in
document.documentElement.className="hasJS";
if (isDom()){addLoadEvent(setup)};