var videos =
{
	init: function()
	{
		videoCategories.init();
		videoSearch.init();
		videos.defaultPlayer = $(videos.defaultPlayer);
		if(videos.defaultPlayer.length == 1 && videos.defaultPlayer.html() != "")
		{
			var str = videos.defaultPlayer.html();
			videos.defaultPlayer.html("");
			var strings = new Array();
			strings = str.split('[e]');
			for(var i=0; i<=3; i++) 
			{
				if(strings[i] == "") strings[i] = null;
				if(strings[i] == 'undefined') strings[i] = null;
			}
			videos.make(strings[0], strings[1], strings[2], strings[3]);
		}
		else
			error.warn("The requested video was not found.");
	},
	multiple: function(listLocation, container)
	{
		videos.defaultPlayer = $(videos.defaultPlayer);
		var c = (typeof(container) != "undefined" && container != "") ? "#" + container : videos.defaultPlayer;
		if($("." + listLocation).length >= 1)
		{
			window.alert("class");
		}
		else if($("#" + listLocation).length == 1)
		{
			$("#" + listLocation + " a").click( function(e)
			{
				e.preventDefault();
				videos.createPlayer($(this).attr("href"), c, true);	
			});
		}
		else
		{
			error.warn("The video list that you specified does not exist.");
		}
	},
	defaultPlayer: "#videoplayer",
	baseDir: "http://www.pallensmith.com/assets/videos/",
	make: function(folder, file, prefolder,  prefile)
	{
		if(prefile !== null && prefolder !== null)
		{
			videos.createPlayer(videos.baseDir + prefolder + '/' + prefile);
			videos.setNext(videos.baseDir + folder + '/' + file);
		}
		else
		{
			videos.createPlayer(videos.baseDir + folder + '/' + file);
		}
	},
	defaultVideoPlayerCSS: "vid-player-class",
	createPlayer: function(filepath, container, autostart, vidPlayerCSS)
	{
		var vpcss = typeof(vidPlayerCSS) == "undefined" || videPlayerCSS == true ? videos.defaultVideoPlayerCSS : videoPlayerCSS;
		var c = typeof(container) != "undefined" && container != "" ? container : videos.defaultPlayer.attr("id");
		if(c.indexOf("#") == 0) { c = c.substr(1); }
		$("#" + c).addClass(vpcss);
		$("#" + c).next().css("clear", "left");
		var a = typeof(autostart) != "undefined" && autostart === true ? "true" : "false";
		var s = new SWFObject('http://www.pallensmith.com/assets/videos/flvplayer.swf','mpl','360','240','9');
		s.addParam('allowfullscreen','true');
		s.addParam('allowscriptaccess','always');
		s.addVariable('file',filepath);
		s.addParam("allowfullscreen","true");
        s.addParam("wmode", "transparent");
		s.addVariable("width","360");
		s.addVariable("height","240");
		s.addVariable("enablejs","true");
		s.addVariable("javascriptid","thePlayerId"); 
		s.addVariable("autostart",a);
		s.write(c);
	},
	finishRuntime: function(callback)
	{
		var runtime = setInterval(function()
		{
			if(videos.currentState == 3) { callback(); window.alert("video done"); clearInterval(runtime); }
		}, 500);
	},
	setNext: function(filename)
	{
		this.next = filename;
	},
	next: false,
	currentState: false
};


var player = null;
function playerReady(thePlayer)
{
	player = window.document[thePlayer.id];
}


function getUpdate(typ, pr1, pr2, pid)
{
	if(typ == 'state' && pr1 == 3)
	{
		videos.createPlayer(videos.next, "", true);
	}
}

		








var videoCategories =
{
	init: function()
	{
		this.setupList("div#video-categories ul li a");
	},
	setupList: function(listname)
	{
		$(listname).click( function(e)
		{
			e.preventDefault();
			videoCategories.currentID = 0;
			$("div.recipe-categories").css("width", "800px");
			var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "videos_listbycategory.php?c=" + escape($(this).html());
			$("#article").html("Loading...");
			$.ajax(
			{
				url: u,
				success: function(data)
				{
					$("#article").html(data);
					videoCategories.setupNav();
					videoCategories.setupList(".video-categories2 ul li a");
				},
				error: function(xhr)
				{
					error.warn(page.httpStatus(xhr.status) + "<br />" + u);
				}
			});
		});
	},
	setupNav: function()
	{
		if($("#row3").length !== 0)
		{
			$("div.next").html('<a href="#">next</a>');
			$("div.prev").html('<a href="#">previous</a>');
			$("div.next a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (videoCategories.currentID+videoCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + videoCategories.currentID).offset().top) - parseInt($("#row" + (videoCategories.currentID+videoCategories.increment)).offset().top);
					var oldMargin = $(".videos-inner").css("margin-top")=='auto' || $(".videos-inner").css("margin-top")==0 ? 0 : parseInt($(".videos-inner").css("margin-top"));
					newMargin += oldMargin;
					videoCategories.currentID += videoCategories.increment;
				}
				else
				{
					newMargin = parseInt($(".videos-inner").css("margin-top"));
				}
				newMargin += "px";
				$(".videos-inner").animate({marginTop:newMargin}, 500);
			});
			$("div.prev a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (videoCategories.currentID-videoCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + videoCategories.currentID).offset().top) - parseInt($("#row" + (videoCategories.currentID-videoCategories.increment)).offset().top);
					var oldMargin = $(".videos-inner").css("margin-top")=='auto' || $(".videos-inner").css("margin-top")==0 ? 0 : parseInt($(".videos-inner").css("margin-top"));
					newMargin += oldMargin;
					videoCategories.currentID -= videoCategories.increment;
				}
				else
				{
					newMargin = 0;
				}
				newMargin += "px";
				
				$(".videos-inner").animate({marginTop:newMargin}, 500);
			});
		}
	},
	increment: 2,
	currentID: 0
};


var videoSearch =
{
	init: function()
	{
		if(browser.b().name == "ie") { this.ie = true; }
		$("a.video-search").click( function(e)
		{
			e.preventDefault();
			u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + videoSearch.file.form;
			$.ajax(
			{
				url: u,
				success: function(data)
				{
					$("body").append(data);
					$("#video-search").css("top", $("a.video-search").offset().top+15);
					$("#video-search").css("left", $("a.video-search").offset().left-38);
					if(this.ie) { $("#video-search").html('<br />' + $("#video-search").html()); }
					if(videoSearch.ie) { $("#video-search").css("display", "block"); }
					else { $("#video-search").animate({opacity:"show"}, 500); }
					$("body").append('<div id="video-search-mask"></div>');
					$("#video-search-mask").click( function(e) { videoSearch.destroy(); });
					videoSearch.events();
				},
				error: function(xhr) { error.warn("AJAX Error"); }
			});
		});
	},
	destroy: function()
	{
		if(this.ie) { $("#video-search").remove(); }
		else { $("#video-search").animate({opacity:"hide"}, 500, function() { $(this).remove(); }); }
		$("#video-search-mask").remove();

	},
	events: function()
	{
		$("#video-search div a").click( function(e) { e.preventDefault(); videoSearch.destroy(); });
/*		$("#video-search-form").submit( function(e)	
		{
			e.preventDefault();
			var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + videoSearch.file.result + "?q=" + escape($(this).children('input:first').val()) + "&r=" + window.location;
			videoSearch.destroy();
			$.ajax(
			{
				url: u,
				success: function(data)
				{
					$("div#article").html(data);
					videoCategories.setupNav();
				},
				error: function(xhr) { error.warn("AJAX Error"); }
			});
		});
*/	},
	ie: false,
	file:
	{
		form: "videos_search_form.php",
		result: "videos_search.php"
	}
}

