/* original class name of selected row */
var row_selected_class = null;
/* reference to currently selected row */
var row_selected = null;
/* link to next set of rows */
var link_next = null;
/* link to previous set of rows */
var link_previous = null;
/* id of query string var that selected row is passed through */
var link_id = null;

/**
 * rows()
 *
 * Initializes all the "row" divs. Add "row" to the class of every div that
 * needs to have row events applied to it. Then encase all the "row" divs in
 * a parent "rows" div. The divs w/ "row" as their class will have "mouseover"
 * as an id on mouseover, as well as fire the select function w/ the onclick
 * event.
 */
function rows() {
  if(document.getElementsByTagName && document.createTextNode) {
    // find all the divs in the document with a class = "rows", these are
    // containers for row elements
    var rows = getElementsByClassName('rows');
    if(rows != null) {
      for(var i = 0; i < rows.length; i++) {
        // loop through all the divs contained in the "rows" div
        var divs = rows[i].getElementsByTagName('div');
        for(var j = 0; j < divs.length; j++) {
		      divs[j].onmouseover = function(){ hover(this); };
		      divs[j].onmouseout = function(){ unhover(this); };
		      images = divs[j].getElementsByTagName('img');
		      for(var k = 0; k < images.length; k++) {
		        images[k].onclick = function(){ imageClick(this); }
		      }
        }
      }
    }
  }
}

/**
 * imageClick(element)
 *
 * Hack-ish way to make images in a row div go to the same href that the whole
 * div is linked to. In IE everything in the div works as a link except images.
 */
function imageClick(element) {
  window.location = findParentHref(element);
}

/**
 * findParentHref(element)
 *
 * Traverses up the document tree until it finds the first anchor containing
 * the given element and returns the href for the anchor.
 */
function findParentHref(element) {
  e = element;
  while(e.tagName != 'A' && e.tagName != 'BODY')
    e = e.parentNode;
  e.tagName == 'BODY' ? e = false : null;
  return e.href;
}

/**
 * setLinks()
 *
 * If the page spans rows across multiple pages, the currently selected row
 * needs to be saved when the page changes. This is done via server side code
 * though so setLinks() will find the next and previous page links (should have
 * id's next and previous) and change the link href based on the currently
 * selected row, so when the user goes to another page the currently selected
 * row is not lost or reset.
 *
 * @param string id is the id in the query string to set to the selected row id
 */
function setLinks(id) {
  if(document.getElementById) {
    link_next = document.getElementById('next');
    link_previous = document.getElementById('previous');
  }
  link_id = id;
}

function hrefHover(href, key) { hover(document.getElementById(getQueryValue(getQueryFromString(href.href), key))); }

function hrefUnhover(href, key) {
  unhover(document.getElementById(getQueryValue(getQueryFromString(href.href), key)));
}

function hrefSelect(href, key, target) {
  select(document.getElementById(getQueryValue(getQueryFromString(href.href), key)), target);
}

function hover(element) { element.className = addClass(element, 'mouseover'); }

function unhover(element) { element.className = removeClass(element, 'mouseover'); }

function select(element, target) {
  if(row_selected != null)
    row_selected.className = removeClass(row_selected, 'selected');
  row_selected = element;
    
  element.className = addClass(element, 'selected');
  
  // update page links if they exist
  if(link_next)
    link_next.search = '?' + setQuery(link_id, element.id, link_next.search);
  if(link_previous)
    link_previous.search = '?' + setQuery(link_id, element.id, link_previous.search);
  
  // redirect to selection
  // grab link url
  //var href = element.parentNode;
  //parent.classified ? parent.classified.location = href : window.location = href;
  //preview(element, 'pre', target);
  
  return true;
}
