﻿
/* initialiser les lients defeaut */

var bex_favories = new Array("Facebook=>http://www.facebook.com/","Gmail=>http://mail.google.com","Google=>http://www.google.fr/","Hotmail=>http://www.hotmail.com/","Bing=>http://www.bing.com/","Yahoo Mail=>http://mail.yahoo.fr/","YouTube=>http://www.youtube.com","Wissal=>http://www.wissal.dz/","Dailymotion=>http://www.dailymotion.com","Wikipedia=>http://fr.wikipedia.org");


var favories = {

init : function ()
{
// init
/* verification de cookie: si le cookie "profile" existe alore on lance la function logged qui est juste decu */
favories.logged();
/* la fonction logged ajoute les outils "editer" et "supprimé" dans chaque lient */
favories.search();
/* apré initialise d'action modifier */
favories.hoverShow();


},
hoverShow: function()
{
jQuery("#options-set #edit").each(function(index){
jQuery(this).click(function() {
jQuery(".edit #edit_forma:eq("+index+")").hide();
var link_href = jQuery(".edit a")[index].getAttribute("href");
var link_val = jQuery(".edit a:eq("+index+")").html();
var last_index=index;
// form creation
var html='<h3 class="box_title">Modifier un lien hypertexte</h3>'
        +'<span id="error_link" class="error"></span>'
        +'<input name="link_val" class="input_req" id="link_val" type="text" value="'+link_val+'" />'
        +'<input name="link_href" class="input_req" id="link_href" type="text" value="'+link_href+'" />'
        +'<center><input type="button" id="save_edit" name="edit" value="OK" /></center>';
jQuery.facebox(html);
jQuery("#save_edit").click(function(){
var saved_val = jQuery("#link_val").val();
var saved_href = jQuery("#link_href").val();
favories.save(last_index,saved_val,saved_href);
});
});
});
/* ######################################**** delete link ****############################################ */
jQuery("#options-set #delete").each(function(index){
jQuery(this).click(function(){
favories.delete_l(index);
});
});
}
,
setFavicon: function(index,href){
var url_domain = favories.getDomain(href);
    jQuery.ajax({
    url: "ping.php",
    cache: false,
    type: "POST",
    complete: function(XHR, status) {
if(XHR.status == 200)
{   jQuery("#lients li:eq("+index+")").css("background","transparent url(http://www.google.com/s2/favicons?domain="+url_domain+") no-repeat scroll 0 5px");
  }
else {
jQuery("#lients li:eq("+index+")").css("background","transparent url(style/images/favicons.gif) no-repeat scroll 0 5px");

}
    }
});
}
,
search: function()
{
jQuery("#lients li .edit").each(function(index){
if(jQuery.cookie("bex_favories_"+index)==null)
{
 var lient_favori = bex_favories[index].split("=>");
 var val_favori = lient_favori[0];
 var href_favori = lient_favori[1];
jQuery(this).html('<a href="'+href_favori+'" target="_blank">'+val_favori+'</a>');
favories.setFavicon(index,href_favori);
}
/* get from cookie :) */
else if(jQuery.cookie("bex_favories_"+index)!=null) {
 var lient_favori = jQuery.cookie("bex_favories_"+index).split(",");
var val_favori = lient_favori[0];
 var href_favori = lient_favori[1];
jQuery(this).html('<a href="'+href_favori+'" target="_blank">'+val_favori+'</a>');
favories.setFavicon(index,href_favori);
   }
});
}
,
filter: function(strInputCode){

 		/*
  			This line is optional, it replaces escaped brackets with real ones,
  			i.e. < is replaced with < and > is replaced with >
 		*/
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
return strTagStrippedText;   // Use the alert below if you want to show the input and the output text

   }
,
isURL : function (s) {
 	var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
 	return regexp.test(s);
},
isURL2 : function (b) {
 	var regexp2 = /https:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
 	return regexp2.test(b);
},

getDomain: function(url) {
    return url.match(/:\/\/(.[^/]+)/)[1];
}

,
/* ici vous pouvez appelé une action ajax, exemple: envoyer le lient(href) et le nom du site(val) */
save: function (index,val,href) {
val = favories.filter(val);
href = favories.filter(href);
if(val == '' || href == '')
{
jQuery("#error_link").html("Le lien ou le nom de site ne doit pas être vide.").slideDown(300);
return false;
}
else if((!favories.isURL(href))&&(!favories.isURL2(href)))
{
jQuery("#error_link").html("Le lien indiqué est incorrecte").slideDown(300);
return false;
}
else {
jQuery("#error_link").slideUp(300);
}



if((favories.isURL(href))||(favories.isURL2(href)))
{
jQuery(".edit:eq("+index+")").html('<a target="_blank" href="'+href+'">'+val+'</a>');
favories.setFavicon(index,href);
jQuery.cookie("bex_favories_"+index, [val, href],{expires: 1000000});
jQuery.facebox.close();
}

/*
jQuery.ajax({
  type: "POST",
  url : 'http://fibladi/ajax/save.php?lient='+href+'&nom='+val,
  data : data,
  success: function(data) {
jQuery(".edit:eq("+index+")").html('<a target="_blank" href="'+href+'">'+val+'</a>');
}
 });
 */
},
delete_l: function(index)
{
var resp = confirm("Etes-vous sûr de vouloir supprimer ce lien?");
if(resp)
{
 var lient_favori = bex_favories[index].split("=>");
 var val_favori = lient_favori[0];
 var href_favori = lient_favori[1];
jQuery(".edit:eq("+index+")").html('<a target="_blank" href="'+href_favori+'">'+val_favori+'</a>');
jQuery.cookie("bex_favories_"+index, null);
favories.setFavicon(index,href_favori);
}
else { return false; }
}
,
logged: function() {
var html = '<div id="options-set">'
         +'<span class="options"><a href="javascript:void(0)" id="delete"><img src="../images/trash.gif" alt="" /></a></span>'
         +'<span class="options"><a href="javascript:void(0)" id="edit"><img src="../images/field_input.gif" alt="" /></a></span>';
jQuery("#lients li .edit").each(function(index){
jQuery(this).after(html);
});
}
}

var actualite = {
init : function()
{
jQuery("#act_liste li").each(function(index){

 var lient_act = jQuery.cookie("bex_act_"+index).split("=>");
var val_act = decodeURIComponent(lient_act[0].replace(/\+/g, " "));;
 var href_act = lient_act[1];
jQuery(this).html('<a class="liste_actualite" href="'+href_act+'" target="_blank">'+val_act+'</a>');
});
}
}


/// on load page
jQuery(function() {
// mouse hover init
jQuery("#lients li").each(function(index){
jQuery(this).hover(function() {

jQuery("#lients li #options-set:eq("+index+")").show();
});
jQuery(this).mouseleave(function() {
jQuery("#lients li #options-set:eq("+index+")").hide();
});
});
// main function initialisation
favories.init();

});
