/*** Add Any Template Javascript here ***/

var fontPrefsLoaded = false;
var defaultFontSize = 12;
var currentFontSize = defaultFontSize;

function resetFontSize() {
  currentFontSize = defaultFontSize;
  changeFontSize(0);
}

function changeFontSize(sizeDifference){
  currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 2);

  if(currentFontSize > 20){
    currentFontSize = 20;
  } else if(currentFontSize < 10){
    currentFontSize = 10;
  }

  setFontSize(currentFontSize);
};

function setFontSize(fontSize){
  var stObj = (document.getElementById) ? document.getElementById('content_area') : document.all('content_area');
  document.body.style.fontSize = fontSize + 'px';
  
  //alert (document.body.style.fontSize);
};

function createSETmplCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
};

function readSETmplCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
};

window.onload = saveUserFontSize;

function saveUserFontSize() {
  if(!fontPrefsLoaded){
    cookie = readSETmplCookie("seTmplFontSize");
    currentFontSize = cookie ? cookie : defaultFontSize;
    setFontSize(currentFontSize);
    fontPrefsLoaded = true;
  }
}

window.onunload = saveFontSettings;

function saveFontSettings() {
  createSETmplCookie("seTmplFontSize", currentFontSize, 365);
}

function even_article_cols(numRows, numCols, idString) {
  if (typeof numRows == 'undefined' ) numRows = 0;
  if (typeof numCols == 'undefined' ) numCols = 0;
  if (typeof idString == 'undefined' ) idString = '';

  var myid_array = 0;
  var curr_id_array_pos = 0;

  if(idString != '') {
    myid_array = idString.split(":");
  }

  if(myid_array) {
    for(var r=1; r <= numRows; r++) {
      var greatestHeight = 0;
      var greatestHeadHeight = 0;
      var greatestHeadHeightwPadd = 0;

      for(var tc=1; tc <= numCols; tc++) {
        var colContentDiv = document.getElementById('article-content-'+myid_array[curr_id_array_pos]);
        var articleContentHead = document.getElementById('article-header-'+myid_array[curr_id_array_pos]);

        if(articleContentHead) {
          var articleContentHeadHeight = 0;
          if(articleContentHead.offsetHeight) {
            articleContentHeadHeight = articleContentHead.offsetHeight;
          }
          else if(articleContentHead.style.pixelHeight) {
            articleContentHeadHeight = articleContentHead.style.pixelHeight;
          }

          if(articleContentHeadHeight > greatestHeadHeightwPadd) {
            greatestHeadHeightwPadd = articleContentHeadHeight;
          }

          var paddingComp = 0;

          var padTopVal = 0;
          var padBottVal = 0;

          var padTopValStr = String(getElementStyle(articleContentHead, 'padding-top', 1));
          var padBottValStr = String(getElementStyle(articleContentHead, 'padding-bottom', 1));

          padTopValStr = padTopValStr.replace(/px/, '');
          padBottValStr = padTopValStr.replace(/px/, '');

          if(padTopValStr) {
            padTopVal = Number(padTopValStr);
          }

          if(padBottValStr) {
            padBottVal = Number(padBottValStr);
          }

          if(padTopVal) {
            paddingComp += padTopVal;
          }

          if(padBottVal) {
            paddingComp += padBottVal;
          }

          articleContentHeadHeight = articleContentHeadHeight - paddingComp;

          if(articleContentHeadHeight > greatestHeadHeight) {
            greatestHeadHeight = articleContentHeadHeight;
          }
        }

        if(colContentDiv) {
          var colContentDivHeight = 0;
          if(colContentDiv.offsetHeight) {
            colContentDivHeight = colContentDiv.offsetHeight;
          }
          else if(colContentDiv.style.pixelHeight) {
            colContentDivHeight = colContentDiv.style.pixelHeight;
          }

          var paddingComp = 0;

          var padTopVal = 0;
          var padBottVal = 0;

          var padTopValStr = String(getElementStyle(colContentDiv, 'padding-top', 1));
          var padBottValStr = String(getElementStyle(colContentDiv, 'padding-bottom', 1));

          padTopValStr = padTopValStr.replace(/px/, '');
          padBottValStr = padTopValStr.replace(/px/, '');

          if(padTopValStr) {
            padTopVal = Number(padTopValStr);
          }

          if(padBottValStr) {
            padBottVal = Number(padBottValStr);
          }
          
          if(padTopVal) {
            paddingComp += padTopVal;
          }

          if(padBottVal) {
            paddingComp += padBottVal;
          }

          colContentDivHeight = colContentDivHeight - paddingComp;

          if(colContentDivHeight > greatestHeight) {
            greatestHeight = colContentDivHeight;
          }
          curr_id_array_pos++;
        }
      }

      curr_id_array_pos = (r * 2) - 2;

      if(greatestHeight !== 0) {
        for(var c=1; c <= numCols; c++) {
          var colContDiv = document.getElementById('article-content-'+myid_array[curr_id_array_pos]);
          var articleContentHead = document.getElementById('article-header-'+myid_array[curr_id_array_pos]);

          if(colContDiv) {
            var headHeightDiff = 0;
            var newContentBoxHeight = 0;

            if(articleContentHead) {
              var articleContentHeadHeight = 0;
              if(articleContentHead.offsetHeight) {
                articleContentHeadHeight = articleContentHead.offsetHeight;
              }
              else if(articleContentHead.style.pixelHeight) {
                articleContentHeadHeight = articleContentHead.style.pixelHeight;
              }

              if(articleContentHeadHeight != greatestHeadHeightwPadd) {
                var paddingComp = 0;

                var padTopVal = 0;
                var padBottVal = 0;

                var padTopValStr = String(getElementStyle(articleContentHead, 'padding-top', 1));
                var padBottValStr = String(getElementStyle(articleContentHead, 'padding-bottom', 1));

                padTopValStr = padTopValStr.replace(/px/, '');
                padBottValStr = padTopValStr.replace(/px/, '');

                if(padTopValStr) {
                  padTopVal = Number(padTopValStr);
                }

                if(padBottValStr) {
                  padBottVal = Number(padBottValStr);
                }

                if(padTopVal) {
                  paddingComp += padTopVal;
                }

                if(padBottVal) {
                  paddingComp += padBottVal;
                }

                articleContentHeadHeight = articleContentHeadHeight - paddingComp;

                if(articleContentHeadHeight < greatestHeadHeight) {
                  headHeightDiff = greatestHeadHeight - articleContentHeadHeight;
                }
              }
            }

            if(headHeightDiff != 0) {
              newContentBoxHeight = greatestHeight + headHeightDiff;
            } else {
              newContentBoxHeight = greatestHeight;
            }

            colContDiv.style.height = newContentBoxHeight+"px";
            curr_id_array_pos++;
          }
        }
      }
    }
  }
}

function getElementStyle(el, styleProperty, elIsObject) {
  if (typeof elIsObject == 'undefined' ) elIsObject = 0;

  var x = null;
  var y = 0;

  if(elIsObject) {
    x = el;
  } else {
    x = document.getElementById(el);
  }

  if (x.currentStyle) {
    var y = x.currentStyle[styleProperty];
  } else if (document.defaultView) {
    var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProperty);
  }

  return y;
}

function twitterCallbackSECustom(obj) {
  var twitters = obj;
  var statusHTML = "";
  var username = "";
  var isOdd = false;
  var singleEntry = false;
  var singleEntryOverrideClass = "";
  var fullSingleEntryOverrideClass = "";

  for (var i=0; i < twitters.length; i++) {
    if(twitters.length == 1) {
      singleEntry = true;
      singleEntryOverrideClass = ' singleTwitterEntryBorders';
      fullSingleEntryOverrideClass = ' class="singleTwitterEntryBorders"';
    }

    if (i%2 == 0) {
      isOdd = false;
    } else {
      isOdd = true;
    }

    username = twitters[i].user.screen_name;

    if(isOdd) {
      if(i == 0) {
        statusHTML += ('<li class="twitter_odd_entry twitter_first_entry'+singleEntryOverrideClass+'"><span class="twitter_entry_text">'+twitters[i].text+'</span> - <a class="twitter_status_link" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>')
      } else {
        statusHTML += ('<li class="twitter_odd_entry'+singleEntryOverrideClass+'"><span class="twitter_entry_text">'+twitters[i].text+'</span> - <a class="twitter_status_link" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>')
      }
    } else {
      if(i == 0) {
        statusHTML += ('<li class="twitter_first_entry'+singleEntryOverrideClass+'"><span class="twitter_entry_text">'+twitters[i].text+'</span> - <a class="twitter_status_link" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>')
      } else {
        statusHTML += ('<li'+fullSingleEntryOverrideClass+'><span class="twitter_entry_text">'+twitters[i].text+'</span> - <a class="twitter_status_link" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>')
      }
    }
  }
  
  document.getElementById('twitter_update_list').innerHTML = statusHTML;
}

