var vurl="";
var vkey="";
var qwindow = '';
var qwindow_height = 300;
var qwindow_width = 550;

/**
 * attach a function to object event
 *
 * @param object or id
 * @param string event type (load, mouseover, focus, ...)
 * @param function to be attached
 */
function addEvent(obj, type, fn)
{
  if (obj.attachEvent) {
    obj['e' + type + fn] = fn;
    obj[type + fn] = function() {obj['e' + type + fn](window.event);}
    obj.attachEvent('on' + type, obj[type + fn]);
  } else {
    obj.addEventListener(type, fn, false);
  }
}

/**
 * detach/remove a function from an object event
 *
 * @param object or id
 * @param event type (load, mouseover, focus, ...)
 * @param function naem of function to be attached
 */
function removeEvent(obj, type, fn)
{
  if (obj.detachEvent) {
    obj.detachEvent('on' + type, obj[type + fn]);
    obj[type + fn] = null;
  } else {
    obj.removeEventListener(type, fn, false);
  }
}


function FP_windowSetFocus()
{
	document.getElementById('query').focus();
}

function FP_windowResize()
{
  // for Gecko
  if (typeof(self.sizeToContent) == 'function') {
    self.sizeToContent();
    //self.scrollbars.visible = false;
    // give some more space ... to prevent 'fli(pp/ck)ing'
    self.resizeBy(10, 50);
    return;
  }

  // for IE, Opera
  if (document.getElementById && typeof(document.getElementById('ss_wp')) != 'undefined') {

    // get content size
    var newWidth  = document.getElementById('ss_wp').offsetWidth;
    var newHeight = document.getElementById('ss_wp').offsetHeight;

    // set size to contentsize
    // plus some offset for scrollbars, borders, statusbar, menus ...
    self.resizeTo(newWidth + 25, newHeight + 55);
//    self.resizeTo(newWidth + 15, 750);
  }
}

/**
 * brings query window to front and inserts query to be edited
 */
function focus_window()
{
	vurl=jQuery("select#url option:selected").val();
	vkey=jQuery("#key").val();

  if ( !qwindow || qwindow.closed || !qwindow.location) {
	  open_qwindow();
  } else {
    //var qwindow = qwindow;
    qwindow.focus();
  }
  return true;
}

function open_qwindow( url ) {
  if ( ! url ) {
    url = '/search/match?_url='+encodeURIComponent(vurl)+'&_key='+encodeURIComponent(vkey);
  }

  if (!qwindow.closed && qwindow.location) {
    goTo( url, 'query' );
    qwindow.focus();
  } else {
    qwindow = window.open( url, '',
        'toolbar=0,location=0,directories=0,status=1,menubar=0,' +
        'scrollbars=yes,resizable=yes,' +
        'width=' + qwindow_width + ',' +
        'height=' + qwindow_height );
    qwindow.opener.name="main_content";
  }

  if ( ! qwindow.opener ) {
   qwindow.opener = window.window;
  }

  if ( window.focus ) {
    qwindow.focus();
  }
  return true;
}

/**
 * opens new url in target frame, with default being left frame
 * valid is 'main' and 'qwindow' all others leads to 'left'
 *
 * @param    string    targeturl    new url to load
 * @param    string    target       frame where to load the new url
 */
function goTo(targeturl, target) {
  //alert(targeturl);
  if ( target == 'main' ) {
    target = window.frame_content;
  } else if ( target == 'query' ) {
    target = qwindow;
  } else if ( ! target ) {
    target = window.frame_navigation;
  }

  return true;
}
