/*
 * 	Inline Preview 0.1 - jQuery plugin
 *	written by Martin Stanek	
 *	Copyright (c) 2009 Martin Stanek
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function($) {

	$.fn.inlinePreview = function(options){
	  
		// default configuration properties
		var defaults = {	
			numericId:		't',		
			hideDelay: 		500,
			maxHeight: 		360,
			optWidth:		257,
                        optHeight:		360,
			ratio:			7/10
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var im = $("#gallery-image", obj);
			var th = $("#gallery-thumbs", obj);
			var mask = $("#gallery-mask", obj);
                        var tags = $("#tags p");
			var hasImage = false;
			var optHeight = options.optWidth/options.ratio;
				   	    
                        // Onclick function
			$("a", th).click(function(){		
				loadImage($("img", this).attr("src"), $(this).attr("href"), true);
                                loadLabel($(this).parent().next("dd").text());
				return false;
			});
								
			
                        function loadLabel(text){
                            if(tags) {
                                tags.text(text);
                            }
                        };
                        
			function loadImage(source, href, clicked){
				im.append("<span id='loading2'></span>");
				if(hasImage) {
					var image1 = $('a', im);
					image1.remove();  
				}			        							
				
				var img = new Image();
				var link = $("<a href=\""+href+"\"></a>");
					
				$(img).load(function () {					        
				      $(this).css("display", "none");				   
				      $("#loading2").remove();
				     				     
				      // image height
				      var h = $(this).height();
				      var w = $(this).width();
				      
				      if(h < options.optHeight) {
                                        im.css({
                                            "display": "table-cell",
                                            "vertical-align": "middle"
                                        });                                  
                                        $("img", im).addClass("smallimg");
				      }				      
				      else {
				      	im.css("width", options.optWidth + "px");			
				      }
				     

				      // fade our image in to create a nice effect
				      link.append(this);
				      im.append(link);				      
				      $(this).fadeIn();
				      
				    })
				    .error(function () {  })
				    .attr('src', source);
				    
				 link.click(function(e){                             
                    $.fancybox({'href': $(this).attr("href"),'autoScale': true});
                    e.preventDefault();
                  })

				 hasImage = true;
				   				 										
			};
			// init	
			/* nahrat prvni obrazek */	
			var first = $("#gallery-thumbs a").eq(0);			
			loadImage($("img",first).attr("src"), first.attr("href"), false);
                        loadLabel($("#gallery-thumbs .gallery-caption").eq(0).text());
			
		});
	  
	};

})(jQuery);




