//Ladowanie i kontrolopwanie rozmarow zdjec w tle
function backgroundimages(jasnoscmenu){
    
    $(document).ready(function(){
        
        var zdjeciaTab1 = new Array(                                            //Tablice z seriami zdjec
            'photos/background/018xx.jpg', 'false',
            'photos/background/019xx.jpg', 'false',
            'photos/background/029xx.jpg', 'false',
            'photos/background/036xx.jpg', 'true'
            );
        var zdjeciaTab2 = new Array(
            'photos/background/059xx.jpg', 'false',
            'photos/background/078xx.jpg', 'true',
            'photos/background/084xx.jpg', 'true',
            'photos/background/085xx.jpg', 'true'
            );
        var zdjeciaTab3 = new Array(
            'photos/background/092axx.jpg', 'false',
            'photos/background/152xx.jpg', 'false',
            'photos/background/169xx.jpg', 'true',
            'photos/background/170xx.jpg', 'false'
            );
        
        var losowanaTab = Math.round( Math.random() * 2 ) + 1;                  //Losowanie tablicy ze zdjeciami
        var zaladowaneZdjecia = 0;
        
        ladujWszystkieZdjecia();
        
        function ladujWszystkieZdjecia(){
            ladujZdjecie();
        }
        
        //Ladowanie zdjecia tla
        function ladujZdjecie(){
            
            var img = new Image();
            $(img).load(function(status) {
                
                $(img).css('display', 'none');
                $('#bgimages').append(img);
                
                if (status != "error"){
                    zaladowaneZdjecia = zaladowaneZdjecia + 2;
                    if(zaladowaneZdjecia != eval('zdjeciaTab' + losowanaTab).length){ ladujWszystkieZdjecia(); }        //Jezeli nie zaladowano wszystkich, laduj nastepne
                    if(zaladowaneZdjecia == eval('zdjeciaTab' + losowanaTab).length){                                   //Jezeli zaladowano wszystkie zdjecia
                        $('#bgimages').removeClass('loading');
                        pokazSlajdow();
                        resizeImg();
                    }      
                }
                
                }).attr('src', eval('zdjeciaTab' + losowanaTab)[zaladowaneZdjecia]);
            
        }//ladujZdjecie
        
        var licznik = 0;
        var intId;
        
        function pokazSlajdow(){
            
            $('#bgimages img:eq(' + licznik + ')').fadeIn(500);
            licznik++;
            
            ustawMenu();
            intId = setInterval(zmienZdjecie, 5000);
        
        }//pokazSlajdow
        
        function zmienZdjecie(){
            
            
            $('#bgimages img').fadeOut(500);
            
            $('#bgimages img:eq(' + licznik + ')').fadeIn(500, function() {
                licznik++;
            ustawMenu();
                if(licznik == (eval('zdjeciaTab' + losowanaTab).length / 2)){ licznik = 0; }
                });
        
        }//zmienZdjecie
        
        
        //Sprawdza w tablicy jasnosc zdjecia i ustawia odpowiednie menu
        var jasnoscLicznik = -1;
        function ustawMenu(){
            if(jasnoscmenu != false){
                if(eval('zdjeciaTab' + losowanaTab)[jasnoscLicznik + 2] == 'true'){
                    $('#bottom').addClass('dark');
                    $('#bottom').removeClass('light');
                    jasnoscLicznik = jasnoscLicznik + 2;
                    }else{
                    $('#bottom').removeClass('dark');
                    $('#bottom').addClass('light');
                    jasnoscLicznik = jasnoscLicznik + 2;
                }
            }
            if(jasnoscLicznik - 3 == (eval('zdjeciaTab' + losowanaTab).length / 2)){ jasnoscLicznik = -1 }
        }//chBgBrightness
        
        $(window).resize(function() {
            resizeImg();
        });
        
        //Kontrona szerokosci i wysokosci zdjec w tle
        function resizeImg(){
            
            var browserWidth = $(window).width();
            var browserHeight = $(window).height();
            var imgWidth = $("#bgimages img").width();
            var imgHeight = $("#bgimages img").height();
            var ratio = $("#bgimages img").height() / $("#bgimages img").width();
            
            if(browserWidth > browserHeight){
                imgWidth = browserWidth;
                imgHeight = browserWidth * ratio;
                $("#bgimages img").css("width", imgWidth);
                $("#bgimages img").css("height", imgHeight);
                
                if(browserHeight > imgHeight){
                    imgWidth = browserHeight / ratio;
                    imgHeight = browserHeight;
                    $("#bgimages img").css("width", imgWidth);
                    $("#bgimages img").css("height", imgHeight);
                }
            }
            
            if(browserHeight > browserWidth){
                imgWidth = browserHeight / ratio;
                imgHeight = browserHeight;
                $("#bgimages img").css("width", imgWidth);
                $("#bgimages img").css("height", imgHeight);
                
                if(browserWidth > imgWidth){
                    imgWidth = browserWidth;
                    imgHeight = browserWidth * ratio;
                    $("#bgimages img").css("width", imgWidth);
                    $("#bgimages img").css("height", imgHeight);
                }
            }
            
        }//resizeImg
    });
    
}//backgroundimages

//Cetrowanie w pionie boksow z na podstronach
function centrujzawartosc(){
    
    $(document).ready(function(){
        
        var browserHeight;      //wysokosc okna przegladarki
        var divCenterContent;       //wysokosc diva z zawartoscia id="divCenterContent"
        var margintop;         //margines gorny dla diva "top"
        
        centruj();
        
        $(window).resize(function() {
            centruj();
        });
        
        
        function centruj(){
            browserHeight = $(window).height();
            divCenterContent = $('#top #centercontent #content').height();
            margintop = (browserHeight - divCenterContent - 160) / 2;
            $('#top #centercontent').css( "margin-top", margintop );
            
        }//centruj
    });
    
}//centrujzawartosc

//Przewijanie na podstronie o mnie
function omnieprzewijanie(){
    
    $(document).ready(function(){
        
        var margines = 0;
        var itemNumb = $('body#omnie #wrap ul li').length;
        var current = 0;
        
        $('body#omnie #navleft span').fadeOut(0,0);
        $("body#omnie h4 ul #counter").text((current + 1) + "/" + (itemNumb));
        
        $('body#omnie #navright span').click(function() {
            if(current < itemNumb - 1){
                $('body#omnie #navleft span').fadeIn(0,1);
                $('body#omnie span').fadeIn(0,1);
                margines = margines - 860;
                przesun(margines);
                current++;
                $("body#omnie h4 ul #counter").text((current + 1) + "/" + (itemNumb));
            }
            if(current == itemNumb - 1){
                $('body#omnie #navright span').fadeOut(0,0);
            }
		});
        
        $('body#omnie #navleft span').click(function() {
            if(current > 0){
                $('body#omnie #navleft span').fadeIn(0,1);
                $('body#omnie #navright span').fadeIn(0,1);
                margines = margines + 860;
                przesun(margines);
                current--;
                $("body#omnie h4 ul #counter").text((current + 1) + "/" + (itemNumb));
            }
            if(current == 0){
                $('body#omnie #navleft span').fadeOut(0,0);
            }
		});
        
        function przesun(margines){
            $("body#omnie #wrap ul").animate({
                    marginLeft: margines
				}, 1000, "jswing" );

        }
        
    });
    
}//omnieprzewijanie

function portfolio(imgname, imgid){
    
    $(document).ready(function(){
        
        var browserHeight = $(window).height();                  //wysokosc okna przegladarki
        var browserWidth = $(window).width();                    //szerokosc okna przegladarki
        var margintop;                                           //margines gorny
        var imgWidth;
        var imgHeight;
        
        slider();
        
        //$('body#portfolio #photo #prev, body#portfolio #photo #next').fadeTo(0, 0);
        $('#slider #sliderleft, #slider #sliderright').fadeTo(0, 0.7);
        
        var img = new Image();
        $(img).load(function(status) {
                
            $('body#portfolio #photo div').css('visibility', 'hidden');
            $('body#portfolio #photo div').append(img);
                
            if (status != "error"){
            
            	/*pausecomp(2000);
				function pausecomp(ms) {
					ms += new Date().getTime();
					while (new Date() < ms){}
				}*/
				if (jQuery.browser.safari&&  document.readyState != "complete"){
                	setTimeout( arguments.callee, 100 );
                	return;
                }
            
                imgWidth = $("body#portfolio #photo img").width();
                imgHeight = $("body#portfolio #photo img").height();
                
                resizeImg();
                
                $(window).resize(function() {
                    browserWidth = $(window).width();
                    browserHeight = $(window).height();
                    resizeImg();
                });
                
                $('body#portfolio #photo').removeClass("loading");
                $('body#portfolio #photo div').css("visibility", "visible");
                $('body#portfolio #photo div').fadeOut(0);
                $('body#portfolio #photo div').fadeIn("slow");
                $('body#portfolio #photo div span').fadeTo(0, 0.6);
                
            }
                
        }).attr('src', 'photos/large/' + imgname);
        
        //Kontrona szerokosci i wysokosci zdjecia
        var ratio;
        var newImgWidth = 0;
        var newImgHeight = 0;
        function resizeImg(){
            ratio = imgHeight / imgWidth;
            newImgWidth = browserWidth - 20;
            newImgHeight = browserHeight - 140;
            
            if(browserWidth > (browserHeight - 120)){
                $("body#portfolio #photo img").height( newImgHeight );
                $("body#portfolio #photo img").width( newImgHeight / ratio );
                
                if($("body#portfolio #photo img").width() > browserWidth){
                    $("body#portfolio #photo img").width( browserWidth );
                    $("body#portfolio #photo img").height( browserWidth * ratio );
                }
            }else if((browserHeight - 120) > browserWidth){
                $("body#portfolio #photo img").width( browserWidth );
                $("body#portfolio #photo img").height( browserWidth * ratio );
                
                if($("body#portfolio #photo img").height() > (browserHeight - 120)){
                    $("body#portfolio #photo img").height( newImgHeight );
                    $("body#portfolio #photo img").width( newImgHeight / ratio );
                }
            }
            $("body#portfolio #photo div").width($("body#portfolio #photo div img").width());
            $("body#portfolio #photo div").height($("body#portfolio #photo div img").height());
            //alert($("body#portfolio #photo div").height() +", "+newImgHeight);
            
            centruj();
            
        }//resizeImg

        function centruj(){
            margintop = (browserHeight - 120 - $('body#portfolio #photo img').height()) / 2;
            marginleft = (browserWidth - $('body#portfolio #photo img').width()) / 2;
            $('body#portfolio #photo div').css( "margin-top", margintop );
            $('body#portfolio #photo div').css( "margin-left", marginleft );
            
			$('body#portfolio #photo #prev span').css("top", (browserHeight - 120 - 25) / 2);
			$('body#portfolio #photo #next span').css("top", (browserHeight - 120 - 25) / 2);
			
			$('body#portfolio #photo #prev span').css("right", ($('body#portfolio #photo img').width() / 2) - 25);
			$('body#portfolio #photo #next span').css("left", ($('body#portfolio #photo img').width() / 2) - 25);
			
			$("body#portfolio #photo #prev span, body#portfolio #photo #next span").css("visibility", "visible")
			
        }//centruj
        
		$("body#portfolio #photo #prev, body#portfolio #photo #next").hover(
            function() {
				$("span", this).fadeTo(0, 1);
			},function() {
				$("span", this).fadeTo(0, 0);
            });
		
        function slider(){
            
            var iloscZdjec = $('#wrap img').length;
            var szerokoscZdjec = iloscZdjec * 80;
            var wrapSzerokosc = 80 * (Math.round( (browserWidth - 400) / 80 ));
            var margines = 0;
            var poprawej = szerokoscZdjec - wrapSzerokosc - (margines * -1);
            var polewej = margines * -1;
            
            ustawszerokosc();
            obliczmarginesy();
            sciemnij();
            ustawaktywny();
			sprstrzalki();
            
            function sciemnij(){
                $('#slider ul li').fadeTo(0, 0.5);
                $('#slider ul li:eq(' + imgid + ')').fadeTo(0, 1);
            }
            
            $(window).resize(function() {
                $("#slider #wrap ul").stop(true, false);
                ustawszerokosc();
                ustawaktywny();
                obliczmarginesy();
                sciemnij();
            });
            
            function ustawszerokosc(){
                browserWidth = $(window).width();
                wrapSzerokosc = 80 * (Math.round( (browserWidth - 400) / 80 ));
                obliczmarginesy();
                $('#wrap').width( wrapSzerokosc );
                $('#wrap ul').width( szerokoscZdjec );
            }
            
            $('#sliderright').click(function() {
                
                if(poprawej > 0){
                    if( poprawej < wrapSzerokosc ){
                        margines = margines - poprawej;
                    }else{
                        margines = margines - wrapSzerokosc;    
                    }
                
                obliczmarginesy();
            	przesun(margines);
                }
            });
            
            $('#sliderleft').click(function() {
                
                if(polewej > 0){
                    if( polewej < wrapSzerokosc ){
                        margines = margines + polewej;
                    }else{
                        margines = margines + wrapSzerokosc;    
                    }
                
                obliczmarginesy();
            	przesun(margines);
                }
            });
            
            //Animacja
            function przesun(margines){
                
                $("#slider #wrap ul").animate({
                        marginLeft: margines
            		}, 1000, "jswing" );
				sprstrzalki();
        
            }
            
            function obliczmarginesy(){
                poprawej = szerokoscZdjec - wrapSzerokosc - (margines * -1);
                polewej = margines * -1;
                $('#trace').text("po lewej: " + polewej + ", po prawej: " + poprawej );
            }
            
			function sprstrzalki(){
				
				if(polewej == 0){
					$("#slider #sliderleft span").css("display", "none");
				}else{
					$("#slider #sliderleft span").css("display", "block");
				}
				
				if(poprawej == 0){
					$("#slider #sliderright span").css("display", "none");
				}else{
					$("#slider #sliderright span").css("display", "block");
				}
				
			}
			
			$("#slider #sliderleft, #slider #sliderright").hover(
            function() {
				$(this).fadeTo(0, 1);
			},function() {
				$(this).fadeTo(0, 0.7);
            });
			
            $("#slider li").hover(
            function() {
                $("a", this).before("");
                    $(this).fadeTo("slow", 1);
                },
			function() {
                if( $("#slider li").index(this) != imgid ){
                    $('div', this).remove();
                    $(this).fadeTo("slow", 0.5);
                }
            });

            function ustawaktywny(){
                
                //$('#slider li:eq(' + imgid + ')')
                var widoczne = wrapSzerokosc / 80;
                var polowa = Math.round( (widoczne / 2) - 1 );
                                
                if( (imgid > polowa) && (imgid < (iloscZdjec - polowa)) ){
                    
                margines = -((imgid - polowa) * 80);
                $('#slider ul').css( "margin-left", -((imgid - polowa) * 80) );
                obliczmarginesy();
                
                }else if( imgid >= (iloscZdjec - polowa) ){
                    
                    margines = -(szerokoscZdjec - wrapSzerokosc);
                    $('#slider ul').css( "margin-left", -(szerokoscZdjec - wrapSzerokosc) );
                    obliczmarginesy();
                    
                }
                
            }
            
        }
        
    });
    
}//portfolio

function kontakt(){
    $(document).ready(function(){
        
        $('body#kontakt .input').focus(function() {
            if($(this).attr('value') == ""){
                if( ($(this).attr('class') == "input imie") || ($(this).attr('class') == "input imie error") ){
                    $(this).css("background-image", "none");   
                }
                if( ($(this).attr('class') == "input tresc") || ($(this).attr('class') == "input tresc error") ){
                    $(this).css("background-image", "none");   
                }
                if( ($(this).attr('class') == "input email") || ($(this).attr('class') == "input email error") ){
                    $(this).css("background-image", "none");   
                }
            }
        });
        
        $('body#kontakt .input').blur(function() {
            if($(this).attr('value') == ""){
                if( ($(this).attr('class') == "input imie") || ($(this).attr('class') == "input imie error") ){
                    $(this).css("background-image", "url(images/mages,0,1.png");   
                }
                if( ($(this).attr('class') == "input tresc") || ($(this).attr('class') == "input tresc error") ){
                    $(this).css("background-image", "url(images/mages,0,1.png");   
                }
                if( ($(this).attr('class') == "input email") || ($(this).attr('class') == "input email error") ){
                    $(this).css("background-image", "url(images/mages,0,1.png");   
                }
            }
        });
        
        if($('body#kontakt input.imie').attr('value') != ""){
            if( ($('body#kontakt input.imie').attr('class') == "input imie") || ($('body#kontakt input.imie').attr('class') == "input imie error") ){
                $('body#kontakt input.imie').css("background-image", "none");   
            }
        }
		if($('body#kontakt input.email').attr('value') != ""){
            if( ($('body#kontakt input.email').attr('class') == "input email") || ($('body#kontakt input.email').attr('class') == "input email error") ){
                $('body#kontakt input.email').css("background-image", "none");   
            }
        }
        if($('body#kontakt textarea').text() != ""){
            
            if( ($('body#kontakt textarea').attr('class') == "input tresc") || ($('body#kontakt textarea').attr('class') == "input tresc error") ){
                $('body#kontakt .tresc').css("background-image", "none");   
            }
        }
    
    });
    
}
