$(document).ready(function() {
  $("div.column-sidebar p:first-child, div.column-sidebar h2:first-child").each(function(i) {
    $(this).addClass("flush");
  });

  $("div.rounded").each(function(i) {
    if (!$(this).hasClass("leavealone")) {
      divContent = document.createElement("div");
      divContent.className = "rounded-content";
      $(this).wrapInner(divContent);

      divTop = document.createElement("div");
      divTop.className = "rounded-top";
      divTop.appendChild(document.createElement("div"));
      $(this).prepend(divTop);

      divBottom = document.createElement("div");
      divBottom.className = "rounded-bottom";
      divBottom.appendChild(document.createElement("div"));
      $(this).append(divBottom);
    }
  });

  if ($("#ctl00_txtEmail").val() == "") {
    $("#ctl00_txtEmail").val("Email");
    $("#ctl00_txtPassword2").hide();
    $("#ctl00_txtPassword1").val("Password").show();

    $("#ctl00_txtEmail").focus(function() { updateEmailBox("focus"); });
    $("#ctl00_txtPassword1").focus(function() { updatePasswordBox("focus"); });

    $("#ctl00_txtEmail").blur(function() { updateEmailBox("blur"); });
    $("#ctl00_txtPassword2").blur(function() { updatePasswordBox("blur"); });
  }

  if ($("#ctl00_cphMain_cblType_5")) {
    $("#ctl00_cphMain_cblType_5").click(function() { updateLibraryList(); });
    updateLibraryList();
  }

  $("a.toggle-link").each(function(i) {
    $(this).click(function() { toggleArea(i); });
  });

  if (typeof sIFR == "function") {
    sIFR.replaceElement(named({ sSelector: "div.title h1", sFlashSrc: "/flash/futuramedium.swf", sColor: "#FFFFFF", sBgColor: "#4B5E83", sFlashVars: "offsetTop=-5" }));
    sIFR.replaceElement(named({ sSelector: "h1", sFlashSrc: "/flash/futuramedium.swf", sColor: "#4B5E83", sBgColor: "#FFFFFF", sFlashVars: "offsetTop=-5" }));
    sIFR.replaceElement(named({ sSelector: "div.dark h2", sFlashSrc: "/flash/futuramedium.swf", sColor: "#F8A243", sBgColor: "#3D527A2", sFlashVars: "offsetTop=-4" }));
    sIFR.replaceElement(named({ sSelector: "div.light h2", sFlashSrc: "/flash/futuramedium.swf", sColor: "#F68121", sBgColor: "#F1F1F1", sWmode: "transparent", sFlashVars: "offsetTop=-4" }));
    sIFR.replaceElement(named({ sSelector: "div.orange h2", sFlashSrc: "/flash/dfuturamedium.swf", sColor: "#FFFFFF", sBgColor: "#F68121", sFlashVars: "offsetTop=-4" }));
    sIFR.replaceElement(named({ sSelector: "h2.blue", sFlashSrc: "/flash/futuramedium.swf", sColor: "#3D527A", sBgColor: "#FFFFFF", sFlashVars: "offsetTop=-5" }));
    sIFR.replaceElement(named({ sSelector: "h2", sFlashSrc: "/flash/futuramedium.swf", sColor: "#F68121", sBgColor: "#FFFFFF", sFlashVars: "offsetTop=-4" }));
  };

});

var textbox = "";
var addthis_pub = "alnap";

function sendEmail() {
  $("#emailresult").html("Sending...");
  $.post("/ajax/email.aspx", { url: "" + window.location + "", title: "" + document.title + "", to: "" + $("#emailto").val() + "", from: "" + $("#emailfrom").val() + "", message: "" + $("#emailmessage").val() + "" }, function(data) {
    $("#emailresult").html(data);
  });
}

function updateEmailBox(event) {
  if (event == "focus") {
    if ($("#ctl00_txtEmail").val() == "Email") $("#ctl00_txtEmail").val("");
  } else {
    if ($("#ctl00_txtEmail").val() == "") $("#ctl00_txtEmail").val("Email");
  }
}

function updatePasswordBox(event) {
  if (event == "focus") {
    $("#ctl00_txtPassword1").hide();
    $("#ctl00_txtPassword2").show();
    $("#ctl00_txtPassword2").focus();
  } else {
    if ($("#ctl00_txtPassword2").val() == "") {
      $("#ctl00_txtPassword2").hide();
      $("#ctl00_txtPassword1").show();
    }
  }
}

function updateLibraryList() {
  if ($("#ctl00_cphMain_cblType_5:checked").val() != null) {
    $("#rowlibraries").slideDown(400);
  } else {
    $("#rowlibraries").slideUp(400);
  }
}

function addToFavourites() {
  if (window.sidebar) { // Firefox Bookmark
    window.sidebar.addPanel(document.title, window.location, "");
  } else if (window.external) { // IE Favorite
    window.external.AddFavorite(window.location, document.title);
  }
}

function toggleArea(index) {
  var areas = $("div.toggle-area");
  var links = $("a.toggle-link");

  if ($(links[index]).hasClass("selected")) {  
      $(links[index]).removeClass("selected");
      $(areas[index]).hide();    
  } else {
      $(links[index]).addClass("selected");
      $(areas[index]).fadeIn(500);  
  }
  
}

function browseImages(target) {
  textbox = target;
  window.open('/admin/browsers/image.aspx', "imagebrowser", "menubar=no,width=700,height=500,toolbar=no,scrollbars=yes");
}

function browseFiles(target) {
  textbox = target;
  window.open('/admin/browsers/files.aspx', "imagebrowser", "menubar=no,width=700,height=500,toolbar=no,scrollbars=yes");
}


function SetUrl(url) {
  $('#' + textbox).val(url.substring(url.lastIndexOf('/') + 1));
}

function InsertDate(target) {
  var now = new Date();
  var date = ((now.getDate() < 10) ? "0" : "") + now.getDate();
  var month = ((now.getMonth() + 1 < 10) ? "0" : "") + (now.getMonth() + 1);
  var year = (now.getYear() < 1000) ? now.getYear() + 1900 : now.getYear();
  $(target).value = date + '/' + month + '/' + year;
}