// Video Magic JS by Anders Eskholm (c) 2007 - anders@eskholm.dk

// README
//
// Renders inline video (embedded) from Streetfire.net, Youtube.com, Google Video and MSN Soapbox
// Searches given page elements for links/urls to above mentioned video links, and replaces with embedded video
// This version is custom for Benjamin Magazines online (http://www.benjamin.dk/)
// Modify as you like, but keep original author comment, please.
//
// Replacing only if any valid link is present. Searching both plaintext urls and anchored links.
// Adding proper event listener to onload chain, for script to run at page load end.
//
// Enjoy.

function getPageContent(divId) {
    var content = "";
    try { content = document.getElementById(divId).innerHTML; } catch(E) {;}
    return content ? content : false;
}

function updatePageContent(divId, content) {
    var success = false;
    try { document.getElementById(divId).innerHTML = content; success = true; } catch(E) {;}
    return success;
}

function videoMagic() {
    var content = getPageContent('article');

    if(content) {
        var orig = content;
        var serial = 0;


        // Vimeo
        content = content.replace(/<a[^>]*?vimeo\.com\/(\d+).*?<\/a>/gi, "<div style=\"text-align: center;\" id=\"video____vimeo____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/(?:www\.)?vimeo\.com\/(\d+)/gi, "<div style=\"text-align: center;\" id=\"video____vimeo____$1"+"____"+serial+"\"></div>");
        serial++;

        // Streetfire
        content = content.replace(/<a[^>]*?videos\.streetfire\.net\/video\/([a-zA-Z0-9\-]+).htm.*?<\/a>/gi, "<div style=\"text-align: center;\" id=\"video____streetfire____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/videos\.streetfire\.net\/video\/([a-z0-9\-]+).htm/gi, "<div style=\"text-align: center;\" id=\"video____streetfire____$1"+"____"+serial+"\"></div>");
        serial++;

        // Youtube
        content = content.replace(/<a[^>]*?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+).*?<\/a>/gi, "<div style=\"text-align: center;\" id=\"video____youtube____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+)/gi, "<div style=\"text-align: center;\" id=\"video____youtube____$1"+"____"+serial+"\"></div>");
        serial++;

        // Google Video
        content = content.replace(/<a[^>]*?video\.google\.com\/videoplay\?docid=(\d+).*?<\/a>/gi, "<div style=\"text-align: center;\" id=\"video____google____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/video\.google\.com\/videoplay\?docid=(\d+)/gi, "<div style=\"text-align: center;\" id=\"video____google____$1"+"____"+serial+"\"></div>");
        serial++;

        // MSN SoapBox
        content = content.replace(/<a[^>]*?soapbox\.msn\.com\/video\.aspx\?vid=([a-zA-Z0-9\-]+).*?<\/a>/gi,"<div style=\"text-align: center;\" id=\"video____soapbox____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/soapbox\.msn\.com\/video\.aspx\?vid=([a-z0-9\-]+)/gi,"<div style=\"text-align: center;\" id=\"video____soapbox____$1"+"____"+serial+"\"></div>");
        serial++;

        // MSN Video
        content = content.replace(/<a[^>]*?video\.msn\.com\/video\.aspx\?vid=([a-zA-Z0-9\-]+).*?<\/a>/gi,"<div style=\"text-align: center;\" id=\"video____msn____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/video\.msn\.com\/video\.aspx\?vid=([a-z0-9\-]+)/gi,"<div style=\"text-align: center;\" id=\"video____msn____$1"+"____"+serial+"\"></div>");
        serial++;
        
        // 23Video
        // Like this: http://bil-tv.23video.com/video/544519/chevrolet-camaro-ss
        content = content.replace(/<a[^>]*?bil-tv\.23video\.com\/video\/([a-zA-Z0-9\-]+)<\/a>/gi,"<div style=\"text-align: center;\" id=\"video____bil-tv____$1"+"____"+serial+"\"></div>");
        serial++;
        content = content.replace(/http:\/\/bil-tv\.23video\.com\/video\/([a-z0-9\-]+)/gi,"<div style=\"text-align: center;\" id=\"video____bil-tv____$1"+"____"+serial+"\"></div>");
        serial++;        
        
        if(orig != content) {
			updatePageContent('article',content);
			var divs = document.getElementsByTagName('DIV');

			if (document.getElementById('article') != undefined && document.getElementById('article').className.match(/video/)) {
				width = 620;
				height = 375;
			} else {
				width = 380;
				height = 230;
			}

			for(var i = 0; i < divs.length; i++) {
				if(divs[i].id.indexOf('video____') != -1) {
					var id = divs[i].id;
					var params = id.split("____");
					var provider = params[1];
					var videoId = params[2];
					var flashvars = {};
					var params = {};
					params.scale = "noscale";
					params.quality = "high";
					params.menu = "false";
					params.allowFullscreen = "true";
					var attributes = {};
					attributes.id = id;
					if(provider == 'youtube') {
						swfobject.embedSWF('http://www.youtube.com/v/'+videoId+'?fs=1', id, width, height, '9.0.0', false, flashvars, params, attributes);
					}
					else if(provider == 'google') {
						swfobject.embedSWF('http://video.google.com/googleplayer.swf?docId='+videoId+'&hl=en', id, width, height, '9.0.0', null, flashvars, params, attributes);
					}
					else if(provider == 'streetfire') {
						flashvars.video = videoId;
						swfobject.embedSWF('http://videos.streetfire.net/vidiac.swf', id, width, height, '9.0.0', null, flashvars, params, attributes);
					}
					else if(provider == 'vimeo') {
						swfobject.embedSWF('http://vimeo.com/moogaloop.swf?clip_id='+videoId+'&server=vimeo.com&show_title=1&show_byline=1', id, width, height, '9.0.0', null, flashvars, params, attributes);
					}
					else if(provider == 'soapbox') {
						flashvars.c = v;
						flashvars.v = videoId;
						swfobject.embedSWF('http://images.soapbox.msn.com/flash/soapbox1_1.swf', id, width, height, '9.0.0', null, flashvars, params, attributes);
					}
					else if(provider == 'msn') {
						flashvars.c = v;
						flashvars.v = videoId;
						swfobject.embedSWF('http://images.video.msn.com/flash/soapbox1_1.swf', id, width, height, '9.0.0', null, flashvars, params, attributes);
					}
					else if(provider == 'bil-tv') {
						flashvars.FlashVars = "token=ba782b21c7383c58642da5cb695c2dee&photo%5fid="+videoId;
            // params.movie = "http://bil-tv.23video.com/544837.swf";
            // params.FlashVars = "token=ba782b21c7383c58642da5cb695c2dee&photo%5fid="+videoId;
						swfobject.embedSWF('http://bil-tv.23video.com/544837.swf', id, width, height, '9.0.0', null, flashvars, params, attributes);
					}
				}
			}
		}
	}
}

if(window.addEventListener)
    window.addEventListener("load", videoMagic, false);
else if(window.attachEvent)
    window.attachEvent("onload", videoMagic);
else if(document.getElementById)
    window.onload=videoMagic;


