/*
 * Custom launch page embed js.
 */

(function(){
	
	var root = this;
	if (typeof root.Vidaroo !== "undefined" && typeof root.Vidaroo.embed !== "undefined"){
		return;
	}
	
	typeof root.Vidaroo === "undefined" && (root.Vidaroo = {});

	function generate_embed(source, width, height){
		return '' + 
			'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' +
				width + '" height="' + height + '" id="vidaroo_player" name="vidaroo_player">' +
				'<param name="movie" value="' + source + '" />' +
				'<param name="allowfullscreen" value="true" />' +
				'<param name="allowscriptaccess" value="always" />' +
				'<param name="wmode" value="transparent" />' +
				'<embed id="vidaroo_player" name="vidaroo_player" src="' + source + 
					'" width="' + width + '" height="' + height + '" ' +
					'allowscriptaccess="always" allowfullscreen="true" wmode="transparent" ' + 
					'type="application/x-shockwave-flash" />' + 
			'</object>';
	}
	
	function create_token(){
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var string_length = 8;
		var randomstring = '';

		for (var i = 0; i < string_length; i++) 
		{
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum, rnum + 1);
		}
		return randomstring;
	}
	
	var embed = function(args){
		var width = args.width || 300,
			height = args.height || 350,
			embed = args.embed || '',
			uri_args = args.check_uri,
			loc = window.location.href;
			
		typeof uri_args === "undefined" && (uri_args = true);

		if (embed){

			embed.charAt(embed.length-1) == '/' || (embed += '/');

			var query_pos = uri_args ? loc.indexOf('?') : -1;
			if (query_pos !== -1){
				var query = loc.substring(query_pos+1).replace(/#.*$/,''),
					params = query.split('&');

				for (var i = 0; i < params.length; i++){
					var parts = params[i].split('=');
					if (parts[0] == 'asset_id' || parts[0] == 'channel_id'){
						embed += parts[0] + '/' + parts[1] + '/';
					}
				}
			}

			var id = 'vidaroo_' + create_token(),
				embed_html = generate_embed(embed,width,height);

			document.write(
				'<div id="' + id + '" class="embed">' + embed_html + '</div>'
			);
		}
	};
	
	root.Vidaroo.embed = embed;
})();
