// Written by Waqqas Marij Usmani
// SDXPhoto Album v1.1 ( hhtp://www.y3kimaginations.co.in )
// Updated April 24th, 06'

var SDXCtrlSelect=false;
function SDX_PictureAlbum(S_Name,S_DivName)
{
 this.Name=S_Name;
 this.DivName=S_DivName;
 this.Properties=new Properties(this);
 this.Folders=new Folders(this);
 this.Fields=new Fields(this);
 this.Pictures=new Pictures(this);
 this.Events=new Events(this);
 this.Languages=new Languages(this);
 this.Initialize=Initialize;
 this.Fields.AddField("FOLDER","FOLDER",false)
 this.Fields.AddField("HEADER","HEADER",true)
 this.Fields.AddField("PATH","PATH",false)
 function Properties(OBJ_this)
 {
  this.MainObject=OBJ_this;
  this.CurrentPage=1;
  this.RowCount=3;
  this.ColumnCount=3;
  this.Width=null;
  this.Height=null;
  this.SearchValue="";
  this.CanEdit=false;
  this.CanDelete=false;
  this.CanAdd=false;
  this.CanSearch=true;
  this.ChangePage=ChangePage;
  function ChangePage(N_Page)
  {
   this.CurrentPage=N_Page;
	     this.MainObject.Pictures.Initialize();
  }
 }
 function Fields(OBJ_this)
 {
	    this.MainObject=OBJ_this;
  this.Field=Field;
  this.Fields=new Array();
  this.AddField=AddField;
	    this.PictureParameters=new Array();
  function Field(S_Name,S_Header,B_Visible)
  {
   this.Name=S_Name;
   this.Header=S_Header;
   this.Visible=B_Visible;
  }
  function AddField(S_Name,B_Visible)
  {
   // FOLDER_ID IS FIELD NAME
   this.Fields[S_Name]=new this.Field(S_Name,B_Visible);
   this.MainObject.Pictures.Picture.prototype[S_Name]=null;
   this.MainObject.Pictures.PictureParameters[this.MainObject.Pictures.PictureParameters.length]=S_Name
   return this.Fields[S_Name];
  }
 }
 function Folders(OBJ_this)
 {
	    this.MainObject=OBJ_this;
  this.SelectedFolders=new Array();
  this.Folder=Folder;
   this.Folders=new Array();
   this.AddFolder=AddFolder;
  this.Initialize=Initialize;
  	  function Folder(OBJ_this,N_ID,S_Name,OBJF_ParentFolder)
	 	  {
   this.MainObject=OBJ_this;
   this.ID=N_ID;
   this.HTML_ID="SDX_PA_Folder_" + this.ID
   this.Class_Name=(!OBJF_ParentFolder==false?"sdxRow2":"sdxRow1")
   this.ParentFolder=OBJF_ParentFolder;
   this.Name=S_Name;
   this.Width=this.MainObject.Properties.Width;
   this.Height=this.MainObject.Properties.Height;
   this.FirstSpace=(!OBJF_ParentFolder==false?OBJF_ParentFolder.FirstSpace + "<blockquote>":"")
   this.Selected=false;
   this.AddFolder=AddFolder
   this.SelectFolder=SelectFolder;
   this.DeselectFolder=DeselectFolder;
   this.Pictures=new Array();
   this.GET_HTML=GET_HTML;
   function AddFolder(S_Name)
   {
    return this.MainObject.Folders.AddFolder(S_Name,this);
   }
   function SelectFolder()
   {
    if(SDXCtrlSelect)
    {
     if(this.Selected)
     {
      this.DeselectFolder();
      return;
     }
     this.MainObject.Folders.SelectedFolders[this.MainObject.Folders.SelectedFolders.length]=this
     document.getElementById(this.HTML_ID).className="SelectedRow"
     this.Selected=true;
     this.MainObject.Pictures.Initialize();
    }
    else
    {
     for(var nX=this.MainObject.Folders.SelectedFolders.length-1;nX>-1;nX--)
        this.MainObject.Folders.SelectedFolders[nX].DeselectFolder();
     this.MainObject.Folders.SelectedFolders=new Array();
     this.MainObject.Folders.SelectedFolders[this.MainObject.Folders.SelectedFolders.length]=this
     document.getElementById(this.HTML_ID).className="SelectedRow"
     this.Selected=true;
     this.MainObject.Pictures.Initialize();
    }
   }
   function DeselectFolder()
   {
    var aDueArray=new Array()
    for(var nX in this.MainObject.Folders.SelectedFolders)
       if(this.MainObject.Folders.SelectedFolders[nX]!=this)
         aDueArray[aDueArray.length]=this.MainObject.Folders.SelectedFolders[nX];
       else
       {
         this.Selected=false;
         document.getElementById(this.HTML_ID).className=this.Class_Name
       }
       this.MainObject.Folders.SelectedFolders=aDueArray
   }
   function GET_HTML()
   {
    return "<tr>" +
               "<td class='" + this.Class_Name + "' id=\"SDX_PA_Folder_" + this.ID + "\"><a href='#' onclick=\"" + this.MainObject.Name + ".Folders.Folders[" + this.ID + "].SelectFolder()\">" + this.FirstSpace + this.Name + "</a></td>" +
               "</tr>"
   }
  }
  function Initialize()
  {
	     var sTable="<table bordercolor='#9999cc' cellspacing=0 cellpadding=3 width='100%' border=1>"
	     for(var nX in this.Folders)
      sTable+=this.Folders[nX].GET_HTML()
       sTable+="</table>"
   document.getElementById(this.MainObject.Name + "_L_HTML_Folder").innerHTML=sTable;
  }
  function AddFolder(S_Name,OBJF_ParentFolder)
  {
   // FOLDER_ID IS ARRAY LENGTH
   this.Folders[this.Folders.length]=new Folder(this.MainObject,this.Folders.length,S_Name,(!OBJF_ParentFolder==false?OBJF_ParentFolder:null));
   return this.Folders[this.Folders.length-1];
  }
 }
 function Pictures(OBJ_this)
 {
	    this.MainObject=OBJ_this;
  this.Picture=Picture;
  this.Pictures=new Array();
  this.AddPicture=AddPicture;
  this.PictureParameters=new Array();
  this.Initialize=Initialize;
  function Picture(OBJ_this)
  {
   this.MainObject=OBJ_this;
   this.ID=this.MainObject.Pictures.length;
   this.Active=true;
       this.Field=Field;
       this.GET_HTML=GET_HTML;
   for(var nX=0;nX<Picture.arguments.length;nX++)
     if(nX>0)
        this[this.MainObject.PictureParameters[nX-1]]=Picture.arguments[nX];
       function Field(N_No)
       {
        return this[this.MainObject.PictureParameters[N_No]]
       }
       function GET_HTML(nWidth,nHeight)
       {
        var sExtraTable=""
        for(var nX=3;nX<this.MainObject.MainObject.Pictures.PictureParameters.length;nX++)
         sExtraTable+="<tr>" +
	      	         "<td class='sdxRow2'><b>" + this.MainObject.MainObject.Pictures.PictureParameters[nX] + ":</b></td>" +
	      	         "<td class='sdxRow2'>" + this[this.MainObject.MainObject.Pictures.PictureParameters[nX]] + "</td>" +
	      	         "</tr>"

        return  "<table width='100%' height='100%'>" +
                "<tr>" +
                (this.MainObject.MainObject.Properties.CanDelete?"<td class='sdxRow1' width=2 height='10'><input type='checkbox' name='H_SelectedPictures' value='" + Math.round(this["ID"]) + "'></td>":"") +
                "<td class='sdxRow1' width='100%' colspan=" + (this.MainObject.MainObject.Properties.CanDelete?"1":"2") + "><b>" + this["HEADER"] + "</b></td>" +
	      	   "</tr>" +
	      	   "<tr>" +
	      	   "<td class='sdxRow2' colspan=2 align=center><a href='" + this["PATH"] + "' target='_blank'><img src='" + this["PATH"] + "'" + (nWidth!=null?" width='" + nWidth + "'":"") + (nHeight!=null?" Height='" + nHeight + "'":"") + "></a></td>" +
	      	   "</tr>" +
	      	   sExtraTable +
	      	   "</table>"
       }
  }
  function AddPicture()
  {
   var OBJ_Picture=new Picture(this);
   AddPicture.arguments[0].Pictures[AddPicture.arguments[0].Pictures.length]=OBJ_Picture;
   for(var nX=0;nX<AddPicture.arguments.length;nX++)
           OBJ_Picture[this.PictureParameters[nX]]=AddPicture.arguments[nX];
   this.Pictures[this.Pictures.length]=OBJ_Picture
   return OBJ_Picture;
   }
  function Initialize()
  {
   if(!document.getElementById(this.MainObject.Name + "_SearchInput")==false)
      this.MainObject.Properties.SearchValue=document.getElementById(this.MainObject.Name + "_SearchInput").value
       var sSearchValue=this.MainObject.Properties.SearchValue.toLowerCase()
   nCurrentPage=this.MainObject.Properties.CurrentPage;
   nRow=this.MainObject.Properties.RowCount;
   nColumn=this.MainObject.Properties.ColumnCount;
   var aSelectedFolders=this.MainObject.Folders.SelectedFolders;
   var aPictures=new Array();
   for(var nX in aSelectedFolders)
  	 	  for(var nY in aSelectedFolders[nX].Pictures)
  	 	     if(this.MainObject.Properties.SearchValue=="")
  	 	        aPictures[aPictures.length]=aSelectedFolders[nX].Pictures[nY];
  	 	     else
  	 	     {
  	 	        if(aSelectedFolders[nX].Pictures[nY]["HEADER"].toLowerCase().indexOf(sSearchValue)>-1)
  	 	        {
  	 	         aPictures[aPictures.length]=aSelectedFolders[nX].Pictures[nY];
  	 	        }
  	 	     }

   var sTable="<table bordercolor='#9999cc' cellspacing=0 cellpadding=3 width='100%' border=1>"
   nPageCount=Math.ceil(aPictures.length/(nRow*nColumn))

   document.getElementById(this.MainObject.Name + "_L_Pictures_Pager").innerHTML="<table><tr><td class=\"sdxPageBy\" align=\"right\">" +
                    (nCurrentPage>1?"<span class=\"sdxPageByActive\" onclick=\"" + this.MainObject.Name + ".Properties.ChangePage(1)" + "\">&lt;&lt; |</span>":"<span class=\"sdxPageByPassive\">&lt;&lt; |</span>") +
                    (nCurrentPage>1?"<span class=\"sdxPageByActive\" onclick=\"" + this.MainObject.Name + ".Properties.ChangePage(" + (nCurrentPage-1) + ")" + "\"> " + this.MainObject.Languages.Previous + " </span>":"<span class=\"sdxPageByPassive\"> " + this.MainObject.Languages.Previous + " </span>") +
                    "      &nbsp;&nbsp;<input class=\"sdxInput\" id=\"sss\" size=\"2\" value=\"" + nCurrentPage + "\" />" +
                    "<span class=\"sdxPageByPassive\"> / " + nPageCount + "</span>&nbsp;&nbsp;" +
                    (nCurrentPage<nPageCount?"<span class=\"sdxPageByActive\" onclick=\"" + this.MainObject.Name + ".Properties.ChangePage(" + (nCurrentPage+1) + ")" + "\"> " + this.MainObject.Languages.Next + " </span>":"<span class=\"sdxPageByPassive\"> " + this.MainObject.Languages.Next + " </span>") +
                    (nCurrentPage<nPageCount?"<span class=\"sdxPageByActive\" onclick=\"" + this.MainObject.Name + ".Properties.ChangePage(" + nPageCount + ")" + "\"> |":"<span class=\"sdxPageByPassive\"> |") +
                    "      &gt;&gt;</span>" +
                    "     </td></tr></table>"

       for(var nX=(nCurrentPage-1)*(nRow*nColumn);(nX<(nCurrentPage)*(nRow*nColumn) && nX<aPictures.length);nX+=nColumn)
       {
        sTable+="<tr>"
        for(var nY=0;(nY<nColumn && (nX + nY)<aPictures.length);nY++)
        {
         var nRecordNo=(nX + nY)
         sTable+="<td valign=top height='100%'>" +
         aPictures[nRecordNo].GET_HTML(aPictures[nRecordNo]["FOLDER"].Width,aPictures[nRecordNo]["FOLDER"].Height) +
         "</td>"
        }
        if(nY!=nColumn)
          if(nX>((nCurrentPage-1)*(nRow*nColumn) + nColumn-1))
            sTable+="<td colspan='" + (nColumn-nY) + "'>&nbsp;</td>"
        sTable+="</tr>"
       }
       sTable+="</table>"
       document.getElementById(this.MainObject.Name + "_L_HTML_Pictures").innerHTML=sTable
  }
 }
 function Events(OBJ_this)
 {
      this.MainObject=OBJ_this;
      this.Initialized=false;
      this.OnKeyUp=OnKeyUp;
      this.OnKeyDown=OnKeyDown;
      this.Initialize=Initialize;
      function OnKeyUp()
      {
       SDXCtrlSelect=false;
      }
      function OnKeyDown(e)
      {
       var nKeyCode=(navigator.appName=="Microsoft Internet Explorer"?event.keyCode:e.keyCode)
       switch(nKeyCode)
       {
        case 17:
         SDXCtrlSelect=true;
         break;
       }
      }
      function Initialize()
      {
       if(this.Initialized)return;
       document.onkeyup=this.OnKeyUp
       document.onkeydown=this.OnKeyDown
       this.Initialized=true;
      }
 }
 function Languages(OBJ_this)
 {
      this.MainObject=OBJ_this;
      this.SearchTitle="Click here for search";
      this.FoldersTitle="Folders";
      this.PicturesTitle="Pictures";
      this.Previous="Previous";
      this.Next="Next";
     }
 function Initialize()
 {
  sTable="<table bordercolor='#9999cc' cellspacing=0 cellpadding=3 width='100%' border=1>" +

         (this.Properties.CanSearch==true?" <tr>" +
             "  <td class='sdxSearch' align='middle' colspan=2>" +
             "   <img title=\"" + this.Languages.SearchTitle + "\" style='CURSOR: pointer' src='sp_icoSearch.gif' align='absMiddle' onclick=\"" + this.Name + ".Pictures.Initialize()\" />&nbsp;" +
             "   <input class='sdxInput' id=\"" + this.Name + "_SearchInput\" value=\"" + this.Properties.SearchValue + "\" />" +
             "  </td>" +
             " </tr>":"") +

             " <tr>" +
             "  <td class='sdxPageBy'>" + this.Languages.FoldersTitle + "</td>" +
             "  <td class='sdxPageBy'>" +
             "   <table width='100%'>" +
             "    <tr>" +
             "     <td class='sdxPageBy'>" + this.Languages.PicturesTitle + "</td>" +
             "     <td nowrap='nowrap' align='right' id='" + this.Name + "_L_Pictures_Pager'></td>" +
             "    </tr>" +
             "   </table>" +
             "  </td>" +
             " </tr>" +
             " <tr>" +
             "  <td class='sdxRow2' valign='top' id='" + this.Name + "_L_HTML_Folder'></td>" +
             "  <td class='sdxRow2' id='" + this.Name + "_L_HTML_Pictures'></td>" +
             " </tr>" +
             "</table>"
  document.getElementById(this.DivName).innerHTML=sTable
  this.Folders.Initialize()
  this.Pictures.Initialize()
  this.Events.Initialize()
 }
}

