function hideElem(id) {
  var elem = document.getElementById(id);
  if (elem) {
    if (elem.style.display != 'none') {
      elem.style.display = 'none';
    }
  }
}
function showHideElem(id) {
  var elem = document.getElementById(id);
  if (elem) {
    if (elem.style.display != 'none') {
      elem.style.display = 'none';
    } else {
      elem.style.display = 'block';
    }
  }
}

function showWait(event) {
  var query = document.getElementById('srchfrm_q').value;
  document.getElementById('embed_ifr').src = '/mashups/tickr_wait?q=' + escape(query);
}

function stopEvent(e) {
  YAHOO.util.Event.stopEvent(e);
}

function showInit() {
  document.getElementById('embed_ifr').src = '/mashups/tickr_init';
}

function showError() {
  document.getElementById('embed_ifr').src = '/mashups/tickr_error';
}

function handleSubmit(event) {
  stopEvent(event);
  var q = document.getElementById("srchfrm_q").value;
  var w = ( document.getElementById("srchfrm_w_u").checked ? "u" : "q" );
  var l = document.getElementById("srchfrm_l").value;
  doSearch(q,w,l);
}

function doSearch(q,w,l) {
  showWait();
  q=q.replace(/ /g, '_');
  q=escape(q);
  q=q.replace(/%2C/g, ',');

  l=l.replace(/ /g, '_');
  l=escape(l);
  l=l.replace(/%2C/g, ',');

  if (q == '') {
    q = '_';
  }

  YAHOO.util.Dom.addClass('srchfrm_submit', "loading");
  if (l) {
    window.location.href= [ "/mashups/tickr/", ( w == 'u' ? 'user/' : '' ), q, '/', l ].join("");
  } else {
    window.location.href= [ "/mashups/tickr/", ( w == 'u' ? 'user/' : '' ), q ].join("");
  }
}

function doRandom() {
  var querylist = [
    { q:"war", l:"Africa", w:'q' },
    { q:"garden gnome", l:"World", w:'q' },
    { q:"gondola", l:"Venice, Italy", w:'q' },
    { q:"soldier", l:"Iraq", w:'q' },
    { q:"September 11", l:"New York, NY", w:'q' },
    { q:"northern lights", l:"", w:'q' },
    { q:"barack obama", l:"", w:'q' },
    { q:"earthquakes", l:"", w:'q' },
    { q:"Hillary Clinton", l:"", w:'q' },
    { q:"John McCain", l:"", w:'q' },
    { q:"Natalie Portman", l:"", w:'q' },
    { q:"Britney Spears", l:"Las Vegas, NV", w:'q' },
    { q:"Grateful Dead", l:"World", w:'q' },
    { q:"hippies", l:"", w:'q' },
    { q:"", l:"Central Park, New York, NY", w:'q' },
    { q:"Leah Culver", l:"", w:'q' },
    { q:"Kevin Rose", l:"", w:'q' },
    { q:"Alcatraz", l:"", w:'q' },
    { q:"Burning man", l:"", w:'q' },
    { q:"Monks", l:"", w:'q' },
    { q:"art", l:"World", w:'q' },
    { q:"paintings", l:"France", w:'q' },
    { q:"architecture", l:"Rome", w:'q' },
    { q:"sunset", l:"World", w:'q' },
    { q:"glaciers", l:"Alaska", w:'q' },
    { q:"minifig", l:"", w:'u' },
    { q:"grace.amazing", l:"", w:'u' },
    { q:"gazzigger", l:"", w:'u' },
    { q:"Burning Man", l:"", w:'q' },
    { q:"Guantanamo Bay", l:"", w:'q' },
    { q:"opera house", l:"Sydney", w:'q' },
    { q:"ufo sightings", l:"", w:'q' },
    { q:"engrish", l:"World", w:'q' },
    { q:"bonnaroo", l:"", w:'q' },
    { q:"Worlds Fair", l:"", w:'q' },
    { q:"Christmas", l:"World", w:'q' },
    { q:"Diwali", l:"", w:'q' },
    { q:"protest", l:"World", w:'q' },
    { q:"peta protest", l:"", w:'q' },
    { q:"new years", l:"World", w:'q' },
    { q:"Blue Angels", l:"United States", w:'q' }
  ];

  var idx = parseInt( Math.random() * querylist.length );
  var q = querylist[idx];
  doSearch(q.q, q.w, q.l);
}

