// Globals Rotator
var rotatorOldImg = 0;
var rotatorFirstImg = 1;
var imgElements = 0;
var rotatorCount = 0;
var rotatorMovCount = 0;
var rotatorCurrent = 1;
var rotatorMovCurrent = 1;
var periodTimer = 0;
var homeRotatorActive = "orange";
var homeRotatorOld = "orange";
var homeRotatorAtWork = false;
var periodHomeRotatorTimer = 0;
var periodHomeRotatorTextTimer = 0;
window.addEvent('domready', function() {
	$$('div.blockRotator.green').addEvent('mouseover', function() {
		if (homeRotatorActive != "green"){	
			homeRotatorActive = "green";	
			$$('div.blockRotator div.rotatorArrow').removeClass('active');
			$$('div.green div.rotatorArrow').addClass('active');
			$clear(periodHomeRotatorTimer);			
			periodHomeRotatorTimer = nextHomeRotator.periodical(300); 
			$clear(periodHomeRotatorTextTimer);			
			periodHomeRotatorTextTimer = nextHomeTextRotator.periodical(700); 
		}
	});
	
	$$('div.blockRotator.orange').addEvent('mouseover', function() {
		if (homeRotatorActive != "orange"){	
			homeRotatorActive = "orange";	
			$$('div.blockRotator div.rotatorArrow').removeClass('active');
			$$('div.orange div.rotatorArrow').addClass('active');			
			$clear(periodHomeRotatorTimer);			
			periodHomeRotatorTimer = nextHomeRotator.periodical(300); 
			$clear(periodHomeRotatorTextTimer);			
			periodHomeRotatorTextTimer = nextHomeTextRotator.periodical(700); 
		}
	});
	
	$$('div.blockRotator.blue').addEvent('mouseover', function() {
		if (homeRotatorActive != "blue"){	
			$$('div.blockRotator div.rotatorArrow').removeClass('active');
			$$('div.blue div.rotatorArrow').addClass('active');
			homeRotatorActive = "blue";
			$clear(periodHomeRotatorTimer);			
			periodHomeRotatorTimer = nextHomeRotator.periodical(300); 
			$clear(periodHomeRotatorTextTimer);			
			periodHomeRotatorTextTimer = nextHomeTextRotator.periodical(700); 
		}
	});
	
	
	$$('div.blockRotator.red').addEvent('mouseover', function() {
		if (homeRotatorActive != "red"){	
			$$('div.blockRotator div.rotatorArrow').removeClass('active');
			$$('div.red div.rotatorArrow').addClass('active');
			homeRotatorActive = "red";
			$clear(periodHomeRotatorTimer);			periodHomeRotatorTimer = nextHomeRotator.periodical(300); 
			$clear(periodHomeRotatorTextTimer);			periodHomeRotatorTextTimer = nextHomeTextRotator.periodical(700); 
		}
	});

	
	if ( $$('div.introPhoto').length != 0 ){	
		imgElements = $$('div.rotatorImages img');
		rotatorCount = imgElements.length;
		
		movieElements = $$('div.rotatorMovies div');
		rotatorMovCount = movieElements.length;
		
		if (rotatorCount > 1){
			periodTimer = null;
			startRotator(imgElements);
		} 
		if (rotatorMovCount > 1){
			periodMovTimer = null;
			startMovRotator(movieElements);
		} 
	}
	setClickEvents();
	
	if (rotatorMovCount < 1 && rotatorCount < 1 && $$("div.introPhoto").length == 1){
		$$("div.introPhoto").setStyle('display', 'none');
	}
	else {
		if (rotatorMovCount < 1){
			$$("div.photoSwitch").setStyle('display', 'none');
		}
		if ( rotatorCount < 1){
			$$("div.photoSwitch").setStyle('display', 'none');
		}
	}
});

nextHomeRotator = function(){
	
		$$('div.rotatorHomeImage img').setStyle('z-index', '99');
		$$('div.rotatorHomeImage img').setStyle('opacity', 0);
		
		valueNewImage = "div.rotatorHomeImage img." + homeRotatorActive;
		$$(valueNewImage).setStyle('z-index', '100');
		
		valueTemp = "div.rotatorHomeImage img." + homeRotatorActive;
		valueTemp = $$(valueTemp);
		valueTemp.setStyle('opacity', '1');
		
		
}

nextHomeTextRotator = function() {
	if (homeRotatorActive != homeRotatorOld && homeRotatorAtWork === false){
			homeRotatorAtWork == true;
			$$('div.overlayText').setStyle('opacity', '0');
			$$('div.overlayText').setStyle('z-index', '99');
			
			textBlockValue = 'div.overlayText.' + homeRotatorOld;
			$$(textBlockValue).setStyle('z-index', '100');
			$$(textBlockValue).setStyle('opacity', '1');			
			homeRotatorOld = homeRotatorActive;	
				textBlockValue = $$(textBlockValue);
				self.fx = new Fx.Tween(textBlockValue[0] ,{
					duration: 200,
					onComplete: function() {
						
						textBlockActiveValue = 'div.overlayText.' + homeRotatorActive;
						textBlockActiveValue = $$(textBlockActiveValue);
						textBlockActiveValue.setStyle('z-index', '101');
						selffx = new Fx.Tween(textBlockActiveValue[0] ,{
							duration: 200,
							onComplete: function() {
								homeRotatorAtWork == false;
							}
						}).start('opacity', [0,1]);
					}
				}).start('opacity', [1,0]);			
		}
}

startRotator = function(imgElements) {
  imgElements.each( function(img, key){
    var temp = key + 1;
    if (temp == rotatorCurrent){
      img.setStyle('display','block');
    }
    else {
      img.setStyle('display','none');
    }
  });
  $clear(periodTimer);
  periodTimer = nextRotator.periodical(5000); 
}

function nextRotator() {    
	var tempValueCurrent = rotatorCurrent + 1;
	if (tempValueCurrent <= rotatorCount){
		rotatorCurrent ++;
	}
	else {
		rotatorCurrent = 1;
	}
  changeImage();
}

changeImage = function(){
    imgElements.each( function(img, key){
      var temp = key + 1;
      if (temp == rotatorCurrent){
        img.setStyle('display','block');
      }
      else {
        img.setStyle('display','none'); 
      }
    });
  $clear(periodTimer);
  periodTimer = nextRotator.periodical(5000); 
}

setClickEvents = function() {
	
	if (rotatorCount >= 1){
		if (rotatorCount > 1){
			$$('a.rotatorNext').addEvent('click', function() {
				if (rotatorCurrent < rotatorCount){
					rotatorCurrent = rotatorCurrent.toInt() + 1;
				}
				else {
					rotatorCurrent = 1;
				}
				changeImage();
				return false;
			});
			
			$$('a.rotatorPrev').addEvent('click', function() {
				picNr = rotatorCurrent - 1;
				if (rotatorCurrent == 1){
					rotatorCurrent = rotatorCount.toInt();
				}
				else {
					rotatorCurrent = rotatorCurrent.toInt() - 1;
				}
				changeImage();
				return false;
			});
		}
		else {
			$$('a.rotatorPrev').setStyle('display', 'none');
			$$('a.rotatorNext').setStyle('display', 'none');	
		}
		$$('a.photoSwitch').addEvent('click', function() {
			$clear(periodTimer);
			periodTimer = nextRotator.periodical(5000); 
			$$('a.photoSwitch').addClass('active');
			$$('a.movieSwitch').removeClass('active');
			$$('div.movies').setStyle('display', 'none');
			$$('div.photos').setStyle('display', 'block');
			return false;
		});
	}
	else { 
		$$('div.movies').setStyle('display', 'block');
		$$('div.photoSwitch a').setStyle('margin-left', '-15px');
		$$('a.photoSwitch').setStyle('display', 'none'); 
		$$('div.introPhoto div.photos').setStyle('display', 'none');
		$$('a.movieSwitch').addClass('active');
	}
	
	if (rotatorMovCount >= 1){
		if (rotatorMovCount > 1){
		$$('a.rotatorMovNext').addEvent('click', function() {
			if (rotatorMovCurrent < rotatorMovCount){
				rotatorMovCurrent = rotatorMovCurrent.toInt() + 1;
			}
			else {
				rotatorMovCurrent = 1;
			}
			
			changeMovImage();
			return false;
		});
		
		$$('a.rotatorMovPrev').addEvent('click', function() {
			picNr = rotatorMovCurrent - 1;
			if (rotatorMovCurrent == 1){
				rotatorMovCurrent = rotatorMovCount.toInt();
			}
			else {
				rotatorMovCurrent = rotatorMovCurrent.toInt() - 1;
			}
			changeMovImage();
			return false;
		});
		}
		else {
			$$('a.rotatorMovPrev').setStyle('display', 'none');
			$$('a.rotatorMovNext').setStyle('display', 'none');
		}
		$$('a.movieSwitch').addEvent('click', function() {
			$clear(periodTimer);
			$$('a.photoSwitch').removeClass('active');
			$$('a.movieSwitch').addClass('active');
			$$('div.movies').setStyle('display', 'block');
			$$('div.photos').setStyle('display', 'none');
			return false;
		});
	}
	else {
		$$('a.movieSwitch').setStyle('display', 'none'); 
		$$('a.photoSwitch').addClass('active'); 	
	}
	return false;
}

startMovRotator = function(imgElements) {
  movieElements.each( function(img, key){
    var temp = key + 1;
    if (temp == rotatorMovCurrent){
      img.setStyle('display','block');
    }
    else {
      img.setStyle('display','none');
    }
  });
  
}

function nextMovRotator() {    
	var tempValueCurrent = rotatorMovCurrent + 1;
	if (tempValueCurrent <= rotatorMovCount){
		rotatorMovCurrent ++;
	}
	else {
		rotatorMovCurrent = 1;
	}
  changeMovImage();
}

changeMovImage = function(){
    movieElements.each( function(img, key){
      var temp = key + 1;
      if (temp == rotatorMovCurrent){
        img.setStyle('display','block');
				tempHtmlValie = img.get('html');
				img.set('html', tempHtmlValie);
      }
      else {
        // Move old image down.
        img.setStyle('display','none'); 
				tempHtmlValie = img.get('html');
				img.set('html', tempHtmlValie);
      }
    });
}
