$(document).ready( function() 
{
	var BASE_URL_FOR_VIDEOS = "../../../";
	// for(var i in window) if(window.hasOwnProperty(i)) // console.log(i);
	
	/*
	
	<div class="dynamic-video-wrapper">
		<a href="http://fireflyband.co.uk/wp-content/uploads/2010/11/sample_iPod.m4v">Sample video 002</a>
		<div class="video-component">
			<div class="stage">
				
			</div>
			<a class="close" href="#">Close</a>
		</div>
	</div>
	
	*/
	
	if(parseInt(swfobject.ua.pv[0]) <= 9)
	{
		$(".fb-video").each( function(i) 
		{
			var link = $(this).find("object embed").attr("src");
				link = link.split("http://www.facebook.com/v/").join("http://www.facebook.com/video/video.php?v=")
			
			var $link = $("<p><a href=" + link + ">" + $(this).find("span").text() + "</a></p>");
			
			$(this).html( $link )
		})
	}
	
	$("a[href$=flv]").each( function(i) 
	{
		var url = $(this).attr("href");
		var videoId = "video-component-"+i;
		var $item = $(this);
		
		if($item.parent().get(0).tagName == "P")
		{
			// console.info("Parent is <p> tag")
			$item.parent().addClass("collapseParagraph")
		}
		
			$item.attr("videoId", videoId);
			$item.attr("url", url);
			$item.addClass("video-component-item")
			// $item.attr("href", "#");
			$item.removeAttr("href");
			$item.attr("title", $(this).text());
			$item.html("Play <strong>" + $(this).attr("title") + "</strong>")
			$item.addClass("in-active");
			$item.addClass("open");
			$item.click(function() 
			{
				// // console.info("Show video : " + $(this).attr("url"))
				
				var p = "#" + $(this).attr("videoId")+"-wrapper";
				var m = $("#video-component-0");
				
				// // console.info()
				
				
				
				if($(this).hasClass("active"))
				{
					$(this).removeClass("active")
					$(this).addClass("in-active")
					// $("#video-component-0-wrapper").hide();
					$(p).hide();
					
					$(this).html("Play <strong>" + $(this).attr("title") + "</strong>")
					
					// // console.info("inactive")
					
					
					
				} else 
				{
					$(this).removeClass("in-active")
					$(this).addClass("active")
					//$("#video-component-0-wrapper").show()
					$(p).show()
					$(this).html("Stop <strong>" + $(this).attr("title") + "</strong>")
					// // console.info("active")
				}
				
				// $("#video-component-0-wrapper").hide();
				// console.info("@ " + p + " : " + m)

				if($($(this).attr("videoId")) == "false")
				{
					// console.info("Show video " + $($(this).attr("videoId")))
					
				} else 
				{
					// console.info("Hide video " + $($(this).attr("videoId")))
				}
				
				
			})
			
		var $videoComponent = $item.wrap("<div class='dynamic-video-wrapper'>")
				
			// console.info("Video == " + BASE_URL_FOR_VIDEOS + url)
			$videoComponent.attr("url", BASE_URL_FOR_VIDEOS + url);
			// $videoComponent.attr("isActive", "false");
			$videoComponent.after("<div id='" + videoId + "-wrapper' class='video-component'><div id='" + videoId + "' class='stage'></div></div>")
			// $videoComponent.after("<div id='" + videoId + "-wrapper' class='video-component'><div id='" + videoId + "' class='stage'></div><a class='close' href='#'>Close</a></div>")
			// $videoComponent.append("")
			
		// Flash embed Start.
		var minFlashVersion = "9.0.0";
		var expressInstallSwf = false;
		var contentSwf	= "http://fireflyband.co.uk/wp-content/themes/firefly/swf/videoplayer.swf";
		var contentContainer = videoId;
		var contentWidth = "370px";
		var contentHeight = "200px";
		var contentFlashvars = 
		{
			isAutoPlay:false, 
			// videoUrl:"sample_iPod (1).m4v", 
			// videoUrl:"hancock-tsr2_h480p.flv", 
			// videoUrl:"TITLE01-ANGLE1.mp4", 
			videoUrl:BASE_URL_FOR_VIDEOS + url,
			playerWidth:370, 
			playerHeight:200
		};
		var contentParams = 
		{ 
			domain:"*", 
			allowFullScreen:"true", 
			allowscriptaccess:"always", 
			wmode:"transparent"
		};
		var contentAttributes = 
		{ 
			id:"video-website"+i 
		};
		
		swfobject.embedSWF( 
			contentSwf, 
			contentContainer, 
			contentWidth, 
			contentHeight, 
			minFlashVersion, 
			expressInstallSwf, 
			contentFlashvars, 
			contentParams, 
			contentAttributes );
			
		// Flash embed End.
		
		// // console.info("Video component: " + $videoComponent)
			
			
			
			
		// // console.log("Found video: " + url)
		
		//$(this).removeAttr("href");
		
		
		// $(this).addClass("audioPlayerLink");
		// $(this).parent().attr("id", "audio"+i);
		// $(this).wrap("<div class='audioItem'>")
		// $(this).parent().prepend("<div class='audioPlayerIcon'><div id='" + "audio"+i + "'></div></div>")
		// $(this).html( $(this).html() )
		
		// audioPlayers.push({htmlId:"audio"+i, url:audioUrl })
		
	})
	
})
