/**
 * uGallery jQuery plugin
 * Author: Miro Zoricak (zoricak at udesign.sk)
 * Website: http://zori.udesign.sk
 * v 1.0 beta
 */
(function($){
		  
		  
	var settings = {
		width: 800,
		height: 388,
		thumbWidth: 127,
		thumbHeight: 50,
		thumbOpacity: 0.5,
		thumbHoverOpacity: 1.0,
		displayAlt: true
	}
	
	$.uGallery = function(userSettings){
		var images = [];
		$.extend(settings, userSettings);
		// parse input structure to images
		$("ul.gal>li>img").each(function(index, element){
			images[index] = $(element)
				.css({width: element.width+"px", height: element.height+"px"})
				.attr("src", $(element).attr("src"))
				.attr("alt", $(element).attr("alt"));
			
		});
		// recreate gallery structure using divs
		$("ul.gal").replaceWith("<div class='gal'><div class='gal-thumbs'></div><div class='gal-main-viewer'></div></div>");
		for(var i = 0; i < images.length; i++){ // fill it with images
			$("div.gal-thumbs").append(makeThumb(images[i]));
		}
		$("div.gal-thumbs>img").wrap("<div class='gal-thumb'><div class='gal-thumb-padder'></div></div>");
		// display the first thumb image in main viewer
		$("div.gal-thumbs>div.gal-thumb>div.gal-thumb-padder:first>img").trigger('click');
		// style the gallery
		setupCSS(images);
		// fade thumbs to the initial state
		$("div.gal-thumb-padder>img").fadeTo("slow", settings.thumbOpacity);
		// add thumb highlight onmouseover behaviour
		$("div.gal-thumb-padder>img").hover(
			function(){ $(this).fadeTo("fast", settings.thumbHoverOpacity); }, 
			function(){ $(this).fadeTo("slow", settings.thumbOpacity); }
		);
	}
	
	/**
	 * crates proportionally resized image with onclick showing full image in image viewer
	 */
	makeThumb = function(img){
		var image = $("<img src='"+$(img).attr("src")+"' alt='"+$(img).attr("alt")+"' />");
		image.css({width: "127px", height: "50px"});
		image.css({msInterpolationMode: "bicubic"}); // smooth out thumbs in IE7
		image.bind("click", img, function(e){
										  
			
			var image = $("<img src='"+$(img).attr("src")+"' alt='"+$(img).attr("alt")+"' />");
			
			var title = $("<div class='gal-title' style='overflow:auto'>"+$(img).attr("title")+"</div>");
			//image.css(proportionalDimensions(img, {x: settings.width - 20, y: settings.height - 20}));
			var alt = $("<div class='gal-alt' style='overflow:auto;'>"+"<i class='title'>"+$(img).attr("title")+"</i>"+"<span class='alt' style='font-weight:normal;'>"+$(img).attr("alt")+"</span>"+"</div>");
			alt.css({
				
			});
			
			alt.fadeTo("fast", 0.7);
			$("div.gal-main-viewer").fadeOut("slow", function(){
				$(this).html(image).hide().fadeIn("slow").append(alt);
				
				if(settings.displayAlt){
					$("div.gal-alt").animate({"marginTop": -60+"px"}, 600);
				}
				
				
			});
		});
		return image;
	}
	
	/**
	 * style the gallery
	 */
	 

	 

})(jQuery)