
	//|	This file written by Brian Franklin
	//|	http://www.kudos-js.com
	//|	http://www.caoine.com

	//|	bcss imports BEML stylesheets attached via link tags and applies their values to players on startup
	var bcss = new function () {
	
		this.bc;
		this.experience;
		this.player;
		this.queue = null;
	
		//|	Kick things off
		this.init = function () {
	
			//|	The stylesheet will be linked by a <link> tag with a rel attribute of 'brightcove-css'
			var tags = document.getElementsByTagName('link');
			
			for (var i = 0; i < tags.length; i++) {
			
				if ((typeof tags[i].getAttribute('rel') != undefined) && (tags[i].getAttribute('rel') == 'brightcove-css')) {
				
					//|	When we find the appropriate link tag, we pass the path of the file to the inject method
					bcss.inject(tags[i].getAttribute('href'));
				
				}
			
			}
			
		};
	
		//|	The inject method takes a path to a css file and creates an iframe to draw its contents into the page
		this.inject = function (s) {
			
			//|	Create the iframe element and assign appropriate properties to prevent it from being seen
			var n = document.createElement('iframe');
			n.setAttribute('src', s);
			n.setAttribute('id', 'bcss');
			n.style.display = 'none';
			n.style.height = '1px';
			n.style.width = '1px';
			document.body.appendChild(n);
			
			//|	When the contents of the iframe are loaded, we know we can access the css within
			if (n.addEventListener) {
				n.addEventListener('load', bcss.loader, false);
			} else if (n.attachEvent) {
				n.attachEvent('onload', bcss.loader);
			} else {
				n.onload = bcss.loader;
			}
			
		};

		//|	This is what we do when the iframe loads		
		this.loader = function () {
			
			var n = document.getElementById('bcss');
	
			//| Cross-browser nonsense, as always
			var d = ((typeof n.contentDocument == undefined) || (n.contentDocument == null)) ? n.contentWindow.document : n.contentDocument;
		
			var c = d.body.innerHTML;
		
			//|	Remove the iframe so we don't clutter the dom with trash
			setTimeout('document.body.removeChild(document.getElementById("bcss"));', 500);
		
			//|	Pass the altered css to the set method
			bcss.translate(c);
		
		};
		
		//|	Parses css into a Brightcove-readable string to send to the player
		this.translate = function (css) {
		
			//|	Take out any HTML that firebug might add, and remove all whitespace to make parsing easier
			css = css.replace(/<.*?>/ig, '');
			css = css.replace(/\s{1,}?/ig, '');
			
			//|	Find each class-level chunk
			var p = css.split('}');
			var s = '';
			
			for (var i in p) {
			
				//|	Get the classname for use in the atribute names below
				var n = p[i].substr(0, p[i].indexOf('{'));
				var r = p[i].substr(p[i].indexOf('{') + 1).split(';');
				
				//|	Step through the key/value pairs within this selector chunk
				for (var j in r) {
				
					//|	Some browsers will give us some blank lines after the splits, so we take them out here
					if (r[j].length < 1) { continue; }
					var t = r[j].substr(0, r[j].indexOf(':'));
					var v = r[j].substr(r[j].indexOf(':') + 1);
					
					//|	Combine selector name with property name for our new key, assign the old value
					s += (n.replace('.', '')+'-'+t+':'+v+';');
				
				}
				
			}
	
			try {
			
				//|	In some cases, the player will have already loaded, which means we have to set styles now
			    this.player.setStyles(s);
			
			} catch (e) {
			
				//|	Otherwise, we queue the change
				this.queue = s;
			
			}
		    
		};
		
		//|	This is what we do when onTemplateLoaded fires
		this.loaded = function (id) {
		
			bcss.bc = brightcove.getExperience(id);
		    bcss.experience = bcss.bc.getModule('experience');
		    bcss.player = bcss.bc.getModule(APIModules.VIDEO_PLAYER);

			if (bcss.queue != null) {
				bcss.player.setStyles(bcss.queue);
			}
		
		};
		
		//|	We can't assume we're the only javascript trying to use the player, so we check for another onTemplateLoaded and modify it if it exists
		if (typeof onTemplateLoaded == 'function') {
		
			var temp = onTemplateLoaded;
			
			window.onTemplateLoaded = function (id) {
			
				bcss.loaded(id);
			
				temp(id);
			
			}
		
		} else {
		
			window.onTemplateLoaded = function (id) {
			
				bcss.loaded(id);
			
			}
		
		}
	
	}();
	
	//|	Finally, set the window onload the same way as onTemplateReady
	if (typeof window.onload == 'function') {
		
		var temp = window.onload;
		
		window.onload = function (id) {
		
			bcss.init();
		
			temp(id);
		
		}
	
	} else {
	
		window.onload = bcss.init;
	
	}
