// JavaScript Document
function slideBox(){

var cOpacity = .99;
var nOpacity = 0;
var rand=0;
var imgs = new Array();
var timing=4600;
var transition=.06;
var slideBox;

this.boxAndImages = function(){
  this.loop = [];
  this.slideBox = $('header_slide');
  var totalImg;
  $el(this.slideBox, '.slide').forEach(function(item,index){
    addClass(item,'show');
    imgs[index] = item; 
    totalImg = index+1;
  });
  this.total = totalImg; 
};

this.sendData = function(data){
  
  var xmlData = data.responseXML;
	var imgsSrc = xmlData.getElementsByTagName('src');
  var imgsClass = xmlData.getElementsByTagName('class');

	for (var i=0;i<imgsSrc.length;i++){
    imgs[i+this.total] = addElement('img',{
      'class':'slide image_'+imgsClass[i].firstChild.nodeValue,
      'opacity':0,
      'styles':{'display':'none'},
      'src':imgsSrc[i].firstChild.nodeValue
    })
	  this.slideBox.appendChild(imgs[i+this.total]);
	  this.all = i+this.total+1;
	}
	
	this.randOrder();
  if(i>0) this.nextImage(this.rand()); 
};

this.nextImage = function(numIm){

  
	var nextImage = new Image();
  addEvent(nextImage,'load',function(event,fnc) {
    
    //get the image with the same class
    var current = parseInt(imgs[numIm].className.split('_')[1]);

    var imgLeaveIndex = fnc.imgLeave;
    $el($('header_slide'),'img.image_'+current).forEach(function(item){
      if(hasClass(item,'show'))imgLeaveIndex = item;
    });
    fnc.imgLeave = imgLeaveIndex;
    addClass(imgs[numIm],'show');
    
    setOpacity(imgs[numIm],0);
    imgs[numIm].style.display = 'block';
    fnc.imgShow = imgs[numIm];
    
    cOpacity = 1;
    nOpacity = 0;
  
    setTimeout(function(){
      fnc.xFade();
    },timing);
    
  },this);
  nextImage.src = imgs[numIm].src;
};

this.xFade = function(){ 

  cOpacity-=transition;
	nOpacity+=transition;

  setOpacity(this.imgLeave,cOpacity);
 	setOpacity(this.imgShow,nOpacity); 
  
	if(nOpacity>.99){ 
       
    removeClass(this.imgLeave,'show');
    
		this.imgLeave.style.display = 'none';

    nextLoad = this.rand();
    
    this.nextImage(nextLoad); 
   
  }else{      
    setTimeout(function(fnc){
      return function(){
        fnc.xFade();
      }
    }(this),40);
  }

};

this.randOrder = function(){ 

  this.order = [];
  
  var limit = (this.total>this.all-this.total)? this.all-this.total : this.total;
  var firstLoop = [],firstLoop = [],totalLoop = [];
  
  firstLoop = this.alea(this.total,this.all);
  secondLoop = this.alea(0,limit);
  
  firstLoop.forEach(function(item){totalLoop.push(item)});
  secondLoop.forEach(function(item){totalLoop.push(item)});
  
  this.order = totalLoop;

};

this.alea = function(to,from){
  var randomArray = [],tmp;
  for (var i=to;i<from;i++){
    tmp='no';
    //loop if dont find no existing random 
    while(tmp=='no'){
      tmp = to+Math.floor(Math.random()*(from-to));
      randomArray.forEach(function(item){if(tmp == item)tmp = 'no';});
      if(tmp!='no')randomArray.push(tmp);
    }
  }
  return randomArray;
};


this.rand = function(){
  if(rand < this.order.length ){
    var temp = this.order[rand];
    rand++;
  }else{
    this.randOrder();
    var temp = this.order[0];
    rand=1;   
  }
  return temp;
};

}

