var curr_section = '';
var curr_sub_section = '';
var ticker_duration = 0;
var rotation_images = null;
var index_currentPhoto = 0;
var index_lastPhoto = -1;
var transition_delay = 6500;
var transition_time = 1000;

function menuOver() {
	tar = this;
	while (tar.tagName != 'LI') {
		if (tar.getAttribute('id') == 'menu') {
			break;
		}
		tar = tar.parentNode;
	}
	if (tar.tagName == 'LI') {
		tar.addClass("sfhover");
	}
}

function menuOut() {
	tar = this;
	while (tar.tagName != 'LI') {
		if (tar.getAttribute('id') == 'menu') {
			break;
		}
		tar = tar.parentNode;
	}
	if (tar.tagName == 'LI') {
		tar.removeClass("sfhover");
	}
}

function rollOver() {
  if (!this.hasClass('selected')) {
    var SourceOfImage = this.getAttribute("src");
    var LetterToChange = ((SourceOfImage.length * 1) - 5);
    if (SourceOfImage.charAt(LetterToChange) == "s") {
      this.src = this.src.replace(/_s\./, '_o.');
      document.body.style.cursor='pointer';
    }
  }
}

function rollOut() {
  if (!this.hasClass('selected')) {
    var SourceOfImage = this.getAttribute("src");
    var LetterToChange = ((SourceOfImage.length * 1) - 5);
    if (SourceOfImage.charAt(LetterToChange) == "o") {
      this.src = this.src.replace(/_o\./, '_s.');
      document.body.style.cursor='default';
    }
  }
}
function slideTicker() {
  $('TickerSlider').effect('left', {duration: ticker_duration, onComplete:slideTicker, transition:Fx.Transitions.linear}).start(50, 0-$('TickerSlider').offsetWidth)
}

window.addEvent('domready', function() {
  var heightNeeded=window.getScrollHeight() - $('footer').scrollHeight - $('MenuHolderTop').scrollHeight - $('WhiteContentHolder').getStyle('padding-bottom').toInt();
  if (!isNaN(heightNeeded)) {
    if( typeof( window.innerWidth ) != 'number' ) {
      $('WhiteContentHolder').setStyle('height',heightNeeded+'px');
    }
    $('WhiteContentHolder').setStyle('minHeight',heightNeeded+'px');
  }

  new Asset.images([base_url+'images/menus/i_home_o.gif',
                    base_url+'images/menus/i_contact_o.gif',
                    base_url+'images/menus/emcsupersites_o.gif',
                    base_url+'images/menus/netrail_o.gif',
                    base_url+'images/menus/trivisions_o.gif',
                    base_url+'images/menus/posters_o.gif',
                    base_url+'images/menus/regionalqld_o.gif',
                    base_url+'images/menus/billboardmap_o.gif',
                    base_url+'images/menus/whybillboards_o.gif',
                    base_url+'images/menus/buyingbillboards_o.gif',
                    base_url+'images/menus/production_o.gif',
                    base_url+'images/menus/creativegallery_o.gif',
                    base_url+'images/menus/resources_o.gif',
                    base_url+'images/menus/aboutgoa_o.gif',
                    base_url+'images/menus/contact_o.gif'
                   ]);
  $$("#menu img").each(function(el) {
    el.addEvent('mouseenter', rollOver.bind(el) );
    el.addEvent('mouseleave', rollOut.bind(el) );
  });

  $$("#MenuHolderLeft img").each(function(el) {
    el.addEvent('mouseenter', rollOver.bind(el) );
    el.addEvent('mouseleave', rollOut.bind(el) );
  });

	if (window.ie6) {
		$$("#menu li ul").each(function(el) {
			el.addEvent('mouseenter', rollOver.bind($E('img', el.parentNode)));
			el.addEvent('mouseleave', rollOut.bind($E('img', el.parentNode)));
		});

		$$("#menu li").each(function(el) {
			el.addEvent('mouseenter', menuOver.bind(el));
			el.addEvent('mouseleave', menuOut.bind(el));
		});
	}

  if ($('Ticker')) {
    ticker_duration = Math.ceil(($('TickerSlider').offsetWidth+50)/25*1000);
    slideTicker();
  }

	rotation_images = $$('#IntroHeader .rotation_image');
  if (rotation_images.length) {
    setOpaque();
    setInterval ( "showNextBackground()", transition_delay );
  }

});

function setOpaque()
{
	var i = 0;
	rotation_images.each(function(el) {
		if (i != index_currentPhoto) {
			el.setOpacity(0);
		}
		i++;
	});
}

function showNextBackground() {
  var new_background = (index_currentPhoto+1 == rotation_images.length) ? 0 : (index_currentPhoto+1);
  changeBackground(new_background);
}

function changeBackground(index) {
	if (index_currentPhoto != index || index_lastPhoto == -1)
	{
		setOpaque();
		var effect1 = null;
    var effect2 = null;
		if (index >= index_currentPhoto) {
		  if (index_lastPhoto >= 0) {
	      effect2 = rotation_images[index_currentPhoto].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
	    }
	    effect1 = rotation_images[index].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
     }
		else {
	    effect1 = rotation_images[index].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
	    effect2 = rotation_images[index_currentPhoto].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
		}
    if (effect1 != null) {
      effect1.start(0,1);
    }
    if (effect2 != null) {
      effect2.start(1,0);
    }

    index_lastPhoto = index_currentPhoto;
		index_currentPhoto = index;
	}
}