function vimeoEmbed(w, h, clip) {
  var templ = '<iframe src="http://player.vimeo.com/video/%clip?title=0&amp;byline=0&amp;portrait=0" '
            + 'width="%width" height="%height" frameborder="0"></iframe>';
            
  return templ.replace(/%width/g, w)
              .replace(/%height/g, h)
              .replace(/%clip/g, clip);
}

$(window).load(function(){  
  var size = {w: 640, h: 448};
  $("a.videolink").click(function(e){
    // redirect to vimeo
    // if(iphone) return true;
    
    var href = $(this).attr("href");
    var id;
    if (href.indexOf("vimeo.com/") != -1)
      id = href.split("vimeo.com/")[1];

    var row = $(this).parent().parent();
    var video = $("<div class='videowrapper' style='width:"+size.w+"px'></div>");
    video.html(vimeoEmbed(size.w, size.h, id));
    video.prepend($("<a href='#' class='videoclose'>close</a>").click(function(){
      video.remove();
      row.children().fadeIn();
      row.css({height:null});
      return false;
    }));

    row.animate({height:size.h+45}).children().fadeOut("slow", function() {
      row.append(video);
      $.scrollTo(video, {duration:500, offset: -40});
    });
    return false;
  });
});


