/* nic tu ciekawego nie znajdziesz */

function jmap_load(coorx, coory)
{
	$('#map').jmap('init', {'mapType':'map','mapCenter':[coorx, coory], 'mapZoom':14});
	$('#map').jmap('AddMarker', {'pointLatLng': [coorx, coory]});
}

function hide()
{
	$(this).fadeOut('slow');
}

function overlay()
{
	if(!$('.overlay').width())
	$('body').append('<div class="overlay"></div>');
	
	$('.overlay').height($(document).height()).css('opacity', '0.8');
	
	$('.overlay').fadeIn();
}


function validateMail(address)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(address) == false)
		return false;
	else
		return true;
}

$(document).ready(function() {

	$('select[name=kwota]').change(function() {
		value = $(this).val();
		if(value=='')
		rep = 'Wybierz typ płatności';
		else
		rep = 'Do zapłaty '+value+'zł';

	
		$('.pay_info').html(rep);

	});

	$('.pay form').submit(function() {
		if($('input[name=opis]').val()=='')
		{
			$('.error').html('Wpisz numer ogłoszenia');
			return false;
		}
		if($('select[name=kwota]').val()=='')
		{
			$('.error').html('Wybierz typ płatności');
			return false;
		}
	});
	

	$('.btn_search').click(function() {
		if(($('select[name=hours_from]').val()!=='0' || $('select[name=hours_to]').val()!=='0') && $('select[name=days]').val()=='0')
		{
			alert('Wybierz dzień tygodnia');
			return false;
		}
	});

	$('textarea').focus(function() {
		if($(this).val()=='opis...') $(this).val('');
	});
	
	$('textarea').blur(function() {
		if($(this).val()=='') $(this).val('opis...');
	});


	$('.btn_next').each(function() {

		move = $(this).attr('alt');
		counter = $(this).parent().parent().children('.person').length;

		$(this).parent().attr('limit', Math.ceil(counter/move)-1);
		$(this).parent().attr('page', 0);
		
		if(counter <= move) $(this).parent().hide();
	});

	$('.btn_next').click(function() {
		page = $(this).parent().attr('page');
		limit = $(this).parent().attr('limit');


		move = $(this).attr('alt');
		
		if($(this).hasClass('blocked'))
			return false;

		$(this).prev().removeClass('blocked');

		page++;

		start = page * move;

		$(this).parent().parent().children('.person').each(function(i) {

			if(i >= start && i <= start+(move-1))
			$(this).show();
			else
			$(this).hide();
		});

		if(page==limit)
		$(this).addClass('blocked');

		
		$(this).parent().attr('page', page)

		return false;
	});

	$('.btn_prev').click(function() {
		page = $(this).parent().attr('page');
		limit = $(this).parent().attr('limit');
		
		move = $(this).attr('alt');
		

		if($(this).hasClass('blocked'))
			return false
	
		$(this).next().removeClass('blocked');

		page--;
		start = page * move;

		$(this).parent().parent().children('.person').each(function(i) {

			if(i >= start && i <= start+(move-1))
			$(this).show();
			else
			$(this).hide();
		});


		if(page==0)
		$(this).addClass('blocked');
		
		
		$(this).parent().attr('page', page)

		return false;
	});



	$('.btn_s').mouseover(function() {$(this).css('backgroundPosition', '0px 100%')});
	$('.btn_s').mouseout(function() {$(this).css('backgroundPosition', '0px 0px')});


   $(".only_digits").keypress(function (e)  
	{ 
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
	  {
		if(!$(this).next().hasClass('error'))
		$(this).after('<p class="error">Tylko cyfry!</p>');
		else $(this).next().show();
		
		$(this).next().fadeOut("slow")
	    return false;
      }	
	});

	$('.partners a').click(function() {
	
		$(this).parent().children('a').css('fontWeight', 'normal');
		$(this).css('fontWeight', 'bold');
		
		data = $(this).attr('alt');
		data = data.split('|');
		
		id = data[0];
		type = data[1];


		if(type=='F')
		{
			$('.partner_'+id+' .man').attr('disabled', true).hide();
			$('.partner_'+id+' .mus').html('Biust');
		}
		else
		{
			$('.partner_'+id+' .man').attr('disabled', false).show();
			$('.partner_'+id+' .mus').html('Muskulatura');
		}

		$('.partner_'+id).show();
		$('.gender_'+id).val(type);

		return false;
	});


	$('.add_form').submit(function() {

		myReturn = true;
		file_counter = 0;

		$('.add_form input, .add_form select').each(function() {
		
			if($(this).hasClass('is_req') && $(this).val()=='')
			{
				$(this).addClass('error2');
				myReturn = false;
			}
			else
			$(this).removeClass('error2');

			if(this.name=='file1[]')
			file_counter++;

			if(this.name=='email')
			{
				if(this.value!=='' && validateMail(this.value)==false)
				{
					$(this).addClass('error2');
					myReturn = false;
				}
				else
				$(this).removeClass('error2');
			}
			if(this.name=='phone' && this.value=='+48')
			{
					$(this).addClass('error2');
					myReturn = false;
			}
		});


		err_msg = 'proszę uzupełnić pola w ramkach oznaczonych na czerwono';

		for(s=1; s<=7; s++)
		{
			if((this['day_'+s+'_from'].disabled==false && this['day_'+s+'_from'].value=='') || (this['day_'+s+'_to'].disabled==false && this['day_'+s+'_to'].value==''))
			{
				err_msg = 'Ustal godziny pracy';
				myReturn = false;
			}
		}
		

		if(myReturn==true && file_counter <= 1)
		{
			err_msg = 'Proszę dodać przynajmniej 1 zdjęcie';
			myReturn = false;
		}

		if($('.gender_8').val()=='')
		{
			err_msg = 'Proszę wybrać płeć 1 partnera';
			myReturn = false;
		}
		if($('.gender_9').val()=='')
		{
			err_msg = 'Proszę wybrać płeć 2 partnera';
			myReturn = false;
		}

		if(myReturn == false)
		{
			$('.btn_zapisz').before('<div class="add_msg">'+err_msg+'</div>').prev().animate({'opacity':'1'}, '5000', '', hide);
		}

		if(myReturn == true)
		$('.btn_zapisuje').show();


		return myReturn;
	});

	$('.multi').change(function() {
	
	$(this).after('<div class="add_next">można dodać kolejne zdjęcia!</div>');
	
	});

	$('.contact_form').submit(function() {

		myReturn = true;

		$('.contact_form input, .contact_form textarea').each(function() {
		
			if($(this).hasClass('is_req') && $(this).val()=='')
			{
				$(this).addClass('error2');
				myReturn = false;
			}
			else
			$(this).removeClass('error2');


			if(myReturn!==false && this.name=='email')
			{
				if(this.value!=='' && validateMail(this.value)==false)
				{
					$(this).addClass('error2');
					myReturn = false;
				}
				else
				$(this).removeClass('error2');
			}

		});

		if(myReturn==true)
		{
			$(this).hide();

			resultList = '';
			$('input, textarea').each(function(i) {
				resultList += this.name+'='+this.value+'&';
			});


			$.ajax({   
				type: "POST",   
				url: "/extras/contact.php",  
				data: resultList,   
				success: function(html)
				{  
					$('.contact_form').after('<p>'+html+'</p>');
				}	 
			});
				
		}
		else
		{
			err_msg = 'proszę uzupełnić pola w ramkach oznaczonych na czerwono';
			$('.btn_add').after('<div class="add_msg2">'+err_msg+'</div>').next().animate({'opacity':'1'}, '5000', '', hide);

		}
		
		return false;
	});

	types = new Array();
	types[1] = 'kobieta';
	types[2] = 'mezczyzna';
	types[3] = 'para';
	types[4] = 'lokal';


	$('.btn_head1, .btn_head2, .btn_head3, .btn_head4').click(function() {
		
		$(this).parent().children().removeClass('active');


		type = $(this).attr('alt');
		$('.search_type').val(type);
		
		
		$('.search_link').attr('href', '/szukaj/'+types[type]);

		if($(this).hasClass('btn_head4') || $(this).hasClass('btn_sea4'))
		{
			$('.place_hide input, .place_hide select').val('').attr('disabled', true);
			$('.place_hide').hide();
		}
		else
		{
			$('.place_hide input, .place_hide select').attr('disabled', false);
			$('.place_hide').show();
		}
		

		$(this).addClass('active');

		return false;
	});

	$('#header input, #header select').each(function(i) {
	
		if($(this).attr('disabled')==true) $('.place_hide').hide();
	
	});

	$('.sort').change(function() {
		 document.location=$(this).val();
	});

	$('.btn_fav').livequery('mouseover', function() {
		$(this).parent().children('.fav_info').show();
	});

	$('.btn_fav').livequery('mouseout', function() {
		$(this).parent().children('.fav_info').fadeOut('fast');
	});

	$('.photo').mouseover(function() {
		$(this).parent().children('.photo_big').show();
	});

	$('.photo').mouseout(function() {
		$(this).parent().children('.photo_big').hide();
	});

	$('#fav li').livequery('mouseover', function() {
		$(this).children('.photo_big').show();
	});

	$('#fav li').livequery('mouseout', function() {
		$(this).children('.photo_big').hide();
	});

	$('.btn_fav').livequery('click', function() {
		
		data = $(this).attr('alt');
		data = data.split('|');
		id = data[0];
		nick = data[1];
		curl = data[2];
		url = data[3];
		
		if(nick.length > 10)
		nick = nick.substr(0,10)+'...';

		html = '<li><div class="photo_big"><img src="photo.php?object='+id+'&w=180"></div><a href="#" class="btn btn_favdel delfav_'+id+'" alt="'+id+'"></a><a href="/ogloszenie/'+curl+'/'+url+'">'+nick+'</a></li>';

		if($('#fav ul li').length < 4)
		$('#fav ul').append(html);

		$.ajax({   
			type: "POST",   
			url: "/extras/ajax.php",  
			data: 'object_id='+id,   
			success: function(html)
			{  
				$('.fav_'+id).fadeOut();
			}
				 
		});
		return false;
	});

	$('.btn_favdel').livequery('click', function() {
		
		id = $(this).attr('alt');
		$.ajax({   
			type: "POST",   
			url: "/extras/ajax.php",  
			data: 'del_id='+id,   
			success: function(html)
			{  
				$('.delfav_'+id).parent().fadeOut().remove();

				if($('.btn_favdel').length==0)
				$('.btn_fav_more').fadeOut();


			}
				 
		});
		return false;
	});

	$('.btn_favdel').livequery('mouseover', function() {
		$(this).after('<p class="del_info">usuń!!</p>');
	});

	$('.btn_favdel').livequery('mouseout', function() {
		$(this).next().remove();
	});




	$('.btn_lang').mouseover(function() {
		$(this).prev().show();
	});
	
	$('.btn_lang').mouseout(function() {
		$(this).prev().hide();
	});

	$('.stars').rating(5);

	$('.btn_add_comment').click(function() {
		$(this).hide();
		$('.comment').show();

		loc = document.URL.split('#');


		if($.browser.msie)
		document.location = '#bottom';
		else
		document.location = loc[0]+'#bottom';

		return false;
	});

	$('.btn_add_comment2').click(function() {
		
		body = $('.comment_body').val();
		data = $(this).attr('alt');
		data = data.split('|');
		id = data[0];

		if(body=='')
		{
			$(this).before('<p class="comment_error">Wpisz komentarz!</p>').prev().fadeOut('slow');
			return false;
		}

		$.ajax({   
			type: "POST",   
			url: "/extras/comment.php",  
			data: 'id='+id+'&body='+body,   
			success: function(html)
			{  
				$('.comment').hide();
			}
				 
		});
		return false;

	});
   
	$('.gallery a').lightBox();
   

	//welcome
	if(!$.cookie('accept'))
	overlay();

	domain = document.URL;
	domain = domain.split('/');

	$('.btn_yes').click(function() {
	
		$.cookie('accept', '1', { path: '/', expires: 10, domain: domain[2] });
		$('.overlay').hide();
		$('.msg').remove();
	});

	$('.btn_dojazd').click(function() {
	
		overlay();

		loc = document.URL.split('#');

		
		if($.browser.msie)
		{
			$('.map_handler').css({'left': '50%'});
			document.location = '#header';
		}
		else
		{
			$('.map_handler').animate({'left': '50%'});
			document.location = loc[0]+'#header';
		}

		return false;
		

	});

	$('.map_close').click(function() {
		$('.overlay').fadeOut();
		$('.map_handler').css('left', '-50%');
		return false;
	});

//Godziny Pracy
$('.chbx_free').click(function() {
	if($(this).attr('checked')==true)
	{
		$(this).parent().parent().children().children('div').hide();
		$(this).parent().parent().children().children('.grafik').attr('disabled', true);
		$(this).parent().parent().children().children('.chbx').attr('checked', false);
	}
	else
	{
		$(this).parent().parent().children().children('.picker').show();
		$(this).parent().parent().children().children('.grafik').attr('disabled', false);
	}
});

$('.chbx').click(function() {
	if($(this).attr('checked')==true)
	{
		$(this).parent().parent().children().children('.grafik').attr('disabled', false);		
		$(this).parent().parent().children().children('.chbx_free').attr('checked', false);
		$(this).parent().parent().children().children('.picker').eq(0).html('00:00').unbind('click').show();;
		$(this).parent().parent().children().children('input').eq(0).val('1');
		$(this).parent().parent().children().children('.picker').eq(1).html('24:00').unbind('click').show();;
		$(this).parent().parent().children().children('input').eq(1).val('49');
	}
	else
	{
		$(this).parent().parent().children().children('.picker').click(function() {
			$('.dateList').hide();
			$(this).next().show();
		});
	}
});

$('.picker').click(function() {
	$('.dateList').hide();
	$(this).next().show();

});

$('.dateList li').mouseover(function() {
	$(this).addClass('active');
});

$('.dateList li').mouseout(function() {
	$(this).removeClass('active');
});

$('.dateList li').click(function() {
	value = $(this).html();
	position = $(this).attr('position');
	
	$(this).parent().parent().prev().html(value);
	$(this).parent().parent().prev().prev().val(position);
	$(this).parent().parent().hide();
	
	
	if($(this).parent().parent().hasClass('from'))
	{
		$(this).parent().parent().parent().next().children('.dateList').children().children().show();
		$(this).parent().parent().parent().next().children('.dateList').children().children().each(function(i) {
			if(i<=position) $(this).hide();
		});
	}
	else
	{
		$(this).parent().parent().parent().prev().children('.dateList').children().children().show();
		$(this).parent().parent().parent().prev().children('.dateList').children().children().each(function(i) {
			if(i>=position) $(this).hide();
		});
	}
});

$('.newsletter').submit(function() {

	myReturn = true;
	$('.newsletter input, .newsletter select').not('.send_btn').each(function() {

			if(this.name=='email')
			{
				if(this.value=='' || validateMail(this.value)==false)
				{
					$(this).addClass('error2');
					myReturn = false;
				}
				else
				$(this).removeClass('error2');
			}
			else 
			{
				if(this.value=='0')
				{
					$(this).addClass('error2');
					myReturn = false;
				}
				else
				$(this).removeClass('error2');
			}
	});

	return myReturn;
});




});