YAHOO.namespace('dez.gallery');

//==============================================================================
// class YAHOO.dez.gallery.manager
//==============================================================================
YAHOO.dez.gallery.manager={};
YAHOO.dez.gallery.manager.add=function(name_, value_)                           {YAHOO.dez.gallery.manager[name_]=value_;}
YAHOO.dez.gallery.manager.get=function(name_)                                   {return YAHOO.dez.gallery.manager[name_];}
//------------------------------------------------------------------------------

//==============================================================================
// class YAHOO.dez.gallery.Gallery
//==============================================================================
YAHOO.dez.gallery.Gallery=function(name_, carouselElementId_, carouselItemWidth_, carouselItemHeight_, visibleItems_, isCircular_)
{
  YAHOO.dez.gallery.manager.add(name_, this);

  // properties  
  this.carouselElementId=carouselElementId_;
  this.carouselItemWidth=carouselItemWidth_;
  this.carouselItemHeight=carouselItemHeight_;  
  
  this.spotlightEnabled=false;      // display large view of current carousel item?
  this.spotlightElementId;          // id of div to display spotlight
  this.spotlightItemWidth;
  this.spotlightItemHeight;
  this.spotlightImageUris;
  
  this.paginationEnabled=false;     // display prev/next control to go to next carousel page?
  this.paginationPrevElementId;     // id of div to display prev-control
  this.paginationPrevElementLabel;  // '< prev';
  this.paginationNextElementId;     // id of div to display next-control
  this.paginationNextElementLabel;  // 'next >';
  this.paginationScrollItemOnly;    // determines if the paginator scroll one item or whole page
  
  this.photoboxEnabled=false;       // enable photobox for detail view?
  this.photoboxImageElementId;      // id of photobox-div (the one that is hidden by default)
  this.photoboxImageUris;           // array of all possible big images-uris to be displayed in photobox
  this.photoboxLinkElementId;       // id of link-div which will be clicked to display photobox 
                                    // (if not set, but spotlight is enabled, spotlight will be the link)
  
  this.autoplayEnabled=false;       // autostart animation (normaly circulating carousel)
  this.autoplayIntervall;           // intervall in ms to switch automaticaly to next item
  
  this.name=name_;
  this.visibleItems=visibleItems_;  // number of visible items at a time 
  this.isCircular=isCircular_;      // circulating or ending on very prev/next? 
  
  this.customData={};               // field to be used for custom data needed for custom callbacks
  this.customCallbacks={};          // custom callbacks
  
  this.currentItemIndex=0;
  //----------------------------------------------------------------------------
  
  // private
  this.carousel;
  this.prevPaginator
  this.nextPaginator
  //----------------------------------------------------------------------------
  
  // initalization
  this.init=function()
  {
    // init carousel carousel
    this.carousel=new YAHOO.widget.Carousel(this.carouselElementId, {
      numVisible:this.visibleItems,
      isCircular: this.isCircular=='true'?true:false});
      
    if(this.autoplayEnabled)
      this.carousel.set("autoPlayInterval", this.autoplayIntervall);
    
    // render
    this.carousel.render();
    this.carousel.show();
    
    // start autoplay if enabled
    if(this.autoplayEnabled)
      this.carousel.startAutoPlay();
    
    // - change thumb-click into switching the spotlight instead of showing a photobox
    //   (the photobox will now be shown on-click on the photoboxLinkElement or the spotlight itself, see eventOnItemSelected)
    if(this.photoboxEnabled && this.spotlightEnabled)
    {
      var photobox=YAHOO.mzag.PhotoBox.manager.find(this.photoboxImageElementId);
      for (var i=0;i<photobox.contents.length;i++) 
      {
        url=photobox.contents[i];
        if(url.indexOf('#')>=0)
          continue;
        pattern=url.replace(/\.thumb_[0-9]+_[0-9]+_/,'\.thumb_[0-9]+_[0-9]+_');
        nodes=YAHOO.util.Dom.getElementsBy(function(node){return node.src.match(pattern);}, 'IMG');
        for(var j=0;j<nodes.length;j++)
          YAHOO.util.Event.removeListener(nodes[j],'click');
      }
    }  
    
    if(this.photoboxEnabled || this.spotlightEnabled)
    {
      // register change event and trigger it to select first image by default
      this.carousel.addListener("itemSelected", eventOnItemSelected, this);
      eventOnItemSelected(0, this);
    }
      
    // init paginator
    if(this.paginationEnabled)
    {
      // only go through items NOT through whole pages
      if(this.paginationScrollItemOnly)
      {
        // prev
        this.prevPaginator=document.createElement("a");
        this.prevPaginator.setAttribute("href", "#");        
        $(this.paginationPrevElementId).appendChild(this.prevPaginator);
        YAHOO.util.Event.addListener(this.prevPaginator, "click", changePrevItemRequest, this); 
        
        // next
        this.nextPaginator=document.createElement("a");
        this.nextPaginator.setAttribute("href", "#");        
        $(this.paginationNextElementId).appendChild(this.nextPaginator);
        YAHOO.util.Event.addListener(this.nextPaginator, "click", changeNextItemRequest, this);     
      }
      // page regularly
      else
      {
        var numItems=this.carousel.get("numItems");
        var numVisible=this.carousel.get("numVisible");
      
        // prev
        this.prevPaginator=new YAHOO.widget.Paginator({
          containers           : this.paginationPrevElementId,
          rowsPerPage          : 1,
          template             : "{PreviousPageLink} ",
          previousPageLinkLabel: this.paginationPrevElementLabel,
          totalRecords         : Math.ceil(numItems / numVisible)
        });
        this.prevPaginator.subscribe("changeRequest", changePrevRequest, this);
        this.prevPaginator.render();
        
        // next
        this.nextPaginator=new YAHOO.widget.Paginator({
          containers       : this.paginationNextElementId,
          rowsPerPage      : 1,
          template         : "{NextPageLink} ",
          nextPageLinkLabel: this.paginationNextElementLabel,
          totalRecords     : Math.ceil(numItems / numVisible)
        });
        this.nextPaginator.subscribe("changeRequest", changeNextRequest, this);
        this.nextPaginator.render();
        
        this.carousel.addListener("pageChange", eventPageChange, this);
      }
    }
  }
  //----------------------------------------------------------------------------
  
  // features
  this.enableSpotlight=function(elementId_, itemHeight_, itemWidth_, imageUris_)
  {
    this.spotlightEnabled=true;
    this.spotlightElementId=elementId_;
    this.spotlightItemWidth=itemWidth_;
    this.spotlightItemHeight=itemHeight_;
    this.spotlightImageUris=imageUris_;
  }
  this.enablePagination=function(prevElementId_, nextElementId_, prevLabel_, nextLabel_, paginationScrollItemOnly_)
  {
    this.paginationEnabled=true;
    this.paginationPrevElementId=prevElementId_;
    this.paginationNextElementId=nextElementId_;
    this.paginationPrevElementLabel=prevLabel_;
    this.paginationNextElementLabel=nextLabel_;
    this.paginationScrollItemOnly=paginationScrollItemOnly_
  }
  this.enablePhotobox=function(imageElementId_, imageUris_, linkElementId_)
  {
    this.photoboxEnabled=true;
    this.photoboxImageElementId=imageElementId_;
    this.photoboxImageUris=imageUris_;
    this.photoboxLinkElementId=linkElementId_;
  }
  this.enableAutoplay=function(intervall_)
  {
    this.autoplayEnabled=true;
    this.autoplayIntervall=intervall_;
  }
  //----------------------------------------------------------------------------
  
  // helpers
  function getImage(parent, ths) 
  {
    var el=parent.firstChild;
  
    while(el) 
    {
      if (el.nodeName.toUpperCase()=="IMG") 
      {
        return el.src.replace(ths.carouselItemWidth+"_"+ths.carouselItemHeight, ths.spotlightItemWidth+"_"+ths.spotlightItemHeight);
      }
      el=el.nextSibling;
    }  
    return "";
  }
  //----------------------------------------------------------------------------
  
  // events
  function eventOnItemSelected(index, ths)
  {
    ths.currentItemIndex=index;
  
    // item has the reference to the Carousel's item
    var item=ths.carousel.getElementForItem(index);
    
    if(item) 
    {
      // change spotlight image (big one)
      if(ths.spotlightEnabled)
      {
        var src=null;
        if(ths.spotlightImageUris && typeof(ths.spotlightImageUris[index])!='undefined')
          src=ths.spotlightImageUris[index];
        else
          src=getImage(item, ths);
        $(ths.spotlightElementId).innerHTML = "<img src=\""+src+"\">";
      }
      // change photobox link
      if(ths.photoboxEnabled)
      {
        // use some link-control
        if(ths.photoboxLinkElementId)
          $(ths.photoboxLinkElementId).href=ths.photoboxImageUris[index];
        // use the spotlight image as link
        else if(ths.spotlightEnabled)
          $(ths.spotlightElementId).innerHTML = "<a href=\""+ths.photoboxImageUris[index]+"\" rel=\"photobox\" target=\"_self\">"+$(ths.spotlightElementId).innerHTML+"</a>";                 
    
        YAHOO.mzag.PhotoBox.manager.find(ths.photoboxImageElementId).renderLinks('photobox', false); 
      }
    }    
  }
  function changePrevRequest(state, ths) 
  {
    ths.carousel.set("selectedItem", (state.page - 1) * ths.carousel.get("numVisible"));
    ths.prevPaginator.setState(state);
  }
  function changeNextRequest(state, ths) 
  {
    ths.carousel.set("selectedItem", (state.page - 1) * ths.carousel.get("numVisible"));
    ths.nextPaginator.setState(state);
  }
  function eventPageChange(page, ths) 
  {
    ths.prevPaginator.setPage(page + 1, true);
    ths.nextPaginator.setPage(page + 1, true);
  }
  function changeNextItemRequest(event, ths)
  {
    ths.carousel.selectNextItem();   
  }
  function changePrevItemRequest(event, ths)
  {
    ths.carousel.selectPreviousItem();
  }
  //----------------------------------------------------------------------------
  
  return this;
  
//------------------------------------------------------------------------------
}