
(function($){  
 $.fn.efFrontGallery = function( options ) {  
        
     return this.each(function() {  
            var efgID = $(this).attr("id");
            var efgInstance = $(this);                 
            //Preklady z EdgeTranslations / nacist ajaxem z externiho zdroje!              
            efgInstance.ID = efgID;    
            //Nastaveni catalogu
            defaultOptions = {
                selected    :  1   //poradi defaultne vybranyho boxu                               
            }
            efgInstance.options = $.extend(defaultOptions,options);
                  
            $.fn.efFrontGallery.initialize( efgInstance );
                         
        });      
  };  
   
 $.extend($.fn.efFrontGallery, {
             initialize: function (e) {             
            //pocet obrazku v poli         
           e.options.elementsLength = $("li",e).length;
           //alert( e.options.elementsLength);
            
           $("li",e).each(function(i) {
                // $("#main_image").append($("span",this).html());
                var item = this;                       
                $("img",item).fadeTo("fast", 0.33);
                $.fn.efFrontGallery.onThumbHover(e, item);
                $("img",this).click(function () {                   
                    $.fn.efFrontGallery.onThumbClick(e, item, i);                        
                });
                if (i == 0) {
                    $("img",this).click();
                    $("img",item).stop().fadeTo("fast", 1);
                };
            });             
            
        },                              
        onThumbClick: function(e, thumb, position){        
            $("li > img",e).stop().fadeTo("fast", 0.33);
            $("img",thumb).stop().fadeTo("fast", 1);
            var bigImageSrc = $("span",thumb).html();
            var caption =  $("img",thumb).attr("title");
            caption = (caption == '' || typeof(caption) == 'undefined') ? '' : caption;
            var title =  $("img",thumb).attr("alt");
            title = (title == '' || typeof(title) == 'undefined') ? '' : title;            
            var gallery = $(e).parent();
            $(".efFrontGalleryBigImage", gallery).unbind();

            var re = new RegExp(/\.flv$/);
            if (bigImageSrc.match(re)) {
            	 var fileRe = new RegExp(/\/([^\/]+)$/);
            	 var filename = bigImageSrc.match(fileRe)
            	 var resRe = new RegExp(/\/([0-9]+)\/r([0-9]+)e([0-9]+)\/.+/);
            	 var size = bigImageSrc.match(resRe); 
            	 size[2] = 480;
            	 size[3] = 352;          
            	// console.log(size);
            	 var template = '<object id="myId" height="'+size[3]+'" width="'+size[2]+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">'+
            	 				'<param value="noscale" name="scale"/>'+
            	 				'<param value="true" name="allowfullscreen"/>'+
            	 				'<param value="/public/flash/flvplayer.swf?video=/cs-CZ/files/'+size[1]+'/'+filename[1]+'" name="movie"/>'+
            	 					'<object height="'+size[3]+'" width="'+size[2]+'" scale="noscale" data="/public/flash/flvplayer.swf?video=/cs-CZ/files/'+size[1]+'/'+filename[1]+'" type="application/x-shockwave-flash">'+
            	 					'</object>'+
            	 				'</object>';
            	$(".efFrontGalleryBigImage", gallery).html(template);
            	
            } else {
            	$(".efFrontGalleryBigImage", gallery).html('<img  src="'+bigImageSrc+'" alt="'+title+'" title="'+caption+'">');
            }            
            //style="max-width: '+$('.efFrontGallery').width()+'px; width: expression(do­cument.body.cli­entWidth > '+$('.efFrontGallery').width()+'? \"'+$('.efFrontGallery').width()+'px\" : \"auto\" );"
            $(".efFrontGalleryCaption", gallery).html(caption);       
            $(".efFrontGalleryTitle", gallery).html(title);       
            $(".efFrontGalleryBigImage", gallery).click(function()  {
                 $.fn.efFrontGallery.onImageClick(e, this, position); 
            });
        },
        onThumbHover: function(e, thumb) {
            $("img", thumb).hover(function () {               
                        $(this).fadeTo("fast", 1)
                    }, function () {
                        $(this).fadeTo("fast", 0.33);
                    });

        },
        onImageClick: function(e, bigImage, position) {        
           // $("img", bigImage).attr("src");
            var max = $("li",e).length - 1;
            if (position >= max) position = -1; 
            
         //  alert(max + " " +position);
           
           $("li:eq("+( position+1 )+") > img",e).click();
            
        }
         
 });
    
})(jQuery);