(function() {
var notices = {

WiknicUS:
{
  begin: '7 May 2013 00:00 UTC',
  end: '23 June 2013 00:00 UTC',
  country: 'US',
  text: "Join the [[Wikipedia:Wiknic|Great American Wiknic]], a Wiki-Picnic being celebrated in 10+ US cities around June 22!"
},

LosAngelesWikinic:
{
  begin: '19 May 2013 00:00 UTC',
  end: '23 June 2013 00:00 UTC',
  corners: [ [34.5,-120.0], [33.5,-117.0] ],
  text: "Wikipedians in the Los Angeles metropolitan area are invited to the [[Wikipedia:Meetup/LA/8|Los Angeles Wikinic]] (Wikipedia picnic) at Pan-Pacific Park in Los Angeles on Saturday, June 22, 2013." 
},

KarachiMay13:
{
  begin: '20 May 2013 00:00 UTC',
  end: '27 May 2013 00:00 UTC', 
  corners: [ [23.85, 68], [25.85, 66] ],
  text: "You are invited to join us at Karachi's Wikipedia meetup on May 26. See [[Wikipedia:Meetup/Karachi 2]] for details and to sign up."
},

MinneapolisMay13:
{
  begin: '20 May 2013 00:00 UTC',
  end: '2 June 2013 00:00 UTC', /* repost in mid-June */
  corners: [ [45.2, -93.6], [44.7, -92.8] ],
  text: "You are invited to Minnesota's first monthly Wikipedia meetups on June 1 and July 6 at Minneapolis Central Library. See [[WT:Meetup/Minnesota|the meetup talk page]] for details and to sign up (not required)."
},

WalsallApril13: 
{
   begin: '16 April 2013 12:00 UTC',
   end: '1 June 2013 12:00 UTC',
   corners:[ [51,-5], [55, 1] ],
   text: "A series of [[Wikipedia:GLAM/NAGW|backstage pass tours and editathons]] will be held at The New Art Gallery, Walsall, throughout May and early June 2013. All welcome, but booking essential!" 
},

OxfordJournals13: 
{
   begin: '12 May 2013 12:00 UTC',
   end: '24 May 2013 16:00 UTC',
   corners:[ [50,1.5], [54,-3] ],
   text: "An editathon [[Wikipedia:WikiProject University of Oxford/QueenVictoriasJournals|focusing on the journals of Queen Victoria]] will be held at the Radcliffe Science Library, Oxford, on 24 May. All welcome, but please sign up!" 
},

CapeTownMay13:
{ begin : '18 May 2013 00:00 UTC',
  end : '27 May 2013 00:00 UTC',
  corners : [ [-33.6, 18.16], [-34.32, 19.1] ],
  text : "Join a special Wikipedia meetup at the Mexican Kitchen in the Cape Town City Bowl area at 13 Bloem Street at 11am on Sunday, May 26, 2013 - [[Wikipedia:Meetup/Cape Town#Saturday 26 May 2013|RSVP here!]]"
},

UK130527: /* last 6 chars of ID is date of last amendment in YYMMDD format - change this if making major amendment or adding a meetup; leave alone if minor amendment or removing a meetup */
{
   begin: '26 May 2013 12:00 UTC',
   end: '22 June 2013 15:00 UTC', /* set this to date of last meetup shown */
   corners:[ [60,-9], [50, 2] ],
   text: "Interested in having a chat with fellow Wikipedians? There are forthcoming meetups in: [[m:Meetup/Oxford/5|Oxford, 2 June]]; [[m:Meetup/London/70|London, 16 June]]; and [[m:Meetup/Manchester/18|Manchester, 22 June]]!" /* try to limit this to four meetups, no more than one per town/city, and no more than four weeks in advance; shorten month names to three letters if four meetups are shown */
}

/*******************************************
 *             End of list.
 *           Edit only above!
 *
 * Format is:
 *
 * ID:
 * { begin: 'date',
 *   end: 'date',
 *   corners: [ [lat,lon], [lat,lon] ],
 *   text: 'message'
 * }
 *
 * There also is an option to use country instead of corners.  For example:
 * ID:
 * { begin: 'date',
 *   end: 'date',
 *   country: 'US',
 *   text: 'message'
 * }
 *
 * * There should be commas between the notices, but not after the last one. BE SURE TO ESCAPE YOUR APOSTROPHES (put a backslash in front of it, like this: \')!
 * Use [[meta:w:en:link|link]] format for internal links, so that they work properly on the other projects where the geonotice is displayed.
 *
 *******************************************/
};

function geoWikiLinker (str, page, text) {
  text = text || page;
  return mw.html.element(
    'a', {
      href: mw.util.wikiGetlink( page ),
      title: page
    }, text
  );
}

function addGeonoticeCss() {
  mw.util.addCSS( [
    '#watchlist-message .geonotice {',
      'width:98%;',
      'background:transparent;',
      'text-align:left;',
      'line-height:1.8em;',
    '}',
    '#watchlist-message .geonotice span {',
      'font-size:144.5%;',
    '}',
    '#watchlist-message .geonotice small {',
      'font-style:italic;',
      'margin-left:.5em;',
    '}',
    '#watchlist-message .geonotice small a::before {',
      'content: "[";',
    '}',
    '#watchlist-message .geonotice small a::after {',
      'content: "]";',
    '}'
  ].join( ' ' ) );
}

function hideGeonotice() {
  var parentId = $(this).closest('li').attr('id');
  var date = new Date();

  date.setTime(date.getTime()+8640000000);

  var expireDate = date.toGMTString();

  document.cookie = 'hide' + parentId + '=1; expires=' + expireDate + ';path=/';
  
  $( '#' + parentId ).hide();
  $( '#geonotice-hr' ).hide();

  return false;
}

var firstnotice = true;
var regexForInternalLinks = /\[\[([^{|}\[\]\n]+)(?:\|(.*?))?\]\]/g;

function displayGeonotice(notice) {
  var geonoticeText = notice.text.replace( regexForInternalLinks, geoWikiLinker );

  if (firstnotice) {
    firstnotice = false;
    
    $('#watchlist-message').prepend(
      $( '<hr>' ).attr({ 'id' : 'geonotice-hr' })
    );
    
    addGeonoticeCss();
  }

  $('#watchlist-message').prepend(
    $('<li>')
      .attr({
        'class' : 'geonotice plainlinks',
        'id' : 'geonotice' + notice.id
      })
      .append(
        $( '<span>' )
          .html( geonoticeText )
        )
      .append( $( '<small>' )
        .append(
          $('<a>')
            .text( 'hide' )
            .click( hideGeonotice )
         )
      )
  );
}

if ( Geo !== undefined && notices ) {
  var now = new Date(),
      id, notice, minlat, maxlat, minlon, maxlon,
      startNotice, endNotice;

  for (id in notices) {
    if (!document.cookie.match('hidegeonotice'+id+'=1')) {

      notice = notices[id];
      notice.id = id;

      if (!notice || !notice.begin || !notice.end) {
        continue;
      }

      startNotice = Date.parse(notice.begin);
      endNotice = Date.parse(notice.end);

      if ( now.getTime() > startNotice &&
        now.getTime() < endNotice ) {
          if (notice.country && Geo.country === notice.country) {
            displayGeonotice(notice);
          } else {
            if (notice.corners) {
              minlat = Math.min(notice.corners[0][0], notice.corners[1][0]);
              maxlat = Math.max(notice.corners[0][0], notice.corners[1][0]);
              minlon = Math.min(notice.corners[0][1], notice.corners[1][1]);
              maxlon = Math.max(notice.corners[0][1], notice.corners[1][1]);

              if ( minlat < Geo.lat && Geo.lat < maxlat && minlon < Geo.lon && Geo.lon < maxlon ) {
                displayGeonotice(notice);
              }
            }
          }
      }
    }
  }
}
})();