/*		grabEvents plugin												*/
/*		   must set 'JavaScriptSchoolID' variable before calling		*/
/*	example CSS as follows:

ul.eventsReturn { float:left; height:20px; list-style:none; margin:0; padding:0; }
	li.eventsItem { height:20px; margin:0; padding:0; }
		div.startdate {}
		div.enddate { display:none; }
		div.titleNoLink { display:none; }
		a.titleLink { display:block; font-family:Verdana; font-weight:normal; font-size:12px; color:#000000; text-decoration:underline; }
			a.titleLink:hover { text-decoration:none; }
		p.eventsBrief { display:none; padding:0; margin:0; }
		p.eventsLong { display:none; padding:0; margin:0; }
		p.location { display:none; }
		a.mailTO { display:none; }
		
usage: $("#myElem").grabEvents();

default options:
			xml_path:	"/xml/default.asp",		//path to xml file
			maxEvents:10,						//max number of stories
			uniqueListID:"",					//if set, will prepend list ID with a unique identifier (string value)
			charTitle:999,						//max number of characters in title (uses .fSplit())
			charBrief:9999,						//max number of characters in brief (uses .fSplit())
			charLong:9999,						//max number of characters in long (uses .fSplit())
			noMailTo:0,							//if 1, will not write the MailTo link to the page
			callback:null						//callback function, passes unique id of parent (ul) - called immediately after data written to page (this is where you should call .cycle())
*/

(function($){
	$.fn.grabEvents = function(options) {
		var defaults = {
			xml_path:"/xml/default.asp",
			maxEvents:10,
			uniqueListID:"",
			charTitle:999,
			charBrief:9999,
			charLong:9999,
			noMailTo:0,
			callback:null
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var	obj = $(this),
				groupid = obj.attr("eid"),
				events = new Array();
			if(groupid.length > 0){
				$.ajax({
					type: "GET",
					url: options.xml_path+"?sid="+JavaScriptSchoolID+"&type=events&useCDATA=false&id="+groupid,
					dataType: "xml",
					success: function(xml) {
						var ecount = 0;
						$(xml).find('item').each(function(){
							events[ecount++] = {
								eventsID: $(this).find("title").attr("eventID"),
								title:$(this).find("title").text(),
								url:$(this).find("url").text(),
								brief:$(this).find("brief").text(),
								longDesc:$(this).find("full").text(),
								startdate:$(this).find("startdate").text(),
								enddate:$(this).find("enddate").text(),
								location:$(this).find("location").text(),
								mailTO:$(this).find("emailSignup").text(),
								mailTOdisplay:$(this).find("emailDisplay").text()
							};
						});
					},
					error: function(request,tStatus,eThrown){ if(window.console && window.console.firebug){ console.log("grabEvents plugin error: request='"+request+"', tStatus='"+tStatus+"', eThrown='"+eThrown+"'"); } },
					complete: function() {
						if(events.length>0){
							$AllEvents = $("<ul></ul>").attr("id",options.uniqueListID+"events"+groupid).addClass("eventsReturn");
							for(var i=0; i<events.length && i<options.maxEvents; i++) {
								$eventsItem = $("<li></li>").attr("id","event"+events[i].eventsID).addClass("eventsItem").addClass("eventNum_"+(i+1));
									$("<div></div>")
										.html(events[i].startdate)
										.addClass("startdate")
										.appendTo($eventsItem);
									$("<div></div>")
										.html(events[i].enddate)
										.addClass("enddate")
										.appendTo($eventsItem);
									$("<div></div>")
										.html(events[i].title)
										.fSplit({maxChar:options.charTitle})
										.addClass("titleNoLink")
										.appendTo($eventsItem);
									$("<a></a>")
										.attr("href",events[i].url)
										.html(events[i].title)
										.fSplit({maxChar:options.charTitle})
										.addClass("titleLink")
										.appendTo($eventsItem);
									$("<p></p>")
										.addClass("eventsBrief")
										.html(events[i].brief)
										.fSplit({maxChar:options.charBrief})
										.appendTo($eventsItem);
									$("<p></p>")
										.addClass("eventsLong")
										.html(events[i].longDesc)
										.fSplit({maxChar:options.charLong})
										.appendTo($eventsItem);
									$("<p></p>")
										.addClass("location")
										.html(events[i].location)
										.appendTo($eventsItem);
									if(options.noMailTo==0){
										$("<a></a>")
											.attr("href","mailto:"+events[i].mailTO)
											.html(events[i].mailTOdisplay)
											.addClass("mailTO")
											.appendTo($eventsItem);
									}
									$("<div></div>")
										.css("clear","both")
										.appendTo($eventsItem);
								$eventsItem.appendTo($AllEvents);
							}
							obj.html($AllEvents);
						}
						else { if(window.console && window.console.firebug){ console.log("grabEvents plugin error: events array empty"); } }
						if($.isFunction(options.callback)){ options.callback(options.uniqueListID+"headlines"+groupid); }
					}
				});
			}else{ if(window.console && window.console.firebug){ console.log("grabEvents plugin error: no or bad group id passed"); } }
		});
	};
})(jQuery);

(function($){
	$.fn.fSplit = function(options) {
		var	defaults = { maxChar:999999 },
			options = $.extend(defaults, options);
		return this.each(function() {
			var	strDummy=this.innerHTML, strOut="", strArray=strDummy.split(" ");
			if(strDummy.length>options.maxChar) {
				for(var i=0; i<strArray.length; i++) {
					if((strOut.length + strArray[i].length + 1) < options.maxChar) {
						if(i==0){ strOut = strArray[0]; }
						else{ strOut = strOut + " " + strArray[i]; }
					}
					else { strOut = strOut + "..."; i=9999999; }
				}
			} else { strOut = strDummy; }
			$(this).html(strOut);
		});
	};
})(jQuery);
