var BaseObject = Class.create();
BaseObject.prototype=
{
  initialize : function()
  {
  },  
  toDebugString : function()
  {
  	var returnStr = new String();
  	for (var field in this)			
  	  if (typeof(this[field]) != "function")
  		returnStr += field +":" +this[field] +"\r\n";
  	return returnStr;
  }
}

//For the album.jsp page.
var PhotoDataRequest = Class.create();
PhotoDataRequest.prototype = Object.extend(new BaseObject(),
{
  initialize : function(dataGridName, webContext, albumShortName, orderByCode, hasChapters)
  {
    this.dataGridName = dataGridName;
    this.webContext = webContext;
    this.albumShortName = albumShortName;
    this.hasChapters = hasChapters;
    this.chapterIndex = 1;
    this.orderByCode = orderByCode;
          
    this.getPhotos();
  },
  changeOrderByCode : function()
  {      
    this.orderByCode = $F('orderBy');
    this.getPhotos();
    
    postChangeOrderByCode(this.orderByCode);
  },
  showAsList : function(showList)
  {
    //get the photos only if you are switching modes.
    if (showAsList != showList)
    {
      showAsList = showList;
      this.getPhotos();
      
      postChangeShowAsList(showAsList);
    }
  },
  gotoChapter : function (chapterIndex)
  {
    this.chapterIndex = chapterIndex;
    this.getPhotos();
    
    //On changing the chapter, update the slideshow data
    var ssAjaxCall = getAjaxRequest();
    var ssUrl = this.webContext +"/photo/album_slideshow.jsp?albumShortName=" +this.albumShortName +"&chapterIndex=" +this.chapterIndex;
    ssAjaxCall.open("GET", ssUrl, true);
    ssAjaxCall.onreadystatechange = function()
    {
      if (ssAjaxCall.readyState == 4)
      { 
        var ssResponse = ssAjaxCall.responseText;    
        $('photoList').update(ssResponse);
        var slideShowJS = ssResponse.substring(ssResponse.indexOf("<script>") +9, ssResponse.length -9);
        eval(slideShowJS);      
      }
    };
    ssAjaxCall.send(null);
    
  },
  getPhotos : function()
  {      
    $(this.dataGridName).innerHTML = "<center><br/><br/><br/><br/>Please wait, loading content...<br/><br/><br/><img src=\"" +this.webContext +"/images/icon/spinner.gif\"></center>";
    ajaxCall = getAjaxRequest();     
    var url;
    if (this.hasChapters)
      url = this.webContext +"/photo/album_renderer.jsp?showAsList=" +showAsList +"&orderByCode=" +this.orderByCode +"&chapterIndex=" +this.chapterIndex +"&albumShortName=" +this.albumShortName;
    else
      url = this.webContext +"/photo/album_renderer.jsp?showAsList=" +showAsList +"&orderByCode=" +this.orderByCode +"&albumShortName=" +this.albumShortName;
    ajaxCall.open("GET", url, true);
    ajaxCall.onreadystatechange = this.renderPhotos;
    ajaxCall.send(null);
  },
  renderPhotos : function()
  {
    if (ajaxCall.readyState == 4)
    { 
      var response = ajaxCall.responseText;    
      //Since "this" is only this function and not the object,
      //Using global var of dataGridName.
      $(dataGridName).innerHTML = response;
      $(dataGridName).scrollTop = 0;  
      loadedPhotos(this.hasChapters);                
    }
  }
});

//--------------For Albums Pages-----------------//
var dataGridInUse;
var AlbumDataRequest = Class.create();
AlbumDataRequest.prototype = Object.extend(new BaseObject(),
{
  initialize : function(dataGridName, webContext, pageIndex, ajaxJspPage, isSimpleMode)
  {    
    this.dataGridName = dataGridName;
    this.webContext = webContext;
    this.pageIndex = pageIndex;
    this.query = "";
    this.xml = "";
    this.isSimpleSearchMode = true;
    this.reQuery = false;
    this.ajaxJspPage = ajaxJspPage;    
    if (isSimpleMode == true || isSimpleMode == false)
      this.isSimpleSearchMode = isSimpleMode;    
    this.getAlbums();    
  },
  gotoNextPage : function()
  {
    this.pageIndex++;
    this.getAlbums();
    postGotoPage(this.pageIndex);
  },
  gotoPage : function(pageIndex)
  {
    this.pageIndex = pageIndex;
    this.reQuery = true;
    this.getAlbums();
    
    postGotoPage(this.pageIndex);
  },
  getAlbums : function()
  {   
    $(this.dataGridName).innerHTML = "<center><br/><br/><br/><br/>Please wait, loading content...<br/><br/><br/><img src=\"" +this.webContext +"/images/icon/spinner.gif\"></center>";
    ajaxCall = getAjaxRequest();
    var url;
    var params;
    if (this.isSimpleSearchMode)
    {          
      url= this.webContext +"/photo/" +this.ajaxJspPage;
      params =  "showAsList=" +showAsList  +"&orderByCode=" +orderByCode +"&" +this.dataGridName +"_pageIndex=" +this.pageIndex +"&pageSize=" +pageSize +"&portalId=" +portalId +"&" +this.dataGridName +"_query=" +this.query +"&simpleSearchString=" +simpleSearchString +"&dataGridName=" +this.dataGridName;
    }    
    else
    {
      url= this.webContext +"/photo/" +this.ajaxJspPage;
      if ( ! this.reQuery)
        params =  "showAsList=" +showAsList  +"&orderByCode=" +orderByCode +"&" +this.dataGridName +"_pageIndex=" +this.pageIndex +"&pageSize=" +pageSize +"&portalId=" +portalId +"&" +this.dataGridName +"_query=" +this.query +"&" +this.dataGridName +"_xml=" +this.xml +"&advSearchString=1" +advSearchString +"&dataGridName=" +this.dataGridName;
      else
        params =  "showAsList=" +showAsList  +"&orderByCode=" +orderByCode +"&" +this.dataGridName +"_pageIndex=" +this.pageIndex +"&pageSize=" +pageSize +"&portalId=" +portalId +"&" +this.dataGridName +"_query=" +this.query +"&" +this.dataGridName +"_xml=" +this.xml +"&advSearchString=0" +advSearchString +"&dataGridName=" +this.dataGridName;
    }
    //this method needs to be created by each page that uses this object.
    //This is to modify the url before sending it to the ajax request.
    params = customizeUrl(params);    
    
    dataGridInUse = this;
    ajaxCall.open("POST", url, true);
    ajaxCall.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxCall.setRequestHeader("Content-length", params.length);
    ajaxCall.setRequestHeader("Connection", "close");
    ajaxCall.onreadystatechange = this.renderAlbums;
    ajaxCall.send(params);            
  },
  renderAlbums : function()
  {
    if (ajaxCall.readyState == 4)
    {
      var response = ajaxCall.responseText;
      $(dataGridInUse.dataGridName).innerHTML = response;
      $(dataGridInUse.dataGridName).scrollTop = 0;              
      dataGridInUse.updatePageInfo();
      dataGridInUse = null;
      
      if (isBrowserIE6())
      {
        fixPNG();
      }
      loadedAlbums();
      
    }
  },
  updatePageInfo : function()
  {    
    totalPages = $F(this.dataGridName +'_totalPageSize');
    totalAlbumCount = $F(this.dataGridName +'_totalAlbumCount');
    this.query = $F(this.dataGridName +'_query');
    this.xml = $F(this.dataGridName +'_xml');
    var html = "";
    if (totalPages != 1)
    {
      if (this.pageIndex != 1)
        html += "<a href=\"javascript:" +this.dataGridName +".gotoPage(" +(this.pageIndex -1) +")\">&laquo;&nbsp;prev</a>&nbsp;\r\n";
      else
        html += "<span style=\"color:gray\">&laquo;&nbsp;prev</span>&nbsp;\r\n";
      
      for (var i=1; i<=totalPages; i++)
      {
        if (i == this.pageIndex)
          html += "<b>" +i +"</b>&nbsp\r\n";
        else
          html += "<a href=\"javascript:" +this.dataGridName +".gotoPage(" +i +")\">" +i +"</a>&nbsp;\r\n";
      }       
      if (totalAlbumCount == 0)
        html += "<span style=\"color:gray\">next&nbsp;&raquo;</span>\r\n";
      else if (this.pageIndex != totalPages && totalPages != 1)
        html += "<a href=\"javascript:"+this.dataGridName +".gotoPage(" +(this.pageIndex +1) +")\">next&nbsp;&raquo;</a>\r\n";                    
      else if (totalPages != 1)
        html += "<span style=\"color:gray\">next&nbsp;&raquo;</span>\r\n";  
    }    
    $(this.dataGridName +"_pages").innerHTML = html;
    
    //the logic for summary and more
    var startIndex = (this.pageIndex * pageSize) - pageSize +1;
    var endIndex = this.pageIndex * pageSize;
    if (endIndex > totalAlbumCount)
     endIndex = totalAlbumCount;
    
    if (this.pageIndex == totalPages)
    {
      if (startIndex == totalAlbumCount)            
        $(this.dataGridName +'_summary').innerHTML = "Showing Album " +endIndex +" of " +totalAlbumCount;
      else
        $(this.dataGridName +'_summary').innerHTML = "Showing Albums " +startIndex +"-" +endIndex +" of " +totalAlbumCount;          
    }
    else
    {
      if (totalAlbumCount == 0)
        $(this.dataGridName +'_summary').innerHTML = "There are no Albums";
      else 
        $(this.dataGridName +'_summary').innerHTML = "Showing Albums " +startIndex +"-" +endIndex +" of " +totalAlbumCount;
      var moreLink = "<br/><br/><center><b><a href=\"javascript:" +this.dataGridName +".gotoPage(" +(this.pageIndex +1) +")\"><nokeyword>more&nbsp;Albums...</nokeyword></a></b></center><br/><br/>";
      try
      {
        $(this.dataGridName +'_more').innerHTML = moreLink;
      }
      catch(err)
      {
      }
    }  
  }
});

