$(document).ready(function(){
	//pathname
	var baseUrl = window.location.href;
	var baseLength = baseUrl.length
	baseUrl = baseUrl.substring(7, baseLength - 7);
	baseUrl = baseUrl.split("/");
	//alert(baseUrl[0]);
	var flow_path = "http://"+baseUrl[0]+"/js/swf/flowplayer-3.2.1.swf";
	var audio_plugin = "http://"+baseUrl[0]+"/js/swf/flowplayer.audio-3.2.0.swf";
	var v=1;
	var b=1;
	/* NEW GPI FLOWPLAYER SETUP */
	var links = $("#story_container a").get();
	var flvarray = new Array();
	var mp3array = new Array();
	
	/* Get all of the MP3 Links */
	
	for(a=0; a <= links.length - 1; a++){
		
		var mp3file = new String(links[a]);
		var mp3_len = mp3file.length
		var mp3start_pos = mp3_len -3;
		var mp3file1 = mp3file.substr(mp3start_pos,3);
		
		if(mp3file1 == "mp3"){
			$(links[a]).text('Play Audio '+b+' ');
			mp3array.push(links[a]);
			b++;
		}
	}
	if($(mp3array).length > 0){
		$('<br/><div id="audioplayer" style="width:100%; height:30px;"></div>').appendTo("#story_container");
		
		/* Initialize Flowplayer for Audio */
		$f("audioplayer", flow_path, {
			plugins: {
				controls: {
				fullscreen: false,
				height: 30,
				autoHide: false
				},
				audio: {
					url: audio_plugin
				}
			}
		});
		$("#audioplayer").hide();
				
		for (var c=0; c <= mp3array.length-1; c++) {
			mp3array[c].onclick = function() {
				$("#audioplayer").show();
				$f("audioplayer").play(this.getAttribute("href", 2));
				return false;
			}
		}
	
	} //end if mp3 files
	
	
	
	
	/* Get all of the FLV links */
	for(j=0; j <= links.length - 1; j++){
		
		var flvfile = new String(links[j]);
		var name_len = flvfile.length
		var start_pos = name_len -3;
		var flvfile1 = flvfile.substr(start_pos,3);
		
		if(flvfile1 == "flv"){
			$(links[j]).text('Play Video '+v+' ');
			flvarray.push(links[j]);
			v++;
		}
	}
	
	if(flvarray.length > 0){
		/* Play video if clicked */
		for (var i = 0; i <= flvarray.length-1; i++) {
			flvarray[i].onclick = function() {
				$f().play(this.getAttribute("href", 2));
				return false;
			}
		}
		
		/*===========================================
		1. Create the Flowplayer
		2. Wrap a Div around the Flowplayer
		3. Place A Div next to the Flowplayer
		4. Clear the Floats
		5. Add div to display the embed code
		===========================================*/
		/*1.*/ $('<a href="'+flvarray[0]+'" style="float:left; display:block;width:480px;height:360px;" id="gpiplayer"></a>').appendTo("#story_container");
		/*2.*/ $("#gpiplayer").wrap('<div id="flow_wrapper" style="width:700px;" />');
		/*3.*/ $('<div id="flembed" style="float:left; padding-left:20px; width:200px; height:360px;"></div>').appendTo("#flow_wrapper");
		/*4.*/ $("<br style=\"clear:both\"/>").appendTo("#flow_wrapper");
		/*5.*/ $("<div id=\"flembedcode\"></div>").appendTo("#story_container");
		
		for(var l=0; l<=flvarray.length - 1;){
			var r = l+1;
			$('<button class="flowembedme">Embed Video '+r+' Code</button><br/>').appendTo("#flembed");
			$('<span class="flowembed_code"></span>').appendTo("#flembedcode");
			
			/* flowplayer embed code*/
			var flow_embed;
			flow_embed ="<div style=\"width:700px\"><object width=\"480\" height=\"360\" id=\"gpiplayer\" name=\"gpiplayer\" data=\""+flow_path+"\" type=\"application/x-shockwave-flash\">";
			flow_embed += '<param name="movie" value="'+flow_path+'" />';
			flow_embed += '<param name="allowfullscreen" value="true" />';
			flow_embed += '<param name="allowscriptaccess" value="always" />';
			flow_embed += '<param name="flashvars" value='+"'config={"+'"clip":{"autoPlay":false,"autoBuffering":true,"url":"'+flvarray[l]+'"},';
			flow_embed += '"playlist":[{"autoPlay":false,"autoBuffering":true,"url":"'+flvarray[l]+'"}]}'+"'"+' />';
			flow_embed += '</object></div>';
			
			var flowplayer_embed = $(flow_embed).text(flow_embed);
			$(".flowembed_code").eq(l).html(flowplayer_embed);
		
			l++;
		}
		
		/* Toggle the Flowplayer Embed Code */
		if($(".flowembed_code").length){
			$(".flowembed_code").each(function(){ $(this).hide(); });
			
			$(".flowembedme").toggle(function(){
				var myindex = $(this).index(".flowembedme");
				$(".flowembed_code").each(function(){ $(this).hide(); });
				$(".flowembed_code").eq(myindex).show();
			}, function() {
				var myindex = $(this).index(".flowembedme");
				$(".flowembed_code").eq(myindex).hide();
			});
		
		}
		/* Initialize flowplayer */
			$f("gpiplayer", flow_path, {
				clip: {
					autoPlay: false,
					autoBuffering: true
				} 
			});
	} //end if flvarray > 0
	/* END GPI FLOWPLAYER SETUP */
	
	/* YOUTUBE SETUP */
	for(var k=0; k < links.length; k++){
		var url = new String(links[k]);
		var url_root = url.search(/youtube.com/);
		
		if(url_root != -1){
			var url_strip = url.search(/watch\?v=/);
			var new_url = url.substring(0,22)+'/v/'+url.substring(31);
			var youtube = '<p><object width="480" height="385">';
				youtube += '<param name="movie" value="'+new_url+'&hl=en_US&fs=1&rel=0"></param>';
				youtube += '<param name="allowFullScreen" value="true"></param>';
				youtube += '<param name="allowscriptaccess" value="always"></param>';
				youtube += '<embed src="'+new_url+'&hl=en_US&fs=1&rel=0" ';
				youtube += 'type="application/x-shockwave-flash"';
				youtube += 'allowscriptaccess="always"';
				youtube += 'allowfullscreen="true" width="480" height="385"></embed></object></p>';
			$(youtube).appendTo("#story_container");
			
			$('<button class="embedme">Embed Code</button><br/>').appendTo("#story_container");
			$('<p class="embed_code"></p>').appendTo("#story_container");
			var youtube_code = $(youtube).text(youtube);//.html();
			$(".embed_code").html(youtube_code);
			
		}
	}
	/* END YOUTUBE */
	
	/* Toggle the Youtube Embed Code */
	if($(".embed_code").length){
		$(".embed_code").each(function(){ $(this).hide(); });

		$(".embedme").toggle(function(){
			var myindex = $(this).index(".embedme");
			$(".embed_code").eq(myindex).show();
		}, function() {
			var myindex = $(this).index(".embedme");
			$(".embed_code").eq(myindex).hide();
		});
	}
});
