/*
  cp_imager.js
  Version 1.1
  Copyright (C) 2006, Cinematica Producciones Ltda.
  Author: Andres Melguizo Velez
  Created: 20070329
  Modified: 20070330
*/

function TUI_Image(szFileName, iWidth, iHeight) {
  this.szFileName = szFileName;
  this.iWidth = iWidth;
  this.iHeight = iHeight;
  return(this);
}

function TUI_Imager(szTagName, szCodeName) {
  this.className = "TUI_Imager";
  this.szCodeName = szCodeName;
  this.aImages = null;
  this.szTagName = szTagName;
  this.iCurrentImage = 0;
  this.iNumImages = 0;
  this.szStyleName = "";
  this.szUserFunc = "";

  this.setImages = function(aImages) {
    this.aImages = aImages;
    this.iNumImages = this.aImages.length;
  }

  this.setStyle = function(szStyleName) {
    this.szStyleName = szStyleName;
  }

  this.moveFirst = function() {
    this.iCurrentImage = 0;
    this.fixNumImage();
  }

  this.moveLast = function() {
    this.iCurrentImage = this.iNumImages - 1;
    this.fixNumImage();
  }

  this.moveNext = function() {
    this.iCurrentImage++;
    this.fixNumImage();
  }

  this.movePrev = function() {
    this.iCurrentImage--;
    this.fixNumImage();
  }

  this.fixNumImage = function() {
    if(this.iCurrentImage < 0)
      this.iCurrentImage = 0;
    if(this.iCurrentImage > this.iNumImages - 1)
      this.iCurrentImage = this.iNumImages - 1;
  }

  this.setUserFunc = function(szUserFunc) {
    this.szUserFunc = szUserFunc;
  }

  this.Draw = function() {
    var s = "";

    var szClass = ((this.szStyleName != "") ? "class='" + this.szStyleName + "'" : "");
    var szLeftLink = "<a href='#' title='Anterior' onclick='" + this.szCodeName + ".movePrev();" + this.szCodeName + ".Draw(); return false;'>&nbsp;&nbsp;&lt;&lt;&nbsp;&nbsp;</a>";
    var szPosition = (this.iCurrentImage + 1) + " / " + this.iNumImages;
    var szRightLink = "<a href='#' title='Siguiente' onclick='" + this.szCodeName + ".moveNext();" + this.szCodeName + ".Draw(); return false;'>&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;</a>";
    var szToolbar = szLeftLink + szPosition + szRightLink;
    var oImg = this.aImages[this.iCurrentImage];

    s += "<table width='100%' border=0 cellpadding=0 cellspacing=0 " + szClass + ">";
    // Toolbar
    s += "<tr><td id='" + this.szTagName + "_c1' align=center>" + szToolbar + "</td></tr>";
    // Image
    s += "<tr><td valign='top' id='" + this.szTagName + "_c2' align=center height='" + oImg.iHeight + "'>" + "<img " + (this.szUserFunc != "" ? " onload='" + this.szUserFunc + "()'" : "") + " src='" + oImg.szFileName + "' width='" + oImg.iWidth + "' height='" + oImg.iHeight + "' border='0'>" + "</td></tr>";
    s += "</table>";

    // Set the HTML
    var oTag = document.getElementById(this.szTagName);
    if(oTag)
      oTag.innerHTML = s;
  }
}

