// JavaScript Document

// + Updates
// + ------------------------------------------------------------
// +
// + 2009-04-24 => Modification appel ajout dans mes notes
// +
// + ------------------------------------------------------------

function mes_notes_split () {
	var cookie_ary = Array();
	
	if ($.cookie('mesNotes') != null) {
		var sep1 = new RegExp("[|]+", "g");
		var sep2 = new RegExp("[;]+", "g");
		
		var cookie_ary = $.cookie('mesNotes').split(sep1);
		
		for (var i = 0; i < cookie_ary.length; i++) {
			cookie_ary[i] = cookie_ary[i].split(sep2);
		}
	}
	
	return cookie_ary;
}

function mes_notes_build () {
	var cookie_ary = mes_notes_split();
	var mes_notes_content = '';
	//var img_path = '/atlantic/images/mes-notes/';
	var img_path = '/images/mes-notes/';
	
	if (cookie_ary.length > 0) {
		mes_notes_content += '<table border="0" cellpadding="0" cellspacing="0">\n';
		
		for (var i = 0; i < cookie_ary.length; i++) {
			mes_notes_content += '<tr>\n';
			mes_notes_content += '<td align="center" valign="middle"><a href="' + cookie_ary[i][2] + '"><img src="' + img_path + 'picto-' + cookie_ary[i][0] + '.png" alt="' + cookie_ary[i][1] + '" title="' + cookie_ary[i][1] + '" /></a></td>\n';
			mes_notes_content += '<td align="left" valign="middle"><span class="normal-link"><a href="' + cookie_ary[i][2] + '">' + cookie_ary[i][1] + '</a></span></td>\n';
			mes_notes_content += '<td align="right" valign="middle"><img src="' + img_path + 'right-block-bullet.gif" alt="Supprimer" title="Supprimer" onClick="mes_notes_delete(' + i + ')" style="cursor:pointer" /></td>\n'
			mes_notes_content += '</tr>\n';
		}
		
		mes_notes_content += '</table>\n';
	}
	else {
		mes_notes_content += '<p class="side-block-content-p">Retrouvez rapidement les pages qui vous intéressent en les ajoutant dans vos notes grâce à cet icône</p>';
		mes_notes_content += '<div class="text-align-center"><img src="' + img_path + 'picto-no-bookmark.gif" alt="Mes notes" title="Mes notes" /></div>';
	}
	
	$('#mes-notes > div.side-block-content').empty();
	$('#mes-notes > div.side-block-content').append(mes_notes_content);
	$('#mes-notes > div.side-block-content > table > tbody > tr:even').addClass('blue');
}


function mes_notes_add (mes_notes_type, mes_notes_text) {
	var mes_notes_url = document.location.href;
	var cookie_value = '';
	var cookie_ary = mes_notes_split();
	var mes_notes_elt_exist = false;
	
	if (cookie_ary.length > 0) {
		for (var i = 0; i < cookie_ary.length; i++) {
			if (cookie_ary[i][0] == mes_notes_type && cookie_ary[i][1] == mes_notes_text && cookie_ary[i][2] == mes_notes_url) {
				alert('Déjà enregistré dans Mes notes');
				mes_notes_elt_exist = true;
				break;
			}
		}
		
		cookie_value = $.cookie('mesNotes') + '|'
	}
	
	if (!mes_notes_elt_exist) {
		cookie_value += mes_notes_type + ';' + mes_notes_text + ';' + mes_notes_url;
		$.cookie('mesNotes', cookie_value, { path: '/'});
		if ($('#mes-notes').length) { // Updted on 2009-04-24
			mes_notes_build();
		}
	}
	
	return false;
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
}

function mes_notes_delete (mes_notes_id) {
	var cookie_ary = mes_notes_split();
	var cookie_value = null;
	var tmp_ary = Array();
	
	cookie_ary.remove(mes_notes_id);
	
	if (cookie_ary.length) {
		for (var i = 0; i < cookie_ary.length; i++) {
			tmp_ary[i] = cookie_ary[i].join(";");
		}
		cookie_value = tmp_ary.join("|");
	}
	
	$.cookie('mesNotes', cookie_value, { path: '/'});
	mes_notes_build();
}

(function($) {
	$(function(){
		/* Document is ready
		---------------------------------------- */
		mes_notes_build();
		
		// Added on 2009-04-24
		$('#mes-notes-btn-add').click(function () {
			var mesNotesUrl = this.href;
			//var mesNotesType = mesNotesUrl.substring(mesNotesUrl.lastIndexOf('/')+1);
      var mesNotesType = $(this).attr('tabindex');
			var mesNotesText = this.title;
			
			mes_notes_add(mesNotesType,mesNotesText);
			
			return false;
		});
		
	});
})(jQuery);
