function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 


function addEvent(o,t,fn){
	if( o.attachEvent ){
       		o['e'+t+fn] = fn;
	        o[t+fn] = function(){o['e'+t+fn](window.event);};
	        o.attachEvent('on'+t, o[t+fn]);
	}else{
       		o.addEventListener(t,fn,false);
	}
}

if( ! Array.push ){
 Array.prototype.push = function(v){
  this[ this.length ] = v;
  return this.length;
 };
}


var cr_boxes = [];
var halt_swap = false;
var timeout_id;
var content_1;
var content_0;

function swapBoxes(){
 if( ! halt_swap ){
  if( timeout_id ){
   clearTimeout( timeout_id );
   timeout_id = null;
  }
  content_0 = cr_boxes[0].innerHTML;
  content_1 = cr_boxes[1].innerHTML;
  // faders
  opacity( cr_boxes[0].id , 100, 0, 1000);
  opacity( cr_boxes[1].id , 100, 0, 1000);
  setTimeout( "clearContent()", 200 );
  setTimeout( "setContent()", 200 );
  opacity( cr_boxes[1].id , 0, 100, 1000);
  opacity( cr_boxes[0].id , 0, 100, 1000);
  timeout_id = setTimeout( "swapBoxes()", 5000 );
 }
}

function clearContent(){
 cr_boxes[0].innerHTML = "";
 cr_boxes[1].innerHTML = "";
}

function setContent(){
 cr_boxes[0].innerHTML = content_1;
 cr_boxes[1].innerHTML = content_0;
}

function haltSwap(){
 halt_swap = true;
 if( timeout_id ){
  clearTimeout( timeout_id );
 }
 timeout_id = null;
}

function startSwap(){
 halt_swap = false;
 if( ! timeout_id ){
  timeout_id = setTimeout( "swapBoxes()", 5000 );
 }
}

function initSwapBoxes(){
 // 
 var boxes = ['cp_tilda','cp_foroige'];
 for( box in boxes ){
  var el = document.getElementById( boxes[ box ] );
  if( el ){
   addEvent( el, "mouseover", haltSwap );
   addEvent( el, "mouseout", startSwap );
   cr_boxes.push( el );
  }
 } 
 if( cr_boxes.length ){
  timeout_id = setTimeout( "swapBoxes()", 5000 );
 }
}

addEvent( window, "load", initSwapBoxes );
