// Funkcja przeliczania lini elementu

jQuery.fn.preventTextIndent = function(classname) {
	rowSize = (parseInt($(this).css('lineHeight'), 10) || parseInt($(this).css('fontSize'), 10));
	if($.browser.safari) rowSize += 2;
	if($.browser.msie) rowSize += 3;
	if($.browser.opera) rowSize += 2;
	height = $(this).height();
	
	br = $(this).html().match(/<br>/gi);
	
	if(br!=null){
		r = br.length+1;
		if($.browser.mozilla && r>2) height+=1;
		if((rowSize*r) == height) $(this).addClass(classname);
	} else {
		if(rowSize == height) $(this).addClass(classname);
	}
};

jQuery.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

jQuery.fn.borderColor = function(color, duration){
	duration = duration || 500;
	$(this).animate({
		borderLeftColor  : color,
		borderRightColor : color,
		borderTopColor   : color,
		borderBottomColor: color
	}, duration);
};

function titleToFormValue(){
	$('input[title], textarea[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}

		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('');
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).borderColor('#e7e7e7');
			}
		});
	});
};

function initialize() {
	if (GBrowserIsCompatible()) {
    	var map = new GMap2(document.getElementById("map"));
    	map.addControl(new GSmallMapControl());
    	map.addControl(new GMapTypeControl());
    	var center = new GLatLng(52.430014,16.823598);
        map.setCenter(center, 16);
        var marker = new GMarker(center);
        map.addOverlay(marker);
    }
}

function image_center(){
	var rightWidth = $('.right').width();
	var thisImg = $('.floatCenter');
	$.each(thisImg,function(){
		var obj = $(this);
		if(obj.hasClass('mediaFile')){
			if(obj.children().is('embed')){
				var width = obj.children('embed').width();
			} else {
				var width = obj.width();
			}
			obj.css({'margin-left':(rightWidth/2)-(width/2),'margin-right':(rightWidth/2)-(width/2)});
			if($.browser.msie && $.browser.version === '6.0'){
				obj.css({'margin-right': rightWidth/2-(width/2)-20});
				obj.css('border','none');
			}
		} else {
			var width = obj.width();
			obj.css({'margin-left':(rightWidth/2)-100,'margin-right':(rightWidth/2)-100}); 
			if($.browser.msie && $.browser.version === '6.0'){
				obj.css({'margin-right': (rightWidth/2)-112}); 
			}
		}
	});
	
	//centrowanie panoramek
	$('.right > a > .pano').each(function(){
		$(this).removeClass('floatRight floatLeft floatCenter');
		
		$(this).css({'margin-left':(rightWidth/2)-325,'margin-right':(rightWidth/2)-325}); 
		if($.browser.msie && $.browser.version === '6.0'){
			$(this).css({'margin-right': (rightWidth/2)-325}); 
		}
	});
}

function tabs_activation(){
	// Zakladki do podkategorii
	var tabContainers = $('.right > div');
	$('#catTabs li a').click(function() {
		var thisa = $(this).attr('href');
		tabContainers.hide();
		tabContainers.each(function(){if(('#'+$(this).attr('id')) === thisa) $(this).show();});
		//tabContainers.filter(this.hash).show();
		$('#catTabs li a').removeClass('current');
		$(this).addClass('current');
		return false;
	}).filter(':first').click();
}

function ukrycie() {
$('#beta_image embed').hide();
$('#beta_image').css({'width' : '25px', 'height' : '25px'});
}

function mailForm() {
	$('#submit').click(function(e){
		e.preventDefault();
		$(':input').css('border-color', '#e7e7e7');
		var inputs = 'input[title], textarea[title]';
		$(inputs).each(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('');
			}
		});
		var form = $('#smallBox').html();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var phoneReg = /^[0-9]+$/;

		var emailVal = $(":input[name=email]").val();
		if(emailVal == '') {
			$(":input[name=email]").borderColor('#FF0000');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$(":input[name=email]").borderColor('#FF0000');
			hasError = true;
		}
		
		var nameVal = $(":input[name=name]").val();
		if(nameVal == '') {
			$(":input[name=name]").borderColor('#FF0000');
			hasError = true;
		}
		
		var telVal = $(":input[name=tel]").val();
		if(telVal == '') {
			$(":input[name=tel]").borderColor('#FF0000');
			hasError = true;
		} else if(!phoneReg.test(telVal)){
			$(":input[name=tel]").borderColor('#FF0000');
			hasError = true;
		}

		var msgVal = $(":input[name=msg]").val();
		if(msgVal == '') {
			$(":input[name=msg]").borderColor('#FF0000');
			hasError = true;
		}
		
		if(hasError == false) {
			$('#smallBox').wrapInner('<div id="formwrapper"></div>');
		} else {
			titleToFormValue();
		}
		
		$.post("kontakt",
  		{ email: emailVal, name: nameVal, tel: telVal, msg: msgVal },
   		function(data){
			$("#formwrapper").hide("normal", function() {
				$('#smallBox').append('<span class="shadow"/>');
				$("#smallBox").append('<p>Wiadomość została doręczona</p>');
				$('#smallBox > p').css({'position':'relative','top':'100px','left':'70px'});
			});
		}
		);

		return false;
	});
	
	//kasuj do title inputa
	$('#reset').click(function(e){
		e.preventDefault();
		$('input[title], textarea[title]').each(function(){
			$(this).val($(this).attr('title')).borderColor('#e7e7e7', 100);
		});
	});
}
 
$(document).ready(function() {

	// zmiana z-indexu po najechaniu na flasha
	$('#beta_image embed').hide();
	$('#beta_image').css({'width' : '25px', 'height' : '25px'});

	$('#beta_image').mouseover(function() {
		$('#beta_image').css({'width' : '340px', 'height' : '340px'});
		$('#beta_image embed').show();
	});

	//Pieprzone IE
	if ( $.browser.msie && (  $.browser.version === '7.0' || $.browser.version === '6.0' || $.browser.version === '8.0' )){
		$('.right > div > .simple3Table').parent().css('float','left');
	}
	if ( $.browser.msie && ( $.browser.version === '7.0' || $.browser.version === '6.0') && $('#contentWrapper').children().is('ul.left')){
		$('#contentWrapper').css({'margin-bottom':'-60px'});
	}

	if ($.browser.msie && $.browser.version === '6.0' && $('.right').children('ul').hasClass('gallery')){
		$('.right').addClass('fix')
				   .css({'height':'440px'});
	}
	
	if($('.brutal').siblings().is('.brutal')){
		$('.brutal').parent('p').addClass('clearIndent');
	}
	
	//Realizacje - paginacja!
	$('#pages > li > a').click(function(e){
		e.preventDefault();
		var href = $(this).attr('href').replace('#','');
	
		$('.gallery').hide();
		$('.'+href).show();
		$('#pages .current').removeClass('current');
		$(this).addClass('current');
	});
	
	$('.rightSide .gallery').cycle();
/*galeira - co z nią ?
	//Realizacje - galeria na stronie głównej
	function gallery_zoom(){
	$('.rightSide .gallery > li > span').live("mouseover", function(){
		if($.browser.msie && ($.browser.version === '6.0' || $.browser.version ==='7.0')){$('#Top .gallery li').css('position','static');}
		if($.browser.msie && $.browser.version === '6.0'){
			$(this).css('border-color','#12437d');
		} else {
			$(this).addClass('current');
			$('.rightSide .gallery li:last').hide();
			$('.galleryWrapper').css('overflow','visible');
		}
	});
	$('.rightSide .gallery > li > span').live("mouseout",function(){
		if($.browser.msie && ($.browser.version === '6.0')){
			$(this).css('border-color','#d9d9d9');
		} else {
			$(this).removeClass('current');
			$('.rightSide .gallery li:last').show();
			$('.galleryWrapper').css('overflow','hidden');
		}
		if($.browser.msie && ($.browser.version === '6.0' || $.browser.version ==='7.0')){$('#Top .gallery li').css('position','relative');}
	});
	}
	function gallery(){
	var easing = 'easeOutBack';
	if($.browser.opera) var easing = '';
	$(".rightSide .gallery").everyTime(5000,function(){
		$('.rightSide .gallery > li > span').die();
		$("li:last",this).animate({top:'-74px'}, 800, easing);
		$("li:last",this).prev().wait(300).animate({top:'-74px'}, 800, easing);
		$("li:last",this).prev().prev().wait(700).animate({top:'-74px'}, 800, easing,function(){
			var first = $(this).prev().html();
			$(this).prev().remove();
			$('.rightSide .gallery > li').css('top','0px');
			$('.rightSide .gallery').append('<li>'+first+'</li>');
			gallery_zoom();
		});
		
	});
	}
	gallery_zoom();
	gallery();
	$('.rightSide .gallery').hover(
		function(){$(this).stop(true).stopTime()},
		function(){gallery();}
	);
*/
	
	
	//NoFlash
	$(".mediaFile:not(:has('embed'))").addClass('noFlash');
	/*
if(!$('.mediaFile').children().is('embed')){
		$('.mediaFile').addClass('noFlash');
	}
*/
	
	// scrollpane
	if($('ul.certs').children().length > 3){
		$('.scrollarea').scrollbars();
	}
		
	//link wideo
	$('.left > li > a, #navTop > li > ul > li > a').each(function(){
		if($(this).text() == 'Materiały wideo') $(this).addClass('wideo').attr('href','wideo');
	});
	$('a.wideo').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'frameHeight': 600, 'frameWidth': 518 , 'overlayShow': true, 'hideOnContentClick': false });
	
	//Certy i ref fancybox
	$('#smallBox > a').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300});
		
	//Certyfikaty i referencje
	if(sectionName === 'certyfikaty' || sectionName == 'referencje'){
		$('.certs').children('li').bind('click', function(){
			$('.certs > .current').removeClass('current');
			$(this).addClass('current');
			var certImg = 'cert'+($(this).children('img').attr('src').replace(new RegExp('img/(.*?)_min.jpg', 'ig'), '$1'));
			$('#smallBox > a > .current').hide();
			$('#smallBox > a').each(function(){
				if($('img',this).attr('id') === certImg) $('img',this).addClass('current').show();
			});
		});
	}
	
	//galeria imagów
	$('.right > .galeria > li > a > img').each(function(){$(this).removeClass('image')});
		
	// Rozwijane menu podkategorii
	$('#contentWrapper ul.left li ul').each(function(){
		$(this).hide();
		if($(this).hasClass('rozwin')) { 
			$(this).show();
			$(this).prev().css('background-image','url(images/arrow_open.gif)');
		}
	});
		
	$('ul.left a.drop').click(function(e) {
		e.preventDefault();
		if($(this).parent().is('span')){
			var parentUl = $(this).parent('span').siblings('ul');
		} else {
			var parentUl = $(this).siblings('ul');
		}
		
		if($.browser.msie){
			if(parentUl.is(':hidden')){
				parentUl.css({'display':'block'});
			} else {
				parentUl.css('display','none');
			}
		}else{
		if (parentUl.is(':hidden')) {
			parentUl.slideDown();
			$(this).animate({backgroundImage: 'url(images/arrow_67.5.gif) !important'}, 60)
				   .animate({backgroundImage: 'url(images/arrow_45.gif) !important'}, 60)
				   .animate({backgroundImage: 'url(images/arrow_22.5.gif) !important'}, 60)
				   .animate({backgroundImage: 'url(images/arrow_open.gif) !important'}, 60);
			return false;}
		else {
			parentUl.slideUp();
			$(this).animate({backgroundImage: 'url(images/arrow_22.5.gif) !important'}, 60)
					.animate({backgroundImage: 'url(images/arrow_45.gif) !important'}, 60)
					.animate({backgroundImage: 'url(images/arrow_67.5.gif) !important'}, 60)
					.animate({backgroundImage: 'url(images/arrow.gif) !important'}, 60)
			return false;}
		}
	});
	
	// Blokowanie klikalnosci .drop i .expand
	if($.browser.msie && $.browser.version === '6.0'){
		
	} else {
		$('li > .drop, li > .expand').click(function(e){e.preventDefault();return false;});
	}
	
	
	// Powiekszanie miniaturek certyfikatow
	if($.browser.msie && $.browser.version === '6.0'){
		$('ul.thumb li').hover(function() {
			$(this).css('z-index', '999');
			$(this).find('img')
			.css({
				'height': '160px',
				'width': '116px',
				'margin-top': '-70px',
				'margin-left': '-45px',
				'padding': '11px',
				'border': '0',
				'filter': 'Glow(Color=#bfbfbf, strength=5)',
				'background-position': '-138px 0'});
		},
		function(){
			$(this).find('img')
			.css({
				'height': '38px',
				'width': '27px',
				'margin-top': '0',
				'margin-left': '0',
				'padding': '0'
			}).css({'border-right': '1px solid #afb7c0', 'borderBottom': '1px solid #afb7c0'});
			$(this).css('z-index', '0');
			
			
		}
		);
	} else {
	$('ul.thumb li').hover(function() {
		$(this).css('z-index', '999');
		$(this).find('img').stop()
			.animate({
				height: '160px',
				width: '116px',
				marginTop: '-70px',
				marginLeft: '-45px',
				padding: '11px',
				border: '0'
			}, 200)
			.animate({backgroundPosition: '0 0'}, 0); // centruje pozycje imagesa aby byl widoczny
			},
		function () {
			$(this).find('img').stop()
			.animate({backgroundPosition: '-136px 0'}, 1) // przenosi images poza widoczny obszar
			.animate({
				height: '38px',
				width: '27px',
				marginTop: '0',
				marginLeft: '0',
				padding: '0'
			}, 100, 'linear', function() {
				$(this).parents('li').css('z-index', '0');
				$(this).css({'border-right': '1px solid #afb7c0', 'borderBottom': '1px solid #afb7c0'})
			}); // resetuje z-index
		}
		
	);
	}
	
		
	// Rotator aktualnosci
	$('.leftSide').tabs({fx:{opacity: 'toggle'}}).tabs('rotate', 10000, true);
	$('.leftSide').hover(
		function(){ 
			$(this).tabs('rotate', 0, true);
		}, function(){ 
			$(this).tabs({fx:{opacity: 'toggle'}}).tabs('rotate', 10000, true);
		}
	);
	
	// dodawanie clearIndent do elementow jednowierszowych
		
	$('.right p').each(function(){$(this).preventTextIndent('clearIndent')});
	
	//Zakładki w kategorii
	tabs_activation();
	
	// Usuwanie value w formularzu kontaktowym
	titleToFormValue();
	
	
	//kontakt
	if(sectionName === 'kontakt'){
		//Google map
		initialize();
		$('#smallBox a.button.map').click(function(e){
			e.preventDefault();
			$('#mapWrapper').css({'visibility':'visible'});
			$('#mapWrapper').animate({'bottom': '0px'},800,'easeOutExpo');
			
		});
		$('#mapWrapper .closeMap').click(function(e){
			e.preventDefault();
			$('#mapWrapper').animate({'bottom': '-435px'},500,'easeInExpo', function(){
				$('#mapWrapper').css({'visibility':'hidden'});
			});
		});
		//mailform
		mailForm();
	}
	//centrowanie imagesów
	image_center();
	//centreowane imagesy margin-top
	if($('.right').children('a:first').children('.image').hasClass('floatCenter')){
		$('.right').children('a:first').children('.image').css('margin-top','5px');
	}
	if(!$('.right').hasClass('editable')){
		$('a.group').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true }); 
	}	
	
	
	//pano fix
	$('.gallery > li > a > img.pano').each(function (){
		$(this).parent('a').addClass('panoFix');
	})
});
