/*						getPhotoAlbums plugin					*/
/*	must set 'JavaScriptSchoolID' variable before calling	*/
/*	example CSS as follows:

ul.photosReturn { float:left; height:260px; list-style:none; margin:0; padding:0; }
	li.photoItem { display:block; margin-bottom:5px; }
	li.listitem_X { margin-left:10px; }
		li.photoItem img { margin:3px 0; }
	
usage: $("#myElem").getPhotoAlbums();

default options:
			xml_path:	"/xml/default.asp",		//path to xml file
			uniqueListID:"",					//if set, will prepend list ID with a unique identifier (string value)
			maxAlbums:3,						//number of albums to return
			randomizeAlbums:0,					//if 1, returns random albums from set
			randomizePhotos:0,					//if 1, returns randomized photos within albums
			startRandomKeepOrder:0,				//if 1, returns photos starting from a random position, but keeps the order intact
			writeInHorizontalOrder:0,			//if 1, will write photos to page for film-strip style sets showing horizontal order (See The Buckley School homepage for example)
			maxItems:10,						//max number of list items
			photosPerItem:1,					//number of images per list item
			setTitle:0,							//if 1, pulls sets title attribute to caption
			hardPhotoWidth:0,					//if greater than 1, sets photo width attribute to specified width (overrides hardPhotoHeight)
			hardPhotoHeight:0,					//if greater than 1, sets photo height attribute to specified height (overridden by hardPhotoWidth)
			minPhotosPerAlbum:1,				//album will be ignored if it contains less than the specified number of photos
			callback1:null,						//callback function 1, passes unique id of album (ul) - called immediately after ul written to page
			callback2:null						//callback function 2, passes total # of albums - called after all data written to page (this is where you should call .cycle(), etc)

NOTE: if neither hardPhotoWidth nor hardPhotoHeight are set, the images native width/height are set to the image as provided by db
*/

(function($){
	$.fn.getPhotoAlbums = function(options) {
		var defaults = {
			xml_path:"/xml/default.asp",
			uniqueListID:"",
			maxAlbums:3,
			randomizeAlbums:0,
			randomizePhotos:0,
			startRandomKeepOrder:0,
			writeInHorizontalOrder:0,
			maxItems:10,
			photosPerItem:1,
			setTitle:0,
			hardPhotoWidth:0,
			hardPhotoHeight:0,
			minPhotosPerAlbum:1,
			callback1:null,
			callback2:null
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var	obj = $(this),
				groupid = obj.attr("pid"),
				photos = new Array();
			if(groupid.length > 0){
				$.ajax({
					type: "GET",
					url: options.xml_path+"?sid="+JavaScriptSchoolID+"&type=photoall&id="+groupid,
					dataType: "xml",
					success: function(xml) {
						var albumCount = 0;
						$(xml).find('item').each(function(){
							photos[albumCount] = new Array();
							var pcount = 0;
							$(this).find('image').each(function(){
								photos[albumCount][pcount++] = {
									path:$(this).find("path").text(),
									cap:$(this).find("cap").text(),
									height:$(this).find("height").text(),
									width: $(this).find("width").text()
								};
							});
							albumCount++;
						});
					},
					error: function(request,tStatus,eThrown){ if(window.console && window.console.firebug){ console.log("getPhotoAlbums plugin error: request='"+request+"', tStatus='"+tStatus+"', eThrown='"+eThrown+"'"); } },
					complete: function() {
						if(photos.length>0){
							var $AllPhotos = new Array();
							if(options.randomizeAlbums>0){ photos.sort(function(){ return Math.round(Math.random())-0.5; }) }
							for(var h=0, ttlCnt=0; h<photos.length && ttlCnt<options.maxAlbums; h++){
								var startIndex = (options.startRandomKeepOrder>0)?Math.floor(Math.random()*(photos[h].length-1)):0;
								if(photos[h].length>=options.minPhotosPerAlbum){
									if(options.randomizePhotos>0){ photos[h].sort(function(){ return Math.round(Math.random())-0.5; }) }
									var	i=startIndex,
										totalPhotos=0,
										totalPhotosPerItem = (options.photosPerItem <= (photos[h].length/options.maxItems)||(options.photosPerItem==1)) ? options.photosPerItem : (photos[h].length/options.maxItems),
										horizPhotoAdjustment = (options.writeInHorizontalOrder>0)?(options.maxItems):1;
									$AllPhotos[h] = $("<ul></ul>").attr("id",options.uniqueListID+"photoAlbum_"+h+"_"+groupid).addClass("photosReturn");
									for(var listitem=1; listitem<=options.maxItems; listitem++ ){
										$PhotoItem = $("<li></li>").addClass("photoItem").addClass("listitem_"+listitem);
										for(var photosInItem=0; (photosInItem<totalPhotosPerItem) && (totalPhotos<photos[h].length); totalPhotos++,photosInItem++ ) {
											if(photos[h][i].path.length>0){
												$Image = $("<img />")
													.attr("src",photos[h][i].path)
													.attr("alt",photos[h][i].cap)
													.attr("title",options.setTitle==1 ? photos[h][i].cap : "");
												if(options.hardPhotoWidth>0){ $Image.attr("width",options.hardPhotoWidth); }
												else if(options.hardPhotoHeight>0){ $Image.attr("height",options.hardPhotoHeight); }
												else{ $Image.attr("width",photos[h][i].width).attr("height",photos[h][i].height); }
												$Image.appendTo($PhotoItem);
											}
											if(options.writeInHorizontalOrder>0){ i += ((i+horizPhotoAdjustment)>(photos[h].length-1))?(horizPhotoAdjustment-1)-(photos[h].length-1):horizPhotoAdjustment; }
											else{ i++; }
										}
										$PhotoItem.appendTo($AllPhotos[h]);
										if(!options.writeInHorizontalOrder>0){i=(i>photos[h].length-1)?0:i;}
										else{i=(i>=(photos[h].length-1))?0:i+1;}
									}
									ttlCnt++;
								}
							}
							$($AllPhotos).each(function(){
								$(this).children().each(function(){if($(this).children().length<1){$(this).remove();}});
								obj.append($(this));
								if($.isFunction(options.callback1)){ options.callback1($(this).attr("id")); }
							});
						}
						else{ if(window.console && window.console.firebug){ console.log("getPhotoAlbums plugin error: no photos in array"); } }
						if($.isFunction(options.callback2)){ options.callback2(photos.length,groupid); }
					}
				});
			}else{ if(window.console && window.console.firebug){ console.log("getPhotoAlbums plugin error: no or bad groupid passed"); } }
		});
	};
})(jQuery);
