

function scaleImage(orig, size, elem) {
	var imgW = orig[0],
		imgH = orig[1],
		destW = size[0],
		destH = size[1],
		ratioX, ratioY, ratio, newWidth, newHeight, marginLeft, marginTop;
	
	ratioX = destW / imgW;
	ratioY = destH / imgH;

	ratio = ratioX > ratioY ? ratioX : ratioY;

	newWidth = parseInt(imgW * ratio, 10);
	newHeight = parseInt(imgH * ratio, 10);

	marginLeft = Math.floor((destW - newWidth) / 2);
	marginTop = Math.floor((destH - newHeight) / 2);
	
	elem.css({
		"width": newWidth,
		"height": newHeight,
		"left": marginLeft,
		"top": marginTop
	});
	
	return [newWidth,newHeight,marginLeft,marginTop]
}

function resizeSlides() {
	var bHeight = $('body').height();
	if (bHeight<670) { 
		if ($.browser.msie && $.browser.version.substr(0,1)<=7) {	
			$('html').css('overflow-y','scroll');
		} else {
			$('body').css('overflow-y','scroll');
		}
		bHeight = 900;
	} else {
		if ($.browser.msie && $.browser.version.substr(0,1)<=7) {	
			$('html').css('overflow-y','hidden');
		} else {
			$('body').css('overflow-y','hidden');
		}
	}
	var bWidth = $('body').width();
	if (bWidth > contentWidth) {
		destWidth = bWidth;
	} else {
		destWidth = contentWidth;
	}
	$('.glowny,#kontener-slajdy,.pod-slide-kontener').width(destWidth * ($('.slide').length));
	$('.glowny,#kontener-slajdy,.pod-slide-kontener').height(bHeight);
	$('.slide-main-column').height(bHeight);
	
	$('.slide,.sub-slide').width(destWidth);
	$('.slide,.sub-slide').height(bHeight);
	
	$('.slide-back').each(function() {
		var origW = 0; var origH = 0; var tt;
		if ($(this).attr("class") != undefined) {
			var classes = $(this).attr("class").split(" ");
			for (var i = 0; i < classes.length; i++){
				if (classes[i].substr(0,5) == "size-"){
					tt = classes[i].split("-");
					origW = tt[1];
					origH = tt[2];
				}
			}
			scaleImage([origW,origH], [destWidth,bHeight], $(this));
		}
	});
	
	$('.pod-slide-kontener').cycle('resize');
	
	if (currentSlide == undefined) {
		var left = 0;
	} else {
		var left = (-1 * currentSlide * destWidth)
	}
	$('#kontener-slajdy').css('left',left + 'px');
	$('#kontener-slajdy .slide .slide-content').each(function() {
		$(this).css('left',parseInt(($(this).parent().width() - $(this).width())/2 + (contentWidth/2)) + 'px');
	});
}

function beforeSubSlide(curr,next,opts) {
	if (curr!=next) {
		$(next).parents('.slide').find('.news-text-kontener').cycle('next');
	}
}

function beforeSlide(curr,next) {
	$('#menu_lewa>.tresc>a').removeClass('mg_active');
	$('#menu_lewa>.tresc>a').eq($(next).index()).addClass('mg_active');
	
	var diff = Math.abs($(curr).index() - $(next).index()) - 1;
	if ($(curr).index() < $(next).index()) {
		$(next).find('.slide-content').css('left',(parseInt($(next).width()+(contentWidth/2))) + 'px');
		setTimeout(function() {
			$(next).find('.slide-content').animate({'left':parseInt(($(next).width()-contentWidth)/2+(contentWidth/2)) + 'px'},slideContentOfftimeAnimation + (70* diff));
		},10);
	} else if ($(curr).index() > $(next).index()) {
		$(next).find('.slide-content').css('left',-1 * (parseInt($(next).width()+(contentWidth/2))) + 'px');
		setTimeout(function() {
			$(next).find('.slide-content').animate({'left':parseInt(($(next).width()-contentWidth)/2+(contentWidth/2)) + 'px'},slideContentOfftimeAnimation + (70* diff));
		},10);
	}
	
	if ($(next).children('.pod-slide-kontener').length>0) {
		if ($(next).children('.pod-slide-kontener').data("cycle-initialized") == undefined) {
			$(next).children('.pod-slide-kontener').cycle({
				fx: 'scrollVert', 
				timeout: subSliderPauseBetweenSlides,
				slideExpr: 'div.sub-slide',
				before: beforeSubSlide
			});
			$(next).children('.pod-slide-kontener').data("cycle-initialized","true");
			$(next).find('.news-text-kontener').cycle({
				fx: 'scrollVert', 
				timeout: 0,
				slideExpr: 'div.news-slide-text'
			});
		} 
	}
	
	if (($(curr).index()!=$(next).index()) && $(curr).children('.pod-slide-kontener').length>0) {
		$(curr).children('.pod-slide-kontener').cycle('pause');
	}
}

function afterSlide(curr,next) {
	if ($(next).children('.pod-slide-kontener').length>0) {
		if ($(next).children('.pod-slide-kontener').data("cycle-initialized") == "true") {
			$(next).children('.pod-slide-kontener').cycle('resume');
		}
	}
		
}

function changeSlide(index) {
	curr = $('#kontener-slajdy .slide').eq(currentSlide);
	next = $('#kontener-slajdy .slide').eq(index);
	wd = $(next).width();
	beforeSlide(curr,next);
	setTimeout(function() {
		$('#kontener-slajdy').stop().animate({
			left: (-1 * index * wd) + 'px'
		}, slideDuration, 'easeOutQuad', function() {
			currentSlide = index;
			afterSlide(curr,next)
		});
	},10);
}

$(function() {





    $('.error').hide();
	
    $(".przycisk_submit").click(function() {
     	$('.error').hide();
  		var email = $("input[name=email]").val();
		var imie_nazwisko = $("input[name=imie_nazwisko]").val();
		var telefon = $("input[name=telefon]").val();
		var tresc = $("textarea[name=tresc]").val();
		
		// walidacja
	  		if(email == "" || email == "Adres e-mail*" || imie_nazwisko == "" || imie_nazwisko == "Imię i nazwisko*" || telefon == "" || telefon == "Telefon*" ||	tresc == "" || tresc == "Treść wiadomości*") {
			alert("Nie wypełniłeś wszystkich wymaganych pól");
	        return false;
			}
	  
		var dataString = 'email='+ email + '&tresc=' + tresc + '&imie_nazwisko=' + imie_nazwisko + '&telefon=' + telefon;
		  $.ajax({
		    type: "POST",
		    url: "main/kontakt",
		    data: dataString,
		    success: function() {
				
			  $('#kontakt_tekst').hide();	
		      $('#kontakt_info').html("<h3 style='color:white'>Dziękujemy, formularz został wysłany!</h3>");
			  
			  
			  setTimeout(function() {
				    $('#kontakt_info').fadeOut('fast');
					$('[name=imie_nazwisko]').val('Imię i nazwisko*');
					$('[name=telefon]').val('Telefon**');
					$('[name=email]').val('Adres e-mail*');
					$('[name=tresc]').val('Treść wiadomości*');
					$('#kontakt_tekst').show();						

					
				}, 2600);
			  
		    }
		  });
		return false;
    });

	$('.link-nasze-prace').click(function() {
		changeSlide((currentSlide+2));
		return false;
	});

	$(window).resize(function() {
		resizeSlides();
	});
	resizeSlides();
	
	$('#menu_lewa>.tresc>a').each( function(index, Element) {
		$(this).click(function() {
			changeSlide(index);
			return false;
		});
	});
	
$("#link_glowna").addClass("mg_active"); 
		
	// Thumbs
	$('.praca_zdjecie').hover(function(){
		$(this).find('img').stop(true, true).animate({'marginTop': 0}, 250);
		$(this).find('span').stop(true, true).animate({'top': '125px'}, 300)
	}, function(){
		$(this).find('img').stop(true, true).animate({'marginTop': '-157px'}, 250);
		$(this).find('span').stop(true, true).animate({'top': '156px'}, 300)
	});	

	
	    $('.slide_prezentacja').cycle({
		fx: 'scrollHorz', 
		next:   '.prez_next', 
  		prev:   '.prez_prev',
		timeout:       0,
		easing: "easeInOutQuint" 
	});
				$('#link_glowna').click(function(){
					$(".mg").removeClass("mg_active");
					$(this).addClass("mg_active");
				});
				
				$('#link_dlaczego_my').click(function(){
					$(".mg").removeClass("mg_active");
					$(this).addClass("mg_active");
				});
				
				$('#link_nasze_prace').click(function(){
					$(".mg").removeClass("mg_active");
					$(this).addClass("mg_active");
					
				});
				
				$('#link_kontakt').click(function(){
					$(".mg").removeClass("mg_active");
					$(this).addClass("mg_active");
				});
			
			$('.scroll-pane').jScrollPane(
				{
			horizontalDragMinWidth: 109,
			horizontalDragMaxWidth: 109
				}
				);
	
	$('.slide-main-box .a-find-more').click(function() {
		changeSlide((currentSlide+1));
		return false;
	});
		
	$(window).load(function() {
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$('#loading').fadeOut(1500,function() {
				$('.header-content').css('border','1px solid #BBBCBC');
				$('.header-content').css('border','0');
			});
		} else {
			$('#loading').fadeOut(1500);
		}
		currentSlide = 0;
		changeSlide(0);
		
		$(document).bind('cbox_complete', function(){
    		$("#mapa").gMap({
			clickable:false,
			markers: [{ latitude: 53.052120,
			longitude: 18.589810 }],
		    zoom: 13
			});
});


$("a[rel='lightbox']").colorbox();
$(".inline").colorbox({inline:true, width:"1112px",rel:'gal',transition:'fade'});
$(".inline_map").colorbox({inline:true, width:"1112px",rel:'gal2',transition:'fade'});

  $(".hide").hide();
  $(".hide_head").click(function()
  {
    $(this).prev(".hide").fadeToggle(300, "linear");

return false;
  });

  
   $(".st_next").click(function()
  {
 	$.colorbox.next()
  });
  
  $(".st_prev").click(function()
  {
 	$.colorbox.prev()
  });
 

  
	$(".tab_content").hide(); //Hide all content
	$("#przelacz div:first a").addClass("akt").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	$("#przelacz div").click(function() {

		$("#przelacz div a").removeClass("akt"); //Remove any "active" class
		$(this).find("a").addClass("akt"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn('fast'); //Fade in the active ID content
		return false;
	});
	$(".przelacz:first a").addClass("akt").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	$(".przelacz a").click(function() {

		$(".przelacz a").removeClass("akt"); //Remove any "active" class
		$(this).addClass("akt"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn('fast'); //Fade in the active ID content
		return false;
	});
		
	});
	
	
	$(document).scroll(function() {$(document).scrollLeft(0);});
});





