YAHOO.namespace("mzag.PhotoBox");

YAHOO.mzag.PhotoBox=function(el, name, userConfig) 
{
  this.name=name;
  
  if (arguments.length > 0)
  {
    if(userConfig.effect)
      userConfig.effect.effect=eval(userConfig.effect.effect);
    YAHOO.mzag.PhotoBox.superclass.constructor.call(this, el, userConfig);
  }
  YAHOO.mzag.PhotoBox.manager.register(this);
}

YAHOO.extend(YAHOO.mzag.PhotoBox, YAHOO.widget.Panel);

YAHOO.mzag.PhotoBox.manager=new YAHOO.widget.OverlayManager();
YAHOO.mzag.PhotoBox.prototype.contents=new Array();
YAHOO.mzag.PhotoBox.prototype.linkNodes=new Array();
YAHOO.mzag.PhotoBox.prototype.titles=new Array();
YAHOO.mzag.PhotoBox.prototype.contentElements=new Array();
YAHOO.mzag.PhotoBox.prototype.name='photobox';
YAHOO.mzag.PhotoBox.prototype.currentContent=0;
YAHOO.mzag.PhotoBox.prototype.animation={
  remove:{attributes:{opacity:{to:0}},duration:0.25,easing:YAHOO.util.Easing.easeNone},
  add:{attributes:{opacity:{to:1}},duration:0.25,easing:YAHOO.util.Easing.easeNone}
}
YAHOO.mzag.PhotoBox.prototype.context=null;
YAHOO.mzag.PhotoBox.prototype.contentLoaded=false;
YAHOO.mzag.PhotoBox.prototype.contentAnimated=false;
YAHOO.mzag.PhotoBox.prototype.init=function(el,  userConfig, keepOldContents) 
{
  YAHOO.mzag.PhotoBox.superclass.init.call(this, el);
  this.beforeInitEvent.fire(YAHOO.mzag.PhotoBox);
  if(userConfig)
  {
    this.cfg.applyConfig(userConfig, true);
    YAHOO.util.Dom.addClass(this.innerElement, userConfig.cssClass);
  }
  
  this.hideEvent.subscribe(function(){this.context.innerHTML='';}, this, true);
  this.render();
  this.context=$($(el).id+'_context');
  this.renderLinks(this.name, keepOldContents);
  this.findThumbnailsForContents();
  YAHOO.mzag.ajax.NodeReplacement.subscribe(function(event, finished){if(finished[0]) this.ajaxReload();}, this, true);
}; 
YAHOO.mzag.PhotoBox.prototype.ajaxReload=function()
{
  this.destroy();
  YAHOO.mzag.PhotoBox.load();
}
YAHOO.mzag.PhotoBox.prototype.destroy=function()
{
  YAHOO.mzag.PhotoBox.superclass.destroy.call(this);
  for(var i=0;i<this.linkNodes.length;i++)
  {
    this.linkNodes[i].href=this.contents[i];
    YAHOO.util.Event.removeListener(this.linkNodes[i],'click',function(event,index){this.show(index);return false});
  }
  this.hideEvent.unsubscribeAll();
  if(this.element)
    this.element.parentNode.removeChild(this.element);
  YAHOO.mzag.PhotoBox.manager.remove(this);
  
}
YAHOO.mzag.PhotoBox.prototype.checkLinks=function()
{
  if(this.currentContent==0 && this.contents.length > 1)
  {
    $(this.id+'-backlink').style.display='none';
    $(this.id+'-nextlink').style.display='inline';
  }
  else if(this.currentContent==0)
  {
    $(this.id+'-backlink').style.display='none';
    $(this.id+'-nextlink').style.display='none';
  }
  else if(this.currentContent+1==this.contents.length)
  {
    $(this.id+'-backlink').style.display='inline';
    $(this.id+'-nextlink').style.display='none';
  }
  else{
    $(this.id+'-backlink').style.display='inline';
    $(this.id+'-nextlink').style.display='inline';
  }
}
YAHOO.mzag.PhotoBox.prototype.show=function(contentIndex)
{
  this.currentContent=contentIndex;
  // get new content
  var newContent=this.getContent(contentIndex);
  
  if(!newContent)
    return;
  // build Animations
  removeAnim=new YAHOO.util.Anim(
    this.context,
    this.animation.remove.attributes,
    this.animation.remove.duration,
    this.animation.remove.easing
  );
  addAnim=new YAHOO.util.Anim(
    this.context,
    this.animation.add.attributes,
    this.animation.add.duration,
    this.animation.add.easing
  );
  addAnim.onTween.subscribe(
    function()
    {
      if(this.cfg.getProperty('fixedcenter')) this.center();
      var region=YAHOO.util.Dom.getRegion(this.context);
      var contextHeight=region.bottom-region.top;
      var diff=YAHOO.util.Dom.getViewportHeight()-10-contextHeight;
      if(diff<0)
      {
        this.context.style.height=diff+contextHeight;
        this.center();
      }
    },
    this,true
  );
  if(!this.context.firstChild)
    removeAnim.duration=0;
  removeAnim.onComplete.subscribe(
    function()
    {
      this.replaceContent(this.context.firstChild,newContent);
      this.contentAnimated=true;
      if(this.cfg.getProperty('fixedcenter')) 
        this.center();
      if(this.contentLoaded)
      {
        addAnim.animate();
      }
      this.checkLinks();
    },
    this,true
  );

  this.contentLoaded=false;
  this.contentAnimated=false;

  this.contentLoaded=true;
  YAHOO.mzag.PhotoBox.superclass.show.call(this);
  removeAnim.animate();
}
YAHOO.mzag.PhotoBox.prototype.replaceContent=function(oldContent,newContent)
{
  if(oldContent)
    this.context.replaceChild(newContent,oldContent);
  else
    this.context.appendChild(newContent);
  images=this.context.getElementsByTagName('IMG');
  for(var i=0;i<images.length;i++)
    YAHOO.util.Event.addListener(images[i],'load',this.center,this,true);
  $(this.id+'_title').innerHTML='';
  if(this.titles[this.currentContent])
    $(this.id+'_title').innerHTML=this.titles[this.currentContent];
}

YAHOO.mzag.PhotoBox.prototype.back=function()
{
  if(this.currentContent>0)
    this.show(this.currentContent-1);
}
YAHOO.mzag.PhotoBox.prototype.next=function()
{
  if(this.currentContent<(this.contents.length-1))
    this.show(this.currentContent+1);
}

YAHOO.mzag.PhotoBox.prototype.copyNode=function(node)
{
  node=node.cloneNode(1);
  
  node.removeAttribute('id');
  var subnodes=node.getElementsByTagName('*');
  for(i=0;i<subnodes.length;i++)
  {
    subnodes[i].removeAttribute('id');
  }
  return node;
}

YAHOO.mzag.PhotoBox.prototype.getContent=function(index)
{
  if(this.contentElements[index])
    return this.contentElements[index];
  if(this.contents[index].indexOf('#')>=0)
  {
    this.contentElements[index]=$(this.contents[index].substr(this.contents[index].indexOf('#')+1));
    YAHOO.util.Dom.removeClass(this.contentElements[index],'mzag_photobox_content_hide');
  }
  else
  {
    switch (this.contents[index].substr(this.contents[index].lastIndexOf('.')+1).toLowerCase())
    {
      case 'jpg':
      case 'gif':
      case 'png':
      case 'jpeg':
        this.contentElements[index]=document.createElement('img');
        this.contentElements[index].src=this.contents[index];
      break;
    }
  }
  return this.contentElements[index];
}

YAHOO.mzag.PhotoBox.prototype.renderLinks=function(setName_, keepOldContents_)
{
  if(!keepOldContents_)
  {
    this.contents=[];
    this.titles=[];
    this.contentElements=[];
    this.linkNodes=[];
  }
  var nodes=YAHOO.util.Dom.getElementsBy(function(node){return (node.rel && node.rel==setName_ && node.href!='#');});
  for (var i=0; i<nodes.length; i++) 
  {
    var node=nodes[i];
    this.addTitle(node.title);
    if(node.href.indexOf('#')>=0)
    {
      var ident=node.href.substr(node.href.indexOf('#'));
      this.addContent(ident);
      YAHOO.util.Dom.addClass(node.href.substr(node.href.indexOf('#')+1),'mzag_photobox_content_hide');
    }
    else
      this.addContent(node.href);
    node.href='#';
    this.linkNodes.push(node);
    YAHOO.util.Event.addListener(node,'click',function(event,index){this.show(index);return false},this.contents.length-1,this);
  }
}

YAHOO.mzag.PhotoBox.prototype.findThumbnailsForContents=function()
{
  var nodes,url;
  for (var i=0;i<this.contents.length;i++) 
  {
    url=this.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++)
    {
      var listeners=YAHOO.util.Event.getListeners(nodes[j],'click');
      if(!listeners)
      {
        nodes[j].style.cursor='pointer';
        YAHOO.util.Event.addListener(nodes[j],'click',function(event,index){this.show(index);return false},i,this);
      }
    }
  }
}


YAHOO.mzag.PhotoBox.prototype.addContent=function(contentIdent)                 {this.contents.push(contentIdent);}
YAHOO.mzag.PhotoBox.prototype.contentExists=function(contentIdent)
{
  for(var i=0;i<this.contents.length;i++)
  {
    if(this.contents[i]==contentIdent)
      return true;
  }
  return false;
}

YAHOO.mzag.PhotoBox.prototype.addTitle=function(title)
{
  this.titles.push(title);
}
YAHOO.mzag.PhotoBox.loadPhotobox=new YAHOO.util.CustomEvent('loadPhotobox');
YAHOO.mzag.PhotoBox.load=function()
{
  YAHOO.mzag.PhotoBox.loadPhotobox.fire();
  var found=YAHOO.mzag.PhotoBox.manager.find('photobox');
  if(!found)
  {
    var div=document.createElement("div");
    div.id="photobox";
    var backlink='<a id="photobox-backlink" class="back" href="javascript:YAHOO.mzag.PhotoBox.manager.find(\'photobox\').back()"></a>';
    var nextlink='<a id="photobox-nextlink" class="next" href="javascript:YAHOO.mzag.PhotoBox.manager.find(\'photobox\').next()"></a>';
    div.innerHTML="<div class=\"hd\"><div class=\"lt\"></div><span id=\"photobox_title\"></span><div class=\"rt\"></div></div><div class=\"bd\" id=\"photobox_context\"></div><div class=\"ft\">"+backlink+nextlink+"</div></div>";
    document.body.appendChild(div);
    new YAHOO.mzag.PhotoBox('photobox','photobox',
    {
      effect:{effect:'YAHOO.widget.ContainerEffect.FADE',duration:0.45},
      width:'500px',
      fixedcenter:true,
      cssClass:'mzag_gui_photobox',
      visible:false,
      modal:true,
      draggable:false
    });
  } 
}
YAHOO.mzag.util.onDomReady.subscribe(YAHOO.mzag.PhotoBox.load);
