// Javascript resources for talbots.co.uk
// ensure this file is saved as UTF-8!

window.talbots={
  // generic event handler hook
  eventListener:
    function ( element, event, action ) {
      if (typeof element=='string') element=document.getElementById(element);

      if (window.addEventListener)
	element.addEventListener(event, action, false);
      else if (window.attachEvent)
	element.attachEvent('on'+event, action);
      else {
	var old=element['on'+event];

	element['on'+event]=
	  !old ? action :
	  function (e) {
            if (!e) e=window.event;
	    old(e, element);
	    return action(e, element);
          };

      }
    },

  // switch between tabs
  tab:
    function ( tab ) {
      document.getElementById('to'+tab).className='';
      tab=tab=='buy' ? 'rent' : 'buy';
      document.getElementById('to'+tab).className='hidden';
    },

  // rotate background promo image
  bgRotate:
    function () {
      if (++talbots.bgImage>6) talbots.bgImage=0;
      talbots.rotate.backgroundImage='url("resources/bg.'+talbots.bgImage+'.png")';
      setTimeout(arguments.callee, 10000);
    },

  // link demangling
  etacsufbo:
    function ( to, code, action ) {
      var decode=action || action==null;
      if (decode) to=decodeURIComponent(to);
      var count=to.length;
      var link=[];

      for (var i=0; i<count; i++)
	link.push(to.charCodeAt(i)^code.charCodeAt(i%code.length));

      link=String.fromCharCode.apply(window, link);

      if (action)
	document.location.href='mailto:'+link;
      else if (decode)
	document.write(link.replace(/@/, '﹫'));
      else
	return encodeURIComponent(link);

    },

  // page initialisation
  pageLoaded:
    function () {
      // preload images
      for (var i=1; i<6; i++) new Image().src='resources/bg.'+i+'.png';
      for (i=0; i<5; i++) new Image().src='resources/'+talbots.menu[i]+'.1.png';
      new Image().src='resources/to.rent.png';

      // setup and start promo image rotation
      talbots.rotate=document.getElementById('rotate').style;
      talbots.bgImage=0;
      setTimeout(talbots.bgRotate, 10000);
    },

  menu: [ 'buying', 'selling', 'renting', 'testimonials', 'contact' ],
  code: 'gtC6s2üAfMjUs*hjmblpaZ51dqA',
  text: '%0E%1A%25Y3F%C2%9D-%04%22%1E%26%5DI%07D%18%09'
};


talbots.eventListener(window, 'load', talbots.pageLoaded);

