// JavaScript Document$
$(document).ready(function() {
	$($('#menu li')[0]).css('background', 'none');
	if ($('.gallery').length) {
		startGalleries()
	}
	if ($('.galleryk').length) {
		startClientsGalleries()
	}
	if ($('.nms_submission').length) {
		newsletterForm();
	}
	startFontReplacement();
	/*
	$('.glossary .term .termname').hover(function() { $(this).find('.definition_selected').hide().stop(true, true).fadeIn(); }, function() { $(this).find('.definition_selected').hide() } );
	*/
	if (typeof(isHome) != 'undefined') {
		$('.box').hover(function() {$(this).find('.c li a').css('text-decoration','underline')}, function() {$(this).find('.c li a').css('text-decoration','none')});  
		$('.box').addClass('pointer').click(function() { location.href='oferta'; });
		
		$('.NewsSummary .left_').click(function() { return false; });
		$('.NewsSummary').each(function() {

		var h = -1*$(this).find('.news_content').height();
		$(this).tooltip({
			bodyHandler: function() {
				return $($(this).find('.news_content')).html();
			},
			showURL: false,
			track: true, 
    		delay: 0, 
    		opacity: 1, 
    		fixPNG: true, 
    		showBody: " - ", 
    		extraClass: " mae2", 
   			/*top: h-12,*/
    		left: -37
    		//top: 0, 
    		//left: $(this).width()
		});
		
		
		if ($(this).find('.NewsSummaryField .link').length) {
			var linkURL = $(this).find('.NewsSummaryField .link').text();
			$(this).click(function() { window.location.href = linkURL });
			$(this).find('a').click(function() { window.location.href = linkURL });
		}


			
		});
	}
	
	glossary()
	if ($('.submenu ul').length) {
		$(window).scroll(function() {
			if ($(window).scrollTop() >= 109 ) {
				$('.submenu').css({'position': 'fixed', 'top':'5px'});
			} else {
				$('.submenu').css({'position': 'absolute', 'top':'0px'});
			}
		});
	}
	
});

function glossary() {
	$('.glossary .term .termname').each(function() {
	$(this).find('.definition_selected').css({'width':271});
	
	var h = -1*$(this).find('.definition_selected').height();
	$(this).tooltip({
		bodyHandler: function() {
			return $($(this).find('.definition_selected')).html();
		},
		showURL: false,
		track: true, 
    	delay: 0, 
    	opacity: 1, 
    	fixPNG: true, 
    	showBody: " - ", 
    	extraClass: " mae", 
   		top: h-12,
    	left: -37
    	//top: 0, 
    	//left: $(this).width()
		});
	});
}

function checkMail(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	alert("Proszę wpisz poprawny email");
	return false;
}

function newsletterForm() {
	var nms_text = 'Newsletter...wpisz email';
	$('.nms_submission .cms_textfield').val(nms_text);
	$('.nms_submission .cms_textfield').focus(function() {
		if($(this).val() == nms_text) { $(this).val(''); }
	});
	$('.nms_submission .cms_textfield').blur(function() {
		if($(this).val() == '') { $(this).val(nms_text); }
	});	
	$('.nms_submission form').submit(function() {
		return checkMail($('.nms_submission .cms_textfield').val());
	});
}

function startClientsGalleries() {
	$('.galleryk').data({'i':0});
	$('.galleryk .left').click(onClientPrevClick);
	$('.galleryk .right').click(onClientNextClick);
}

function onClientPrevClick() {
	var i = $('.galleryk').data('i')
	i = i == 0 ? $('.galleryk .img').length - 1 - 5 : i - 1; 
	showClientImage(i);
}

function onClientNextClick() {
	var i = $('.galleryk').data('i')
	i = i == $('.galleryk .img').length - 1 - 5 ? 0 : i + 1; 
	showClientImage(i);
}

function showClientImage(i) {
	//console.log(i, $('.galleryk .img').length);
	$('.galleryk').data({'i':i});
	///$('.galleryk .imglist')
	var left = -125*i;
	if (i+6 == $('.galleryk .img').length) {
		left = 750 - $('.galleryk .imglist .img').last().position().left - $('.galleryk .imglist .img').last().width(); 
	} else if (i == 0) { 
		left = 0; 
	} else {
		left = 750 - $('.galleryk .imglist .img').eq(i+6).position().left; 
	}
	
	
	$('.galleryk .imglist').stop().animate({'left': left + 'px'});
}

function startGalleries() {
	$('.gallery').each(function() {
		var gal = this;
		$(gal).data({'i':0});
		if($(this).find('.img a').length <= 1) {
			$(this).find('.contr').hide();
		}
		$(this).find('.img a').each(function(index, value) {
			
			$(gal).find('.contr').append('<img class="d d'+index+'" src="uploads/images/gui/dot' + (index == 0 ? '2' : '1') + '.gif"/> ')			
			$(gal).find('.d' + index).data({'i':index, 'gal':gal}).click(onDotClick);
		});
		$(this).find('.contr .l').data({'gal':gal}).click(onPrevClick);
		$(this).find('.contr .r').data({'gal':gal}).click(onNextClick);
		
		$(this).find('.imglist .img a').click(onImageClick);		
		$(this).find('.imglist .img a').each(function() {
			if ($(gal).parent().find('.galleryurl').attr('href')!= null) {
				$(this).attr('href', $(gal).parent().find('.galleryurl').attr('href'));
				$(this).unbind('click');
			}
		});
		
	});

}

function onImageClick() {
	//alert('aaa');
	//textResize(); 
	return false;
}

function onPrevClick() {
	var gal = $(this).data('gal');
	var i = $(gal).data('i')
	i = i == 0 ? $(gal).find('img.d').length - 1 : i - 1; 
	showImage(gal, i);
}

function onNextClick() {
	var gal = $(this).data('gal');
	var i = $(gal).data('i')
	i = i == $(gal).find('img.d').length - 1 ? 0 : i + 1; 
	showImage(gal, i);
}

function showImage(gal, i) {
	$(gal).data({'i': i}).find('img.d').attr('src', 'uploads/images/gui/dot1.gif');
	$($(gal).find('img.d')[i]).attr('src', 'uploads/images/gui/dot2.gif');
	var left = -670*i;
	$(gal).find('.imglist').stop().animate({'left': left + 'px'});
}

function onDotClick() {
	var i = $(this).data('i');
	var gal = $(this).data('gal');
	showImage(gal, i);
}

function testResize() { 
	var nW = 670 + 30;
	$('.gallery .mask').animate({'width':nW});
	$('.gallery .c .images').animate({'width':nW+27});
	$('.gallery').animate({'width':nW+27+31});
}

function startFontReplacement() {	
	FLIR.init( { path: '/facelift/' } );
	FLIR.replace('h1,h2');
}

