/* menu ================================================================ */

var menu_last_active = null;
var menu_show_pending = null;
var menu_hide_pending = null;
var menu_visible = false;

function menu_clear_timeouts () {
  if (menu_show_pending) {
    clearTimeout (menu_show_pending);
    menu_show_pending = null;
  }
  if (menu_hide_pending) {
    clearTimeout (menu_hide_pending);
    menu_hide_pending = null;
  }
}

var fix_flash_overlay_bug = false;

var menu_last_move_msecs = false;

function menu_mousemove () {
  menu_last_move_msecs = get_msecs ();
}

function menu_expire () {
  if (menu_visible && menu_last_move_msecs !== false) {
    now = get_msecs ();
    if (now - menu_last_move_msecs > 5000) {
      menu_show (null);
    }
  }
}

function menu_show (target)
{
  var li_menu_item;
  var now, delta;

  now = get_msecs ();
  if (menu_last_active === false) {
    menu_last_active = now;
    delta = 5000;
  } else {
    delta = now - menu_last_active;
  }
  menu_last_active = now;
  menu_last_move_msecs = false;

  if (menu_visible || menu_hide_pending) {
    delta = 0;
  }
  menu_clear_timeouts ();

  li_menu_item = target;
  while (li_menu_item && ! $(li_menu_item).hasClass("menu_item")) {
    li_menu_item = li_menu_item.parentNode;
  }
	
  if (! li_menu_item) {
    $(".menu_item").removeClass("menu_item_active");
    $(".menu_body:animated").stop(false, true);
    $(".menu_body").hide();
    $(".menu_body").unbind ("mousemove");
    menu_visible = false;
  } else {
    var menu_body = $(".menu_body", li_menu_item);

    var top = $(li_menu_item).outerHeight();

    $(menu_body).css("top", top + "px").css("left","0px");

    $(".menu_item").removeClass("menu_item_active");
    $(".menu_body:animated").stop(false, true);
    $(".menu_body").hide();

    menu_visible = false;

    if (delta >= 300) {
      $(menu_body).slideDown ("medium");
    } else {
      $(menu_body).show ();
    }

    $(li_menu_item).addClass ("menu_item_active");

    menu_visible = true;

    $(".menu_body").unbind ("mousemove");
    $(menu_body).mousemove (menu_mousemove);

    if (fix_flash_overlay_bug) {
      $(".video_holder").each(function () {
				$(this).css ("left",  "-5000px");
			      });
    }
  }
}

function menu_hide () {
  menu_clear_timeouts ();
  $(".menu_body").hide();
  $(".menu_item").removeClass("menu_item_active");
  menu_hide_pending = null;
  menu_last_active = get_msecs ();
  menu_visible = false;

  if (fix_flash_overlay_bug) {
    $(".video_holder").each(function () {
				$(this).css ("left", "0px");
			    });
  }
}

function menu_mouse_over (event) {
  menu_clear_timeouts ();

  if (menu_visible) {
    menu_show (event.target);
  } else {
    menu_show_pending = setTimeout (
      function () { menu_show (event.target); },
      200);
  }
}

function menu_mouse_out (event)
{
  menu_clear_timeouts ();
  menu_hide_pending = setTimeout (menu_hide, 200);
}

$(function () 
  {
    $(".menu_item").hover(menu_mouse_over, menu_mouse_out);
    window.setInterval (menu_expire, 100);
  }
);

/* edit markers=========================================================== */

function new_edit_marker_over (event) {
  var module = event.target;

  while (module && ! $(module).hasClass ("editable")) {
    module = module.parentNode;
  }

  if (module) {
    $(".edit_marker", module).show ();
  }
}

function new_edit_marker_out (event) {
  $(".edit_marker").hide();
}

$(function ()
  {
    if (window.enable_edit_marker) {
      $(".editable").hover (new_edit_marker_over, new_edit_marker_out);
    }
  }
);

/* video ================================================================ */

function unent (s) {
	return (s.replace (new RegExp ('&amp;', 'g'), '&'));
}

var video_idx = 0;

function video_on (video) {
  var args = JSON.parse ($(".video_args",video).html());

  if (args == null) {
    return;
  }
  
  var width = $(video).width();
  var height = $(video).height();

  var id = video.id;
  if (! id) {
    video_idx++;
    id = "video_genid" + video_idx;
  }

  $("img", video).hide ();

  var so = new window.SWFObject(args.swf, id, width, height, 8, "#dddddd");
  so.addParam("wmode", "transparent");
  so.addVariable ('file', args.flv);
  so.addVariable ('width', width);
  so.addVariable ('height', height);
  so.addParam ('allowfullscreen', 'true');
  so.addVariable ('autostart', 'true');
  
  if (args.still) {
    so.addVariable ('image', args.still);
  }

  so.addVariable ('streamscript', 'http://v1.apebble.com/flvstream.php');
  so.addVariable ('callback', 'analytics');

  so.write(video);
}

$(function () {
    var query_string = location.search.substring(1);
    if (query_string.match (new RegExp ("autostart=1"))) {
      var elt = $(".video");
      if (elt.size()) {
	elt = elt[0];
	video_on (elt);
      }
    } else {
      $(".video").click(function () { video_on(this); });
    }
  });

/* ================================================================ */


$(function () {
    if ($.browser.mozilla && navigator.userAgent.match ("Linux")) {
      fix_flash_overlay_bug = true;
    }
    if ($.browser.msie) {
      fix_flash_overlay_bug = true;
    }
  });


function google_custom_search () {
  var f = document.getElementById('cse-search-box');
  if (f && f.q) {
    var q = f.q;
    var n = navigator;
    var l = location;
    if (n.platform == 'Win32') {
      q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';
    }
    var b = function() {
      if (q.value == '') {
	q.style.background = '#FFFFFF url(t-lta/google_custom_search_watermark.gif) left no-repeat';
      }
    };
    var f = function() {
      q.style.background = '#ffffff';
    };
    q.onfocus = f;
    q.onblur = b;
    if (!/[&?]q=[^&]/.test(l.search)) {
      b();
    }
  }
}


$(function () {
    if ($("#cse-search-box").size()) {
      if (false) {
	$("<script type='text/javascript' src='http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en'></script>").insertAfter ("body");
      } else {
	google_custom_search ();
      }
    }
});

$(function () {
    $("a[href=#]").css("background","black").css("color","#00ff00");
  });

/* ================================================================ */

function flashvars_encode (obj) {
  var elts = [];
  for (var name in obj) {
    if (obj.hasOwnProperty (name)) {
      elts.push (encodeURIComponent (name) + "=" + encodeURIComponent (obj[name]));
    }
  }
  return (elts.join ("&"));
}

var video_seq = 0;

function select_video (ev) {
  var elt = $(ev.target)[0];
  while (elt && ! $(elt).hasClass ("video_link")) {
    elt = elt.parentNode;
  }
  if (! elt) {
    return (false);
  }

  var video_id = elt.href.replace (new RegExp ('^.*#'), '');

  var args = JSON.parse ($("#"+video_id).html());

  if (! swfobject.hasFlashPlayerVersion ("8")) {
    return (false);
  }
  
  var att = {};
  var par = {};
  var fv = {};

  fv.file = args.flv;
  if (args.streamscript) {
    fv.streamscript = args.streamscript;
  }

  fv.autostart = "true";

  if (false) {
    fv.callback = "analytics";
  }
  
  par.flashvars = flashvars_encode (fv);
  
  att.data = args.swf;
  att.width = args.width;
  att.height = args.height;

  video_seq++;
  var id = "video_div_" + video_seq;
  
  var holder = "#" + args.dest_id;

  $(holder).children().each (function () { dbg ("remove " + $(this)[0].id); swfobject.removeSWF ($(this)[0]); });

  $(holder).html("");
  $(holder).html("<div id='"+id+"'></div>");

  swfobject.createSWF (att, par, id);

  return (false);
}

$(function () {
    $(".video_link").click (select_video);
  });

$(function () {
	  if ($("#filmstrip_wrapper").size()) {
		  window.setInterval ('fix_filmstrip_pos()', 1000);
	  }
  });

