rr_moveTo = function (obj, goal, step, time,value_callback, finish_callback) {
    goOn=false;
    if (obj.currTopPos>goal) {
	obj.currTopPos-=step;
	if(obj.currTopPos<=goal) {
	     obj.currTopPos=goal;
	} else {
	     goOn=true;
	}
    } else if (obj.currTopPos<goal) {
	obj.currTopPos+=step;
	if(obj.currTopPos>=goal) {
	     obj.currTopPos=goal;
	} else {
	     goOn=true;
	}
    }

    value_callback(obj.currTopPos, goOn);

    if (goOn) 
	 obj.rr_MoveTimeout=window.setTimeout(function(){rr_moveTo(obj, goal, step, time, value_callback, finish_callback);},time);
    else {
// 	if(obj.currTopPos<=0 && hideOnZero) obj.style.display="none";
	if(typeof finish_callback == "function") finish_callback(); 
	// rr_DC_moving = false;
	// if (rr_DC_scrollBuffer) rr_DC_documentScrolled();
    }
    
}


imgCarrousel_updateRotation = function() {
      var img=$("edximagecarrousel_content").childElements()[0].childElements();

      if(!$("edximagecarrousel_content").rotationStopped)
	$("edximagecarrousel_content").currRotation+=$("edximagecarrousel_content").rotationSpeed; 

      $("edximagecarrousel_content").currRotation+=360;
      $("edximagecarrousel_content").currRotation%=360;

      var maxH=$("edximagecarrousel_content").maxH;
      var maxW=$("edximagecarrousel_content").maxW;


       for(i=0;i<img.length; i++) {
	  img[i].style.tmpTop =((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg)+1.0)/2.0)*(($("edximagecarrousel_content").offsetHeight-maxH)))-(img[i].offsetHeight/2.0))+(maxH/2.0);
	  img[i].style.tmpLeft=((((Math.sin((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg)+1.0)/2.0)*(($("edximagecarrousel_content").offsetWidth -maxW)))-(img[i].offsetWidth /2.0))+(maxW/2.0);
 	  img[i].style.top=img[i].style.tmpTop+"px";
	  img[i].style.left=img[i].style.tmpLeft+"px";
	  img[i].style.zIndex=Math.round(((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg)+1.0)/2.0)*100.0);
	  $(img[i]).childElements()[0].style.width= Math.floor((img[i].childElements()[0].defWidth *(((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg))+1.0)/2.0)/10.0*5.0)+0.5)))+"px";
	  $(img[i]).childElements()[0].style.height=Math.floor((img[i].childElements()[0].defHeight*(((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg))+1.0)/2.0)/10.0*5.0)+0.5)))+"px";
	  $(img[i]).childElements()[1].style.width= Math.floor((img[i].childElements()[1].defWidth *(((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg))+1.0)/2.0)/10.0*5.0)+0.5)))+"px";
	  $(img[i]).childElements()[1].style.height=Math.floor((img[i].childElements()[1].defHeight*(((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg))+1.0)/2.0)/10.0*5.0)+0.5)))+"px";
	  $(img[i]).childElements()[0].setOpacity(((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg))+1.0)/2.0)/10.0*8.0)+0.2);
	  //$(img[i]).childElements()[1].setOpacity((((Math.cos((img[i].startRotation+$("edximagecarrousel_content").currRotation)*$("edximagecarrousel_content").oneDeg))+2.0)/2.0)-0.5);
	  var tmp=$(img[i]).cumulativeOffset();
	  $(img[i]).style.backgroundPosition=(-tmp[0])+"px "+(-tmp[1])+"px";
	  
       }

       if(!$("edximagecarrousel_content").rotationStopped)
        $("edximagecarrousel_content").rotationTimeout=window.setTimeout(function(){imgCarrousel_updateRotation();}, 60);

}

imgCarrousel_mousemove = function(e) {
    $("edximagecarrousel_content").rotationSpeed=(((e.pointerX()-$("edximagecarrousel_content").cumulativeOffset()[0])/$("edximagecarrousel_content").offsetWidth)*-6)+3;
}

imgCarrousel_clicked=function(e) {
    window.clearTimeout($("edximagecarrousel_content").rotationTimeout);
    $("edximagecarrousel_content").rotationStopped=true;

	if (typeof e!="undefined" && typeof e.type!="undefined") {
	    var img=Event.element(e).up();
	} else {
	    var img=e;
	}
	if(img.opened) return;
	if($("edximagecarrousel_content").openedImage!=false) {
	    imgCarrousel_closeImg(null, img);
	    return;
	}

//     var img=$("edximagecarrousel_content").childElements();
//     alert(img.length);
    imgPos=(($("edximagecarrousel_content").currRotation+img.startRotation)+360)%360;
     	img.topStart=img.offsetTop;
	img.leftStart=img.offsetLeft;
 	img.childElements()[0].heightStart=img.childElements()[0].offsetHeight;
	img.childElements()[0].widthStart=img.childElements()[0].offsetWidth;
 	img.childElements()[1].heightStart=img.childElements()[1].offsetHeight;
	img.childElements()[1].widthStart=img.childElements()[1].offsetWidth;
    if(imgPos!=0) {
      if(imgPos>180) {
	  if(imgPos+10<360) {
	      $("edximagecarrousel_content").currRotation+=10;
	  } else {
	      $("edximagecarrousel_content").currRotation=360-img.startRotation;
	  }
      } else {
	  if(imgPos-10>0) {
	      $("edximagecarrousel_content").currRotation-=10;
	  } else {
	      $("edximagecarrousel_content").currRotation=360-img.startRotation;
	  }
      }
    }



    if(($("edximagecarrousel_content").currRotation+img.startRotation)%360!=0) {
	$("edximagecarrousel_content").rotationTimeout=window.setTimeout(function(){imgCarrousel_clicked(img);}, 60);
    } else {
 	img.topStart=img.offsetTop;
	img.leftStart=img.offsetLeft;
 	img.childElements()[0].heightStart=img.childElements()[0].offsetHeight;
	img.childElements()[0].widthStart=img.childElements()[0].offsetWidth;
 	img.childElements()[1].heightStart=img.childElements()[1].offsetHeight;
	img.childElements()[1].widthStart=img.childElements()[1].offsetWidth;
	img.growPos=1;
 	imgCarrousel_growImage(img);
    }

    imgCarrousel_updateRotation();
}

imgCarrousel_shrinkImage = function(img, newImage) {
    $("edximagecarrousel_content").openedImage=img;
    img.growPos-=0.2;
    img.childElements()[0].style.height=(img.childElements()[0].heightStart*img.growPos)+"px";
    img.childElements()[0].style.width=(img.childElements()[0].widthStart*img.growPos)+"px";
    img.childElements()[1].style.height=(img.childElements()[1].heightStart*img.growPos)+"px";
    img.childElements()[1].style.width=(img.childElements()[1].widthStart*img.growPos)+"px";

    img.style.top=(($("edximagecarrousel_content").offsetHeight/2.0)-(img.childElements()[0].offsetHeight/2.0))+"px";
    img.style.left=(($("edximagecarrousel_content").offsetWidth/2.0)-(img.childElements()[0].offsetWidth/2.0))+"px";

    if(img.growPos>1) {
	$("edximagecarrousel_content").rotationTimeout=window.setTimeout(function(){imgCarrousel_shrinkImage(img, newImage);}, 60);
    } else {
	img.opened=false;
	$("edximagecarrousel_content").rotationStopped=false;
	$("edximagecarrousel_content").openedImage=false;
	if(newImage!=false) {
	   imgCarrousel_clicked(newImage);
	} else {
	  $("edximagecarrousel_content").rotationTimeout=window.setTimeout(function(){imgCarrousel_updateRotation();}, 60);
	}
    }
}


imgCarrousel_growImage = function(img) {
    img.opened=true;
    $("edximagecarrousel_content").openedImage=img;
    img.growPos+=0.2;
    img.childElements()[0].style.height=(img.childElements()[0].heightStart*img.growPos)+"px";
    img.childElements()[0].style.width=(img.childElements()[0].widthStart*img.growPos)+"px";
    img.childElements()[1].style.height=(img.childElements()[1].heightStart*img.growPos)+"px";
    img.childElements()[1].style.width=(img.childElements()[1].widthStart*img.growPos)+"px";

    img.style.top=(($("edximagecarrousel_content").offsetHeight/2.0)-(img.childElements()[0].offsetHeight/2.0))+"px";
    img.style.left=(($("edximagecarrousel_content").offsetWidth/2.0)-(img.childElements()[0].offsetWidth/2.0))+"px";

    if(img.growPos<2.15) {
	$("edximagecarrousel_content").rotationTimeout=window.setTimeout(function(){imgCarrousel_growImage(img);}, 60);
    } else {
	$("edximagecarrousel_infoBox_title").innerHTML=img.edx_title;
	$("edximagecarrousel_infoBox_description").innerHTML=img.edx_description;
	$("edximagecarrousel_infoBox").setOpacity(0);
	$("edximagecarrousel_infoBox").currTopPos=0;
	$("edximagecarrousel_infoBox").style.display="block";
	$("edximagecarrousel_infoBox").style.left=(img.offsetLeft+img.childElements()[0].offsetWidth-$("edximagecarrousel_infoBox").offsetWidth-10)+"px";
	$("edximagecarrousel_infoBox").style.top=(img.offsetTop+img.childElements()[0].offsetHeight-$("edximagecarrousel_infoBox").offsetHeight-10)+"px";

	$("edximagecarrousel_logo").setOpacity(0);
	$("edximagecarrousel_logo").currTopPos=0;
	$("edximagecarrousel_logo").style.display="block";
	$("edximagecarrousel_logo").style.left=(img.offsetLeft+img.childElements()[0].offsetWidth-$("edximagecarrousel_logo").offsetWidth-10)+"px";
	$("edximagecarrousel_logo").style.top=(img.offsetTop+10)+"px";

	rr_moveTo($("edximagecarrousel_infoBox"), 100, 10, 50, function(v, goOn){
								$("edximagecarrousel_infoBox").setOpacity(v/100);
						      });

	rr_moveTo($("edximagecarrousel_logo"), 100, 20, 50, function(v, goOn){
								$("edximagecarrousel_logo").setOpacity(v/100);
						      });

    }
}

imgCarrousel_closeImg = function(e,img) {
	rr_moveTo($("edximagecarrousel_infoBox"), 0, 10, 50, function(v, goOn){
								$("edximagecarrousel_infoBox").setOpacity(v/100);
							    },
							    function(v, goOn){
								$("edximagecarrousel_infoBox").display="none";
								if(typeof img=="undefined") img=false;
								imgCarrousel_shrinkImage($("edximagecarrousel_content").openedImage,img);
							     });

      rr_moveTo($("edximagecarrousel_logo"), 0, 10, 50, function(v, goOn){
								$("edximagecarrousel_logo").setOpacity(v/100);
							    },
							    function(v, goOn){
								$("edximagecarrousel_logo").display="none";
								if(typeof img=="undefined") img=false;
							     });

}

imgCarrousel_reflection = function(obj) {
	var reflection=new Element("canvas");
	var origImg=obj.childElements()[0];
	var origRefl=obj.childElements()[1];
 	obj.insert({bottom : reflection});
	reflection.width=origRefl.offsetWidth;
	reflection.height=origRefl.offsetHeight;

	if (!reflection.getContext)
	{
		return;			
	}
	cntx = reflection.getContext("2d");
// 	try {
// 		alert(reflection.tagName);
		var img= new Image(origRefl.offsetWidth, origRefl.offsetHeight);
		img.src=origRefl.src;
		img.hspace=0;
		img.vspace=0;
		cntx.save();
		cntx.drawImage(img, 0, 0, origRefl.offsetWidth, origRefl.offsetHeight);
		cntx.restore();
		cntx.globalCompositeOperation = "destination-out";
 		gradient = cntx.createLinearGradient(0, 0, 0, origRefl.offsetHeight);
 		gradient.addColorStop(0, "rgba(255, 255, 255, " + 0.5 + ")");
 		gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
 		cntx.fillStyle = gradient;
 		cntx.fillRect(0, 0, origRefl.offsetWidth, origRefl.offsetHeight);	
		Element.remove(origRefl); return;
// 	} catch(e) {			
// 		return;
// 	}
}

imgCarrousel_init = function() {
      var img=$("edximagecarrousel_content").childElements()[0].childElements();

      var rotVal=360/img.length;
      $("edximagecarrousel_content").oneDeg=(2.0*Math.PI)/360.0;
      $("edximagecarrousel_content").currRotation=0;
      $("edximagecarrousel_content").maxH=0;
      $("edximagecarrousel_content").maxW=0;
      $("edximagecarrousel_content").rotationSpeed=2;
      $("edximagecarrousel_content").rotationStopped=false;
      $("edximagecarrousel_content").openedImage = false;

      var infos = $("edximagecarrousel_infos").childElements();

      for(i=0;i<img.length; i++) {
	  img[i].style.display="block";
	  if(!Prototype.Browser.IE) {
	     imgCarrousel_reflection(img[i]);
	  }else{
	     $(img[i]).childElements()[1].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + 30 + ", style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=" + $(img[i]).childElements()[1].offsetHeight + ")";
	  }

	  img[i].startRotation=rotVal*i;
	  img[i].style.left=(((Math.sin(img[i].startRotation*$("edximagecarrousel_content").oneDeg))*200+100+($("edximagecarrousel_content").cumulativeOffset()[0]))-(img[i].offsetWidth/2))+"px";
	  img[i].style.backgroundImage="url(fileadmin/templates/intro/images/bg_start.jpg)";
	  img[i].childElements()[0].defWidth=img[i].childElements()[0].offsetWidth;
	  img[i].childElements()[0].defHeight=img[i].childElements()[0].offsetHeight;
	  img[i].childElements()[1].defWidth=img[i].childElements()[1].offsetWidth;
	  img[i].childElements()[1].defHeight=img[i].childElements()[1].offsetHeight;
	  img[i].opened=false;
	  img[i].edx_title=infos[i*2].innerHTML;
	  img[i].edx_description=infos[i*2+1].innerHTML;
	  if(img[i].offsetHeight>$("edximagecarrousel_content").maxH) $("edximagecarrousel_content").maxH=img[i].offsetHeight;
	  if(img[i].offsetWidth>$("edximagecarrousel_content").maxW) $("edximagecarrousel_content").maxW=img[i].offsetWidth;
	  Event.observe(img[i],"click",function(e) {imgCarrousel_clicked(e);}); 
      }

      $("edximagecarrousel_infos").remove();
      $("edximagecarrousel_content").style.height="340px";//($("edximagecarrousel_content").maxH*2)+"px";

      Event.observe($("edximagecarrousel_content"),"mousemove",function(e) {imgCarrousel_mousemove(e);}); 
      Event.observe($("edximagecarrousel_infoBox_close"),"click",function(e) {imgCarrousel_closeImg(e);}); 

      window.setTimeout(function() {imgCarrousel_updateRotation();}, 100);
  
}

  Event.observe(window,"load",function() {imgCarrousel_init();});
