function changeFrame(q) {
    document.getElementById("newsFrame").innerHTML="<iframe frameborder='0' width='300' height='250' marginwidth='0' marginheight='0' src='http://www.google.com/uds/modules/elements/newsshow/iframe.html?q=Chicago&topic="+q+"&rsz=large&format=300x250'></iframe>";
    alert(document.getElementById("newsFrame").innerHTML);
}

function clearText(input) {
  if(input.value == _getDefault(input)) {
    input.value = "";
  }
}

function resetText(input) {
  if(input.value == "") {
    input.value = _getDefault(input);
  }
}

function _getDefault(input) {
  var init = input.getAttribute("init");
  if(init != "" && init != null) {
    return init;
  }
  else {
    return input.defaultValue;
  }
}

/*
  Code for tab functionality.
*/

//we should be able to collapse some of this coding with the
//tab group.
function radioGroup(name, inputID) {
  this.input = document.getElementById(inputID);
  this.items = document.getElementsByName(name);
  for(var index=0; index < this.items.length; index++) {
    var item = this.items[index];
    if(item.nodeName == "A" || item.nodeName =="a") {
      item.href="javascript:void(0)";
    }
  }
}

radioGroup.prototype.select = function(selectedElement) {
  for(var index=0; index < this.items.length; index++) {
    var item = this.items[index];
    if(item == selectedElement) {
      this.input.value = item.getAttribute("value");
      item.className = "checked"; 
    }
    else {
      item.className = ""; 
    }
  } 
}


function tabGroup(name) {
  this.tabs = document.getElementsByName(name);
  for(var index=0; index < this.tabs.length; index++) {
    var tab = this.tabs[index];
    if(tab.nodeName == "A" || tab.nodeName =="a") {
      tab.href="javascript:void(0)";
    }
  }
}

tabGroup.prototype.select = function(selectedElement) {
  for(var index=0; index < this.tabs.length; index++) {
    var panel;
    var tab = this.tabs[index];
    if(tab == selectedElement) {
      panel = document.getElementById(tab.getAttribute("panel"));

      panel.className = "open";
      tab.className = "checked"; 
    }
    else {
      panel = document.getElementById(tab.getAttribute("panel"));
      panel.className = "closed";
      tab.className = ""; 
    }
  } 
}

/*
  Code for more group functionality
*/

function moreGroup() {
  this.sections = new Array();
  this.switchOpenClass = "minusbutton";
  this.switchClosedClass = "plusbutton";
}

moreGroup.prototype.add = function (switchID, sectionID) {
  var section = new moreSection(switchID, sectionID, "closed");
  this.sections.push(section);
}

moreGroup.prototype.setSwitchClasses = function(open, closed) {
  this.switchOpenClass = open;
  this.switchClosedClass = closed;
}

moreGroup.prototype.toggle = function (element) {
  var section = this.find(element);
  if (section != null) {
    if(section.isOpen()) {
      section.deselect(this.switchClosedClass);
    }
    else {
      section.select(this.switchOpenClass);
      this.deselectOthers(element);
    }
  }
}

moreGroup.prototype.select = function (element) {
  var section = this.find(element);
  if (section != null) {
    section.select(this.switchOpenClass);
    this.deselectOthers(element);
  }
}

moreGroup.prototype.deselect = function (element) {
  var section = this.find(element);
  if (section != null) {
    if(section.isOpen()) {
      section.deselect(this.switchClosedClass);
    }
  }
}

moreGroup.prototype.find = function (element) {
  for (var i = 0; i < this.sections.length; i++) {
    if(this.sections[i].match(element)) {
      return this.sections[i];
    }
  }
  return null;
}

moreGroup.prototype.deselectOthers = function (element) {
  for (var i = 0; i < this.sections.length; i++) {
    if(!this.sections[i].match(element)) {
      this.sections[i].deselect(this.switchClosedClass);
    }
  }
}


function moreSection(switchID, sectionID, open) {
  this.switchElement = document.getElementById(switchID);
  this.sectionElement = document.getElementById(sectionID);

/*
  if(open=="open") {
    this.select();
  }
  else {
    this.deselect();
  }
*/
}

moreSection.prototype.setSwitchClasses = function(open, closed) {
  this.openClass = open;
  this.closedClass = closed;
}

moreSection.prototype.isOpen = function() {
  return (this.open == "open");
}

moreSection.prototype.match = function (element) {
  return (this.switchElement == element);
}

moreSection.prototype.select = function(switchOpen) {
  this.open = "open";
  this.setElementClassName(this.switchElement, switchOpen); 
  this.setElementClassName(this.sectionElement, "open"); 
}

moreSection.prototype.deselect = function(switchClosed) {
  this.open = "closed";
  this.setElementClassName(this.switchElement, switchClosed); 
  this.setElementClassName(this.sectionElement, "closed"); 
}

moreSection.prototype.setElementClassName = function(element, className) {
  if(element != null && className != "") {
    element.className = className;
  }
  else {
  }
}

/*
  Code for image scroll widget
*/
function imageScroll(photoID, data) {
  this.photo = document.getElementById(photoID);
  this.caption = findFirstDecendantByAttribute(this.photo, "caption", "caption");
  this.link = findFirstDecendantByAttribute(this.photo, "link", "link");

  this.index = 1;
  this.data = data;
}

imageScroll.prototype.inc = function () {
  this.index++;
  this.display();
}

imageScroll.prototype.dec = function () {
  this.index--;
  this.display();
}

imageScroll.prototype.display = function() {
  //this may look a little weird, but it forces the answer to be 
  //positive if its hit -1
  this.index = (this.index + data.length) % data.length;
  var item = data[this.index];
 
  //IE doesn't like setProperty
  //this.photo.style.setProperty("background-image", "url(" + item["image"] + ")", "");
  this.photo.style["backgroundImage"] = "url(" + item["image"] + ")";
  this.caption.innerHTML = item["caption"];
  this.link.setAttribute("href", item["link"]);
}

/*
  Code for select box widgets
*/

var RPLObjs = new Array();
function renderPicklist(id, formname, initialLabel, initialValue, data) {
  //note that RPLObjs.length() is the index of the array element we will
  //be adding momentarily
  var objname = "RPLObjs[\"" + RPLObjs.length +"\"]";
  var html = getPicklistHtml(objname, id, formname, initialLabel, initialValue, data, "");
  document.write(html);
  RPLObjs.push(new picklist(objname));
}


function getPicklistHtml(objname, id, formname, initialLabel, initialValue, data, onChangeAction) {
  var html = "<div id='" + id + "'><select obj='" + objname + "' id='" + formname + 
    "' name='" + formname + "' onchange='setPicklistClasses(this);" + onChangeAction + "' >";

  if(initialLabel != "" && initialLabel != null) {
    var initial = new Object;
    initial[initialValue] = initialLabel;
    html += getOptionListHtml(initial, objname, initialValue);
    initialValue = null;
  }
  
  html += getOptionListHtml(data, objname, initialValue);
  html += "</select></div>";
  
  return html;
}

function getOptionListHtml(data, objname, initialValue) {
  var html = "";
  
  for(var value in data) {
    var label = data[value];
    if((typeof value) == "object") {
      html +=  + getOptionListHtml(value, objname, initialValue);
    }
    else {
      var selected = "";
      if (initialValue != null && value == initialValue) {
        selected = "class='selectedoption' selected='selected'";
      }
        
      html += "<option value='" + value + "' " + selected + " >" + label + "</option>";
    }
  }
  return html;
}

function setPicklistClasses(select) {
  var i;
  for (i=0; i < select.options.length; i++) {
    var xoption = select.options[i];
    if(xoption.selected) {
     //xoption.class = "selectedoption";
    }
    else {
     //select.options[i].class = "";
     //var x = "";
    }
  } 
}

function picklist(objname) {
  //this.main = document.getElementById(id);
  this.main = findFirstDecendantByAttribute(document, "obj", objname);
//  this.list = findFirstDecendantByAttribute(this.main, "list", "list");
//  this.input = findFirstDecendantByAttribute(this.main, "var", "var");
//  this.display = findFirstDecendantByAttribute(this.main, "display", "display");
//  this.isOpen = false;
//  this.onChange = null;
}

//not currently used -- using standard selects instead
//left in for future use
picklist.prototype.open = function() {
  this.isOpen = true;
//  this.input.focus();
  this.list.style.display = "block";
  this.main.style.zIndex = 1000;
  this.list.style.zIndex = 1000;
}

//not currently used -- using standard selects instead
//left in for future use
picklist.prototype.close = function() {
  this.isOpen = false;
  this.list.style.display = "none";
  this.main.style.zIndex = "auto";
}

//not currently used -- using standard selects instead
//left in for future use
picklist.prototype.toggle = function() {
  if(this.isOpen) {
    this.close();
  }
  else {
    this.open();
  }
}

picklist.prototype.setOnchange = function (newOnchange) {
  this.main.onchange = makeOnChange(this.main, newOnchange);
  //this.main.onchange = newOnchange;
}

//not currently used -- using standard selects instead
//left in for future use
picklist.prototype.setValue = function(opt) {
  this.display.innerHTML = opt.innerHTML;
  this.input.value = opt.getAttribute("value");
  
  if(this.onChange != null) {
    this.onchange();
  }

  //this.close();
}

function makeOnChange(select, newOnchange) {
   return function() {setPicklistClasses(select); newOnchange()}; 
}

/* code for counter widgets */

var RCObjs = new Object();
function renderCounter(id, label, initialCount) {
  var objname = "RCObjs[\"" + id +"\"]";
  document.write("<div class='realselects'>");  
  document.write("<div id='" + id + "' name='" + id + 
    "' class='countoption' style='display:block'>" + label + "</div>");  
  document.write("<input name='" + id + "' var='var' type='hidden' value='' />");
  document.write("<a href='javascript:" + objname + ".inc();' class='upbutton'>&nbsp;</a>");
  document.write("<a href='javascript:" + objname + ".dec()' class='downbutton'>&nbsp;</a>");
  document.write("</div>");

  RCObjs[id] = new counter(id, label, initialCount);
}

function counter(id, initialLabel, initialCount) {
  this.main = document.getElementById(id);
  this.input = findFirstDecendantByAttribute(this.main.parentNode, "var", "var");
  
  this.initialLabel = initialLabel;
  this.count = initialCount;
  if(initialCount != 0) {
//    this.set();
  }
}

counter.prototype.inc = function() {
  this.count++;
  this.set();
}

counter.prototype.dec = function() {
  this.count--;
  if(this.count < 1) {
    this.count = 1;
  }
  this.set();
}

counter.prototype.set = function() {
  this.main.innerHTML = this.count;
  this.input.value = this.count;
}

/*
  some potentially useful global variables 
*/


/*
  global Picklist options
*/
var g_days = {
  1:"1", 
  2:"2",
  3:"3",
  4:"4",
  5:"5",
  6:"6",
  7:"7",
  8:"8",
  9:"9",
  10:"10",
  11:"11",
  12:"12",
  13:"13",
  14:"14",
  15:"15",
  16:"16",
  17:"17",
  18:"18",
  19:"19",
  20:"20",
  21:"21", 
  22:"22",
  23:"23",
  24:"24",
  25:"25",
  26:"26",
  27:"27",
  28:"28",
  29:"29",
  30:"30",
  31:"31"
};

var g_months = {
  1:"January",
  2:"Feburary",
  3:"March",
  4:"April",
  5:"May",
  6:"June",
  7:"July",
  8:"August",
  9:"September",
  10:"October",
  11:"November",
  12:"December"
}

function optionSequence(start, end)  {
  var seq = new Object();
  
  if (start > end) {
    var temp = start;
    start = end;
    end = temp;
  }
  
  for(var i = start; i <= end; i++) {
    seq[i] = i;
  }
  return seq;
}



