$(document).ready(function(){ 
	
	jQuery.fn.setActiveG = function(){
		
		var numGallery = arguments[0];

		 if(numGallery === undefined){
			 $(this).find("td#contents div").each(function(){
				 if(($(this).attr("id")).indexOf("gallery_") > -1){
					 $(this).hide();
					}
			 });
			 $("td#contents div").eq(0).show();
			 $("td#list_of_gallery a.a_gallery").eq(0).addClass("activeGalery");
		 }
		 else {
			 $(this).find("td#contents div").each(function(){
				 if(($(this).attr("id")).indexOf("gallery_") > -1){
					 $(this).hide();
						if(($(this).attr("id")) == ("gallery_"+numGallery)){
							
							$(this).show();
						}
					}
			 });
			 $().setBigImg();
		 }
	};
	
	jQuery.fn.getNumActiveG = function() {
		
		var tmp = ""; 
		$(this).find("td#contents div").each(function(){
			if(($(this).attr("id")).indexOf("gallery_") > -1){
				if($(this).is(':visible')){
					tmp = $(this).attr("id").substring(8);
				}
			}
		});
		return tmp;
	};
	
	//set active img to big view
	jQuery.fn.setBigImg = function() {
		
		var activeG = $(this).getNumActiveG();
		var a = $("div#gallery_"+activeG).find("img.activeImg").parent();
		var src = $(a).attr("href");
		var description = $(a).attr("title");
		$("div#photo_img_"+activeG).html('<a href="'+src+'"><img alt="'+description+'" class="bigimgsize" src="'+src+'" /></a>');
		
			
		
		$("div#photo_img_"+activeG+" a").lightBox({
			fixedNavigation:true,
			imageLoading: 'http://yeslogo.pl/lubudubu/images/lightbox-ico-loading.gif',
			imageBtnClose: 'http://yeslogo.pl/lubudubu/images/lightbox-btn-close.gif',
			imageBtnPrev: 'http://yeslogo.pl/lubudubu/images/lightbox-btn-prev.gif',
			imageBtnNext: 'http://yeslogo.pl/lubudubu/images/lightbox-btn-next.gif',
			imageBlank: 'http://yeslogo.pl/lubudubu/images/images/lightbox-blank.gif',
			txtImage: $(this).attr('title')
		});
		
	}
	
	jQuery.fn.setActiveImg = function(){
		
		var whichFoto = arguments[0];
		var activeG = $(this).getNumActiveG();
		
		$("div#gallery_"+activeG+" td.li_photo img.activeImg").removeClass('activeImg').addClass('distanceImg');
		$(whichFoto).removeClass('distanceImg').addClass('activeImg');
		$().setBigImg();
	}
	
	//move last to first
	jQuery.fn.moveLastToFirst = function(){
		
		var activeG = $(this).getNumActiveG();
	    //przesuń obiekt z ostatniego miejsca na miejsce przed pierwszym. Chodzi o to, żeby użytkownik widział wszystkie pozycje ze slidera bez przerwy.
		$("div#gallery_"+activeG+" td.li_photo:first").before($("div#gallery_"+activeG+" td.li_photo:last"));
	}
	
	//move first to last
	jQuery.fn.moveFirstToLast = function(){
		
		var activeG = $(this).getNumActiveG();
	    //przesuń obiekt z ostatniego miejsca na miejsce przed pierwszym. Chodzi o to, żeby użytkownik widział wszystkie pozycje ze slidera bez przerwy.
		$("div#gallery_"+activeG+" td.li_photo:last").after($("div#gallery_"+activeG+" td.li_photo:first"));
	}
	
	//get active foto
	jQuery.fn.getActiveImg = function(){
		
		tmp = "";
		var activeG = $(this).getNumActiveG();
		tmp = $("div#gallery_"+activeG+" img.activeImg");
		return tmp;
	}
	
	//get active foto
	jQuery.fn.getTableSize = function(){
		tmp = "";
		var activeG = $(this).getNumActiveG();
		var widthTable = $("div#gallery_"+activeG+" table.list_photo").outerWidth();
		tmp = widthTable;
		return tmp;
	}
	
	//lightbox on frontend shop
	$("td#shop a.foto_link").each(function(){
		$(this).lightBox({
			fixedNavigation:true,
			imageLoading: 'http://yeslogo.pl/lubudubu/images/lightbox-ico-loading.gif',
			imageBtnClose: 'http://yeslogo.pl/lubudubu/images/lightbox-btn-close.gif',
			imageBtnPrev: 'http://yeslogo.pl/lubudubu/images/lightbox-btn-prev.gif',
			imageBtnNext: 'http://yeslogo.pl/lubudubu/images/lightbox-btn-next.gif',
			imageBlank: 'http://yeslogo.pl/lubudubu/images/images/lightbox-blank.gif',
			txtImage: $(this).attr('title')
		});
	});
	
	
	$("a.a_gallery").css({cursor:"pointer"}).click(function(){
		
		var idGallery = $(this).attr("id").substring(3);
		
		//szerokosc zdjecia * ilosc zdjec widocznych
		var total = 80 * 3;
		
		$("td#list_of_gallery a.a_gallery").each(function(){
			$(this).removeClass("activeGalery");
		});
		$(this).addClass("activeGalery");
		
		$().setActiveG(idGallery);
		
		//na nastepne clik nie przesuwa obrazka
		if($(this).attr("class") != "a_gallery activeGalery"){
			$().moveLastToFirst();
		}
		
		
		if($().getTableSize() >= total){
			$("td.showprev").css("display", "");
			 
			
			if($(this).attr("class") != "a_gallery activeGalery"){
				$().moveLastToFirst();
			}
			
		}
		else
		{
			$("td.showprev").css("display", "none");
			if($().getTableSize() == 80){
				$("td.shownext").css("display", "none");
			}
		}
		
		
	});
	
		
	//click
	$("table.list_photo a").each(function(){
		
		$(this).css({cursor:"default"}).click(function(){
			
			return false;
		});
	});
	
	//show prev foto
	$("td.showprev").click(function(){
		var activeG = $().getNumActiveG();
		if($("div#photo_img_"+activeG+" img").attr("complete")){
			var activeG = $().getNumActiveG();
			$().mySlider("left");
		}
	});
	
	//show next foto
	$("td.shownext").click(function(){
		var activeG = $().getNumActiveG();
		if($("div#photo_img_"+activeG+" img").attr("complete")){
		
			var activeG = $().getNumActiveG();
			$().mySlider("right");
		}
	});
	
	//My slider
	
	var hover_pause = 1;
    var key_slide = 1;
    
    //funkcja przesuwania
    jQuery.fn.mySlider = function(){
    	
    	var where = arguments[0];
    	var activeG = $().getNumActiveG();
    	var activeImg = $().getActiveImg();
    	
    	//pobieranie szerokości pojedynczego obiektu
        var item_width = $("div#gallery_"+activeG+" table.list_photo td.li_photo").outerWidth();

        //sprawdzenie czy animacja ma iść w prawo czy w lewo
        if(where == 'left'){
            //kalkulacja ruchu w lewo
            var left_indent = parseInt($("div#gallery_"+activeG+" table.list_photo").css('left')) + item_width;
            var whichFoto = $(activeImg).parents('td.li_photo').prev().find("img");
        }else{
            //kalkulacja ruchu w prawo
            var left_indent = parseInt($("div#gallery_"+activeG+" table.list_photo").css('left')) - item_width;
            var whichFoto = $(activeImg).parents('td.li_photo').next().find("img");
        }

        $().setActiveImg(whichFoto);
        
    	$("div#gallery_"+activeG+" table.list_photo:not(:animated)").animate({'left' : left_indent},0,function(){
    		
    		
    		 
            //tworzenie iluzji nieskończonej prezentacji
            if(where == 'left'){
                $().moveLastToFirst();
            }else{
                $().moveFirstToLast();
            }
            

            $("div#gallery_"+activeG+" table.list_photo").css({'left' : '-80px'});
        });
    	
    }
        
    $("input#number").keydown(function(event) {
		// Allow only backspace and delete
		if ( event.keyCode == 46 || event.keyCode == 8 ) {
			// let it happen, don't do anything
		}
		else {
			// Ensure that it is a number and stop the keypress
			if (event.keyCode < 48 || event.keyCode > 57 ) {
				event.preventDefault();
				$("div#errmsg").html("Tylko liczby").show().fadeOut(1000);
			    //return false;
			}	
		}
	});

    
	//
    
	$(this).setActiveG();
	$(this).setBigImg();
	//$(this).moveLastToFirst();
	
});




