function clearValue(object) {
  if (object.value == object.defaultValue) {
    object.value = "";
  }
}

function restoreValue(object) {
  if (object.value === "") {
    object.value = object.defaultValue; 
  }
}

function supprimer_favoris(id_document) { 
	var liste = "";
	var newList = "";
	var tmpList = [];
	var item = [];
	var flag = false;
	var count = 0;
	liste = $.cookie('vivendi_bibliotheque');
	if(liste != null) {
		tmpList = liste.split(';');
		for(i=0; i<tmpList.length; i++) {
			item = tmpList[i].split('|');
			if(item[0] == id_document) {
				/* removing data */
				flag = true;
			} else {
				if(newList == "") {
					newList = tmpList[i];
				} else {
					newList = newList+";"+tmpList[i];
				}
				count++;
			}
		}
		/* finished : reset cookie */
		if(newList.length == 0) { newList = null; }
		var date = new Date();
		date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
		$.cookie('vivendi_bibliotheque', newList, {
			path: '/',
			expires: date
		});
		$('#nb_docB',top.document).html(count);
		if (flag) { 
			window.location.reload(); 
		}
	} else {
		/* do nothing */
	}
}

function ajoute_favoris(id_document, titre, titreArticle,  descriptif, logo, url, lb, lang)
{
	//Récupère la liste des documents
	var liste = "";
	var item = "";
	var flag = true;
	var count = 1;
	
	if(lang=="en"){
		var save = "Document added in your library";
		var exists = "Document already added";
	}
	else{
		var save = "Document sauvegardé dans la bibliotheque";
		var exists = "Ce document est déjà dans votre bibliothèque";
	}
	
	liste = $.cookie('vivendi_bibliotheque');
	if (liste != null) {
		a = liste.split(";");
		count = a.length;
		for (k = 0; k < count; k++) {
			item = a[k].split("|");
			if (item[0] == id_document) {
				flag = false;
			}
		}
	}
	if (flag) {
		// Sauve cookie avec ID_DOCUMENT
		var date = new Date();
		date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
		if (liste == null) {
			liste = "";
		}
		else {
			liste = liste + ";";
			count++;
		}
		item = id_document+"|"+titre+"|"+titreArticle+"|"+descriptif+"|"+logo+"|"+url;
	
		$.cookie('vivendi_bibliotheque', liste + item, {
			path: '/',
			expires: date
		});
		alert(save);
		$('#nb_docB',top.document).html(count);
	}
	else{
		alert(exists);
	}
	return false;
}

function get_favori(id_document)
{
	//Récupère la liste des documents
	var liste = "";
	var item = "";
	var flag = false;
	liste = $.cookie('vivendi_bibliotheque');
	
	if (liste != null) {
		a = liste.split(";");
		count = a.length;
		for (k = 0; k < count; k++) {
			item = a[k].split("|");
			if (item[0] == id_document) {
				flag = true;
			}
		}
	}
	return flag;	
}

function load_cookie()
{
	var liste = "";

	liste = $.cookie('vivendi_bibliotheque');
	
	if (liste != null) {
		a = liste.split(";");
		$('#nb_docB').html(a.length);
	}
	else
	{
		$('#nb_docB').html("0");
	}
	
}
function charge_bibliotheque(begin, lb, lang)
{
	var liste = "";
	var html = "";
	var size = 6;
	var iteration = 0;
	var current = 0;
	var total = 0;
	var maxpage = 0;
	var request = 0;
	var realBegin = 0;
	var realLb = 0;
	var image="";
	var a = [];
	var item = "";
	if(begin==undefined){
		begin = 0;
	} else {
		realBegin = begin;
	}
	if(lb==undefined) {
		realLb = 0;
	} else {
		realLb = lb;
	}
	if(lang=="en"){
		var visionner = "Show";
		var supprimer = "Delete";
		var npager = "of";
	}
	else{
		var visionner = "Visionner";
		var supprimer = "Supprimer";
		var npager = "sur";
	}
	//alert("alert at the begining:"+$('#size').val());
	current = realBegin/size+1;
	if($.cookie('vivendi_bibliotheque')!=null){
		liste = $.cookie('vivendi_bibliotheque');
		a = liste.split(";");
		count = a.length;
		total = Math.floor(count/size)+1;
		iteration = begin + size;
		if(iteration > count){
			iteration = count;
		}
		var reg=new RegExp("dist","g");
		for (k = realBegin; k < iteration; k++) {
			item = a[k].split("|");
			image=item[4];
			if (image.match(reg)) {
				/*image=item[4];*/
			} else {
				image='IMG/'+image;
			}
            html += '<div class="Item">';
            html += '        <div class="Image"><img src="' + image +'"/></div>';
            html += '        <div class="Info">';
            html += '                <h4>' + item[1] + '</h4>';
            html += '                <p>' + item[3] + '</p>';
            html += '                <ul>';
            html += '                        <li class="Icon1"><a target="_blank" href="' + item[5] +'" title="'+visionner+'">'+visionner+'</a></li>';
            html += '                        <li class="Icon2" id="doc_' + item[0] + '"><a href="javascript:void(0);" title="'+supprimer+'" onclick="supprimer_favoris(\''+item[0]+'\');">'+supprimer+'</a></li>';
            html += '                </ul>';
            html += '        </div>';
            html += '</div>';
		}
		html += '<div class="Pagination">';
			html += '<form id="paginForm" action="" method="post">';
			html += '<input type="text" id="debut_page" name="debut_page" value="'+current+'" class="Input"/>';
			html += '<input type="hidden" value="y" id="size" name="size" />';
			html += '<input type="hidden" value="" id="debut_liste" name="debut_liste"/>';
			html += '<input type="hidden" value="'+realLb+'" id="lb" name="lb"/> '+npager+' '+total;
			//html += '<input type="image" src="squelettes/images/btn-ok-1.gif" onclick="'alert($("debut_page").value*size')" alt="OK" title="OK"/>';
			//html += '<input type="image" src="squelettes/images/btn-ok-1.gif" onclick="alert($(\'#size\').val());" alt="OK" title="OK"/>';
			html += '<input type="image" src="squelettes/images/btn-ok-1.gif" alt="OK" title="OK"/>';
			if(current!=1){
				html += '<a href="javascript:charge_bibliotheque('+(realBegin-size)+', null,\''+lang+'\');"><img width="14" height="13" src="squelettes/images/ico-prev.gif" alt="Prev"/></a>';
			}
			if(current<total){
				html += '<a href="javascript:charge_bibliotheque('+(realBegin+size)+', null,\''+lang+'\');"><img width="14" height="13" src="squelettes/images/ico-next.gif" alt="Next"/></a>';
			}
			html += '</form>';
        html += '</div>';
		
		$('#bibliotheque').html(html);
		//alert("alert at the end:"+$('#size').val());
		//TODO: implement the pager in a JQuery object
		//$('#pagination').displayPager(current,size);
	}
}

(function($){
        jQuery.fn.displayPager = function(current,size) {
        	var html = "";
        	alert("inside displayPager");
        	alert(current);
        	html += '<form id="paginForm" action="?lb=" method="post">';
			html += '<input type="text" id="debut_page" name="debut_page" value="x" class="Input"/>';
			html += '<input type="hidden" value="y" id="size" name="size" />';
			html += '<input type="hidden" value="" id="debut_liste" name="debut_liste"/>';
			html += '<input type="hidden" value="m" id="lb" name="lb"/> sur n';
			html += '<input type="image" src="squelettes/images/btn-ok-1.gif" alt="OK" title="OK"/>';
			html += '<a href="javascript:charge_bibliotheque(0);"><img width="14" height="13" src="squelettes/images/ico-prev.gif" alt="Prev"/></a>';
			html += '<a href="javascript:charge_bibliotheque(12);"><img width="14" height="13" src="squelettes/images/ico-next.gif" alt="Next"/></a>';
			html += '</form>';
			
			$(this).html(html);
			alert($(this).html);
			
        }
})(jQuery);

$(document).ready(function(){
	/* Fix large */
	if($.browser.msie && ($.browser.version == '6.0')) {
		var obj = $('div.large').get(0);
		var size = $(obj).width()-70;
		$('div.large>div.Info').css({'width':size+'px'});
		//$('div.Item>div.Info>h4').css({'font-size':'1.0em'}); 
	}
});
