/**
 * Configuration
 */
// Define the sizes of 'small', 'medium' and 'large' videos
var video_size = {
  small  : [200,200],
  medium : [400,400],
  large  : [600,600]
};

// Define the CSS classes associated to the 'small', 'medium' and 'large' videos
var css_classes = {
  small  : 'video_small',
  medium : 'video_medium',
  large  : 'video_large'
};

// Define (as a CSS selector) the HTML element which contains the content. This is for
// performance, and must match the template. If in doubt, use 'body'
var content_selector = '#rhscol';

/**
 * Vimeo specific functions
 */
// This function returs false if this is not a viemo link,
// and the parse result (which evaluate to true) if this is one.
function parse_vimeo(url) {
  parts = url.match(/^(http:\/\/)?([^.]+\.)?vimeo\.com\/(\d+)$/)
  if (!parts) return false;
  return parts[3];
}

// This function returns the code to embed the video indicated by
// the 'parse' parameter. This must be the value returned by
// parse_vimeo
function embed_vimeo(parse, size) {
  var s = video_size[size];
  return '<object width="'+s[0]+'"' +
         ' height="'+s[1]+'"' +
         '><param name="allowfullscreen" value="true" />' +
         '<param name="allowscriptaccess" value="always" />' +
         '<param name="movie" value="http://vimeo.com/moogaloop.swf?' +
         'clip_id=' + parse + '&amp;server=vimeo.com&amp;show_byline=1' +
         '&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /> ' +
         '<embed src="http://vimeo.com/moogaloop.swf?clip_id=' + parse +
         '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;'+
         'show_portrait=0&amp;color=&amp;fullscreen=1" '+
         'type="application/x-shockwave-flash" allowfullscreen="true"' +
         ' allowscriptaccess="always" width="'+s[0]+'"' +
         ' height="'+s[1]+'"></embed></object>'; 
}

/**
 * Overstream specific functions
 */
// This function returns false if this is not an overstream link,
// and the parse result (which evaluates to true) if this is one.
function parse_overstream(url) {
  parts = url.match(/^(http:\/\/)?([^.]+\.)?overstream\.net\/view\.php\?oid=(.+)$/)
  if (!parts) return false;
  return parts[3];
}

// This function returns the code to embed the overstream video indicated by
// the 'parse' parameter. This must be the value returned by parse_overstream
function embed_overstream(parse, size) {
  var s = video_size[size];
  return '<object width="' + s[0] +'" height="' + s[1] + '">' +
         '<param name="movie" value="http://www.overstream.net/swf/player/oplx?oid=' +
         parse + '&noplay=1"></param>' + 
        '<param name="allowFullScreen" value="true"></param>' +
        '<embed src="http://www.overstream.net/swf/player/oplx?oid=' +
        parse + '&noplay=1" type="application/x-shockwave-flash"' +
        'width="' + s[0] + '" height="' + s[1] + '"' + 'allowFullScreen="true"></embed></object>';
}


/**
 * YouTube specific functions
 */
// This function returns false if this is not a youtube link,
// and the parse result (which evaluate to true) if this is one. 
function parse_youtube(url) {
  parts = url.match(/^(http:\/\/)?([^.]+\.)?youtube\.com(.*)$/)
  if (!parts) return false;
  id = parts[3].match(/(\?v=|\/v\/)(.*)$/);
  if (!id) return false;

  return 'http://www.youtube.com/v/'+ id[2];
}

// This function returns the code to embed the video indicated by
// the 'parse' parameter. This must be the value returned by
// parse_youtube
function embed_youtube(parse, size) {
  var s = video_size[size];
  return '<object width="'+s[0]+'"'+
         ' height="'+s[1]+'"'+
         '><param name="movie" value="'+parse+'"'+
         '></param><param name="allowFullScreen" value="true">'+
         '</param><embed src="'+parse+'" type="application/x-shockwave-flash" allowfullscreen="true"'+
         ' width="'+s[0]+'"'+
         ' height="'+s[1]+'"'+
         '></embed></object>'; 
}

/**
 * savethechildren video specific functions
 */
// This function returns false if the link is not a savethechildren video link,
// and the parse result (which evaluates to true) if this is one 
function parse_stc(url) {
 parts = url.match(/^(http:\/\/)?(files\.savethechildren\.net\/media\/movies\/(.*))$/);
 if (!parts) return false;

 return 'http://' + parts[2];
}

// This function returns the code to embed the video indicated by the 'parse' parameter
// This must be the value returned by parse_stc
function embed_stc(parse, size) {
  var s = video_size[size];
  parse = encodeURIComponent(parse);

  return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'+
         ' codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"'+
         '  width="'+s[0]+'" height="'+s[1]+'" id="player"'+
         ' align="middle">'+
         '<param name="allowScriptAccess" value="sameDomain" />'+
         '<param name="movie"'+
         ' value="https://www.savethechildren.net/media/flash/player.swf" />'+
         '<param name="quality" value="high" />'+
         '<param name="bgcolor" value="#ffffff" />'+
         '<param name="FlashVars"'+
         ' value="baseurl='+parse+'&startRes=high" />' +
         '<embed src="https://www.savethechildren.net/media/flash/player.swf"' +
         ' FlashVars="baseurl='+parse+'&startRes=high" quality="high"'+
         ' bgcolor="#ffffff" width="'+s[0]+'" height="'+s[1]+'" name="player" align="middle"'+
         ' allowScriptAccess="sameDomain" type="application/x-shockwave-flash"'+
         ' pluginspage="https://www.macromedia.com/go/getflashplayer" />'+
         '</object>';
} 

$(document).ready(function() {
 var content = $(content_selector)[0];
 jQuery.each(css_classes, function(insize, myclass) {
   $('a.'+myclass, content).each(function() {
     var url = $(this).attr('href');
    
     if (parse = parse_youtube(url)) {
       $(this).replaceWith(embed_youtube(parse, insize));
     } else if (parse = parse_stc(url)) {
       $(this).replaceWith(embed_stc(parse, insize));
     } else if (parse = parse_vimeo(url)) {
       $(this).replaceWith(embed_vimeo(parse, insize));
     } else if (parse = parse_overstream(url)) {
       $(this).replaceWith(embed_overstream(parse, insize));
     }
   }); 
  });
});
