﻿/*
NAME:       media_players.js
DATE:       29 oct 2009

SYNOPSIS:   Javascript file to show videos on the forums and blogs
            It does this by searching for urls of the video sites, and replacing
            the url with embed code

USER GUDE:  this script is designed to modify the contents of containers, looking for
            special tags and will replace them with relevant HTML code. modifications
            come in two forms:
            
            1)WHERE TO DO THE REPLACING
            the postContainers array contains the class names of the containers that
            you wish to modify the tags on. adding to this list will set a new container
            class to be processed
            
            2)ADDING A WHOLE NEW TAG
            the array modifyers contains the replacement instrustions. it is an array of arrays
            each array in modifyers is the information for a piece of code.
            of these arrays the format is: NAME,CODE TO EMBED,PATTERS (regexes) TO MATCH
     

HISTORY:    29/10/2009  Created
*/
/*IGNORE THIS*/
function parseVideos() {

    

    //LIST OF CONTAINERS THAT MAY CONTAIN THE TAGS
    var postContainerNames = new Array(
    "ForumPostContentText"
    , "CommonContentBoxContent"

);


    //THE PATTERNS TO MATCH
    var modifyers = new Array();
    modifyers[0] = new Array('YOUTUBE'
    , '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$1&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$1&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>'
    , /http:\/\/www.youtube.com\/watch\?v=(.{11})/
    , /\[YouTube:(.{11})\]/
);
    modifyers[1] = new Array('VIMEO'
    , '<object width="400" height="270"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=$1&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="400" height="270"></embed></object><p><a href="http://vimeo.com/$1">THE MACHINE</a> from <a href="http://vimeo.com/bentimagelab">Bent Image Lab</a> on <a href="http://vimeo.com">Vimeo</a>.</p>'
    , /http:\/\/www.vimeo.com\/([0-9]{7})/
);

    /*WORKER CODE*/

    for (var postContainerID = 0; postContainerID < postContainerNames.length; postContainerID++) {
        //loop through the list of containers that could contain content
        var postContainers;
        postContainers = getElementsByClassName(postContainerNames[postContainerID]);
        if (postContainers.length > 0) {
            //for each container, loop through each of the modifyers
            for (var i = 0; i < postContainers.length; i++) {
                for (var modifyerID = 0; modifyerID < modifyers.length; modifyerID++) {
                    for (var regexID = 2; regexID < (modifyers[modifyerID].length); regexID++) {
                        postContainers[i].innerHTML = postContainers[i].innerHTML.replace(modifyers[modifyerID][regexID], modifyers[modifyerID][1]);
                    }
                }
            }
        }
    }

}

/*SUPPORT FUNCTIONS*/

//Get all the elements of the given classname of the given tag.
/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};