/**
 * case_study.js
 * This is the base class for adding case studies to a portfolio.
 * @author: Cory Simpson (csimpson@fulcrm.com)
 */
CaseStudy.prototype.id = null;
CaseStudy.prototype.companyName = "foo";
CaseStudy.prototype.container = null;
CaseStudy.prototype.currentImgIndex = null;
CaseStudy.prototype.isInit= false;
CaseStudy.prototype.imgs = null;
CaseStudy.prototype.mainImg = null;
CaseStudy.prototype.headerImgName = null;
CaseStudy.prototype.arrowImg = null;
CaseStudy.prototype.lineImg = null;
CaseStudy.prototype.isVisible = false;
CaseStudy.prototype.tableCounterHtml = null;
CaseStudy.prototype.table1TextHtml = null;
CaseStudy.prototype.table2TextHtml = null;
CaseStudy.prototype.zindex = 1;
CaseStudy.prototype.thumbNail = null;
CaseStudy.prototype.commentSections = null;

/**
 * Adds some comments that will be displayed in the case study.
 */
CaseStudy.prototype.newCommentSection = function(commentHeader) {
  var cs = new CommentSection(commentHeader);
  this.commentSections[this.commentSections.length] = cs;
  return cs;
}

CaseStudy.prototype.loadImgs = function(noeffect) {
  var i, c=0;
  for (i=0; i<this.imgs.length; i++) {
    if (this.imgs[i][0].complete) ++c;
  }
  if (c >= this.imgs.length) {
    this.mainImg.src = this.imgs[0][0].src;
    this.createCaseStudyBox(true, noeffect);
  } else {
    if(!noeffect) noeffect = "null";
    setTimeout("currentNode.thumbNailBox.currentThumbNail.casestudy.loadImgs("+noeffect+")", 300);
  }
}

CaseStudy.prototype.createCaseStudyBox = function(noCheck, noeffect) {
  this.mainImg = cbeGetImageByName("casestudy.sample.img");
  if (!this.isInit) {
    if (!noCheck) {
      var k;
      for (k=0; k<this.imgs.length; k++) {
        var url = this.imgs[k][3];
        this.imgs[k][0] = cbeNewImage(id + "." + (k+1), url);
      }
      this.loadImgs(noeffect);
      return;
    }
  }
  this.mainImg.src = this.imgs[this.currentImgIndex-1][0].src;
  if (is.nav4 && is.mac) {
    caseStudyImgCaptionBox.cbe.innerHtml("<font point-size='10' color='#990000' face='arial,verdana,sans-serif'><b><i>"+this.imgs[this.currentImgIndex-1][1]+"</i></b></font>");
  } else if (is.nav4) {
    caseStudyImgCaptionBox.cbe.innerHtml("<font point-size='8' color='#990000' face='arial,verdana,sans-serif'><b><i>"+this.imgs[this.currentImgIndex-1][1]+"</i></b></font>");
  } else {
    caseStudyImgCaptionBox.cbe.innerHtml("<span style='margin:0px;font-family:arial,verdana,sans-serif;font-size:10px;font-style:italic;font-weight:bold;white-space:nowrap;color:#990000;'>"+this.imgs[this.currentImgIndex-1][1]+"</span>");
  }
  caseStudyImgBox.cbe.show();

  if (!this.isInit) {
    this.lineImg = cbeGetPreloadedImageByName('caseStudyImgLine');
    this.arrowImg = cbeGetPreloadedImageByName('caseStudyImgArrow');
    this.tableCounterHtml = '';
    for (i=0; i<this.imgs.length; i++) {
      var link = "";
      if (is.nav4 && is.mac) {
        link = '<a style="text-decoration:none;" href="#" onmouseover="currentNode.thumbNailBox.currentThumbNail.casestudy.moveImgIndexTo('+(i+1)+')"><font point-size="10" color="#838518" face="arial,verdana,sans-serif">' + (i+1) + '</font></a></td><td align="right"><a href="#" onmouseover="currentNode.thumbNailBox.currentThumbNail.casestudy.moveImgIndexTo('+(i+1)+')"><img border="0" name="' + this.id  + '.' + (i+1) + '.counter" height="16" width="10" src=""></a>';
      } else if (is.nav4) {
        link = '<a style="text-decoration:none;" href="#" onmouseover="currentNode.thumbNailBox.currentThumbNail.casestudy.moveImgIndexTo('+(i+1)+')"><font point-size="8" color="#838518" face="arial,verdana,sans-serif">' + (i+1) + '</font></a></td><td align="right"><a href="#" onmouseover="currentNode.thumbNailBox.currentThumbNail.casestudy.moveImgIndexTo('+(i+1)+')"><img border="0" name="' + this.id  + '.' + (i+1) + '.counter" height="16" width="10" src=""></a>';
      } else {
        link = '<a style="font-size:10px;font-family:arial,verdana,sans-serif;text-decoration:none;font-weight:bold;color:#838518;" href="#" onmouseover="currentNode.thumbNailBox.currentThumbNail.casestudy.moveImgIndexTo('+(i+1)+')">' + (i+1) + '</a></td><td align="right"><a href="#" onmouseover="currentNode.thumbNailBox.currentThumbNail.casestudy.moveImgIndexTo('+(i+1)+')"><img border="0" name="' + this.id  + '.' + (i+1) + '.counter" height="16" width="10" src=""></a>';
      }
      this.tableCounterHtml = '<tr><td nowrap="true" valign="middle" align="right">'+link+'</td></tr>' + this.tableCounterHtml;
    }
  }
  caseStudyImgCounterBox.cbe.innerHtml("<table border='0' cellpadding='0' cellspacing='0' width='20' height='188'><tr><td height='" + (188-(this.imgs.length*16)) + "'></td></tr>" + this.tableCounterHtml + "</table>");
  for (i=0; i<this.imgs.length; i++) {
    this.imgs[i][3] = cbeGetImageByName(this.id  + '.' + (i+1) + '.counter');
    if (i == (this.currentImgIndex-1)) {
      this.imgs[i][3].src = this.arrowImg.src;
    } else {
      this.imgs[i][3].src = this.lineImg.src;
    }
  }
  caseStudyImgCounterBox.cbe.show();

  caseStudyImgCaptionBox.cbe.show();

  caseStudyImgDirectionsBox.cbe.show();

  caseStudyHeaderBox.cbe.innerHtml("<img name='casestudy.header.img' height='25' width='345' src=''>");
  cbeSetImage('casestudy.header.img', this.headerImgName);
  caseStudyHeaderBox.cbe.show();

  if (!is.nav4)caseStudyTextBox.style.overflow = 'auto';
  if (!this.isInit) {
    this.table1TextHtml = "<table border='0' cellpadding='0' cellspacing='0' width='160'  height='1'>";
    this.table2TextHtml = "<table border='0' cellpadding='0' cellspacing='0' width='160'  height='1'>";
    for (i=0; i<this.commentSections.length; i++) {
      var header = this.commentSections[i].header;
      if (header) {
        if (is.nav4 && is.mac) {
          if (this.commentSections[i].comments1.length > 0) {
            this.table1TextHtml += "<tr><td valign='bottom' height='15'><font point-size='10' color='#838518' face='arial,verdana,sans-serif'>_</font></td><td valign='bottom'><font point-size='10' color='#838518' face='arial,verdana,sans-serif'><i><b>" + header + "</b></i></font></td></tr>";
          } else {
            this.table2TextHtml += "<tr><td valign='bottom' height='15'><font point-size='10' color='#838518' face='arial,verdana,sans-serif'>_</font></td><td valign='bottom'><font point-size='10' color='#838518' face='arial,verdana,sans-serif'><i><b>" + header + "</b></i></font></td></tr>";
          }
        } else if (is.nav4) {
          if (this.commentSections[i].comments1.length > 0) {
            this.table1TextHtml += "<tr><td valign='bottom' height='15'><font point-size='8' color='#838518' face='arial,verdana,sans-serif'>_</font></td><td valign='bottom'><font point-size='8' color='#838518' face='arial,verdana,sans-serif'><i><b>" + header + "</b></i></font></td></tr>";
          } else {
            this.table2TextHtml += "<tr><td valign='bottom' height='15'><font point-size='8' color='#838518' face='arial,verdana,sans-serif'>_</font></td><td valign='bottom'><font point-size='8' color='#838518' face='arial,verdana,sans-serif'><i><b>" + header + "</b></i></font></td></tr>";
          }
        } else {
          if (this.commentSections[i].comments1.length > 0) {
            this.table1TextHtml += "<tr><td valign='bottom' height='15' style='font-size:10px;font-family:arial,verdana,sans-serif;color:#838518;'>_</td><td valign='bottom'><div style='font-size:10px;font-family:arial,verdana,sans-serif;font-style:italic;font-weight:bold;color:#838518;padding-top:5px;'>" + header + "</div></td></tr>";
          } else {
            this.table2TextHtml += "<tr><td valign='bottom' height='15' style='font-size:10px;font-family:arial,verdana,sans-serif;color:#838518;'>_</td><td valign='bottom'><div style='font-size:10px;font-family:arial,verdana,sans-serif;font-style:italic;font-weight:bold;color:#838518;padding-top:5px;'>" + header + "</div></td></tr>";
          }
        }
      }
      //Write out bullets in col 1.
      for (k=0; k<this.commentSections[i].comments1.length; k++) {
        var text = this.commentSections[i].comments1[k];
        if (is.nav4 && is.mac) {
          this.table1TextHtml += "<tr><td valign='top'><font point-size='10' color='black' face='arial,verdana,sans-serif'>_</font></td><td valign='top'><font point-size='10' color='black' face='arial,verdana,sans-serif'>" + text + "</font></td></tr>";
        } else if (is.nav4) {
          this.table1TextHtml += "<tr><td valign='top'><font point-size='8' color='black' face='arial,verdana,sans-serif'>_</font></td><td valign='top'><font point-size='8' color='black' face='arial,verdana,sans-serif'>" + text + "</font></td></tr>";
        } else {
          this.table1TextHtml += "<tr><td valign='top' style='font-size:10px;font-family:arial,verdana,sans-serif;color:black;'>_</td><td><div style='font-size:10px;font-family:arial,verdana,sans-serif;color:black;text-align:left;'>" + text + "</div></td></tr>";
        }
      }
      //Write out bullets in col 2.
      for (k=0; k<this.commentSections[i].comments2.length; k++) {
        var text = this.commentSections[i].comments2[k];
        if (is.nav4 && is.mac) {
          this.table2TextHtml += "<tr><td valign='top'><font point-size='10' color='black' face='arial,verdana,sans-serif'>_</font></td><td valign='top'><font point-size='10' color='black' face='arial,verdana,sans-serif'>" + text + "</font></td></tr>";
        } else if (is.nav4) {
          this.table2TextHtml += "<tr><td valign='top'><font point-size='8' color='black' face='arial,verdana,sans-serif'>_</font></td><td valign='top'><font point-size='8' color='black' face='arial,verdana,sans-serif'>" + text + "</font></td></tr>";
        } else {
          this.table2TextHtml += "<tr><td valign='top' style='font-size:10px;font-family:arial,verdana,sans-serif;color:black;'>_</td><td><div style='font-size:10px;font-family:arial,verdana,sans-serif;color:black;text-align:left;'>" + text + "</div></td></tr>";
        }
      }
    }
    this.table1TextHtml += "</table>";
    this.table2TextHtml += "</table>";
  }

  caseStudyTextCol1.cbe.innerHtml(this.table1TextHtml);
  caseStudyTextCol1.cbe.show();
  caseStudyTextCol2.cbe.innerHtml(this.table2TextHtml);
  caseStudyTextCol2.cbe.show();
  caseStudyTextBox.cbe.show();

  this.isInit = true;

  //Show the case study.
  if (!noeffect && !is.safari) {
    caseStudyBox.cbe.show();
    caseStudyBox.cbe.autoClip('cen', 'unclip', 20);
  } else {
    caseStudyBox.cbe.show();
  }
  this.isVisible = true;
}

CaseStudy.prototype.magnifyImg = function() {
  var zoomUrl = this.imgs[this.currentImgIndex-1][2];
  if (zoomUrl == null) {
    alert("There is not a full size available for the image at index: " + this.currentImgIndex);
  }
  else {
    if(zoomUrl.substring(0, zoomUrl.indexOf('.')) == "zoom") {
      var params = cbeGetURLArguments(zoomUrl);
      var w = parseInt(params['width'])+20;
      var h = parseInt(params['height'])+25;
      var x = 700;
      var y = 600;
      if(w != null)
        x = screen.width/2 - (w/2);
      if(h != null)
        y = screen.height/2 - (h/2);
      window.open( zoomUrl, 'zoom',
                 'menubar=no,' +
                 'alwaysRaised=yes,' +
                 'directories=no,' +
                 'location=no,' +
                 'resizable=no,' +
                 'scrollbars=no,' +
                 'status=no,' +
                 'toolbar=no,' +
                 'width='+w+',' +
                 'height='+h+',' +
                 'screenX='+x+',' +
                 'screenY='+y+',' +
                 'left='+x+',' +
                 'top='+y );
    }
    else {
      window.open( zoomUrl, 'zoom',
                 'menubar=no,' +
                 'alwaysRaised=yes,' +
                 'directories=no,' +
                 'location=no,' +
                 'status=no,' +
                 'toolbar=no');
    }
  }
}

/**
 * Sets the image that will be used as the header for the case study.
 */
CaseStudy.prototype.setHeaderImg = function(imgName) {
  this.headerImgName = imgName;
}

CaseStudy.prototype.addImg = function(index, url, caption, zoomUrl) {
  this.imgs[index-1] = new Array();
  this.imgs[index-1][0] = null;
  this.imgs[index-1][1] = caption;
  this.imgs[index-1][2] = zoomUrl;
  this.imgs[index-1][3] = url;
}

CaseStudy.prototype.moveImgIndexTo = function(index) {
  this.imgs[this.currentImgIndex-1][3].src = this.lineImg.src;
  this.imgs[index-1][3].src = this.arrowImg.src;
  this.currentImgIndex = index;
  index--;
  this.mainImg.src = this.imgs[index][0].src;
  if (is.nav4 && is.mac) {
    caseStudyImgCaptionBox.cbe.innerHtml("<font point-size='10' color='#990000' face='arial,verdana,sans-serif'><b><i>"+this.imgs[index][1]+"</i></b></font>");
  } else if (is.nav4) {
    caseStudyImgCaptionBox.cbe.innerHtml("<font point-size='8' color='#990000' face='arial,verdana,sans-serif'><b><i>"+this.imgs[index][1]+"</i></b></font>");
  } else {
    caseStudyImgCaptionBox.cbe.innerHtml("<span style='margin:0px;font-family:arial,verdana,sans-serif;font-size:10px;font-style:italic;font-weight:bold;white-space:nowrap;color:#990000;'>"+this.imgs[index][1]+"</span>");
  }
}

/**
 * Sets the thumb nail for this case study.
 */
CaseStudy.prototype.setThumbNail = function(url) {
  /*
  var img = cbeNewImage(this.id + "ThumbNail", url);
  */
  this.thumbNail = new ThumbNail(this.id + "thumbNail", url, this);
  return this.thumbNail;
}

CaseStudy.prototype.addThumbNailToMenu = function(menuArray, itemName) {
  if (this.thumbNail == null) {
    alert('You must first set a thumb nail for the case study.');
    return null;
  }
  var index = menuArray.length;
  menuArray[index] = new Array();
  menuArray[index][0] = itemName;
  menuArray[index][1] = this.thumbNail;
}

/**
 * Makes the case study visible.
 */
CaseStudy.prototype.show = function(noeffect) {
  this.createCaseStudyBox(null, noeffect);
}

/**
 * hides the case study.
 */
CaseStudy.prototype.hide = function() {
  if (this.isVisible) {
    caseStudyBox.cbe.hide();
    this.isVisible = false;
  }
}

CaseStudy.prototype.addToCategoryMenu = function(itemName) {
  this.addThumbNailToMenu(categories, itemName);
}

CaseStudy.prototype.addToIndustryMenu = function(itemName) {
  this.addThumbNailToMenu(industries, itemName);
}

CaseStudy.prototype.addToClientMenu = function(itemName) {
  this.companyName = itemName;
  this.addThumbNailToMenu(clients, itemName);
}

/**
 * Constructor
 */
function CaseStudy(id) {
  this.currentImgIndex = 1;
  this.imgs = new Array();
  this.commentSections = new Array();
  if (id == null) {
    alert('A case study must have a id.');
    return;
  }
  this.id = id;
  caseStudies[caseStudies.length] = this;
}
