(function($){
	$.fn.createQuicklinks = function(options) {
		
		var defaults = {
			link_xml:"/xml/default.asp",
			xType:"link",
			css_path:"/css/quicklinks.css",
			animation:true,
			direction:"down",
			offset:{h:0,v:0}
		};
		var options = $.extend(defaults, options);
		
		/*
		these quicklinks are modified to use the new XML, and also can go up or down depending on what you pass them.
		
		*/
		return this.each(function() {
			//alert("testing");
			var obj = $(this);
			
			$("<link rel='stylesheet' type='text/css' href='"+options.css_path+"'/>").appendTo("head");
			
			var ajaxURL = "";
			
			if(options.schoolid != undefined && options.id != undefined){
				ajaxURL = options.link_xml+"?sid="+options.schoolid+"&type="+options.xType+"&id="+options.id;
			}else{
				//no schoolid set, hide the button
				HideButton();
			}
			var links = new Array();
			var lcount = 0;
			if(ajaxURL.length > 0){				
				$.ajax({
					type: "GET",
					url: ajaxURL,
					dataType: "xml",
					success: function(xml) {						
						
						//load links					
						$(xml).find('item').each(function(){
							links[lcount] = {
								url:$(this).find("url").text(),
								target:$(this).find("target").text(),
								title:$(this).find("title").text()
							};
							lcount++;	
						});
							
												
					},
					complete:function(){
						
						buildQL();
					},
					error: function(request,tStatus,eThrown){
						HideButton();
					}
				});
				
			}
			
			function HideButton(){
				//alert("something is very wrong here.");
				//obj.css("display","none");
				//alert("button hidden");
			}
			
			function buildQL(){
				if(lcount > 0){
							//build the dropdown
							console.debug("build the dd");
							var qlCont = $(document.createElement("div"))
										.attr("id","ql-container")
										.css({"position":"absolute","top":"0px","left":"-9999px"});
							console.debug(obj.hasClass("jmhqlink"));
							qlCont.appendTo(obj);
							
							$("<div id='ql-header'><!-- --></div>").appendTo(qlCont);
							$("<div id='ql-holder'></div>").appendTo(qlCont);
							
							for(i=0; i<links.length; i++){
								if(links[i].target.length > 0){myTarget = " target='_blank'";}else{myTarget = "";}
								$("<a class='quicklinks' href='"+links[i].url+"'"+myTarget+"><span>"+links[i].title+"</span></a>").appendTo("#ql-holder");
							}
							//alert("build Hover");
							obj.hover(function(){
								//alert("over");
								console.debug("over");
								var offset = $(this).offset();
								if($.browser.msie){
									qlCont.css("left",(offset.left + options.offset.h)+"px");
									if(options.direction == "up"){
										qlCont.css("top",(offset.top-qlCont.height()-2)+"px");
									}else{
										qlCont.css("top",(offset.top+$(this).height()-2)+"px");
									}
								}else{
									console.debug(offset.left + options.offset.h);
									qlCont.css("left",(offset.left + options.offset.h)+"px");
									if(options.direction == "up"){
										qlCont.css("top",(offset.top-qlCont.height())+"px");
									}else{
										qlCont.css("top",(offset.top+$(this).height())+"px");
									}
								}
							},function(){
								//alert("off");
								qlCont.css("left","-9999px");
							});								
							
						}else{
							HideButton();
						}
			}
		
		});
		
	};
})(jQuery);