/*============================================================
Structure
==============================================================*/

/*=============================================================
  Class Page
===============================================================*/
    var Page = function(id, titre, url, proprietaire){
        
        this.id = id; 
        this.proprietaire =proprietaire; 
        this.titre =titre; 
        this.url =url; 
        this.categories =''; 
        this.share =''; 
        this.etat =''; 
        this.date =''; 
        this.kid ='yes'; 
        // Playlist ici est un tableau de string
        this.playlists ='1';
        this.pagelogin =''; 
        this.pagecode =''; 
        this.type =''; 
        this.tags =''; 
        this.langue =''; 
        this.score =''; 
        this.globalscore =''; 
        this.description =''; 
        this.externe ='No'; 
        //info 2 in database
        this.domaine =''; 
       
        this.toString = function()
        {
            return this.id + this.proprietaire + this.titre + this.url + this.categories + this.share + this.etat + this.date + this.kid + this.playlists + this.pagelogin + this.pagecode + this.type + this.tags + this.langue + this.score + this.globalscore;
        }
        
        
    }
    
    
/*=============================================================
  Class Playlist
===============================================================*/
    var Playlist = function(titre, pages, kid){
        
        this.titre =titre; 
        this.kid =kid; 
        this.pages = pages;
        this.owner = "users";
        this.variable=titre;
        
              this.toString = function()
        {
            return this.titre + this.kid;
        }
        
        
        /*====================================================
        | toHtml : Cette fonction retourne le code html qui affiche la playlist
        | Entree : 
        | Sortie : String 
          ===================================================*/
        this.toHtml = function()
        {
           var str="<td class='playlistFont1'><a href='javascript:parent.frames[0].mew_showP("+this.titre+")' class='playlistFont1'> <img id='"+ this.titre +"' src='../pic/playlist1-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>"+ this.titre +"</a></td>";
           return str;
        }
        
		 /*=======================================================
		setPExterne()
		Cette fonction configure les pages d'une playlist à externe
		=========================================================*/
		this.setPExterne = function()
		{
		    i=0;
		      for (i =0 ; (i<this.pages.length) ; i++)
		       {
		      		this.pages[i].externe="external";
		       }
		  }
        
    }

/*=============================================================
  Class Personne
===============================================================*/
    var Personne = function(login,sexe,kid){
     
        this.login =login; 
        this.kid =kid; 
        this.sexe =sexe; 
        this.amis =new Array(); 
        this.pages = new Array();
        this.ville =""; 
        this.pays =""; 
        this.connecte="No";
        this.tabPlaylist=new Array();
        
      /*=======================================================
      deletePage(pageid)
      Cette fonction supprime une page dans les pages du candidat
      =========================================================*/
      this.detelePage = function(numpage){
      	//alert(numpage);
      	//alert (this.pages[0].id);
        
      	result = new Array();
      	index=0;
      	for (y=0; y< this.pages.length; y++)
            {
            	//alert(this.pages[y].id);
            	if(this.pages[y].id != numpage)
            	{
            		result[index]=this.pages[y];
            		index++;
            	}
            }
        this.pages = result;   
      
    	}
    	
    	/*=======================================================
      deleteAmis(login)
      Cette fonction supprime un ami
      =========================================================*/
      this.deteleAmis = function(login){
      	
      	result = new Array();
      	index=0;
      	for (y=0; y< this.amis.length; y++)
            {
            	//alert(this.amis[y].login);
            	if(this.amis[y].login != login)
            	{
            		result[index]=this.amis[y];
            		index++;
            	}
            }
        this.amis = result;   
      
    	}
    	
    	/*=======================================================
      addAmis(login)
      Cette fonction ajoute un ami
      =========================================================*/
      this.addAmis = function(login){
      	
      	index=this.amis.length;
      	this.amis[index]= new Personne(login,'h','yes');
      	//Reste a ajouter le sexe et la majotité
    	}

    	/*=======================================================
      addPage(page)
      Cette fonction ajoute une page aux pages de l utilisateur
      =========================================================*/
      this.addPage = function(page){
      	
      	index=this.pages.length;
      	//alert(page.url);
    		this.pages[index]= page;
      }    	
        
      /*=======================================================
      givePlaylists(max, maxcol,taille)
      Cette fonction retourne au maximum max playlists de l'utilisateur dans un tableau à max col
      out: String
      =========================================================*/
      this.givePlaylists = function (max, maxcol,taille, full){
      
         src ="";
         i=0;
         method="one.mew_createAndShowP";
        complement="My links";
        
        // afficheur de la playlist des mail
        mailer="<td class='playlistFont1'><a href='javascript:one.mew_createAndShowMailP()' class='playlistFont1'> <img id='mailer' src='../pic/mail-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>My Mails Player</a></td>";
        
         // afficheur des playlists reçus
        inboxplaylist="<td class='playlistFont1'><a href='f_myRplaylists.php' class='playlistFont1'> <img id='mailer' src='../pic/playlist4-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>Inbox Playlist</a></td>";
        
        
        //On test si le candidat est l'utilisateur connecté
        theowner="one.mew_user";
        if ((this.login != one.mew_user.login) ){
        method="one.mew_createAndShowPFriends";
        //Dans le cas d'une playlist d'un membre de la communauté
        theowner="one.mew_friend";
        mailer="";
        inboxplaylist="";
        complement="All "+this.login+"'s public links";
        }
        
        if (full==1){
        //Public Playlist
        src +="<TABLE class=\"cadrePlaylist\" cellpadding=\"0\" cellspacing=\"0\" with=\""+taille+"px\"><TR><td class='playlistFont1' align='left'><a href='f_worldnews.php' class='playlistFont1'> <img id='worldnews' src='../pic/playlist2-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>World News</a></td>"+mailer+""+inboxplaylist+"<td class='playlistFont1' align='left'><a href='javascript:"+method+"(\"user_all\")' class='playlistFont1'> <img id='user_all' src='../pic/playlist1-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>"+complement+"</a></td> </TR></TABLE><TABLE cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><TR><td class='titre2'  style='_padding-top:10px;'>"+this.login+"'s playlists <input type='text' value='filter' onclick=\"if(this.value=\'filter\') this.value=''\" class='inputfilter' onkeyup=\""+theowner+".refreshPlaylistBox(this.value,\'"+method+"\');\" style='float:right;_margin-top:7px;'></td></TR><!--<TR><td align='right'><input type='text' value='filter' onclick=\"if(this.value=\'filter\') this.value=''\" class='inputfilter' onkeyup=\""+theowner+".refreshPlaylistBox(this.value,\'"+method+"\');\" ></td></TR>--></TABLE>";
        }
        
        //Recuperation de la grille de playlist
        src +="<div id=\"playlistbox\">"+this.grilleplaylist(method, max, maxcol, taille, full, "")+"</div>";
        
        return src;
      }
    
     /*=======================================================
      grilleplaylist(plstab, full, cle)
      Cette fonction retourne la liste des playlist de l'utilisateur contenant la clé cle
      =========================================================*/
      this.grilleplaylist = function (method, max, maxcol, taille,  full, cle){
      	
      	src ="<TABLE class=\"cadrePlaylist\" cellpadding=\"0\" cellspacing=\"0\" with=\""+taille+"px\"><TR>";
        
       //si le maximum est 3 alors on affichera en 2 eme position la playlist des mails parmis toutes les playlists.
       //(Obseleteid= 1) les parametres viennent de la function givePlaylists
       /*mailpls=0;
       if (max==3){
       mailpls=1;
       }
       */
       
       
       //Si la clé est fournis on construit un string des noms des playlists contenant cette clé
       keyplaylists= "";
       if (cle !="")
       keyplaylists=this.playlistofkey(cle);
       
          
       reali=0;
        
        for (i =0 ; (i < this.tabPlaylist.length) && (i<max) ; i++) 
        {  
        
      	  filtre=true;
          
        	if (this.tabPlaylist[i] !=""){
          
          //On recherche si les pages de la playlist fait parti des playlists contenant la clé
          if (cle !="")
          filtre=  ( this.tabPlaylist[i].toLowerCase().search(cle.toLowerCase())> -1) || (keyplaylists.search(this.tabPlaylist[i].toLowerCase())> -1);
          
          
          //On applique le filtre
          if (filtre)
          {	
          src +="<td class='playlistFont1'><a href='javascript:"+method+"(\""+this.tabPlaylist[i]+"\")' class='playlistFont1'> <img id='"+this.tabPlaylist[i]+"' src='../pic/playlist1-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>"+this.tabPlaylist[i]+"</a></td>";
          reali++;
        	}
        	
          }
          
          /*(Obseleteid= 1)     
          // Affichage de la playlist des mails
          if ((mailpls==1) && (i==1)){
           src +=mailer;
          reali++;
         }
           
           */ 
          if ( ((reali%maxcol) == 0) && (i>0) & (filtre)) 
          {
          src +="</TR><TR>";
          }
        }
        
        // Affichage du lien d'exploration de la communauté
        if ((reali<1) && (full!=1))
        {
        src+= "<td class='playlistFont1'><a href=\"addUrl.php\" class=\"playlistFont1\"> <img id=\"mew_peoplePlaylist\"src=\"../pic/UPfav-off.gif\" class =\"noborder\" onMouseOver=\"javascript:onPic(this)\" onMouseOut=\"javascript:offPic(this)\" /></a></td>";
        }
        
        
        if ( ((reali%maxcol) != 0) ) 
        {
         src +="</TR>";
        }
        src +="</TABLE>";
        
        return src;
      }
     
     /*=======================================================
      playlistofkey(cle)
      Cette fonction retourne dans un string le nom des playlists contenant la clé
      =========================================================*/
      this.playlistofkey = function (cle){
      	j=0;
      	strtab = "";
      	
      	for(j=0; (j< this.pages.length) ;j++ )
      	{
      		p = this.pages[j];
      		
      		
      		if ( (p.playlists.indexOf(cle.toLowerCase()) > -1 ) || ( (p.titre.toLowerCase().search(cle.toLowerCase()) > -1 )	||(p.url.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.tags.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.categories.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.type.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.date.search(cle.toLowerCase()) > -1 )||(p.pagecode.toLowerCase().search(cle.toLowerCase()) > -1 )) )
      		
      		strtab += p.playlists.toString().toLowerCase(); 	
      		
      		
      	}
      	
      	//alert("strtab= "+strtab);
      	
      	
      	return strtab;
      }
       
       /* ====================================================
			Cette fonction rafraichit l affichage des playlists
 			=======================================================*/
 			this.refreshPlaylistBox = function (cle,method){
  		 two.document.getElementById("playlistbox").innerHTML = this.grilleplaylist(method, mew_maxplaylistToShow, mew_colplaylistToShow, mew_sizeplaylistToShow, 1, cle);
   		}
      
     /*=======================================================
      createPlaylist(key)
      Cette fonction retourne la playlist du candidat à partir d'une clé recherchée dans les noms des playlists
      =========================================================*/
      this.createPlaylist = function (key){
        
        
        var p = new Playlist(key,new Array(),"Yes");
        var i = 0;
        var y = 0;
        var str="";
        
        if (this.login==undefined) 
        p.owner="users";
        else
        p.owner=this.login;
        
        if (key =="All "+this.login+" links")
        key =="user_all";	
        
        if (key =="user_all"){
          p.pages = this.pages;
          p.titre="All "+this.login+" links";
        } 
        else{
            for (y=0; y< this.pages.length; y++)
            {
              str = this.pages[y].playlists.toString().toLowerCase(); 
              if( (str.indexOf(key.toLowerCase(),0) >= 0) ){
                //alert(this.pages[y].playlists); 
                //alert(this.pages[y].titre); 
                p.pages[i]=this.pages[y];
                i++;
                }
             }
             }
        return p;
      }
     
     
     /*=======================================================
      searchCreatePlaylist(key)
      Cette fonction retourne la playlist du candidat à partir d'une clé
      =========================================================*/
      this.searchCreatePlaylist = function (cle){
        
        
        var pls = new Playlist(cle,new Array(),"Yes");
        var i = 0;
        var y = 0;
        var str="";
        
        if (this.login==undefined) 
        pls.owner="users";
        else
        pls.owner=this.login;
        
        
        if (cle ==""){
          pls.pages = this.pages;
          pls.titre="All "+this.login+" links";
        } 
        else{
        	   cle=cle.replace(/</g,"");
    				 cle=cle.replace(/>/g,"");  
        	   pls.titre=cle+" | "+this.login+" links";
        
            for (y=0; y< this.pages.length; y++)
            {
            	p=this.pages[y];
              str = p.playlists.toString().toLowerCase(); 
              if( (str.indexOf(cle.toLowerCase(),0) >= 0) || ( (p.titre.toLowerCase().search(cle.toLowerCase()) > -1 )	||(p.url.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.tags.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.categories.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.type.toLowerCase().search(cle.toLowerCase()) > -1 )||(p.date.search(cle.toLowerCase()) > -1 )||(p.pagecode.toLowerCase().search(cle.toLowerCase()) > -1 )) ){
                pls.pages[i]=p;
                i++;
                }
             }
             }
        return pls;
      }
     /*=======================================================
      createMailPlaylist()
      Cette fonction retourne la playlist du candidat contenant le pages de type mail
      =========================================================*/
      this.createMailPlaylist = function (){
        
        var p = new Playlist("My Mails player",new Array(),"Yes");
        var i = 0;
        var y = 0;
        str1="gmail";
        str2="mail.live";
        
            for (y=0; y< this.pages.length; y++)
            {
              if ((this.pages[y].type=="Mail") || (this.pages[y].url.indexOf(str1.toLowerCase(),0) >= 0) || (this.pages[y].url.indexOf(str2.toLowerCase(),0) >= 0)){
                p.pages[i]=this.pages[y];
                i++;
                }
             }
         
        return p;
      }


     /*=======================================================
      createStartPlaylist()
      Cette fonction retourne la playlist du candidat contenant les pages de démarrage
      =========================================================*/
      this.createStartPlaylist = function (){
        
        var p = new Playlist("My start playlist",new Array(),"Yes");
        var i = 0;
        var y = 0;
        
            for (y=0; y< this.pages.length; y++)
            {
              if (this.pages[y].domaine=="startplaylist"){
              	//alert (this.pages[y].domaine);
                p.pages[i]=this.pages[y];
                i++;
                }
             }
         
        return p;
      }

      
      /*=======================================================
      setDomaine(urlval, domaine)
      Cette fonction met a jour le domaine d une page du candidat
      =========================================================*/
      this.setDomaine = function (urlval, domaine){
        
        var y = 0;
        
            for (y=0; y< this.pages.length; y++)
            {
              if ((this.pages[y].url==urlval)){
              	//alert (domaine);
                this.pages[y].domaine= domaine;
             }
      }
    }
      
      /*=======================================================
      giveFriends(max, maxcol,taille, full)
      Cette fonction retourne au maximum max amis de l'utilisateur dans un tableau à max col
      =========================================================*/
      this.giveFriends = function (max, maxcol,taille, full, userr){
      
        var src ="";
        var i=0;
        var operation ="";
        
        
        if (full==1){
          //Entete des amis
          if ( this.login !=undefined)  
            src +="<TABLE cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><TR><td class='titre2' >"+this.login+"'s friends</td></TR></TABLE>";
          else 
          	{
            	src +="<form name=\"find\" id=\"findForm\" ><TABLE cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><TR><td class='titre2' align=\"right\" ><nobr><label for='key'>Search Mewob member</label> <input type=\"text\" size=\"30\"  name=\"key\" class=\"inputBarFind\" ></td></TR></TABLE>";
          
          	}
          
        }
        
        src +="<TABLE class=\"cadrePlaylist\" cellpadding=\"0\" cellspacing=\"0\" with=\""+taille+"px\"><TR>";
        for (i =0 ; (i<this.amis.length) && (i<max) ; i++) 
        {  
          if (this.amis[i].login !="") {
              //Ajout des opérations d'administration
              if (userr!=""){
                
                // On annule les opérations pour l utilisateur connecté.
                if (userr.login == this.amis[i].login){
                    operation = "<BR><img id='mewobmemb' src='../pic/star1-off.gif' class='noborder' title='Hello "+userr.login+" '>"; 
                }
                else   
                     if (userr.isfriend(this.amis[i])){
                      operation = "<div id=\'friendboxop"+this.amis[i].login+"\'><a href='#' onClick=\"javascript:confirmDeleteAjax(\'../include/webservices_start.php\',\'friendbox"+this.amis[i].login+"\',\'delfriend\', \'tsk=delfriend&log="+this.amis[i].login+"\',\'one.mew_user.deteleAmis(\\\'"+this.amis[i].login+"\\\');\',\'Delete ("+this.amis[i].login+")?\');\" class='playlistFont1' title='Delete "+this.amis[i].login+" from your relashionship'> <img id='mewobmemb' src='../pic/delete1-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'></a></div>"; 
                    }
                     else
                      operation = "<div id=\'friendboxop"+this.amis[i].login+"\'><a href='#' onClick=\"javascript:confirmDeleteAjax(\'../include/webservices_start.php\',\'friendboxop"+this.amis[i].login+"\',\'addfriend\', \'tsk=addfriend&log="+this.amis[i].login+"\',\'one.mew_user.addAmis(\\\'"+this.amis[i].login+"\\\');\',\'Confirm add ("+this.amis[i].login+") to your friends?\');\" class='playlistFont1' title='Add "+this.amis[i].login+" to your friends'> <img id='mewobmemb' src='../pic/add1-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'></a></div>"; 
              }
                
               }
                    
              if(this.amis[i].sexe=='f')
                src +="<td class='playlistFont1'><div id=\'friendbox"+this.amis[i].login+"\'><a href='f_friendHome.php?p="+this.amis[i].login+"' class='playlistFont1' title='See "+this.amis[i].login+" home page'> <img id='mewobmemb' src='../pic/mewob-girl-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>"+this.amis[i].login+"</a>"+operation+"</div></td>";
              else
                src +="<td class='playlistFont1'><div id=\'friendbox"+this.amis[i].login+"\'><a href='f_friendHome.php?p="+this.amis[i].login+"' class='playlistFont1' title='See "+this.amis[i].login+" home page'> <img id='mewobmemb' src='../pic/mewob-boy-off.gif' class='noborder' onmouseover='javascript:onPic(this)' onmouseout='javascript:offPic(this)'><br>"+this.amis[i].login+"</a>"+operation+"</div></td>";
          
          if ( (((i+1)%maxcol) == 0) && (i>0)) 
          {
          src +="</TR><TR>";
          }
        
        }
        
        // Affichage du lien d'exploration de la communauté
        if (i<1)
        {
        src+= "<td class='playlistFont1'><a href=\"f_community.php\" class=\"playlistFont1\"> <img id=\"mew_peoplePlaylist\"src=\"../pic/community1-off.gif\" class =\"noborder\" onMouseOver=\"javascript:onPic(this)\" onMouseOut=\"javascript:offPic(this)\" /></a></td>";
        }
        
        if  (((i%maxcol) != 0) ) 
        {
         src +="</TR>";
        }
        
        src +="</TABLE>";
        return src;
      } 
      
      /*=======================================================
      isfriend(per)
      Cette fonction test si per est un amis
      =========================================================*/
      this.isfriend = function (per){
        
        b=0;
        if (per.login == this.login)
        b=1; 
         
          for (y=0; (y< this.amis.length) && (b==0) ; y++)
              {
                
                if (this.amis[y].login == per.login){
                b=1;
                }  
              }
        
        
        return (b==1);
      }
      
}

/*=============================================================
  Class Preference
===============================================================*/
    var Preference = function(active, titre, picture, backcolor,backpicture, repeat, texte, textx, texty , textcolor, formstyle, formx, formy,mini,thesource ){
        
        this.active = active; 
        this.titre = titre; 
        this.picture = picture; 
        this.backcolor =backcolor; 
        this.backpicture =backpicture; 
        this.repeat =repeat; 
        this.texte =texte; 
        this.textx =textx; 
        this.texty =texty; 
        this.textcolor =textcolor; 
        this.formstyle =formstyle; 
        this.formx =formx; 
        this.formy =formy; 
 		this.mini =mini; 
 		this.thesource=thesource; 
 
       
        this.toString = function()
        {
            return "'"+this.picture +"', '"+ this.backcolor +"', '"+ this.backpicture +"', '"+ this.url +"', '"+ this.repeat +"', '"+ this.texte +"', '"+ this.textcolor +"', '"+ this.formstyle +"', '"+ this.formx +"', '"+ this.formy ;
        }
       
       //Cette fonction install la préférence sur le bureau 
        this.install = function()
        {
        	lecadre = document.getElementById("finderbox");
        	letexte = document.getElementById("mytextbox");
        	letableau = document.getElementById("searshboxV5");
        	lebody= document.getElementById("maincontent");  
            
            letableau.style.backgroundColor ="transparent";
            letexte.style.backgroundColor ="transparent";
            lecadre.style.backgroundColor ="transparent";
            lebody.style.backgroundColor ="transparent";
            
            
            //Installation des preferences
            //===============================================
          if(this.picture !="")
            letableau.style.backgroundImage = "url(\""+this.picture+"\")";
            
            
            
            if(this.backcolor !="")
            lebody.style.backgroundColor =this.backcolor;
           
          
            
            if(this.repeat !="")
            lebody.style.backgroundRepeat =this.repeat;
            
            if(this.backpicture !="")
            lebody.style.backgroundImage = "url(\""+this.backpicture+"\")";
            
            
              if(this.formy !="")
            lecadre.style.paddingTop = formy;
            
              if(this.formx !="")
              lecadre.style.paddingLeft = formx;
          
              if(this.textcolor !="")
             letexte.style.color = textcolor;
              
          
              if(this.texty !="")
             letexte.style.paddingTop = texty;
            
              if(this.textx !="")
              letexte.style.paddingLeft = textx;
             
              if(this.texte !="")
              letexte.innerHTML = this.texte;
              
          
         }
       
       
        
    }

