

/* Name space for Pecha Kucha */

var pechakucha = new Object();


/* Slide Show class for common use 
 *   new pechakucha.FlickrSlideshow([<ul> element wrapping the slideshow], [flickr photoset id], [rotation time in ms]).run();
 */
pechakucha.FlickrSlideshow = function(element, photosetId, speed) {
  this.requestUrl = '/night/flickr/photosets-get-photos';
  this.photosetId = photosetId;
  this.element = element;
  this.speed = speed;
  this.currentPhoto = 0;
  this.total = 0;
}

pechakucha.FlickrSlideshow.prototype = {
  run: function() {
    var element = this.element;
    var _this = this;
    $.post(this.requestUrl, {photoset_id: this.photosetId}, function(rsp) {
      element.append(rsp);
      _this.total = $('li', element).length;
    }); 
    setTimeout(function() { _this.fade(); }, _this.speed);
  },
  fade: function () {
    $($('li', this.element)[this.nextOf(this.currentPhoto)]).show('slow');
    $($('li', this.element)[this.currentPhoto]).hide();
    this.gotoNext();
    var _this = this;
    setTimeout(function() { _this.fade(); }, _this.speed);
  },
  gotoNext: function() {
    this.currentPhoto = this.nextOf(this.currentPhoto);
  },
  nextOf: function(index) {
    index++;
    if (index == this.total) index = 0;
    return index;
  }
}


/* Ajax login */

$(function() {
  
  $("#login-dialog").dialog({
    dialogClass: 'login',
    bgiframe: true,
    modal: true,
    title: 'Login', // This will not be shown.
    width: 600,
    height: 300,
    autoOpen: false
  });
  $('.login-link').click(function() {
    $("#login-dialog").dialog('open');
    return false;
  });
  
  $('#login-dialog .close').live('click', function() {
    $('#login-dialog').dialog('close');
    return false;
  });
  
  $('#login-dialog input:submit').live('click', function() {
    var form = $(this).parents('form');
    $.ajax({
      url: form.attr('action'),
      data: form.serialize(),
      type: 'post',
      dataType: 'html',
      complete: function(rsp) {
        if ('205' == rsp.status) {
          location.reload();
        } else if ($('#signin_username').siblings('.error_list').length) {
          $('#signin_username').siblings('.error_list').replaceWith(rsp.responseText);
        } else {
          $('#signin_username').after(rsp.responseText);
        }
      }
    });
    return false;
  });
  
  $('.ui-widget-overlay').live('click', function() {
    $('#login-dialog').dialog('close');
  });
});



/* Fancy effect on the upcoming events list */

$(function() {
  $('.city-block a').hover(
    function() {
      $('.city-image', $(this)).slideUp(300);
    },
    function() {
      $('.city-image', $(this)).slideDown(300, 'easeInQuad');
    }
  );
  
  /* *** */
  
  // all-cities
  var search = $('<input type="text" />').attr("id", "search-box-field");
  var lbl    = $('<label></label>').attr("for", "search-box-field").text("Filter");
  var wrap   = $('<div></div>').addClass('search-box').append(lbl).append(search);
  $("#all-cities h2").append(wrap);
  search.keyup(function(){
    var t = search.val().toLowerCase();
    var reg = new RegExp(""+t); 
    $("#city-list span").each(function(){
      var c = $("a", $(this)).text().toLowerCase();
      if(c.match(reg) || t=="") {
        $(this).show()
      }else{
        $(this).hide()
      }
    })
  })
  
  /* *** */
  
});




/* Toggle button on the newsletters setting page */

/*
$(function() {
  $('div#other-cities').hide();
  $('a#hide-other-cities').hide();
  
  $('a#show-other-cities').click(function() {
    $(this).hide();
    $('a#hide-other-cities').show();
    $('div#other-cities').slideDown('slow');
  });
  $('a#hide-other-cities').click(function() {
    $(this).hide();
    $('a#show-other-cities').show();
    $('div#other-cities').slideUp('slow');
  });
});
*/



function showMapEventDetail(eid, latlng)
{
  e = $("#event-"+eid).clone();
  $("div#mapCityEventDetail").empty().append( e );
  
  //var t = $(".city-name", e).text(); t = t.split("#")[0];
  $("div#mapCityEventDetail").prev().html('<span class="no-number">details</span>');
  
  clearInterval(ITID);
  initCounters();
  //latlng = new GLatLng(lat, lng);
  map.panTo(latlng);
}

$(document).ready(function(){
  /*
  $('a.pres-tmb-link').mouseover(function(){
    $(this).find('span.description').slideDown('fast');
  });
  $('a.pres-tmb-link span.description').mouseout(function(){
    $(this).slideUp('fast');
  });
  */
  setTimeout(function(){$('p.edit-message').fadeOut()}, 5000);
});

