var FadeInterval
var FadeItem = 0;
var homeImg;
var homeCopy;

function callNextBGNoSel(){
	homeImg[FadeItem-1].morph({opacity:0,duration:1});
	homeCopy[FadeItem-1].morph({opacity:0,duration:1});
	FadeItem = (FadeItem == homeImg.length) ? 1 : FadeItem + 1;
	homeImg[FadeItem-1].morph({opacity:1,duration:1});
	homeCopy[FadeItem-1].morph({opacity:1,duration:1});	
	if(FadeInterval){
		clearInterval(FadeInterval);
	}
	FadeInterval = setInterval(callNextBGNoSel,8000);
}

window.addEvent('domready', function() {
	if($('rotatingBG') != null){
		homeImg = $$('#contentA .img_cms');
		homeCopy = $$('#contentA .txt_cms');
		//alert(homeImg.length);
		if(homeImg.length > 0){
			FadeItem = Math.floor(Math.random() * homeImg.length) + 1;
			for(i=0;i<homeImg.length;i++){
				if( homeImg[i] && homeCopy[i] ){
					homeImg[i].removeClass('hidden');
					homeCopy[i].removeClass('hidden');
					if( (i+1) != FadeItem){
						homeImg[i].setStyle('opacity', 0);
						homeCopy[i].setStyle('opacity', 0);
					}
				}
			}
		}
	}
	if( $('interactMap') != null ){
		links = $$('#interactMap a');
		for(i=0;i<links.length;i++){
			if( links[i].title ){
				links[i].titleText = links[i].title;
				links[i].title = '';
				links[i].addEvent('mouseover', function(){
					rel = this.rel.split('|');
					newFormElm =  new Element('div');
					newFormElm.id = 'mapTip';
					newFormElm.addClass('mapTipBottom');
					newFormElm.set('html','<div class="mapTip">'+this.titleText+'</div>');
					newFormElm.setStyle('left', parseInt(rel[0])-87);
					newFormElm.setStyle('top', parseInt(rel[1])-90);
					this.parentNode.parentNode.grab(newFormElm);
				});
				links[i].addEvent('mouseout', function(){
					if( $('mapTip') != null ){
						$('mapTip').destroy();
					}
				});
			}
		}
		links = $$('#companyListItems a');
		for(i=0;i<links.length;i++){
			if( links[i].title ){
				links[i].titleText = links[i].title;
				links[i].title = '';
				links[i].addEvent('mouseover', function(){
					rel = this.rel.split('|');
					newFormElm =  new Element('div');
					newFormElm.id = 'mapTip';
					newFormElm.addClass('mapTipBottom');
					newFormElm.set('html','<div class="mapTip">'+this.titleText+'</div>');
					newFormElm.setStyle('left', parseInt(rel[0])-87);
					newFormElm.setStyle('top', parseInt(rel[1])-90);
					$('interactMap').grab(newFormElm);
				});
				links[i].addEvent('mouseout', function(){
					if( $('mapTip') != null ){
						$('mapTip').destroy();
					}
				});
			}
		}
	}
});

window.addEvent('load', function() {
	 if(FadeItem > 0 && homeImg[0]){
		FadeInterval = setInterval(callNextBGNoSel,8000);
	 }
});
