// Common Javascript function

function popWin(url,w,h)
{
	newWindow=window.open(url, 'newWindow', 'height=' + h + ',width=' + w + ',toolbar=no,scrollbars=yes,resizable=yes');		
}

// called by /default.aspx (holding  / launch page) to hold /default_.aspx (actual thinktank homepage)
var siteWin;
function popSite(query)
{
	siteWin = window.open('default_.aspx'+query, 'siteWin', 'width=1000,height=720');
}

function popVideo() {
	newWindow=window.open('/include/swf/video.html', 'newWindow', 'height=288,width=360,toolbar=no,scrollbars=no,resizable=yes');
}

function setCurrentLink(linkID, identifyingClass, settingClass) {
  // safari is a bitch
  //      lnks = document.getElementsByTagNameNS("*", "a");
  if (navigator.userAgent.toLowerCase().indexOf("safari") < 0)
  {
    lnks = document.getElementsByTagName("a");
    if (lnks)
      for (i in lnks)
        if ((lnks[i].className) && (lnks[i].className.indexOf(identifyingClass) == 0))
            lnks[i].className = lnks[i].className.replace(" " + settingClass,"");
    document.getElementById(linkID).className += " " + settingClass;
  }
}

function show(id)
{
  if (document.getElementById)
    if (document.getElementById(id))
      document.getElementById(id).className = "shown";
}

function hide(id)
{
  if (document.getElementById)
    if (document.getElementById(id))
      document.getElementById(id).className = "hidden";
}

//
function viewSubmissions()
{
  // loads brieftoday.aspx into iframe named 'content'
  setFrame('brieftoday.aspx');
}

function setFrame(url) {
  document.getElementById('content').src = url;
}

var weekday=new Array(7)
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

var month=new Array(12)
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="Novemeber";
month[11]="December";

// objects used by the briefs gallery

function objBrief(_id, _tankDayID, _summary, _date, _vote, _comment, _isBigIdea) {
  this.id = _id;
  this.tankDayID = _tankDayID;
  this.summary = _summary;
  this.date = _date;
  this.date = new Date();
  this.date.setFullYear(_date.substr(0,4), _date.substr(5,2) - 1, _date.substr(8,2));
  this.date.setHours(_date.substr(11,2));
  this.date.setMinutes(_date.substr(14,2));
  this.vote = _vote;
  this.comment = _comment;
  this.isBigIdea = _isBigIdea;
  this.dump = function() {
    return "id: " + this.id + "\ntankDayID: " + this.tankDayID + "\nsummary: " + this.summary + "\ndate: " + this.date + "\nvote: " + this.vote + "\ncomment: " + ((this.comment == null) ? '-' : this.comment) + " \n";
  }
}

function objBriefs(cnt) {
  this.briefs = [];
  this.displayCount = cnt;
  this.currentPage = 0;
  this.currentTankDay = 0;
  this.currentBriefs = [];
  this.currentBriefID = 0;

  this.add = function(_id, _tankDayID, _summary, _date, _vote, _comment, _isBigIdea) {
    this.briefs.push( new objBrief(_id, _tankDayID, _summary, _date, _vote, _comment, _isBigIdea) );
  }
  this.dump = function () {
    dumpStr = "";
    for (i in this.briefs) {
      dumpStr += "brief [" + i + "] - \n";
      dumpStr += this.briefs[i].dump();
    }
    alert(dumpStr);
  }

  /* brief manipulation fns */
    
  // goes to the next page of briefs
  this.goNext = function () {
    // see if there are any more pages of briefs in the current day
    this.getCurrent();
    // if there are more briefs in the current day, increment the page
    if (this.currentBriefs.length > (this.currentPage + 1) * this.displayCount)
    {
      this.currentPage++;
    }
    // otherwise reset the page and increment the day
    else
    {
      this.currentPage = 0;
      this.currentTankDay++;
    }
    // if the day is greater than 16, reset to 1
    if (this.currentTankDay > 16)
      this.currentTankDay = 1;
    
    this.update();
  }
  
  this.next = function () {
    // gets the next brief id in the brief arr
    // reset to 0 if greater than length
    nextID = null; // view sets currentBriefID, so no need to set here
    for (i in this.briefs)
      if (this.briefs[i].id == this.currentBriefID)
        break;
    // if we are at the last brief, set the current brief id as the first
    if (i == (this.briefs.length - 1))
      nextID = this.briefs[0].id
    // else set it as the next briefID
    else
      nextID = this.briefs[parseInt(i) + 1].id;
    this.view(nextID);
  }
  
  this.prev = function () {
    // gets the next brief id in the brief arr
    // reset to 0 if greater than length
    nextID = null; // view sets currentBriefID, so no need to set here
    for (i in this.briefs)
      if (this.briefs[i].id == this.currentBriefID)
        break;
    // if we are at the first brief, set the current brief id as the last
    if (i == 0)
      nextID = this.briefs[this.briefs.length-1].id
    // else set it as the previous briefID
    else
      nextID = this.briefs[parseInt(i) - 1].id;
    this.view(nextID);
  }
  
  // goes to the previous page of briefs
  this.goPrev = function () {
    // see if there are any more pages of briefs in the current day
    this.getCurrent();
    // if we are on the first page, decrement the day
    if (this.currentPage == 0) {
      this.currentTankDay--;
      this.getCurrent();
      // work out the max page of the current day
      this.currentPage = parseInt(this.currentBriefs.length / this.displayCount);
    }
    // otherwise decrement the page
    else
      this.currentPage--;
    // if the day is less than 0, reset to 16
    if (this.currentTankDay < 1)
      this.currentTankDay = 16;
    //alert("page: " + currentPage + "\nday: " + currentTankDay);
    this.update();
  }
  
  // set the current tankday
  this.setDay = function (tankDayID) {
    this.currentTankDay = tankDayID
    // reset the current page
    this.currentPage = 0;
    // update the view
    this.update();
    document.getElementById('days').className = "shown";
    document.getElementById('briefs').className = "shown";
    document.getElementById('buttons').className = "shown";
    document.getElementById('votes').className = "hidden";
    document.getElementById('details').className = "hidden";
    document.getElementById('feedback').className = "hidden";
  }
  
  // updates the view with the current apge of briefs
  this.update = function () {
    // get the briefs for the selected tankDay
    this.getCurrent();
    
    start = this.currentPage * this.displayCount;
    brf = 0;
    for (i = start; i < start + this.displayCount; i++,brf++)
    {
      img = document.getElementById('img' + (brf + 1));
      lnk = document.getElementById('brf' + (brf + 1));
      lbl = document.getElementById('lbl' + (brf + 1));
      
      if (i < this.currentBriefs.length)
      {
  	  img.src = "/cms/brief/thumb/" + this.currentBriefs[i].id + ".jpg";
  	  img.alt = this.currentBriefs[i].id + ": " + this.currentBriefs[i].summary;
  	  lbl.innerHTML = weekday[this.currentBriefs[i].date.getDay()].substr(0,3).toUpperCase() + " " + this.currentBriefs[i].date.getHours() + ":" + this.currentBriefs[i].date.getMinutes();
  	  lnk.href = "javascript:b.view(" + this.currentBriefs[i].id + ");";
      }
      else
      {
  	  img.src = "/images/blankbrief.gif";
  	  lbl.innerText = "-";
  	  lnk.href = "#";
      }
    }
    // update the link
    setCurrentLink('dayLink'+this.currentTankDay,'day','current');
  }
  
  // finds the briefs belonging to the current day
  this.getCurrent = function () {
    // find the rows in b that have the same tankday
    this.currentBriefs = [];
    for (i in this.briefs)
      if (this.briefs[i].tankDayID == this.currentTankDay)
        this.currentBriefs[this.currentBriefs.length] = this.briefs[i];
  }
  
  // grabs the id of the current order-eth current brief and show it in the viewer
  this.view = function (id) {
    document.getElementById('days').className = "hidden";
    document.getElementById('briefs').className = "hidden";
    document.getElementById('buttons').className = "hidden";
    document.getElementById('votes').className = "hidden";
    document.getElementById('details').className = "shown";
    document.getElementById('feedback').className = "hidden";
    
    id = parseInt(id);
    
    // get the brief with the corresponding briefid
    brief = null;
    for (i in this.briefs)
      if (this.briefs[i].id == id) {
        brief = this.briefs[i];
        break;
      }
    if (brief != null) {
      document.getElementById('summary').innerHTML = brief.summary;
      document.getElementById('briefdetails').src = "/cms/brief/" + brief.id + ".jpg";
      document.getElementById('lnkfeedback').className = document.getElementById('lnkfeedback').className.replace((brief.comment != null) ? " hidden" : " shown", (brief.comment != null) ? " shown" : " hidden");
      document.getElementById('lnkfeedback').href = (brief.comment == null) ? "#" : "javascript:b.viewComment(" + id + ");";
      document.getElementById('detailsbigidea').className = brief.isBigIdea ? "shown" : "hidden";
      document.forms[0]['hdnVoteBriefID'].value = brief.id;
      // store for next/prev
      this.currentBriefID = brief.id;
    }
    
  }
  
  // grabs the id of the current order-eth current brief and show it in the viewer
  this.viewComment = function (id) {
    document.getElementById('days').className = "hidden";
    document.getElementById('briefs').className = "hidden";
    document.getElementById('buttons').className = "hidden";
    document.getElementById('votes').className = "hidden";
    document.getElementById('details').className = "hidden";
    document.getElementById('feedback').className = "show";

    // get the brief with the corresponding briefid
    brief = null;
    for (i in this.briefs)
      if (this.briefs[i].id == id) {
        brief = this.briefs[i];
        break;
      }

    if (brief != null) {
      document.getElementById('comment').innerHTML = brief.comment;
      document.getElementById('commentdetails').src = "/cms/brief/" + brief.id + ".jpg";
      document.getElementById('lnkdetails').href = "javascript:b.view(" + id + ");";
      document.getElementById('feedbackbigidea').className = brief.isBigIdea ? "shown" : "hidden";
      document.forms[0]['hdnVoteBriefID'].value = brief.id;
      // store for next/prev
      this.currentBriefID = brief.id;
    }
    
  }
  
  // brief vote sorting function
  this.votesortfn = function (a,b)  {
    if (a.vote<b.vote) return 1;
    if (a.vote>b.vote) return -1;
    return 0;
  }
  
  
  // finds the 10 briefs with the highest votes
  this.showTop10 = function () {
    
    var votes = this.briefs;
    votes.sort(this.votesortfn);
    
    
    for (i = 0; i < 10; i++)
    {
      img = document.getElementById('voteimg' + (i + 1));
      lnk = document.getElementById('votebrf' + (i + 1));
      lbl = document.getElementById('votelbl' + (i + 1));
        
      if (votes[i])
      {
        img.src = "/cms/brief/" + votes[i].id + ".jpg";
        img.alt = votes[i].id + ": " + votes[i].summary;
        lbl.innerHTML = weekday[votes[i].date.getDay()].substr(0,3).toUpperCase() + " " + votes[i].date.getDate() + " " + month[votes[i].date.getMonth()].substr(0,3).toUpperCase() + " " + votes[i].date.getHours() + ":" + votes[i].date.getMinutes() + " <br /> " + votes[i].vote + " votes";
        lnk.href = "javascript:b.view(" + votes[i].id + ");";
      }
      else
      {
        img.src = "/images/blankbrief.gif";
      }
        
    }    
    document.getElementById('days').className = "hidden";
    document.getElementById('briefs').className = "hidden";
    document.getElementById('buttons').className = "hidden";
    document.getElementById('votes').className = "shown";
    document.getElementById('details').className = "hidden";
    document.getElementById('feedback').className = "hidden";
  }
  
}

