$(document).ready(function() {
 
 	//dodawanie/usuwanie z ulubionych
	$(".ajax a").click(function(){
		
		var title = $(this).attr('title');
		var href = $(this).attr('href');
		var parent = $(this).parent().get(0).className;
		
		if($(this).attr('class')=='add')
		{
			ajax_add(title,href,parent);
		}
		if($(this).attr('class')=='del')
		{
			ajax_del(title,href,parent);
		}		
		return false;
	});

 	//dodawanie do ulubionych
 	function ajax_add(title, href, parent)
	{
		$.ajax({
			type:   'POST',
			dataType: 'json',
			url:	href,
			data: 'type=ajax&title='+title,
			timeout: 5000,
			beforeSend:  function() {
				
			},
			error: function(msg) {
				$(".add").show();
				alert(msg);
			},
			success:  function(json) {
				if(json.error == 'true') 
				{ 
					alert(json.text); 
				}
				else
				{
					var add = "." + parent + " > .add";
					var del = "." + parent + " > .del";

					$(add).hide();
					$(del).show();					
				}
			}
		});		
	}

 	function ajax_del(title, href, parent)
	{
		$.ajax({
			type:   'POST',
			dataType: 'json',
			url:	href,
			data: 'type=ajax&title='+title,
			timeout: 5000,
			beforeSend:  function() {
				
			},
			error: function(msg) {
				$(".del").show();
				alert(msg);
			},
			success:  function(json) {
				if(json.error == 'true') 
				{ 
					alert(json.text); 
				}
				else
				{
					var add = "." + parent + " > .add";
					var del = "." + parent + " > .del";

					$(del).hide();
					$(add).show();					
				}
			}
		});		
	}









 	//dodawanie/usuwanie z ulubionych; stare
	$(".fav_place a").click(function(){
		if($(this).attr('class')=='add')
		{
			placeAddToFav();
		}
		if($(this).attr('class')=='del')
		{
			placeDelFromFav();
		}		
		return false;
	});
	
	$(".search").focus(function(){
		jQuery(this).val('');
	});	
 
 	//dodawanie do ulubionych; stare
 	function placeAddToFav()
	{
		var place = $(".fav_place a").attr('title');
		$.ajax({
			type:   'POST',
			dataType: 'json',
			url:	$(".fav_place a").attr('href'),
			data: 'type=ajax&place='+place,
			timeout: 5000,
			beforeSend:  function() {
				
			},
			error: function(msg) {
				$(".add").show();
				alert(msg);
			},
			success:  function(json) {
				if(json.error == 'true') 
				{ 
					alert(json.text); 
				}
				else{
					$(".fav_place > .add").hide();
					$(".fav_place > .del").show();					
				}
			}
		});		
	}
	
	//kasowanie z ulubionych; stare
 	function placeDelFromFav()
	{
		var place = $(".fav_place a").attr('title');		
		$.ajax({
			type:   'POST',
			dataType: 'json',
			url:	$(".fav_place a").attr('href'),
			data: 'type=ajax&place='+place,
			timeout: 5000,
			beforeSend:  function() {
				
			},
			error: function(msg) {
				$(".del").show();
				alert(msg);
			},
			success:  function(json) {
				if(json.error == 'true') 
				{ 
					alert(json.text); 
				}
				else{
					$(".del").hide();
					$(".add").show();					
				}
			}
		});		
	}	
	
	//dodawanie x/y gmap
	$("#place_postcode").change(function(){
  		var postcode = $("#place_postcode").val();
		var geo = new GClientGeocoder();
		geo.getLatLng(postcode,function(punkt)  
		{  
		    if (!punkt)  
		    {  
		        // nie ma
		    }  
		    else  
		    {  
		        // jest 
				$("input[@name=place_geo]").val(punkt);
				//alert(punkt);
		    }  
		}); 
		  
	});
	
	//zaznacz/odznacz checkbox
	$('#checkboxSelectAll').click(function() {
		$('input[@type=checkbox]').attr({ checked: "checked"});
	});

	$('#checkboxDeselectAll').click(function() {
		$('input[@type=checkbox]').removeAttr("checked");
	});	

	adjustCalendar();


	$( 'table.calendar td' ).click( function() { 
		window.location = $(this).children('span.day').children('a').attr('href');
	});
	$( 'table.calendar td a' ).click( function() { 
		window.location = $(this).attr('href');
		return false;
	});


function adjustCalendar() {
	// dostosuj szerokosc eventow do szerokosci komorek
	$('table.calendar span.event').css( 'width', $('table.calendar tr td' ).width()+'px' );
	$('table.calendar span.event a').css( 'white-space', 'nowrap' );
	
	// dostosuj rozmiar linku dnia
} 	
	
});
