var messageLinks = new Array();
var messageIDs = new Array();
var currentID = 0;

function lovelyMessages() {
  // 0. Pull most recent messages
    $('body')
      .html('')
      .load('/enotes/showfolder.aspx?folder=Inbox&page=1 form#Form1:first', function(data) {
        $('form#Form1').hide();
    
    // 1. Grab all links that contain send_short_message.phtml
      $('a[href*=read.aspx]').each(function() {
        messageLinks.push($(this).attr('href'));
      });
      $('input[name*=chkID]').each(function() {
        messageIDs.push($(this).val());
      });
    
    // 2. screen off page
      $('<div/>')
        .attr('id','screen')
        .addClass('pop')
        .css({
          display:'block',
          position:'fixed',
          top:'0',
          left:'0',
          right:'0',
          bottom:'0',
          background:'#fff'
        })
        .appendTo($('body'));

      $('<div/>')
        .attr('id','lightbox')
        .addClass('pop')
        .css({
          display:'block',
          position:'relative',
          width:'900px',
          margin:'0 auto 0 auto',
          padding:'.5em',
          background:'#fff'
        })
        .appendTo($('body'));
      
      $('body').append('<iframe src="" style="display:none;" name="xss" id="xss"></iframe>');
		
		$('head').append('<style type="text/css">* {margin: 0;padding: 0;font-size: 1em;border: 0 none;}body {text-align: center;}#lightbox {text-align: left;}label, textarea, #BcEnotesReply1_txtSubject{display: block;}input, textarea {font-family: helvetica, arial, sans-serif;border: 1px solid #ccc;padding: .25em;margin: 0 0 .25em 1em;}textarea {height: 20em; width: 90%;}form {width: 60%;}#pics {position: absolute;top: 2em;width: 41%;right: 1em;}#pics img {max-width: 100%;}#pics span {margin-bottom: 1em;display:block;}</style>');
    
    // 3. load first link into lightbox
      loadMessage(currentID);
  });
}

function loadMessage(messID) {
  $('#lightbox')
    .load('/enotes/reply.aspx?id=' + messageIDs[messID] + ' form:first', function() {
		
		// 1. Get userid
		var userid = $('a[href*=viewprofile]').eq(1).attr('href').split('=')[1];
		
		// 2. load pics
		$('<div/>')
			.attr('id','pics')
			.load('/galleries/memberphotos.aspx?page=1&id=' + userid + ' img.ZoomIn,a[href*=viewphoto]', function() {
				// that stupid hash-check is so annoying and prevents URL guessing.  Touche, BiggerCity developers, Touche.
				$('#pics img.ZoomIn')
					.removeAttr('height')
					.removeAttr('width')
					.attr('src', '/media/pub/index/' + $('#pics img.ZoomIn').attr('src').split('index/')[1])
					.wrap('<span></span>');
				
				$('#pics a').click(function() {
					var url = $(this).attr('href');
					$('#pics span')
						.load(url + ' img[src*=prot]');
					
					return false;
				});
			})
			.appendTo($('#lightbox'));
		
		// 3. fabu-lize the form
		$('#lightbox form a[href*=viewprofile]:second, #lightbox form input, #lightbox form textarea').appendTo($('#lightbox form'));
		$('#lightbox form table, #BcEnotesReply1_cbkSaveEnote, #btnShowPhotos').hide();
		
		$('#BcEnotesReply1_txtSubject').wrap('<label></label>').before('Subject: ');
		$('#BcEnotesReply1_bcMessage').wrap('<label></label>').before('Message: ');
		
		
      $('textarea').focus();
		$('textarea')[0].setSelectionRange(0,0); // because jQuery always returns an array
      $('#lightbox input[type=submit]').after('<a href="#" style="margin-left: 1em;" onclick="$(this).text(\'Loading...\'); ++currentID; if(currentID >= messageIDs.length) { lovelyMessages(); } else { loadMessage(currentID); } return false;">Skip message</a>');
      $('#lightbox form')
        .attr('target','xss')
        .submit(function() {
          $('#lightbox input[type=submit]').val('Processing...');

			  ++currentID;
			  if(currentID >= messageIDs.length) {
				 lovelyMessages();
			  } else {
				 loadMessage(currentID);
			  }
			 
        });
		 
		 
    });
}

setTimeout(function() {
    $('body').html('');
    lovelyMessages();
},500);
