function embedFlv(thumb, videofile, videoid, containerid, vidwidth, vidheight) {
	var flashvars = {
		imagePath: thumb,
		videoPath: videofile,
		autoStart: "false",
		autoHide: "true",
		autoHideTime: "2",
		hideLogo: "true",
		volAudio: "60",
		newWidth: vidwidth,
		newHeight: vidheight,
		disableMiddleButton: "true",
		playSounds: "false",
		soundBarColor: "0x3F3F3F",
		barColor: "0x3F3F3F",
		barShadowColor: "0x5C5C5C",
		subbarColor: "0x8C8C8C"
	};
	var params = {
		allowFullScreen: "true"
	};
	var attributes = {
		id: videoid,
		name: videoid
	};

	swfobject.embedSWF("/inc/flash/flvPlayer.swf", containerid, vidwidth, vidheight, "9.0.0", '', flashvars, params, attributes);
}

function createPlayer(parent) {
	$("body").find("iframe").remove();
	$("body").find("object").remove();
	$("body").find("embed").remove();
	if ($("body").find("video").length > 0) {
		$("body").find(".flareVideo").remove();
	}
	$("body").find("video").remove();

	
	$(parent).find(".mediavideo").each(function(i) {
		var videotype = $(this).find("input[name=videotype]").val();
		var posterimg = $(this).find("input[name=posterimg]").val();
		var videowidth = $(this).find("input[name=width]").val();
		var videoheight = $(this).find("input[name=height]").val();
				
		if (videotype == "video") {
			var videosources = new Array();
			var iossources = new Array();
			$(this).find(".source").each(function(j) {
				var videosource = {
					src:	$(this).find("input[name=sourcefile]").val(),
					type:	$(this).find("input[name=sourcetype]").val() + "; codecs=\""+$(this).find("input[name=sourcecodecs]").val()+"\""
				}
				
				
				var iossource = {
					src:	$(this).find("input[name=sourcefile]").val(),
					type:	$(this).find("input[name=sourcetype]").val() + "; codecs=\""+$(this).find("input[name=sourcecodecs]").val()+"\""
				}
				
				videosources.push(videosource);
				iossources.push(iossource);
			});
			
			if (((videosources.length == 1) && (videosources[0].type.indexOf("video/flv") != -1)) || (!$("html").hasClass("video"))) {
				flashFallback($(this).parents().filter(".media:first").attr("id"));
			} else {
				if (!iOS) {
					var video = $(this).flareVideo({
						width: videowidth,
						height: videoheight,
						poster: posterimg,
						preload: true,
						keyShortcut: false,
						autobuffer: true
					});
					video.load(videosources);
					this.videoplayer = video;
				} else {
					var videoplayer = '<video class="video" width="'+videowidth+'" height="'+videoheight+'" poster="'+posterimg+'" preload="true" autobuffer="false" controls>';
					for (var i=0; i<iossources.length; i++) {
						videoplayer += '<source src="'+iossources[i].src+'" type=\''+iossources[i].type+'\'></source>';
					}
					videoplayer += '</video>';
					$(this).append(videoplayer);
						
				}
			}
		} else {
			var playerid = $(this).find("input[name=playerid]").val();
			var vimeoid = $(this).find("input[name=vimeoid]").val();

			var vimeoPlayer = getVimeoPlayer(playerid, vimeoid, videowidth, videoheight);
			$(this).append(vimeoPlayer);
			initVimeo(playerid);
		}
			
	});
}

function getVimeoPlayer(playerid, vimeoid, width, height) {
	var player = '<iframe id="'+playerid+'" class="vimeo-player" src="http://player.vimeo.com/video/'+vimeoid+'?title=0&amp;byline=0&amp;portrait=0&amp;color=555555&api=1&player_id='+playerid+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>';
	return player
}

function playvideo(id) {
	if ($("#media"+id).find("video").length > 0) {
		if ($("#media"+id).find(".mediavideo").get(0).videoplayer) $("#media"+id).find(".mediavideo").get(0).videoplayer.play();
	} else if ($("#media"+id).find("object").length > 0) {
		try {
			$("#media"+id).find(".mediavideo object").get(0).jsStartVideo();
		} catch (e) {
		}
	} else if ($("#media"+id).find("iframe").length > 0) {
		$("#media"+id).find("iframe").each(function() {
			$f($(this).attr("id")).api('play');
		});
		
	}
}



function flashFallback(mediaId) {
	var flashvideothumb = $("#"+mediaId).find("input[name=posterimg]").val();
	var flashvideosrc = $("#"+mediaId).find("input[name=sourcefile]:first").val();
	var flashvideoid = "flashvideo_" + mediaId;
	var flashcontainerid = "flashvideocontainer_" + mediaId;
	var flashvideowidth = $("#"+mediaId).find("input[name=width]").val();
	var flashvideoheight = $("#"+mediaId).find("input[name=height]").val();
	
	$("#"+mediaId+" .mediavideo").append('<div id="'+flashcontainerid+'"></div>');
		
	embedFlv(flashvideothumb, flashvideosrc, flashvideoid, flashcontainerid, flashvideowidth, flashvideoheight);
}

function showMedia(id) {
	if (!$("#media"+id).hasClass("current")) {
		$(".current").each(function(i) {
			if ($(this).find("video").length > 0) {
				if ($(this).find(".mediavideo").get(0).videoplayer) $(this).find(".mediavideo").get(0).videoplayer.pause();
			} else if ($(this).find("object").length > 0) {
				try {
					$(this).find(".mediavideo object").get(0).jsPauseVideo();
				} catch (e) {
				}
			} else if ($(this).find("iframe").length > 0) {
				$(this).find("iframe").each(function() {
					$f($(this).attr("id")).api('pause');
				});
			}
			
		});
	
		$(".media").hide();
		$(".current").removeClass("current");

		$("#media"+id).addClass("current");
		$("#media"+id).show();
		initVideo(".current");
		
	
		
		//
		/*		
		if (iOS) {
			$(this).find("video").remove();
			createPlayer("#media"+id);
		}
		*/

	}
}

function nextMedia() {
	if ($(".current:first").next().length > 0) {
		var nextid = $(".current:first").next().find("input[name=mediaid]").val();
	} else {
		var nextid = $(".media:eq(0)").find("input[name=mediaid]").val();
	}
	showMedia(nextid);
}

function prevMedia() {
	if ($(".current:first").prev().length > 0) {
		var nextid = $(".current:first").prev().find("input[name=mediaid]").val();
	} else {
		var nextid = $(".media:eq("+($(".media").length - 1)+")").find("input[name=mediaid]").val();
	}
	showMedia(nextid);
}


function initVideo(parent) {
	createPlayer(parent);
}

function initReel() {
	initVideo("body");
}

function initProject() {
	checkBrowsers();
	$(".media").hide();
	$(".media:first").addClass("current").show();
	initVideo(".current");
}

function videosReady() {
	
}

function initProjects() {
	$(".project").hover(function() {
		$(this).find(".projectImg").css('opacity',1.0);
		$(this).find(".projectTitle").show();
	},function(){
		$(this).find(".projectImg").css('opacity',0.6);
		$(this).find(".projectTitle").hide();
	});
}



var iOS;
function checkBrowsers() {
	if ((navigator.userAgent.indexOf('iPad') != -1) || (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1)) {
		iOS = true;
	} else {
		iOS = false;
	}
}

function openCleanWindow(url, width, height) {
	window.open(url, 'HunterGatherer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+'');
}

function openWindow(url) {
	window.open(url);
}


