jQuery(document).ready(function($){
  "use strict";

  var embedLabel = "<br/><p class='control-label'>Embed code:</p>";
  var embedContainerCSS = "<style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; height: auto; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style>";
  var embedGettyContainerCSSFront = "<style>.embed-container { position: relative; padding-bottom: ";
  var embedGettyContainerCSSBack = "%; height: 0; overflow: hidden; max-width: 100%; height: auto; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style>";
  var embedSquareContainerCSS = "<style>.embed-container {position: relative; padding-bottom: 100%; height: 0; overflow: hidden;} .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style>";
  var embedSquarishContainerCSS = "<style>.embed-container {position: relative; padding-bottom: 120%; height: 0; overflow: hidden;} .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style>";
  var embedContainerDivOpen = "<div class='embed-container'>";
  var embedContainerDivClose = "</div>";
  
  var previewLabel = "<p class='control-label'>Preview:</p>";
  var previewPrefix = "<div class='embed-container'>";
  var previewPrefixSquarish = "<div id='squarish' class='embed-container'>";
  var previewPrefixGettyFront = "<div id='getty' class='embed-container'";
  var previewSuffix = "</div>";

  function createYouTubeEmbed(url){
    var youtubeURL = url;
    if (youtubeURL.length > 28){
      var uri = youtubeURL;
      var queryString = {};
      uri.replace(
          new RegExp("([^?=&]+)(=([^&]*))?", "g"),
          function($0, $1, $2, $3) { queryString[$1] = $3; }
      );
      var youtubeID = (queryString['v']);
    } else {
      var youtubeID = youtubeURL.substring(16);
    }
    
    var youtubeEmbed = "<iframe src='http://www.youtube.com/embed/" + youtubeID + "' frameborder='0' allowfullscreen></iframe>";

    return embedContainerCSS + embedContainerDivOpen + youtubeEmbed + embedContainerDivClose;
    
  }

  function createVimeoEmbed(url){
    var vimeoURL = url;
    var protocol = vimeoURL.slice(0,5);
    if (protocol == "https"){
      //then the video is served via https, doy
      var vimeoID = vimeoURL.substring(18);
    } else {
      protocol = "http";
      var vimeoID = vimeoURL.substring(17);
    }
  
    var vimeoEmbed = "<iframe src='"+protocol+"://player.vimeo.com/video/" + vimeoID + "' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>";
    return embedContainerCSS + embedContainerDivOpen + vimeoEmbed + embedContainerDivClose;

  }

  function createDailymotionEmbed(url){
    var dailymotionURL = url;
    var m ;
    var dailymotionID = (m =dailymotionURL.match(new RegExp("\/video\/([^_?#]+).*?"))) ? m[1] : void 0;
    var dailymotionEmbed = "<iframe src='http://www.dailymotion.com/embed/video/" + dailymotionID + "' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>";
    return embedContainerCSS + embedContainerDivOpen + dailymotionEmbed + embedContainerDivClose;
  }
  
  function createGoogleMapsEmbed(url){
    var googlemapsURL = url;
    var escapediFrameURL = googlemapsURL.replace(/\"/g,'\'');
    var escapediFrameURLCode = escapediFrameURL.replace(/</g,'<');
    var escapediFrameURLCodeFinal = escapediFrameURLCode.replace(/>/g,'>');

    return embedContainerCSS + embedContainerDivOpen + escapediFrameURLCodeFinal + embedContainerDivClose;

  }

  function createInstagramEmbed(){
    var instagramURL = $("#instagram-url").val();
    var shortinstagramURL = instagramURL.slice(5);
    var last_character = shortinstagramURL[shortinstagramURL.length-1];
    
    if (last_character!="/") {
      shortinstagramURL = shortinstagramURL + "/";
    }
    
    var instagramEmbedURL = "<iframe src='"+shortinstagramURL+"embed/' frameborder='0' scrolling='no' allowtransparency='true'></iframe>";
    $("#instagramembedCode").html( embedLabel + "<textarea rows='12' class='codebox'>" + embedSquarishContainerCSS + embedContainerDivOpen + instagramEmbedURL + embedContainerDivClose + "</textarea>");
    $("#instagrampreview").html( previewLabel + previewPrefixSquarish + instagramEmbedURL + previewSuffix );
    $("#squarish").css( "padding-bottom", "120%" );
    
  }

  function createVineEmbed(url){
    
    var vineURL = url;
    var shortvineURL = vineURL;
    var last_character = shortvineURL[shortvineURL.length-1];
    var vineScript= "<script async src='//platform.vine.co/static/scripts/embed.js' charset='utf-8'></script>";
  
    if (last_character!="/") {
      shortvineURL = shortvineURL + "/";
    }
    
    var vineEmbedURL = "<iframe width='100%' src='"+shortvineURL+"embed/simple' frameborder='0' scrolling='no' allowtransparency='true'></iframe>"+vineScript;

    return embedSquareContainerCSS + embedContainerDivOpen + vineEmbedURL + embedContainerDivClose;
    
  }
    
  function createGettyEmbed(){
    var gettyURL = $("#getty-url").val();
    var escapedGettyURL = gettyURL.replace(/\"/g,'\'');
    var escapedGettyURLCode = escapedGettyURL.replace(/</g,'<');
    var escapedGettyURLCodeFinal = escapedGettyURLCode.replace(/>/g,'>');
  
    $("#gettyplaceholder").html(gettyURL);
    

    var src = $("#gettyplaceholder").find("iframe").attr("src")
    var width = $("#gettyplaceholder").find("iframe").attr("width")
    var height = $("#gettyplaceholder").find("iframe").attr("height")
    var paddingBottom = ((height / width)*100);
            
    var previewPrefixGettyBack = " style='padding-bottom:" + paddingBottom + "% !important'>";
    
    $("#gettypreview").html( previewLabel + previewPrefixGettyFront + previewPrefixGettyBack + escapedGettyURL + previewSuffix );
    
    $("#gettyembedCode").html( embedLabel + "<textarea rows='12' class='codebox'>" + embedGettyContainerCSSFront + paddingBottom + embedGettyContainerCSSBack + embedContainerDivOpen + escapedGettyURLCodeFinal + embedContainerDivClose + "</textarea>");
  
  }
  
  function createGenericEmbed(){
    var genericURL = $("#generic-url").val();
    var escapediFrameURL = genericURL.replace(/\"/g,'\'');
    var escapediFrameURLCode = escapediFrameURL.replace(/</g,'<');
    var escapediFrameURLCodeFinal = escapediFrameURLCode.replace(/>/g,'>');

    //alert(escapediFrameURL);
    $("#genericembedNote").fadeIn();
    $("#genericembedCode").html( embedLabel + "<textarea rows='12' class='codebox'>" + embedContainerCSS + embedContainerDivOpen + escapediFrameURLCodeFinal + embedContainerDivClose + "</textarea>");
    $("#genericpreview").html( previewLabel + previewPrefix + escapediFrameURL + previewSuffix );

  }

  $("div[flo_video]").each(function(i, item) {
    var item = $(item),
        url = item.attr("flo_video"),
        vresource = url.replace('http://','').replace('https://','').replace("www.", "").split(/[/?#]/)[0].split(".")[0].replace(" ", "").toLowerCase();

    switch (vresource) {
      case "youtube" : 
        item.replaceWith(createYouTubeEmbed(url) );
      break;

      case "vimeo" :
        item.replaceWith(createVimeoEmbed(url) );
      break;

      case "dailymotion" :
         item.replaceWith(createDailymotionEmbed(url) );
      break;

      case "vine" :
        item.replaceWith(createVineEmbed(url) );
      break;
    }
  });
});

