// Bourne VideoPlayer (jQuery) v0.9 22.02.11
String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

var bVideoPlayer = function(div) {
	var player,flash,html5,videoElement,sources,videoFile,block,cachedContents;
	//alert('new player firing up');
	
	/* CONFIGURATION OPTIONS */
	var options = videoplayer_options;
	/* END OF OPTIONS */
	
		var html5native = [/iphone/i,/ipad/i];
	
		if (div.tagName == 'DIV') {
			block = div;
		} else {
		
			/* HACK TO DISABLE HTML5 VIDEO IN FAVOUR OF NATIVE PLAYBACK */
			for (var i = 0; i < html5native.length;i++) {
				if (navigator.userAgent.match(html5native[i])) {
					$(div).addClass('video-block');
					div.controls = true;
					return false;
				}
			}
			/* END OF HACK */
			
			var newdiv = buildElement('div',{},['video-block']);
			block = div.parentNode.insertBefore(newdiv,div);
			block.appendChild(block.parentNode.removeChild(div));
		}
		var parent = block.parentNode;
		if (parent.tagName == 'P') {
			parent.parentNode.insertBefore(block,parent);
			if (parent.innerHTML.trim() == '') {
				parent.parentNode.removeChild(parent);
			}
		}
		sources = {};
		videoElement = $(block).find('video')[0];
		

		if(arguments.length > 1) {
			var flashvars = Object.extend(options.flashvars, arguments[1].flashvars || { });
			var params = Object.extend(options.params, arguments[1].params || { });
			options = Object.extend(options, arguments[1]);
			options.flashvars = flashvars;
			options.params = params;
		}
		getSources();
		
		var flashAvailable = typeof(sources.flv) != 'undefined' || typeof(sources.mp4) != 'undefined';
		if (typeof(videoElement.play) != 'undefined') {
			// Could be HTML5
			if (($.browser.webkit || $.browser.msie) && typeof(sources.mp4) != 'undefined') {
				html5 = true;
				videoFile = sources.mp4;
			} else if (($.browser.opera || $.browser.mozilla) && typeof(sources.ogg) != 'undefined') {
				html5 = true;
				videoFile = sources.ogg;
			} else if (flashAvailable) {
				flash = true;
			}
		} else if (flashAvailable) {
			flash = true;
		}
		goFlash();
	
	
	function getSources() {
		if (document.all && typeof(window.msPerformance) == 'undefined') {
			// IE's HTML5 handling is so haphazard that we have to do this as it can't
			// recognise the parent child relationship between source and video tags
			var srcs = document.getElementsByTagName('SOURCE');
			for (var i = 0; i < srcs.length; i++) {
				if (srcs[i].parentNode == block || srcs[i].parentNode == videoElement) {
					sources[$(srcs[i]).attr('type').replace('video/','')] = srcs[i];
				}
			}
		} else {
			$.each($(videoElement).find('SOURCE'),function(i,el) {
				sources[$(el).attr('type').replace('video/','')] = el;
			});
		}
	}
	function getPlayerHeight() {
		var h = parseInt($(videoElement).attr('height'));
		if (options.autohide) {
			h += options.minControlsHeight;
		} else {
			h += options.controlsHeight;
		}
		return h;
	}
	function getPlayerWidth() {
		return $(videoElement).attr('width');
	}
	function goFlash() {
		if (typeof(sources.flv) != 'undefined') {
			videoFile = sources.flv;
		} else if (typeof(sources.mp4) != 'undefined') {
			videoFile = sources.mp4;
		}
		
		var a = buildElement('a',{'href':'http://www.adobe.com/go/getflashplayer'});
		a.appendChild(buildElement('img',{'src':'http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif','alt':'Get Adobe Flash Player'}));
		
		if (!videoElement.id) {
			var d = new Date();
			videoElement.id = 'video'+d.getSeconds()+d.getMilliseconds();
		}
		
		var videoHolderDiv = buildElement('div',{'id':videoElement.id+'_alt'});
		videoHolderDiv.style.height = getPlayerHeight() + 'px';
		videoHolderDiv.style.width = getPlayerWidth() + 'px';
		videoHolderDiv.appendChild(a);
		videoElement.parentNode.insertBefore(videoHolderDiv,videoElement);
		if($.browser.msie && $.browser.version <= 8){
			//setUniformHeights('#dynamicTabBox .tabBox');
		}
		var flashvars = options.flashvars;
		if (typeof(flashvars.videoPath) != 'undefined') {
			flashvars.videoPath = videoFile.src;
		}
		if (options.params.length == 0) {
			options.params = {'bgcolor':getBackgroundColor()};
		}
		var attributes = {id:videoElement.id,name:videoElement.id};
		//alert(attributes.id);
		flashvars.videoWidth = $(videoElement).attr('width');
		flashvars.videoHeight = $(videoElement).attr('height');
		/*alert(options.swf);
		alert(videoElement.id);
		alert(getPlayerWidth());
		alert(getPlayerHeight());
		alert(options.installswf);
		alert(flashvars.videoWidth);
		alert(flashvars.videoHeight);
		alert(options.params.allowscriptaccess);
		alert(attributes.id);
		alert(attributes.name);*/
		player = swfobject.embedSWF(options.swf, videoElement.id+'_alt', getPlayerWidth(), getPlayerHeight(), "9.0.124", options.installswf, flashvars, options.params, attributes);
		
		$(videoElement).remove();
	}
	function getBackgroundColor() {
		var bgcolor = getParentCssRule(block,'backgroundColor');
		function fillZero(s) {
			if (!s) { s = '00'; } else if (s.length == 1) { s = "0" + s; }
			return s;
		}

		function colorToHex(color) {
			var digits = /(.*?)rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.exec(color);
			if (digits == null) return "";
			var hexcode = fillZero(parseInt(digits[2]).toString(16)) + fillZero(parseInt(digits[3]).toString(16)) + fillZero(parseInt(digits[4]).toString(16));
			return '#' + hexcode.toUpperCase();
		}
		
		if (bgcolor == 'transparent') {
			bgcolor = '#FFFFFF';
		} else if (bgcolor.indexOf('rgb') == 0) {
			bgcolor = colorToHex(bgcolor);
		}
		return bgcolor;
	}
	function getParentCssRule(element,rule) {
		var value;
		// Start from a base element, and go north looking for a CSS rule
		var ref = $(element);
		if (ref) {
			while (ref.tagName != 'BODY' && (ref.getStyle(rule) == null || ref.getStyle(rule) == 'transparent') || ref.getStyle(rule) == 'rgba(0, 0, 0, 0)') {
				ref = ref.up();
			}
			value = ref.getStyle(rule);
		}
		return value;
	}
	var track = {
		callbackFunc: function() {},
		play: function(time) {
			callbackFunc('Videos', 'Play (HTML5)', videoId, time);
		},
		pause: function() {
			callbackFunc('Videos', 'Pause (HTML5)', videoId, time);
		}
	}
}

function buildElement(type,html_attributes,classes,text) {
	// Build an HTML element with attributes
	new_element = document.createElement(type);
	if (typeof(html_attributes) != 'undefined') {
		for (i in html_attributes) {
			if (html_attributes.hasOwnProperty(i)) {
				new_element.setAttribute(i,html_attributes[i]);
			}
		}
	}
	if (classes && typeof(classes) != 'undefined') {
		for (i in classes) {
			if (classes.hasOwnProperty(i)) {
				$(new_element).addClass(classes[i]);
			}
		}
	}
	if (text && typeof(text) != 'undefined') {
		new_element.appendChild(document.createTextNode(text));
	}
	
	return new_element;
}



