$(window).load(function() {

	
	function dodajNotes(id) {
		var thisId = id;
		$.ajax({
		  url: 'notes.php',
		  type: 'POST',
		  data: 'dodaj=' + thisId,
		  success: function(data) {
		    $('#notes_dodaj_' + thisId).remove();
		    var ileWNotesie = $('.oferta_w_notesie').length;
		    if(ileWNotesie ==0) {
		    	//usuniecie tekstu
		    	$('#notesText-left').empty();
		    	$('#notesText-panel').empty();
		    	//create ul
		    	$('#notesText-left').html('<ul class="content" id="oferty_w_notesie-left"></ul>');
		    	$('#notesText-panel').html('<ul class="content" id="oferty_w_notesie-panel"></ul>');
		    }
	    	$('#oferty_w_notesie-left').append('<li id="notes-left_' + thisId + '" class="oferta_w_notesie"></li>');
	    	$('#notes-left_' + thisId).append(data);
	    	$('#oferty_w_notesie-panel').append('<li id="notes-panel_' + thisId + '" class="oferta_w_notesie"></li>');
	    	$('#notes-panel_' + thisId).append(data);
		  }
		});
	}
	
	function usunNotes(id) {
		var thisId = id;
		
		$.ajax({
		  url: 'notes.php',
		  type: 'POST',
		  data: 'usun=' + thisId,
		  success: function(data) {
		    $('#notes-left_' + thisId).remove();
		    $('#notes-panel_' + thisId).remove();
		    
		    var ileWNotesie = $('.oferta_w_notesie').length;
		    if(ileWNotesie ==0) {
		    	$('#notesText-left').empty();
		    	$('#notesText-panel').empty();
		    	var text = '<div class="content" style="padding: 35px 5px 5px 5px;">' + 
		    			'<span class="b">Aktualnie w notesie nie znajduje się żadna oferta</span><br />' + 
						'<br />' + 
						'Notes Ofert jest miejscem w którym możesz przechowywać wybrane przez Siebie oferty.<br />' + 
						'<br />' +
						'Oferty do Notesu Ofert dodawać można wybierając<br />' + 
						'<div style="text-align: center;">' +
						'<img src="public/images/info_notes_list.gif" alt="Dodaj do notesu" style="border: none;" />' +
						'</div>' +
						'na liście ofert, bądź w szczegółach oferty' + 
					'</div>';
		    	$('#notesText-left').append(text);
					
				$('#notesText-panel').append(text);
		    }
		    $('#offer_' + thisId).append('<a href="#" class="note-add" id="notes_dodaj_' + thisId + '">dodaj do notesu</a>');
		  }
		});
	}
	
	$('.note-add').click( function() {
		var $target = $(this);
		dodajNotes($target.attr('id').substring(12, $target.attr('id').length));
		return false;
	});
	$('.note-add').live("click",  function () {
		var $target = $(this);
		dodajNotes($target.attr('id').substring(12, $target.attr('id').length));
		return false;
	});
	
	$('.del').click(function() {
		var $target = $(this);
		usunNotes($target.attr('id').substring(7, $target.attr('id').length));	
		return false;	
	});
	
	$('.del').live ("click", function() {
		var $target = $(this);
		usunNotes($target.attr('id').substring(7, $target.attr('id').length));	
		return false;	
	});
});	
