var username = "";
var searchwait = 0;
var lastsearch = "";
var emptysearch = "";
var lastattsearch = "";
var emptyattsearch = a_slbl;
var lastspeaksearch = "";
var emptyspeaksearch = s_slbl;
var msgonce = false;
var lasttip = false;
var tips = {};
var tooltipwait = 0;

var is_iphone = ((navigator.userAgent.toLowerCase().indexOf("iphone") > -1) || (navigator.userAgent.toLowerCase().indexOf("ipod") > -1));

jQdefer = function(delay, callback) {
	var timer;

	// Return the callback proxy
	return function() {
		// Save the vars for the real callback
		var that = this, args = arguments;

		// Reset the delay
		window.clearTimeout(timer);

		// Delay the real callback
		timer = window.setTimeout(function() {
			callback.apply(that, args);
		}, delay);
	};
};


$(document).ready(function(){
$("span.outer-links a").attr('target', '_blank');
$(".past_block").addClass('clickable')
.click(function(){
	var idx = $(this).attr('rel');
	var status = $(this).attr('rev');

	if(status != 'hidden'){
		$(this).attr('rev','hidden');
		$("tr[rel='past_"+idx+"']").hide();
	} else {
		$(this).attr('rev','');
		$("tr[rel='past_"+idx+"']").show();
	}

})
.click();
});

var delayedHideTooltipTimer = false;
function delayedHideTooltip() {
    if (delayedHideTooltipTimer)
        clearTimeout(delayedHideTooltipTimer);

    delayedHideTooltipTimer = setTimeout(function() {
        $("#tip").hide();
    }, 1000);
}
function delayedHideTooltipCancel() {
    if (delayedHideTooltipTimer) {
        clearTimeout(delayedHideTooltipTimer);
        delayedHideTooltipTimer = false;
    }
}


// main
$(function() {

  $("#searchbox").val(emptysearch);
  $("#searchbox").blur(function() { if($(this).val() == "") $(this).val(emptysearch); });
  $("#searchbox").focus(function() { if($(this).val() == emptysearch) $(this).val(""); });
  var searchEventHandler = jQdefer(500, function(){ searchWord($("#searchbox").val()); });
  $("#searchbox").change(searchEventHandler);
  $("#searchbox").keyup(searchEventHandler);

  $("#searchbox-attendees").val(emptyattsearch);
  $("#searchbox-attendees").blur(function() { if($(this).val() == "") $(this).val(emptyattsearch); });
  $("#searchbox-attendees").focus(function() { if($(this).val() == emptyattsearch) $(this).val(""); });
  var searchEventHandler = jQdefer(500, function(){ searchWordAttendees($("#searchbox-attendees").val()); });
  $("#searchbox-attendees").change(searchEventHandler);
  $("#searchbox-attendees").keyup(searchEventHandler);
  
  $("#searchbox-speakers").val(emptyspeaksearch);
  $("#searchbox-speakers").blur(function() { if($(this).val() == "") $(this).val(emptyspeaksearch); });
  $("#searchbox-speakers").focus(function() { if($(this).val() == emptyspeaksearch) $(this).val(""); });
  var searchEventHandler = jQdefer(500, function(){ searchWordSpeakers($("#searchbox-speakers").val()); });
  $("#searchbox-speakers").change(searchEventHandler);
  $("#searchbox-speakers").keyup(searchEventHandler);
  

    if(!is_iphone) {
        $("#tip").hover(delayedHideTooltipCancel, delayedHideTooltip);
        $("body").click(function() { lasttip = false; delayedHideTooltipCancel(); $("#tip").hide(); });
        // load all tooltips, and store them to the cache
        $.getJSON("/event-get-all-tooltips", function(data, textStatus) { tips = data; });
    }


/*  if($.browser.msie)
  {
    $("ul.lev1 li:has(ul)").hover(function() { $(this).addClass("over"); }, function() { $(this).removeClass("over"); });
    $("ul.lev1 li:has(.scr)").hover(function() { $(this).addClass("over"); }, function() { $(this).removeClass("over"); });
    $("ul#menu li:has(ul)").hover(function() { $(this).addClass("over"); }, function() { $(this).removeClass("over"); });
  }

  if((typeof(no_resize) == "undefined" || !no_resize) && !is_iphone && !$.browser.msie) // all except ie/iphone
  {
    $(window).resize(function() {
      $("#pagetext").css("margin-top", $("#header").height() - 70 + 10); // -(padding-top) + gap
    });
    $(window).resize();
  }
*/
});

function saveEvent(lnk) {
  if(username != "")
  {
    if(lnk.id)
    {
      if($(lnk).hasClass("sub"))
      {
        $(lnk).removeClass("sub");
        //lnk.innerHTML = "ATTEND";
        lnk.innerHTML = g_abl;
        $.get("/event-set?del=" + lnk.id);
      }
      else
      {
        $(lnk).addClass("sub");
        //lnk.innerHTML = "LEAVE";
        lnk.innerHTML = g_lbl;
        $.get("/event-set?add=" + lnk.id);
      }
    }
    else
    {
      var id = lnk;
      var prli = $("#" + id).parent();
      if(prli.hasClass("sub"))
      {
        prli.removeClass("sub");
        $.get("/event-set?del=" + id);
      }
      else
      {
        prli.addClass("sub");
        $.get("/event-set?add=" + id);
      }
    }
  }
  else if(!msgonce)
  {
    alert(L10n['js::signin']);
    msgonce = true;
  }
}

function searchTrigger(wrd)
{
  clearTimeout(searchwait);
  searchwait = setTimeout(function() { searchWord(wrd); }, 350);
}

function searchWord(wrd)
{
  var wrd = $.trim(wrd);
  if(wrd == emptysearch) wrd = "";
  if ('' == wrd) {
    window.location = window.location;
  }
  if(lastsearch == wrd) return;
  lastsearch = wrd;

  $("#mainsched").html("<div class='message'>" + L10n['js::searching'] + "...</div>");
  $.get("/schedule?searchword=" + wrd, function(ret) { $("#mainsched").html(ret); });
}

function searchWordAttendees(wrd)
{
  var wrd = $.trim(wrd);
  if(wrd == emptyattsearch) wrd = "";
  if ('' == wrd) {
    window.location = window.location;
  }
  if(lastattsearch == wrd) {
    return;
  }
  
  lastattsearch = wrd;

  $("table.attendeelist").html("<tr><td><div class='message'>" + L10n['js::searching'] + "...</div></td></tr>");
  $.get("/attendees?searchword=" + wrd, function(ret) { $("table.attendeelist").html(ret); redrawFBML() });
}

function searchWordSpeakers(wrd)
{
  var wrd = $.trim(wrd);
  if(wrd == emptyspeaksearch) wrd = "";
  if ('' == wrd) {
    window.location = window.location;
  }
  if(lastspeaksearch == wrd) {
    return;
  }
  
  lastspeaksearch = wrd;

  $("table.attendeelist").html("<tr><td><div class='message'>" + L10n['js::searching'] + "...</div></td></tr>");
  $.get("/speakers?searchword=" + wrd, function(ret) { $("table.attendeelist").html(ret); redrawFBML() });
}

// date picker related
function setupDatePicker(es, ed)
{
  var cal;
  var $this;

  var checkForMouseout = function(event)
  {
    var el = event.target;
    while (true){
      if (el == cal || el == document.getElementById("toolbar")) {
        return true;
      } else if (el == document) {
        $this.dpClose();
        return false;
      } else {
        el = $(el).parent()[0];
      }
    }
  };

  Date.format = 'mm/dd/yyyy';

  $('.date-pick').datePicker({
    startDate:es,
    endDate:ed
  }).bind("dateSelected", function(e, sd, td) {
    document.location.href = "/cal/" + sd.getFullYear() + (sd.getMonth()+1 < 10 ? "0" : "") + (sd.getMonth()+1) + (sd.getDate() < 10 ? "0" : "") + sd.getDate();
  }).bind(
    'dpDisplayed',
    function(event, datePickerDiv)
    {
      cal = datePickerDiv;
      $this = $(this);
      $(document).bind(
        'mouseover',
        checkForMouseout
      );
    }
  ).bind(
    'dpClosed',
    function(event, selected)
    {
      $(document).unbind(
        'mouseover',
        checkForMouseout
      );
    }
  ).dpSetOffset($("#header").height(), 0);
  $("#caldatebtn").click(function() { $('#caldate').dpDisplay(); return false; }).mouseover(function() { $('#caldate').dpDisplay(); });
  $("#toolbar li.lev1").mouseout(function() {
    if(!$(this).hasClass("ldate"))
      $('#caldate').dpClose();
  });
}

function showToolTip(lnk)
{
  var el = $(lnk);
  var tx = el.position().left + 1;
  tx = Math.min(tx, $(window).width() - 440);
  var ty = el.position().top + el.height() + ($.browser.mozilla ? 3 : ($.browser.safari ? 4 : -1));
  var id = el.find("a.name").attr("id");

  delayedHideTooltipCancel();

  if(tips[id] == undefined)
  {
    clearTimeout(tooltipwait);
    tooltipwait = setTimeout(function() {
      $("#tip").html(L10n['js::loading']).show().css({left: tx + "px", top: ty + "px"});
      $.get("/event-get?tooltip=" + id, function(ret) { tips[id] = ret; $("#tip").html(ret); });
    }, 400);
  }
  else
  {
    $("#tip").html(tips[id]).show().css({left: tx + "px", top: ty + "px"});
    $("#tip span.outer-links a").attr('target', '_blank');
  }
}

function showToolTipMobile(lnk)
{
  var el = $(lnk);
  var id = el.find("a.name").attr("id");
  if(lasttip == id)
  {
    lasttip = false;
    $("#tip").hide();
    return;
  }

  lasttip = id;
  var tx = el.offset().left + 1;
  tx = Math.min(tx, $(window).width() - 440) + 130;
  var ty = el.offset().top + el.height() + 22;

  if(tips[id] == undefined)
  {
    $("#tip").html(L10n['js::loading']).show().css({left: tx + "px", top: ty + "px"});
    $.get("/event-get?tooltip=" + id, function(ret) { tips[id] = ret; $("#tip").html(ret); });
  }
  else
  {
    $("#tip").html(tips[id]).show().css({left: tx + "px", top: ty + "px"});
  }
}

function toggleInfo(lnk, id)
{
  if($(lnk).html().substr(0, 4) == L10n['js::more'])
  {
    $(lnk).html("<< " + L10n['js::less']);
    if($("#infobox_" + id).html() == "")
    {
      $("#infobox_" + id).html("<b>" + L10n['js::loading'] + "</b>");
      $("#inforow_" + id).show();
      $.get("/event-get?more=" + id, function(ret) { $("#infobox_" + id).html(ret); });
    }
    else
    {
      $("#inforow_" + id).show();
    }
  }
  else
  {
    $(lnk).html(L10n['js::more'] + " >>");
    $("#inforow_" + id).hide();
  }

  return false;
}

function printme(text)
{
  text=document;
  print(text);
}

$(document).ready(function() {
  $('#embed').hide();
  $('a#embed-toggle').click(function() {
    $('#embed').toggle(100);
    return false;
  });
});
