function initGallerias(tip,disable){
	_filter(tip,"div.galleria.modal").each(function(e){ initModalGalleria($(this)); });
	_filter(tip,".gallery_demo_unstyled").each(function(e){ initGalleria($(this)); });
	_filter(tip,".openGalleria").css({"cursor":"pointer"}).each(function(e){ initOpenGalleria($(this)); });
}

function initOpenGalleria(obj,disable){
	obj.unbind();
	
	if(disable) return false;
	if(!obj.hasClass("openGalleria_inited")){
		//alert(obj.attr("tagName"));
		if(obj.attr("tagName")=="IMG"){
			obj.parent().after("<a href='#' class=openGalleria>View Photo Gallery &raquo;</a>");
			var lnk = obj.parent().next().css({"float":"right","text-decoration":"none","color":"#333"});
			initOpenGalleria(lnk);
		}
		obj.addClass("openGalleria_inited");
	}
	switch(appMode){
		case "EDIT":
			if(obj.attr("tagName")=="A") obj.html("Edit Photo Gallery");
		break;
		case "VIEW":
			if(obj.attr("tagName")=="A") obj.html("View Photo Gallery &raquo;");
			obj.click(function(e){
				//alert(obj.attr("tagName"));
				stopDef(e);
				$("#galleria").dialog('open');
			});
		break;
	}
	return false;
}

function initModalGalleria(dia){	
	$(dia).dialog({
		modal: false,
		autoOpen: false,
		dialogClass: "diaGallery",
		width: 750,
		open: function(event, ui) { initGalleria($(this).find("ul.gallery_demo_unstyled")); }
	});
}


function initGalleria(ul){
	if(!ul.hasClass("galleria_inited")){
		ul.addClass('gallery_demo'); // adds new class name to maintain degradability
		ul.addClass('galleria_inited');
		ul.parents(".modal").prepend("<a href='#' class=close>close</a>").find("a.close").click(function(e){ 
			stopDef(e);
			$(this).parents(".modal").dialog("close");
		});
		ul.galleria({
				clickNext : true, // helper for making the image clickable
				insert    : '#main_image', // the containing selector for our main image
				onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
					
					// fade in the image & caption
					if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
						image.css('display','none').fadeIn(1000);
					}
					caption.css('display','none').fadeIn(1000);
					
					// fetch the thumbnail container
					var _li = thumb.parents('li');
					
					// fade out inactive thumbnail
					_li.siblings().children('img.selected').fadeTo(500,0.3);
					
					// fade in active thumbnail
					thumb.fadeTo('fast',1).addClass('selected');
					
					// add a title for the clickable image
					image.attr('title','Next image >>');
				},
				onThumb : function(thumb) { // thumbnail effects goes here
					
					// fetch the thumbnail container
					var _li = thumb.parents('li');
					
					// if thumbnail is active, fade all the way.
					var _fadeTo = _li.is('.active') ? '1' : '0.3';
					
					// fade in the thumbnail when finnished loading
					thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
					
					// hover effects
					thumb.hover(
						function() { thumb.fadeTo('fast',1); },
						function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
					)
				}
			});
		}
		ul.find("li:first").addClass("active"); 
}
