/*						footerNav plugin															*/
/*	*can* set 'JavaScriptSchoolID' variable before calling, otherwise must set through options		*/
/*																									*/
/*	html structure:																	

	<ul id="footerNav">
		<li class="first L1s">
			<h2 class="footerL1"><a id="footerNav_126039" class="L1link footerNav" href="/podium/default.aspx?t=126039">About</a></h2>
			<ul class="footerL2" L1="126039"></ul>
		</li>
	</ul>

	NOTE: plugin will insert an <li> for each L2 under supplied L1 within the corresponding <ul>

	example CSS as follows:

	#footerNav { list-style:none; width:950px; margin:47px 0 0 187px; padding:0; }
		#footerNav li { float:left; width:128px; }
			.L1s { margin-left:8px; background:url(/images/dots.gif) top right repeat-y; }
				.L1s.first { margin-left:0; }
				.L1s.last { background:none; }
			.footerL1 { margin:0; padding:0; }
			.footerL1 a.footerNav, .footerL1 a.footerNav:link, .footerL1 a.footerNav:active, .footerL1 a.footerNav:hover, .footerL1 a.footerNav:visited {
				font-size:13px; font-weight:bold; color:#216053;
			}
				.footerL1 a.footerNav:hover { color:#75a89f; }
			.footerL1 em a, .footerL1 em a:link, .footerL1 em a:active, .footerL1 em a:hover, .footerL1 em a:visited { color:#c4c1a0; } 
			
			#footerNav .footerL2 { list-style:none; margin:0; padding:0; }
				#footerNav .L2item {}
					a.footerNav, a.footerNav:link, a.footerNav:active, a.footerNav:hover, a.footerNav:visited {
						display:block; font-family:Arial; font-size:11px; line-height:13px; color:#6c6f70; margin-bottom:2px;
					}
						a.footerNav, a.footerNav:link, a.footerNav:active, a.footerNav:hover, a.footerNav:visited { text-decoration:none; }
							a.footerNav:hover { text-decoration:underline; }
							.footerNav span { padding:0; }

	
usage: $("#footerNav").footerNav();

default options:
			xml_path:	"/xml/default.asp",		//path to xml file,
			sid:		0,						//if not set, defaults to the global JavaScriptSchoolID variable (one of these must be set!)
			L1complete: null,					//callback - runs after each list of L2s is written (passes the ul element written to)
			callback:	null					//final callback - runs after all data written to page (passes the object the plugin was initially run for)
*/

(function($){
	$.fn.footerNav = function(options) {
		var defaults = {
			xml_path:"/xml/default.asp",
			sid:0,
			L1complete:null,
			callback:null
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var	obj = $(this), parsedIDs = "", pages = new Array();
			JavaScriptSchoolID = (options.sid>0)?options.sid:JavaScriptSchoolID;
			$("ul", obj).each(function(){
				parsedIDs = ((parsedIDs.length>0)?parsedIDs + "|":"") + $(this).attr("L1");
			});
			if(parsedIDs.length > 0 && JavaScriptSchoolID){
				$.ajax({
					type: "GET",
					url: options.xml_path+"?sid="+JavaScriptSchoolID+"&type=menu&id="+parsedIDs,
					dataType: "xml",
					success: function(xml) {
						var L1count = 0;
						$(xml).find("L1").each(function(){
							pages[L1count] = new Array(), L2count = 0;
							pages[L1count][0] = $(this).attr("tid");
							pages[L1count][1] = new Array();
							$(this).find("item").each(function(){
								pages[L1count][1][L2count++] = {
									title:$(this).find("title").text(),
									url:$(this).find("url").text(),
									target:$(this).find("target").text()
								};
							});
							L1count++;
						});
					},
					error: function(request,tStatus,eThrown){ if(window.console && window.console.firebug){ console.log("footerNav plugin error: request='"+request+"', tStatus='"+tStatus+"', eThrown='"+eThrown+"'"); } },
					complete: function() {
						if(pages.length>0){
							$("ul", obj).each(function(){
								for(var i in pages){
									if(pages[i][0] == $(this).attr("L1")){
										for(var j in pages[i][1]){
											var $L2item = $("<li></li>").addClass("L2item");
											$("<a></a>")
												.addClass("L2link footerNav")
												.attr("href",pages[i][1][j].url)
												.attr("target",(pages[i][1][j].target>0)?"_blank":"")
												.html("<span>"+pages[i][1][j].title+"</span>")
												.appendTo($L2item);
											$L2item.appendTo($(this));
										}
										if($.isFunction(options.L1complete)){ options.callback($(this)); }
									}
								}
							});
						}
						else{ if(window.console && window.console.firebug){ console.log("footerNav plugin error: no pages in array"); } }
						if($.isFunction(options.callback)){ options.callback(obj); }
					}
				});
			}else{
				if(JavaScriptSchoolID){ if(window.console && window.console.firebug){ console.log("footerNav plugin error: no or bad pageid(s) passed"); } }
				else{ if(window.console && window.console.firebug){ console.log("footerNav plugin error: need school id set"); } }
			}
		});
	};
})(jQuery);
