var DDSPEED = 10;
var DDTIMER = 15;

/* manage fader on homepage bg */
var currentIndex = 0;
var imgHomeCollection = new Array('/img/home/bg_home_0.jpg','/img/home/bg_home_1.jpg', 
	'/img/home/bg_home_2.jpg', '/img/home/bg_home_3.jpg', '/img/home/bg_home_4.jpg','/img/home/bg_home_5.jpg');
	
	// preload the images
function preload(imgs) { 
  var tmp = null; 
  for (var j = 0; j < imgs.length; j++) { 
    tmp = imgs[j]; 
    imgs[j] = new Image(); 
    imgs[j].src = tmp; 
  } 
}
void(preload(imgHomeCollection));

// changement bg home
function switchHomeBg(index) {
	var div = document.getElementById('bgHome');
	if (div != null && imgHomeCollection != null) {
		if (index != currentIndex) {
			currentIndex = index;
			//$('log').innerHTML += 'SET | current : ' + index + '&nbsp;&nbsp;&nbsp;previous : ' + currentIndex + '<br />';
			
			var queue = Effect.Queues.get('menu');
			queue.each(function(e) { e.cancel() });
			
			if (navigator.appName.indexOf('Microsoft') > -1) {
				div.style.filter = 'alpha(opacity=10)';
			}
			else
				div.style.opacity = 0.1;
			div.style.background = 'url('+imgHomeCollection[index].src+')';
			new Effect.Opacity('bgHome', { queue: { from: 0.1, to :1.0, duration: 4, position: 'end', scope: 'menu' }});
		}
	}
}

function restoreHomeBg() {
	var div = $('bgHome');
	
	if (div != null) {
		if (currentIndex > 0) {
			currentIndex = 0;
			//$('log').innerHTML += 'RESTORE | current : ' + 0 + '&nbsp;&nbsp;&nbsp;previous : ' + currentIndex + '<br />';
			
			var queue = Effect.Queues.get('menu');
			queue.each(function(e) { e.cancel() });
			
			if (navigator.appName.indexOf('Microsoft') > -1) {
				div.style.filter = 'alpha(opacity=10)';
			}
			else
				div.style.opacity = 0.1;
				
			div.style.background = 'url('+imgHomeCollection[0].src+')';
			new Effect.Opacity('bgHome', { queue: { from: 0.1, to :1.0, duration: 4, position: 'end', scope: 'menu' }});
		}
	}
}

/* end manage fader on homepage bg */


// main function to handle the mouse events //
function ddMenu(id,d,index){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1,index)},DDTIMER);
    
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
    restoreHomeBg();
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1, 0)},DDTIMER);
  
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d,index){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
    switchHomeBg(index);
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}