function e(id) { return document.getElementById(id); }

function jumpToAnchor(anchor) { window.location = String(window.location).replace(/\#.*$/, "") + "#" + anchor; }

function getXmlHttpRequestObject()
{
  if(window.XMLHttpRequest) return new XMLHttpRequest();
  if(window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  return null;
}

var req = getXmlHttpRequestObject();
var ajax_queue = [];

function eval_ajax()
{
  if(req.readyState == 4)
  {
    eval(req.responseText);

    if(ajax_queue.length)
    {
      var q = ajax_queue.pop();

      req.open("GET", '/ajax-functions?cmd=' + encodeURI(q.cmd) + '&' + q.args, true);
      req.onreadystatechange = eval_ajax;
      req.send(null);
    }
  }
}

function ajax_request(cmd, args)
{
  if(req.readyState == 0 || req.readyState == 4)
  {
    req.open("GET", '/ajax-functions?cmd=' + encodeURI(cmd) + '&' + args, true);
    req.onreadystatechange = eval_ajax;
    req.send(null);
  }
  else if(ajax_queue.length < 20)
  {
    var q = new Object();

    q.cmd = cmd;
    q.args = args;

    ajax_queue.push(q);
  }
}

function add_to_playlist(slnr)
{
  ajax_request('add-to-playlist', 'slnr=' + encodeURI(slnr));
}

function remove_from_playlist(slnr)
{
  ajax_request('remove-from-playlist', 'slnr=' + encodeURI(slnr));
}

function add_to_library(slnr)
{
  ajax_request('add-to-library', 'slnr=' + encodeURI(slnr));
}

function remove_from_library(slnr)
{
  ajax_request('remove-from-library', 'slnr=' + encodeURI(slnr));
}

function add_friend(name)
{
  ajax_request('add-friend', 'name=' + encodeURI(name));
}

function remove_friend(name)
{
  ajax_request('remove-friend', 'name=' + encodeURI(name));
}

var playlist_updated = function()
{
  var playlist = e('playlist');
  var list = playlist.getElementsByTagName('span');
  var i;
  var list_str = "";

  for(i = 0; i < list.length; ++i)
  {
    var slnr = list[i].id.split('_')[1];

    if(i != 0)
      list_str += ':';

    list_str += slnr;
  }

  ajax_request('reorder-playlist', 'list=' + encodeURI(list_str));
}

var library_updated = function()
{
  var library = e('library');
  var list = library.getElementsByTagName('span');
  var i;
  var list_str = "";

  for(i = 0; i < list.length; ++i)
  {
    var slnr = list[i].id.split('_')[1];

    if(i != 0)
      list_str += ':';

    list_str += slnr;
  }

  ajax_request('reorder-library', 'list=' + encodeURI(list_str));
}

function preview_post(id)
{
  e('preview-' + id).innerHTML = 'Lager forh&aring;ndsvisning...';
  $('preview-container-' + id).show();
  $('edit-' + id).hide();

  ajax_request('preview-wiki', 'element=preview-' + encodeURI(id) + '&text=' + encodeURI(e('text-' + id).value));
}

function cancel_post(id)
{
  $('edit-' + id).show();
  $('preview-container-' + id).hide();
}

function reply_post(id)
{
  $('form-' + id).toggle();
}

var current_anchor = null;

function update_anchor()
{
  if(current_anchor != document.location.hash)
  {
    current_anchor = document.location.hash;

    if(!current_anchor)
      return;

    var args = current_anchor.substring(1).split(':');
    var i;

    for(i = 0; i < args.length; ++i)
    {
      var res = /msg-([0-9]*)/.exec(args[i]);

      if(res != null)
      {
        var msg = res[1];

        if(e('placeholder-' + msg))
          ajax_request('fetch-post', 'post=' + msg);
      }
      else if(args[i] == 'article')
      {
        if($('plain'))
          $('plain').hide();
        $('article').show();
      }
      else if(args[i] == 'plain')
      {
        if($('article'))
          $('article').hide();
        $('plain').show();
      }
    }

    if(args[0] == 'page')
    {
      page = parseInt(args[1]) - 1;
      updateHotels();
    }
  }
}

var active_suggestions = new Array();
var suggest_cache = new Array();

function movie_suggest_response(id, query, html)
{
  suggest_cache[query] = html;

  if(e(id).value == query)
  {
    e(id + '-suggestions').innerHTML = html;
    e(id + '-suggestions').style.display = 'block';
  }
}

function suggest_movie_handler(id, query)
{
  id = decodeURI(id);
  query = decodeURI(query);

  var input = e(id);
  var suggestions = e(id + '-suggestions');

  if(input.value != query)
    return;

  query = query.replace(/^\s+$/, '');

  if(!query.length)
  {
    suggestions.style.display = 'none';

    return;
  }

  if(query == active_suggestions[id])
    return;

  if(suggest_cache[query])
  {
    suggestions.innerHTML = suggest_cache[query];
    suggestions.style.display = 'block';

    return;
  }

  active_suggestions[id] = query;

  ajax_request('suggest-movie', 'id=' + encodeURI(id) + '&q=' + encodeURI(query));
}

function suggest_movie(id)
{
  setTimeout("suggest_movie_handler('" + encodeURI(id) + "', '" + encodeURI(e(id).value) + "')", 200);
  check_suggest_ok(id);
}

function select_movie(id, slnr, title)
{
  e(id + '-suggestions').style.display = 'none';
  e(id + '-slnr').value = slnr;
  e(id + '-title').value = title;
  e(id).value = title;
  e(id).className = 'ok';
}

function check_suggest_ok(id)
{
  var el = e(id);

  if(e(id + '-slnr').value.length && el.value == e(id + '-title').value)
  {
    el.className = 'ok';
    return;
  }

  if(!el.value.length)
    el.className = '';
  else
    el.className = 'err';

  e(id + '-slnr').value = '';
  e(id + '-title').value = '';
}

/* Image cropping */

var image_scroll_id;
var image_scroll_capture = 0;
var image_scroll_last_x;
var image_scroll_last_y;
var image_scroll_mouse_x;
var image_scroll_mouse_y;
var image_scroll_position_x = 0;
var image_scroll_position_y = 0;
var image_scroll_orig_position_x = 0;
var image_scroll_orig_position_y = 0;

function image_scroll_update()
{
  var offset;

  offset = e(image_scroll_id + '-offset');

  if(offset)
    offset.value = -image_scroll_position_y;

  e(image_scroll_id + '-preview').style.marginTop = image_scroll_position_y + 'px';

  if(image_scroll_capture)
    setTimeout('image_scroll_update()', 10);
}

function image_scroll_start(event, id)
{
  image_scroll_id = id;
  image_scroll_capture = 1;

  if(event)
  {
    if(event.preventDefault)
      event.preventDefault();
  }

  image_scroll_orig_position_x = image_scroll_position_x;
  image_scroll_orig_position_y = image_scroll_position_y;

  image_scroll_last_x = image_scroll_mouse_x;
  image_scroll_last_y = image_scroll_mouse_y;

  image_scroll_update();
}

function image_scroll_end()
{
  if(image_scroll_capture)
  {
    image_scroll_capture = 0;

    if(image_scroll_orig_position_y != image_scroll_position_y && image_scroll_id == 'top-image')
      ajax_request('set-top-image-offset', 'slnr=' + encodeURI(slnr) + '&offset=' + String(-image_scroll_position_y));
  }
}

function image_scroll_mouse_move(event)
{
  if(!event)
    event = window.event;

  image_scroll_mouse_x = event.pageX;
  image_scroll_mouse_y = event.pageY;

  if(image_scroll_capture)
  {
    image_scroll_position_x += image_scroll_mouse_x - image_scroll_last_x;
    image_scroll_position_y += image_scroll_mouse_y - image_scroll_last_y;

    if(image_scroll_position_y > 0)
      image_scroll_position_y = 0;

    image_scroll_last_x = image_scroll_mouse_x;
    image_scroll_last_y = image_scroll_mouse_y;
  }
}

document.onmousemove = image_scroll_mouse_move;
document.onmouseup = image_scroll_end;

var alt_images = new Array();
var alt_image_offset = 0;

function link_preview()
{
  e('preview-link').disabled = true;
  e('submit-link').disabled = false;
  e('progress-0').style.visibility = 'visible';
  ajax_request("preview-link", "url=" + encodeURI(e("url").value));
}

function link_back()
{
  e("preview-link").disabled = false;
  e('progress-0').style.visibility = 'hidden';
  $("post-1").hide();
  $("post-0").show();
}
function link_submit()
{
  e('submit-link').disabled = true;

  var args = "url=" + encodeURI(e("url").value)
           + "&title=" + encodeURI(e("title").value)
           + "&summary=" + encodeURI(e("summary").value);

  if(alt_images.length)
    args += "&thumbnail=" + alt_images[alt_image_offset];

  ajax_request("submit-link", args);
}

function new_link()
{
  $('post-0').hide();
  $('post-1').show();

  alt_image_offset = 0;

  if(!alt_images.length)
  {
    e('thumbnail').style.visibility = 'hidden';
    e('thumbnail-chooser').style.visibility = 'hidden';

    return;
  }
  else
  {
    e('thumbnail').style.visibility = 'visible';
    e('thumbnail-chooser').style.visibility = 'visible';
    e('thumbnail').src = alt_images[0];
    e('thumbnail-chooser-text').innerHTML = '1 av ' + alt_images.length;
  }
}

function link_thumbnail_next()
{
  if(alt_image_offset + 1 < alt_images.length)
  {
    ++alt_image_offset;
    e('thumbnail').src = alt_images[alt_image_offset];
    e('thumbnail-chooser-text').innerHTML = (alt_image_offset + 1) + ' av ' + alt_images.length;
  }
}

function link_thumbnail_previous()
{
  if(alt_image_offset > 0)
  {
    --alt_image_offset;
    e('thumbnail').src = alt_images[alt_image_offset];
    e('thumbnail-chooser-text').innerHTML = (alt_image_offset + 1) + ' av ' + alt_images.length;
  }
}
