jQuery(document).ready(function() {
	// start slider if configured:
	if(document.SlideArticleInstance != null) {
		document.SlideArticleInstance.loadArticleContent();
	}
	
	jQuery(".sf-menu ul").superfish({
		delay:         	500,
		speed:			100,
		autoArrows:  	true,            
		dropShadows: 	false
	});
	
	
	var imageFolderGalleryDefaultOptions = {
			loadAllThumbs : false,
			showTitle : false,
			theme : "facebook",
			opacity : 0.50
	};
	
	var imageFolderGalleryElements = jQuery("div[rel^='imagefoldergallery_']");
	var imageFolderGalleryElementCount = imageFolderGalleryElements.length;
	imageFolderGalleryElements.each(function(galleryIndex) {
		
		var options;
		if(document.imageFolderGalleryOptions) {
			options = jQuery.extend({}, imageFolderGalleryDefaultOptions, document.imageFolderGalleryOptions);
		} else {
			options = imageFolderGalleryDefaultOptions;
		}
		
		var element = this;
		var rel = element.getAttribute("rel");
		var idx = rel.lastIndexOf("_");
		if(idx == -1) {
			return;
		}
		var myFolderId = rel.substring(idx + 1);
		var galleryId = myFolderId + "_" + galleryIndex;
		Liferay.Service.IG.IGImage.getImages(
			{
				folderId: myFolderId
			},
			function(message) {
				var exception = message.exception;
				if (!exception) {
					var buf = [];
					var images = message;
					if(images.length == 0) {
						return;
					}
					var thumbnail = null;
					for(var i=0; i<images.length; i++) {
						var image = images[i];
						if(image.name == "thumbnail" || image.description == "thumbnail") {
							thumbnail = image;
							break;
						}
					}
					var rel = "galleryImage["+galleryId+"]";
					var first = true;
					for(var i=0; i<images.length; i++) {
						var image = images[i];
						if(image == thumbnail) {
							continue;
						}
						buf.push("<a");
						// href
						buf.push(" href=\"");
						buf.push("/image/image_gallery?img_id=");
						buf.push(image.largeImageId);
						buf.push("\"");
						// style
						if(!first) {
							buf.push(" style=\"display: none\"");
						}
						// rel
						buf.push(" rel=\"");
						buf.push(rel);
						buf.push("\"")
						// title
						if(image.description != null && image.description != "") {
							buf.push(" title=\"");
							buf.push(image.description.replace(/&/g, "&amp;").replace(/"/g, "&quot;"));
							buf.push("\"");
						}
						buf.push(">");
						// show thumbnail with link on first image
						buf.push("<img");
						// src
						buf.push(" src=\"");
						buf.push("/image/image_gallery?img_id=");
						if(thumbnail != null && (first || !options.loadAllThumbs)) {
							buf.push(thumbnail.largeImageId);
						} else {
							buf.push(image.smallImageId);
						}
						buf.push("\"")
						// alt
						buf.push(" alt=\"");
						if(image.name != null && image.name != "") {
							buf.push(image.name);
						}
						buf.push("\"")
						buf.push("/>");
						buf.push("</a>");
						first = false;
					}
					jQuery(element).html(buf.join(""));
				}
				imageFolderGalleryElementCount--;
				if(imageFolderGalleryElementCount == 0) {
					// trigger pretty photo
					jQuery("a[rel~='galleryImage[']").prettyPhoto({
						opacity: options.opacity,
						theme : options.theme,
						showTitle: options.showTitle
					});
				}
			}
		);
	});

});

