/*
############################################################################
#
# Part of the Dr. Rogers Prize site code
# Copyright (C) 2009 Damon Harper
# usrbin design + programming - http://usrbin.ca/
#
# This program is governed by the agreed upon license between Damon Harper
# d.b.a. usrbin design + programming, and AMG Media Inc.  This program may
# be modified for internal use but may not be sold or otherwise distributed
# without express written permission from the copyright holder.
#
############################################################################
*/

function isDef(o) {
  return typeof(o)!='undefined';
}
function isStr(o) {
  return typeof(o)=='string';
}
function isBool(o) {
  return typeof(o)=='boolean';
}

function getById(id) {
  if(!isStr(id))
    return id;
  if(document.getElementById)
    return document.getElementById(id);
  if(document.all)
    return document.all[id];
  return null;
}

function getByClass(classname, node, tag) {
  if(node==null)
    node=document;
  if(tag==null)
    tag='*';
  var elements=new Array();
  var idx=0;
  var e=node.getElementsByTagName(tag);
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)");
  for(var i=0; i<e.length; i++) {
    if(re.test(e[i].className)) {
      elements[idx]=e[i];
      idx++;
    }
  }
  return elements;
}

function getByTagName(tag, node) {
  if(node==null)
    node=document;
  return node.getElementsByTagName(tag);
}

function hasClass(node, classname) {
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)");
  return re.test(node.className);
}

function addClass(node, classname) {
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)");
  if(!re.test(node.className))
    node.className+=(node.className ? ' ' : '')+classname;
}

function delClass(node, classname) {
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)", 'g');
  if(node.className)
    node.className=trim_inside(node.className.replace(re, ' '));
}

function trim(string) {
  return string.replace(/^\s+/, '').replace(/\s+$/, '');
}

function trim_inside(string) {
  return trim(string).replace(/\s{2,}/, ' ');
}

function toggle_blocks() {
  var message='';
  var matchclass=new RegExp("^(show|hide)if(not)?-([^=\\s]+)=(\\S*)$");
  var blocks=getByClass("(show|hide)if(not)?-\\S+");
  for(var i=0; i<blocks.length; i++) {
    var show='undef';
    var classes=blocks[i].className.split(/\s+/);
    for(var j=0; j<classes.length; j++) {
      var matches=matchclass.exec(classes[j]);
      if(matches) {
        var type=matches[1];
        var want=matches[2] ? false : true;
        var control=matches[3];
        var value=matches[4];
        message+=control+' '+type+' '+value+'\n';
        if(show=='undef')
          show=type=='hide';
        var obj=getById(control);
        var button;
        if(obj && obj.tagName=='SELECT') { // workaround for IE
          if((obj.options[obj.selectedIndex].value==value)==want)
            show=type!='hide';
        } else if(button=getById(control+'='+value)) {
          if((button && button.checked)==want)
            show=type!='hide';
        } else if(obj) {
          if((trim(obj.value)==value)==want)
            show=type!='hide';
        }
      }
    }
    if(show && show!='undef') {
      var tag=blocks[i].tagName;
      var display;
      if(tag=='SPAN')
        display='inline';
      else if(tag=='TR')
        display='table-row';
      else if(tag=='TD' || tag=='TH')
        display='table-cell';
      if(!display)
        display='block';
      try {
        blocks[i].style.display=display;
      } catch(e) {
        blocks[i].style.display='block'; // IE doesn't understand table-*
      }
    } else
      blocks[i].style.display='none';
  }
}

function do_save_progress(num) {
  document.forms['default'].elements['save_progress'].value=num ? num : 'all';
  document.forms['default'].submit();
  please_wait();
}

function countwords(input, output, max_words) {
  $(document).ready(function(){
    input=$('#'+input);
    output=$('#'+output);
    if(input.length && output.length) {
      var timeout_count_action=false;
      function count_action() {
        try {
          if(timeout_count_action) {
            clearTimeout(timeout_count_action);
            timeout_count_action=false;
          }
          var words=trim(input.attr('value')).match(/[A-za-z0-9_\']+/g).length;
          var prefix=words>max_words ? '<span class="word-count-over-limit">' : '';
          var suffix=words>max_words ? '</span>' : '';
          output.attr('innerHTML', prefix+words+suffix);
        } catch(e) {
        }
      }
      function count_action_timeout() {
        if(!timeout_count_action)
          timeout_count_action=setTimeout(count_action, 500);
      }
      count_action();
      input.bind('change', count_action);
      input.bind('blur', count_action);
      input.bind('keyup', count_action_timeout);
    }
  });
}

function please_wait() {
  if($('#please-wait').length) {
    window.setTimeout(function() {
      if(typeof document.body.style.maxHeight==="undefined") { //IE6
        $('body','html').css({height: '100%', width: '100%'});
        $('html').css('overflow', 'hidden');
        $('body').append('<iframe id="blackout-iefix"></iframe>');
      }
      $('body').append('<div id="blackout-mask"></div>');
      $('#please-wait').css({display: 'block'});
    }, 1500);
  }
  return true;
}

