/**
 * uGallery jQuery plugin
 * Author: Miro Zoricak (zoricak at udesign.sk)
 * Website: http://zori.udesign.sk
 * v 1.0 beta
 */
(function($){
	var settings = {
		width: 940,
		height: 252,
		thumbWidth: 50,
		thumbHeight: 50,
		thumbOpacity: 0.5,
		thumbHoverOpacity: 1,
		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")+"' />");
			//image.css(proportionalDimensions(img, {x: settings.width - 20, y: settings.height - 20}));
			var alt = $("<div class='gal-alt'>"+$(img).attr("alt")+"</div>");
			alt.css({
				clear: "both", 
				width: 640 +"px", 
				padding: "10px", 
				"background-color": "black", 
				"margin": "auto",
				color: "white",
                textAlign: "left"
			});
			alt.fadeTo("fast", 0.6);
			$("div.gal-main-viewer").fadeOut("slow", function(){
				$(this).html(image).hide().fadeIn("slow").append(alt);
				if(settings.displayAlt){
					$("div.gal-alt").animate({marginTop: "-62px"}, 600);
				}
			});
		});
		return image;
	}
	
	/**
	 * style the gallery
	 */
	setupCSS = function(images){
		$("div.gal-thumb").css({
			float: "left", 
			width: settings.thumbWidth+"px", 
			height: settings.thumbHeight+"px", 
			"text-align": "center", 
			"margin": "5px 5px 0px 0px",
			border: "1px solid #343434",
			padding: "5px",
			overflow: "hidden"
		});
		
		$("div.gal-thumb-padder").css({
			margin: "auto",
			width: settings.thumbWidth-5+"px",
			height: settings.thumbHeight-2+"px",
			overflow: "hidden"
		});
		
		$("div.gal").css({
			width: "940px",
			overflow: "hidden",
            marginLeft: "30px",
            marginTop: "10px"
		});
		
		$("div.gal-main-viewer").css({
			width: "640px", 
			height: settings.height-20+"px", 
			"text-align": "center",
			overflow: "hidden",
			margin: "auto",
			float: "right",
			border: "3px solid #141414",
            marginTop: "5px"
		});
		
		$("div.gal-thumbs-wrapper").css({
			width: settings.width-20+"px", 
			margin: "auto", 
			overflow: "hidden",
			"padding-top": "5px",
			"padding-bottom": "10px"
		});
		
		$("div.gal-thumbs").css({ 
			width: "240px",
			float: "left"
		});
		
		$("div.gal-thumb>img").css("background-color", "black");
	}
})(jQuery)
