// vim:shiftwidth=2:softtabstop=2:expandtab

// update photo
function updatePhoto(json) {
  $('#nextphoto').attr('href', json.n);
  $('#prevphoto').attr('href', json.p);
  $('#albumimg').attr('src', json.i);
  //document.location = json.u;
}

// runonce: deactivate a href links for nextphoto / prevphoto in album_photo
function fixPhoto() {
  $('#nextphoto, #prevphoto').click( function(e) {
    //alert("Getting click.."+$(this).attr('href'));
    //$.getJSON($(this).attr('href')+"?json=true", function(json) {
    $.ajax({
        url: $(this).attr('href')+"?json=true",
        dataType: 'json',
        data: null,
        success: function(json) { updatePhoto(json); },
        error: function(xmlhttpreq, textstatus, errthrown) {
          alert("there was an error: "+textstatus); }
    });
    return false;
  });
}

// runonce: change the <div id="show_flash"> <show_flash_txt> to a popup
function fixFlash() {
  if ($('#show_flash').length != 0) {
    var showflash = $('#show_flash_txt').html();
    $('#show_flash').hide();

    $(function () {
      $.notifyBar({
        html: showflash,
        delay: 4000,
        animationSpeed: "normal"
      });
    });
  }
}

// runonce; change Subject to use font
function fixSubject() {
  $('.subject, .from, .bulletin-date').each( function(i) {
    var theVar = $(this);
    var varParent = theVar.parent();
    var theStr = this.innerHTML;
    theStr = theStr.replace("Subject: ", '~8~M~C5Z_ ~N~4~CA(~CB)__(~CA)~M~8 ~C5Z_ ~N~4~CESubject:~8~CF ~4~I');
    theStr = theStr.replace(   "From: ", "~8~M~C5Z_ ~N~4 ~CA(~C7oo~CA) ~M~8 ~C5Z_ ~N~4   ~CEFrom:~8 ~CF");
    theStr = theStr.replace(   "Date: ", "~8~M~C5Z_ ~N~4~CF==~CA()~CF==~M~8 ~C5Z_ ~N~4   ~CEDate:~8 ~CF");
    this.innerHTML = applify(theStr)[0];
    //varParent.append(theVar);
    //varParent.append(appendme);
    //this.innerHTML = appendme;
  });
}

$(document).ready(function() {

  //window.console.log("Hello");
  //console.log("Hello");

  // change flash to popup
  fixFlash();

  // Round all the corners.
  $('.round').corner();

  // ALBUM PHOTOS - Redo the 'next' and 'prev' buttons.
  fixPhoto();

  fixSubject();

  $('a').each( function(i) {
    //this.innerHTML = this.innerHTML + "\u00a0\ue08d";
    //this.innerHTML = this.innerHTML + "\ue089\ue090";
    this.innerHTML = this.innerHTML + "<span style=\"color: #555\">\u00a0\ue090</span>";
  });

  $('.applify').each( function(i) {
    var str = this.innerHTML;
    str = str.replace("check it out", "~MXY~N ~CAcheck it out ~MM");
    this.innerHTML = applify(str)[0];
  });

  $('.applify-title').each( function(i) {
    var titpre = "~4~C5~I ~M^~I ~8:~CA:>~4~CF~MS~I ";
    var titpost = " ~MS~I~CA~8<:~M@A~N::..~X~I~X";
    this.innerHTML = applify(titpre+this.innerHTML+titpost)[0];
  });

  $('.linkapplify').each( function(i) {
    var txt = this.innerHTML;
    txt = txt.replace("Home", "~CE~I ~M\\\\~I Home ~M\\\\~I ");
    txt = txt.replace("Albums", "~CD~I ~M\\\\~I Albums ~M\\\\~I ");
    var app = applify(txt);
    this.innerHTML = app[0];
    //$(this).css('text-indent', '-9999px');
  });

  var blinkele = $('#header h1');
  //alert(blinkele);
  var blinker = true;
  setInterval( function() {
    if (blinker) {
      //alert(blinkele);
      blinkele.css('width', '420px');
      blinker = false;
    } else {
      blinkele.css('width', '448px');
      blinker = true;
    }
  }, 450);
});

// notifyBar example ----------------------------------------------------------

//  $(function () {
//    $.notifyBar({
//      html: "Thank you, your settings were updated!",
//      delay: 2000,
//      animationSpeed: "normal"
//    });  
//  });

// album photo
//function changephoto() {
//  //alert("Unbinding " + this);
//  //$('#nextphoto, #prevphoto').unbind();
//  $('#nextphoto, #prevphoto').click( function(e) {
//  //$('#nextphoto, #prevphoto').bind("click", function(e) {
//    var url = $(this).attr("href");
//    $.get("http://localhost:4567"+url+"?ajax=true", function(data) {
//      //e.preventDefault();
//      $('#albumphoto').html(data);
//      //$('#albumphoto').after(data).remove();
//      changephoto();
//      //document.location.href = url;
//    });
//    //alert(link);
//    //$(this).unbind();
//    //$(this).bind("click", function() { return false; });
//    return false;
//  });
//}


