function getWidth(newHeight,orginalWidth,originalHeight) {
  if(originalHeight == 0)return newHeight;
  var aspectRatio = orginalWidth / originalHeight;
  return newHeight * aspectRatio;
}
  
  function getPath(imageWidth, imageHeight) {
  
            return [
{left:"138px", top:"147px", height: imageHeight+60, width: getWidth(imageHeight+60,imageWidth,imageHeight),'z-index': '97'}, 
{left:"204px", top:"112px", height: imageHeight+50, width: getWidth(imageHeight+50,imageWidth,imageHeight),'z-index': '96'}, 
{left:"288px", top:"101px", height: imageHeight+40, width: getWidth(imageHeight+40,imageWidth,imageHeight),'z-index': '95'},
{left:"380px", top:"124px", height: imageHeight+30, width: getWidth(imageHeight+30,imageWidth,imageHeight), 'z-index': '94'}, 
{left:"452px", top:"148px", height: imageHeight+15, width: getWidth(imageHeight+15,imageWidth,imageHeight), 'z-index': '93'},
{left:"508px", top:"201px", height: imageHeight-10, width: getWidth(imageHeight-10,imageWidth,imageHeight), 'z-index': '92'}, 
{left:"529px", top:"225px", height: imageHeight-20, width: getWidth(imageHeight-20,imageWidth,imageHeight), 'z-index': '91'},
{left:"555px", top:"303px", height: imageHeight-40, width: getWidth(imageHeight-40,imageWidth,imageHeight), 'z-index': '90'},
{left:"589px", top:"365px", height: imageHeight-60, width: getWidth(imageHeight-60,imageWidth,imageHeight), 'z-index': '89'},
{left:"645px", top:"421px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '88'},
{left:"666px", top:"438px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '87'},
{left:"684px", top:"452px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '86'},
{left:"700px", top:"460px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '85'},
{left:"723px", top:"452px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '84'},
{left:"738px", top:"442px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '83'},
{left:"746px", top:"415px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '90'},
{left:"742px", top:"394px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '91'},
{left:"726px", top:"378px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '92'},
{left:"704px", top:"368px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '93'},
{left:"675px", top:"374px", height: imageHeight-80, width: getWidth(imageHeight-80,imageWidth,imageHeight), 'z-index': '94'},
{left:"649px", top:"391px", height: imageHeight-70, width: getWidth(imageHeight-70,imageWidth,imageHeight), 'z-index': '95'},
{left:"631px", top:"403px", height: imageHeight-60, width: getWidth(imageHeight-60,imageWidth,imageHeight), 'z-index': '96'},
{left:"578px", top:"430px", height: imageHeight-30, width: getWidth(imageHeight-30,imageWidth,imageHeight), 'z-index': '97'},
{left:"545px", top:"475px", height: imageHeight-15, width: getWidth(imageHeight-15,imageWidth,imageHeight), 'z-index': '98'},
{left:"486px", top:"512px", height: imageHeight, width: imageWidth, 'z-index': '99'},
{left:"452px", top:"542px", height: imageHeight+15, width: getWidth(imageHeight+15,imageWidth,imageHeight), 'z-index': '100'},
{left:"375px", top:"560px", height: imageHeight+30, width: getWidth(imageHeight+30,imageWidth,imageHeight), 'z-index': '101'},
{left:"300px", top:"560px", height: imageHeight+40, width: getWidth(imageHeight+40,imageWidth,imageHeight), 'z-index': '102'},
{left:"225px", top:"536px", height: imageHeight+50, width: getWidth(imageHeight+50,imageWidth,imageHeight), 'z-index': '103'}, 
{left:"150px", top:"503px", height: imageHeight+60, width: getWidth(imageHeight+60,imageWidth,imageHeight), 'z-index': '104'},
{left:"100px", top:"471px", height: imageHeight+70, width: getWidth(imageHeight+70,imageWidth,imageHeight), 'z-index': '105'},
{left:"39px",  top:"401px", height: imageHeight+80, width: getWidth(imageHeight+80,imageWidth,imageHeight), 'z-index': '106'},
{left:"-14px", top:"300px", height: imageHeight+130, width: getWidth(imageHeight+130,imageWidth,imageHeight), 'z-index': '107'},
{left:"37px", top:"228px", height: imageHeight+100, width: getWidth(imageHeight+100,imageWidth,imageHeight), 'z-index': '99'},
{left:"91px", top:"182px", height: imageHeight+70, width: getWidth(imageHeight+70,imageWidth,imageHeight),'z-index': '98'}];
  }

function animateMe($element, path, duration, position) {        
    var amount = path.length;
    if(position >= amount) position = amount;
    var originalPosition = position;
    
    for(position;position<amount;position++) {
      $element.animate(path[position],duration,'linear');
      if(position==amount-1 && originalPosition > 0) {
        amount = amount-originalPosition;
        position=0;
        originalPosition=0;
      }
    }
    if(amount < 1) amount = path.length;
    set_time_out($element.attr('id'), function() {animateMe($element, path, duration, position)}, amount*duration);
    
}

var timeout_handles = []    
function set_time_out( id, code, time ) {
    if(id in timeout_handles) {
      clearTimeout(timeout_handles[id]);
    }
    timeout_handles[id] = setTimeout(code, time);
}


$(window).load(function() {
    $("#loading").hide();
    $("#carousel").show();
    $("#carousel .animatedobject").each(function(index, value){
       $(this).click(function(){
         window.location.href = $(this).attr('rel');
       });
       animateMe($(this),getPath($(this).width(), $(this).height()),1500,index);
    });
});
