﻿jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
}; 

function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

function overlay()
{
	if(!$('.overlay').width())
	{
		$('body').prepend('<div class="overlay"></div>');
		$('.overlay').height($(document).height()).css('opacity', '0.8');
	}
	$('.overlay').fadeIn();
}

function holder_check() {
	$('#header_ramka_holder a').click(function() {
		$(this).attr('target', '_blank');
	});
}

function cl_load(id)
{
	$('#nasi_klienci_logo .loader').show();

	$.ajax({   
			type: "POST",   
			url: "/extras/klienci.php",  
			data: 'id='+id,   
			success: function(html)
			{  
				
				info_data = html.split('||');
				
				name = info_data[1];
				link = info_data[2];

				$('#nasi_klienci_logo .holder').append('<img src="upload/klienci/'+id+'.png" alt="'+name+'" />');
				$('#nasi_klienci_logo .holder img:last').css('marginLeft', '280px').show();
				$('#nasi_klienci_logo .holder img').animate({opacity: 1.0}, 500, function() 
				{
					$('#nasi_klienci_logo .loader').fadeOut(); 
					$(this).animate({'marginLeft':'-=280px'});	
				});


				$('#nasi_klienci_belt').html('Klient: <a href="http://'+link+'">'+name+'</a>');


			}
	});

}

stop = false;
function pf_load(id, sub)
{

	if(stop==false)
	{
		$('#header_ramka_img .loader').show();

		$.ajax({   
			type: "POST",   
			url: "/extras/portfolio.php",  
			data: 'id='+id,   
			success: function(html)
			{  	
				info_data = html.split('||');
				
				id = info_data[0];
				name = info_data[1];
				link = info_data[2];


				$('#header_ramka_holder').append('<a href="http://'+link+'"><img src="upload/img/'+id+'1.jpg" alt="'+name+'" /></a>');
				$('#header_ramka_holder img:last').css('marginLeft', '360px');
				
				$('#header_ramka_holder img').animate({opacity: 1.0}, 500, function() 
				{
					$('#header_ramka_img .loader').fadeOut(); 
					$(this).animate({'marginLeft':'-=360px'});
					
					if(sub==false)
					{
						desc = info_data[3];
						$('.header_name img').remove();
						$('.header_name').append('<img src="upload/orange_big/'+id+'.png" />');
						$('.header_name img:last').css('marginTop', '15px');
						$('.header_name img').animate({'marginTop':'-=15px'}, function(){stop = false;});
				

						//$('.header_name').attr('alt', name);



						$('#link_online a').attr('href', 'http://'+link);
						$('#link_online a').html(link);

						$('#header_desc_txt').html(desc);
						$('#header_link_more').attr('href', '/realizacje/'+id);
					}
					else
					{
						desc = info_data[4];
						icons = info_data[5].split('|');
						date = info_data[6].split('|');

						
						$('#data_year').attr('src', 'media/images/dates/'+date[1]+'.png');
						$('#data_month').attr('src', 'media/images/dates/'+date[0]+'.png');

						$('.realizacje_desc2 a').attr('href', 'http://'+link);
						$('.realizacje_desc2 a').html(link);

						$('.pf_current').html(id);
						$('.realization_description').html(desc);

						$('#klient_name img').remove();
						$('#klient_name').append('<img src="upload/orange_small/'+id+'.png" />');
						$('#klient_name img:last').css('marginTop', '-15px');
						$('#klient_name img').animate({'marginTop':'+=15px'}, function(){stop = false;});


						
						$('.realizacje_logos img').attr('src', 'upload/white/'+id+'.png');
						$('.realizacje_logos img').attr('alt', name);

						$('.realizacje_icons').html('&nbsp;');
						for(s=0; s<icons.length; s++)
						{
							icon = icons[s];
							if(icon)
							$('.realizacje_icons').append('<img src="/media/icons/'+icon+'.png" alt="'+icon+'" />');
						}

						$('.numery_01, .numery_02, .numery_03').removeClass('numery_active').bind('click', pf_images).bind('mouseover', num_show).bind('mouseout', num_hide);
						$('.numery_01').addClass('numery_active').unbind('click').unbind('mouseover').unbind('mouseout');
					}

					
					holder_check();
				});

			}
		});
	}
	stop = true;
}

function num_show() {
	$(this).addClass('numery_active');
};

function num_hide() {
	$(this).removeClass('numery_active');
};

function pf_images()
{
	$('#header_ramka_img .loader').show();
	$('.numery_01, .numery_02, .numery_03').removeClass('numery_active');
	$(this).addClass('numery_active');

	id = $('.pf_current').html();
	name = $('#klient_name').attr('alt');
	photo_id = $(this).attr('alt');

	link = $('#header_ramka_holder a:last').attr('href');
	$('#header_ramka_holder').append('<a href="'+link+'"><img src="upload/img/'+id+photo_id+'.jpg" alt="'+name+'" /></a>');
	$('#header_ramka_holder img:last').css('marginLeft', '360px');
	$('#header_ramka_holder img').animate({opacity: 1.0}, 500, function() 
	{
		$('#header_ramka_img .loader').fadeOut(); 
		$(this).animate({'marginLeft':'-=360px'});	
	});

	holder_check();
	
	
	
	$('.numery_01, .numery_02, .numery_03').bind('click', pf_images).bind('mouseover', num_show).bind('mouseout', num_hide);
	$(this).unbind('click').unbind('mouseover').unbind('mouseout');
	return false;
}


info = document.URL;
sub = true;


if(info.substr(info.length-1, 1)=='/') sub = false;

$(document).ready(function() {


	$('#link_online a, .realizacje_desc2 a').click(function() {
		$(this).attr('target', '_blank');
	});

	holder_check()

	if(sub==true)
	link = $('.realizacje_desc2 a').html();
	else
	{
		link = $('#link_online a').attr('href');
		link = link.substr(7, link.length);
	}
	roz = info.split('/');

	if($('.pf_current_sort').html())
	pf_current = $('.pf_current_sort').html();

	cl_current = 1;
	cl_counter = $('#nasi_klienci_logo').attr('title');

	pf_counter = $('.portfolios').html();

	pf_current = pf_counter;
	$('.numery_02, .numery_03').bind('mouseover', num_show);
	$('.numery_02, .numery_03').bind('mouseout', num_hide);
	$('.numery_02, .numery_03').bind('click', pf_images);
	$('.numery_01').addClass('numery_active');
	$('.loader').css('opacity', '0.8');

	$().keypress(function(e) {
		

		if(e.which=='32')
		{
			if(pf_current==pf_counter)
			pf_current=0;
			else
			pf_current++;
		
			pf_load(pf_current, sub);
			
			if(roz[3]!=='kontakt')
			return false;
		}
	});

	$('#btn_mini_l').click(function() {
		
		if(cl_current==1)
		cl_current=cl_counter;
		else
		cl_current--;
		
		cl_load(cl_current);
		return false;
	});

	$('#btn_mini_r').click(function() {
		
		if(cl_current==cl_counter)
		cl_current=1;
		else
		cl_current++;
		
		cl_load(cl_current);
		return false;
	});



	$('.header_go_left').click(function() {
		
		if(pf_current==pf_counter)
		pf_current=0;
		else
		pf_current++;
		
		pf_load(pf_current, false);
		
		return false;
	});

	$('.header_go_right').click(function() {
		
		if(pf_current==0)
		pf_current=pf_counter;
		else
		pf_current--;
		
		pf_load(pf_current, false);
		return false;
	});

	$('.btn_gom_l').click(function() {
		
		if(pf_current==pf_counter)
		pf_current=0;
		else
		pf_current++;
		
		pf_load(pf_current, true);
		
		return false;
	});

	$('.btn_gom_r').click(function() {
		
		if(pf_current==0)
		pf_current=pf_counter;
		else
		pf_current--;
		
		pf_load(pf_current, true);
		return false;
	});


	$('.btn').hover(
			function(){if(!$(this).hasClass('active')) $(this).css('backgroundPosition', '0px 100%')}, 
			function(){if(!$(this).hasClass('active')) $(this).css('backgroundPosition', '0px 0px')}
	);

	$('#top').attr('orgHeight', $('#top').height());

	$('.btn_mapa_strony').click(function() {
		$('.btn_language').removeClass('active');
		$(this).addClass('active');
		$('#lang_links').fadeOut('normal');
		$('#top').animate({height: '200px'});
		$('.logo').animate({'marginTop': '162px'});
		$('#mapa_date').fadeIn('normal');
		return false;

	});

	$('.btn_language').click(function() {
		$('.btn_mapa_strony').removeClass('active');
		$(this).addClass('active');
		$('#mapa_date').fadeOut('normal');
		$('#lang_links').fadeIn('normal');		
		$('.logo').animate({'marginTop': '0px'});
		$('#top').animate({height: $('#top').attr('orgHeight')});
		return false;
	});


	msgs = new Array();
	msgs['name'] = 'Wpisz swoje imię lub nazwę firmy';
	msgs['email'] = 'Wpisz poprawny adres email';
	msgs['telefon'] = 'Wpisz swój telefon';
	msgs['body'] = 'Wpisz wiadomość';


	$('.contact textarea, .contact input').focus(function() {
		if($(this).val()==msgs[this.name]) $(this).val('');
	});
	
	$('.contact textarea, .contact input').blur(function() {
		if($(this).val()=='') $(this).val(msgs[this.name]);
	});

	$('.contact textarea, .contact input').each(function() {
		$(this).val(msgs[this.name]);
	});
	
	$('.contact').submit(function() {
		error=false;

		if($('.form_01').val()=='' || $('.form_01').val()==msgs['name']) 
		{
			$('.form_01').addClass('form_01_r').val(msgs['name']);
			error = true;
		}
		else $('.form_01').removeClass('form_01_r')

		if($('.form_02').val()=='' || $('.form_02').val()==msgs['email'])
		{
			$('.form_02').addClass('form_02_r').val(msgs['email']);
			error = true;
		}
		else if (!checkemail($('.form_02').val()))
		{
			$('.form_02').addClass('form_02_r').val(msgs['email']);
			error = true;
		}
		else $('.form_02').removeClass('form_02_r');

		if($('.form_02a').val()=='' || $('.form_02a').val()==msgs['telefon'])
		{
			$('.form_02a').addClass('form_02a_r').val(msgs['telefon']);
			error = true;
		}
		else $('.form_02a').removeClass('form_02a_r');

		if($('.form_03').val()=='' || $('.form_03').val()==msgs['body']) 
		{
			$('.form_03').addClass('form_03_r').val(msgs['body']);
			error = true;
		}
		else $('.form_03').removeClass('form_03_r')
		
		if(error==false)
		{
			resultList = '';
			$('input, textarea').each(function(i) {
				resultList += this.name+'='+this.value+'&';
			});

			$('.hidden').remove();
			$('.loader').fadeIn();


			$.ajax({   
				type: "POST",   
				url: "/extras/contact.php",  
				data: resultList,   
				success: function(html)
				{  
					$('.loader').fadeOut();
					$('.content_txt').append(html);
				}	 
			});
		}

		return false;
	
	});

	$('#btn_wyslij').click(function() {
		$('.contact').submit();
		return false;
	})

	$('.map_link').click(function() {
		overlay();

		if($.browser.msie)
		$('.map_handler').css({'left': '50%'});
		else
		$('.map_handler').animate({'left': '50%'});
		
	
		return false;
	});

	$('.map_close').click(function() {
		$('.overlay').fadeOut();
		$('.map_handler').css('left', '-50%');
		return false;
	});

	$('#map').jmap('init', {'mapType':'map','mapCenter':[50.209034,18.974504], 'mapZoom':14});
	$('#map').jmap('AddMarker', {'pointLatLng': [50.209034,18.974504]});

	/*
	$('#map1, #map2').hide();

	$('.mapClose').click(function() {$('#map1, #map2').fadeOut(); return false;});
	$('.map1').click(function() {$('#map1').fadeIn(); return false;});
	$('.map2').click(function() {$('#map2').fadeIn(); return false;});

	$('.link').mouseover(function() {$(this).children('p').hide();});
	$('.link').mouseout(function() {$(this).children('p').show();});
	*/



});
