/* Copyright (c) 2008-2009 Smart Media Limited. All Rights Reserved */

var rotators = new Array();

function setupRotate()
{
  // Look for all divs with class rotateslogans
  var ds = document.getElementsByTagName("DIV");
  var dsl = ds.length;
  for( var i=0; i<dsl; i++)
  {
    if( (''+ds[i].className).indexOf('rotateslogans') >= 0 )
    {
      // Look for all DIVs with class containerslogan
      var ss = ds[i].getElementsByTagName("DIV");
      var ssl = ss.length;
      var slogans = new Array();
      for( var j=0; j<ssl; j++ )
      { 
        if( (''+ss[j].className) == 'rotateslogan' ||
            (''+ss[j].className).match(/ rotateslogan$/) != null ||
            (''+ss[j].className).match(/ rotateslogan /) != null ||
            (''+ss[j].className).match(/^rotateslogan /) != null )
        {
          slogans[slogans.length] = ss[j];	
        }
      }
      
      if( slogans.length > 1 )
      {
        // Only setup rotation if there is more than one slogan
        rotators[rotators.length] = {e:ds[i],slogans:slogans,current:0,slogantoggle:0};
        // Start timeout
        window.setTimeout('multiChooseSlogan('+(rotators.length-1)+')',8000);
      }
    }
  }
}

// Add onload event to trigger rotation to start
if( window.addEventListener )
  window.addEventListener('load',setupRotate,false);
else if( window.attachEvent )
  window.attachEvent('onload',setupRotate);


function multiChooseSlogan(rotatorindex)
{
  if( rotatorindex == null ) return;
  var rotator = rotators[rotatorindex];
  var ssl = rotator.slogans.length;
  if( ssl <= 1 ) return;

  // Pick a new slogan to display
  var newslogan = null;
  
  if( ssl == 2 )
    newslogan = (rotator.current+1)%2;
  else
  {
    while( newslogan == null || newslogan == rotator.current )
    {
      var rand = Math.random();
      while( rand == 1 )
      {
        rand = Math.random();
      }
      newslogan = Math.floor(rand*ssl+1);
    }
  }
  rotator.current = newslogan;
  //alert(ssl+' '+newslogan);
  
  // Create the fading slogan divs if neccessary
  var e1 = document.getElementById('rotator'+rotatorindex+'f1');
  if( e1 == null )
  {
    // Create element
    e1 = document.createElement('DIV');
    e1.style.display = 'none';
    e1.id = 'rotator'+rotatorindex+'f1';
    e1.className='rotateslogan';
    rotator.slogans[0].parentNode.insertBefore(e1,rotator.slogans[0]);
  }
  var e2 = document.getElementById('rotator'+rotatorindex+'f2');
  if( e2 == null )
  {
    // Create element
    e2 = document.createElement('DIV');
    e2.style.display = 'none';
    e2.id = 'rotator'+rotatorindex+'f2';
    e2.className='rotateslogan';
    rotator.slogans[0].parentNode.insertBefore(e2,rotator.slogans[0]);
  }  
  
  var e1c = document.getElementById('rotator'+rotatorindex+'captionf1');
  if( e1c == null )
  {
    // Create element
    e1c = document.createElement('DIV');
    e1c.style.display = 'none';
    e1c.id = 'rotator'+rotatorindex+'captionf1';
    e1c.className='rotateslogancaption';
    rotator.slogans[0].parentNode.insertBefore(e1c,rotator.slogans[0]);
  }
  var e2c = document.getElementById('rotator'+rotatorindex+'captionf2');
  if( e2c == null )
  {
    // Create element
    e2c = document.createElement('DIV');
    e2c.style.display = 'none';
    e2c.id = 'rotator'+rotatorindex+'captionf2';
    e2c.className='rotateslogancaption';
    rotator.slogans[0].parentNode.insertBefore(e2c,rotator.slogans[0]);
  }  

  var e = null;
  var ec = null;
  if( rotator.slogantoggle == 0 )
  {
    // First time.
    // In this case, copy over the original content to the first content
    e1.innerHTML = rotator.slogans[0].innerHTML;
    /*var cs = e1.getElementsByTagName('DIV');
    for( var i=0; i<cs.length; i++ )
    {
      if( cs[i].className == 'rotateslogancaption' )
      {
      	e1c.innerHTML = cs[i].innerHTML;
      	e1.removeChild(cs[i]);
      }
    }*/
    rotator.slogantoggle = 2;
  }
  
  if( rotator.slogantoggle == 1 )
  {
    e = e1;
    ec = e1c;
  }
  else
  {
    e = e2;
    ec = e2c;
  }
  
  if( e != null )
  {
    e.innerHTML = rotator.slogans[newslogan].innerHTML;
    /*var cs = e.getElementsByTagName('DIV');
    for( var i=0; i<cs.length; i++ )
    {
      if( cs[i].className == 'rotateslogancaption' )
      {
      	ec.innerHTML = cs[i].innerHTML;
      	e.removeChild(cs[i]);
      }
    }*/

  }
  
  window.setTimeout('multiFade('+rotatorindex+',0)',100);
}

function multiFade(rotatorindex,fadeparam)
{
  var faded = false;
  var rotator = rotators[rotatorindex];
  
  // Once image fully loaded then start fading
  var e1 = null;
  var e2 = null;
  var e1c = null;
  var e2c = null;
  if( rotator.slogantoggle == 1 )
  {
    e1 = document.getElementById('rotator'+rotatorindex+'f2');
    e2 = document.getElementById('rotator'+rotatorindex+'f1');
    e1c = document.getElementById('rotator'+rotatorindex+'captionf2');
    e2c = document.getElementById('rotator'+rotatorindex+'captionf1');
  }
  else
  {
    e1 = document.getElementById('rotator'+rotatorindex+'f1');
    e2 = document.getElementById('rotator'+rotatorindex+'f2');
    e1c = document.getElementById('rotator'+rotatorindex+'captionf1');
    e2c = document.getElementById('rotator'+rotatorindex+'captionf2');
  }
  // Fade between e1 and e2. Check that e2 image has completed loading
  var e2ready = true;
  var imgs = e2.getElementsByTagName('IMG');
  for( var i=0; i< imgs.length; i++ )
  {
    if( imgs[i].complete != true ) e2ready = false;	
  }
  
  if( e2ready )
  {
    // Whilst fading, use absolute positioning for the item being faded up
    e1.style.zIndex=1;
    e2.style.position = 'absolute';
    e2.style.left = '0px';
    e2.style.top  = '0px';
    e2.style.display = 'block';
    e2.style.zIndex = 2;
    e2.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity='+fadeparam+');';
    e2.style.opacity = fadeparam/100;

    if( e2c != null )
    {
      e2c.style.position = 'absolute';
      e2c.style.display = 'block';
      e2c.style.zIndex = 3;
      e2c.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity='+(fadeparam-10)+');';
      e2c.style.opacity = (fadeparam-10)/100;
    }

    if( e1c != null )
    {
      e1c.style.zIndex = 3;
      e1c.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity='+(90-fadeparam)+');';
      e1c.style.opacity = (90-fadeparam)/100;
    }

    if( fadeparam < 100 )
    {
      fadeparam += 2;
      if( fadeparam > 100 )
        fadeparam = 100;
    }
    else
      faded = true;
  }
  
  if( faded == true )
  {
    // Check that original image is hidden
    //if( rotator.slogans[0].style.display != 'none' )
    //  rotator.slogans[0].style.display = 'none';

    // Set to be static
    e1.style.display = 'none';
    //e1c.style.display = 'none';
    //e2.style.position = 'static';
    //e2c.style.position = 'static';
    
    //e1.className = (''+e1.className).replace(' nearlyfaded','');

    // switch toggle
    if( rotator.slogantoggle == 1 )
      rotator.slogantoggle = 2;
    else
      rotator.slogantoggle = 1;
    window.setTimeout('multiChooseSlogan('+rotatorindex+')',8000);
  }
  else
  {
    window.setTimeout('multiFade('+rotatorindex+','+fadeparam+')',50);
  }		
}



