// Show progress of Ajax calls in top right corner
$(document.body).ajaxStart(function() {
   $(document.body).append('<div id="loading">Working...</div>');
   $('#loading').css({padding:"2px", fontSize:"9pt", position:"fixed", top:"0", right:"0", background:"red", color:"white"});
}).ajaxStop(function() {
   $('#loading').remove();
});

function displayMessage(title, message) {

  $("#errorMessage").html('<p>' + message + '</p><p style="text-align:center"><input type="submit" id="Login" value="&nbsp;&nbsp;Ok&nbsp;&nbsp;" onclick="tb_remove()" /></p>');
  return tb_show(title, "#TB_inline?height=90&amp;width=300&amp;inlineId=errorMessage&amp;modal=true", false);
}

function trim(s) {

  return s.replace("/^\s+|\s+$/g", '');
}

function contains(a, o) {
  
  for (var i=0; i<a.length; i++) {

    if (a[i] == o) {
      
      return true;
    }
  }

  return false;
}

this.imagePreview = function(){ 

  $("a.preview").hover(function(e){
  
    if (e.pageY > 300) {
    
      xOffset = 320;
      yOffset = 30;
    } else {
    
      xOffset = 30;
      yOffset = 30;
    }
    
    this.t = this.title;
    this.title = "";  
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                
    $("#preview")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px")
      .fadeIn("fast");            
    },
  function(){
    this.title = this.t;  
    $("#preview").remove();
    }); 
  $("a.preview").mousemove(function(e){
  
    if (e.pageY > 300) {
    
      xOffset = 320;
      yOffset = 30;
    } else {
    
      xOffset = 30;
      yOffset = 30;
    }
    
    $("#preview")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px");
      
  });     
};

function login(resubmitForm) {

  return tb_show("Please Login", "ajaxLogin.jsp?resubmit="+resubmitForm+"&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=290&width=300&modal=true", false);
}

function processXml(responseXML) { 

  var forward = $('forward', responseXML).text();
  var message = $('message', responseXML).text();
  var resubmitForm = $('resubmit', responseXML).text();
  if (forward == 'success' ||
      forward == 'edit.page') {

    // Everything ok  
    if ($("#flash-container").length > 0) {
      
      jQuery.flash.subtle('Done', '');  
    }
  } else if (forward == 'error.not.logged.in') {
  
    login(resubmitForm);
  } else if (forward == 'added.page') {
  
    var oldPageId = $('oldPageId', responseXML).text();
    var newPageId = $('newPageId', responseXML).text();
    var newPageName = $('newPageName', responseXML).text();
    
    // Update on-screen site map
    if (self.addedPage) {
    
      addedPage(oldPageId, newPageId, newPageName);
    }
  } else {
  
    displayMessage("IvyCMS Error", message);
  }
} 
  
function filePicked(frameTitle, uploadFileId, thumbSrc, title, link, fileType) {
  
  if (frameTitle == "Rich Text Editor File Selection") {

    var html = "";

    if (link) {

      html += "<a href='" + link + "'>";
    }

    if (fileType == 'IMAGE' && thumbSrc) {

      html += "<img src='" + thumbSrc + "'";
      if (title) {

        html += " title='" + title + "'";
      }

      html += " />";
    } else if (title) {

      html += title;
    } else {

      html += "File Link";
    }


    if (link) {

      html += "</a>";
    }

    FCKeditorAPI.GetInstance("richTextEditor").InsertHtml(html);

  } else if (frameTitle == "Image Selection") {
  
    $("#contentImage").attr("src", thumbSrc + "&cropSize=150");
    $("#uploadedFileId").attr("value", uploadFileId); 

    if ($("#linkTo")) {

      $("#linkTo").attr("value", link); 
    }
  } else if (frameTitle == "Thumbnail Selection") {
  
    $("#contentThumbnail").attr("src", thumbSrc + "&cropSize=150");
    $("#uploadedThumbnailId").attr("value", uploadFileId); 
    
    if ($("#linkTo")) {

      $("#linkTo").attr("value", link); 
    }
  }
}
