// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// name/init_type.js
//
// copyright (c) 2009 drow <drow@bin.sh>
// all rights reserved.

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// configuration

var google_query = /http:..www.google.+q=([^&]+)/i;
var bing_query = /http:..www.bing.+q=([^&]+)/i;
var msn_query = /http:..search.msn.+q=([^&]+)/i;
var yahoo_query = /http:..search.yahoo.+p=([^&]+)/i;
var misc_query = /http:..(.+)/i;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// initialize type by query

function init_type (form) {
  var form_elem = $(form).elements;
  var query = get_query();
    if (! query) return false;
  var regex = query.map(build_regex);
  var match = search_types(form_elem,regex);
    if (! match) return false;

  if (sub_sel = form_elem[match['type']]) {
    setv(sub_sel,match['sub_type']);
  }
  setv(form_elem['type'],match['type']);

  return true;
}
function build_regex (word) {
  return {
    'full':     new RegExp('\\b' + word + '\\b','i'),
    'first':    new RegExp('\\b' + word        ,'i'),
    'last':     new RegExp(        word + '\\b','i'),
    'misc':     new RegExp(        word        ,'i')
  };
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// get query

function get_query () {
  var string; if (location.search) {
    var query = location.search.toQueryParams();

    if (query['type']) {
      string = query['type'];
    } else {
      string = location.search.substring(1);
    }
  } else if (document.referrer) {
    if (match = google_query.exec(document.referrer)) {
      string = match[1];
    } else if (match = bing_query.exec(document.referrer)) {
      string = match[1];
    } else if (match = msn_query.exec(document.referrer)) {
      string = match[1];
    } else if (match = yahoo_query.exec(document.referrer)) {
      string = match[1];
    } else if (match = misc_query.exec(document.referrer)) {
      string = match[1];
    }
  }
  if (string) {
    return tokenize(string);
  }
  return false;
}
function tokenize (string) {
  string = string.replace(/%\w\w/g,' ');
  string = string.replace(/\W+/g,' ');
  string = string.replace(/_+/g,' ');
  string = string.replace(/\s+/g,' ');
  string = string.replace(/^ /,'');
  string = string.replace(/ $/,'');

  return string.split(' ').uniq().without('name','generator');
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// search types

function search_types (form_elem,regex) {
  var list = { };

  $A(form_elem['type'].options).each(function (option) {
      var text = opt_text(option);
    var score; if (score = score_opt(text,regex)) {
      list[option.value + ':null'] = score;
    }
    $A(form_elem[option.value].options).each(function (sub_opt) {
        var sub_text = text + ' ' + opt_text(sub_opt);
      if (score = score_opt(sub_text,regex)) {
        list[option.value + ':' + sub_opt.value] = score;
      }
    });
  });
  var match; if (match = high_score(list)) {
    var list = match.split(':');
    return { 'type': list[0], 'sub_type': list[1] };
  }
}
function opt_text (option) {
  return option.value + ' ' + option.text;
}
function score_opt (text,regex) {
  var x = 1.0;
  var score = 0;

  tokenize(text).each(function (word) {
    regex.each(function (pattern) {
      if (word.match(pattern['full'])) {
        score += 1.0 * x;
      } else if (word.match(pattern['first'])) {
        score += 0.9 * x;
      } else if (word.match(pattern['last'])) {
        score += 0.8 * x;
      } else if (word.match(pattern['misc'])) {
        score += 0.7 * x;
      }
      x *= 0.9;
    });
  });
  return score;
}
function high_score (list) {
  var max = 0;
  var match = false;

  $H(list).each(function (pair) {
    if (pair.value > max) {
      match = pair.key; max = pair.value;
    }
  });
  return match;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

