// If page loading complete then we need to execute some javascript
$(document).ready(function()
{

 // Submit form on dashboard page on change of portfolios drop-down
 $("select[name='portfolios']").change(function()
 {
  this.form.submit();
 });

 // Code to make list of portfolio's pages and sections as sortable to save order
 $("#pages_and_sections").sortable({
  placeholder: "ui-selected-pages-sections",
  handle: '.title',
  axis: 'y',
  update: function(e, ui) {
   $.get(base_url + "/portfolios/save_order?" + $(this).sortable('serialize'));
  }
 });

 // Code to attach function remove image on click event for needed links
 $(".remove_image_link").click(remove_image);

 // Code to make work's name and description in-place editables
 if ("undefined" != typeof(work_id))
 {
  $("#work_name").editable(base_url + "/works/save_field/" + work_id + "/name", { loadurl: base_url + "/works/load/" + work_id + "/name", type: "text", style: "inherit", tooltip: "click to edit", indicator : "Saving...", cancel : "cancel", submit : "save", onblur: "submit", cssclass: "jeditable" });
  $("#work_description").editable(base_url + "/works/save_field/" + work_id + "/description", { cancel : "cancel", loadurl: base_url + "/works/load/" + work_id + "/description", submit : "save", type: "textarea", style: "inherit", tooltip: "click to edit", indicator : "<img src='/img/indicator.gif'>", onblur: "submit", cssclass: "jeditable" });
 }

 // Code to make portfolio's page's name and description in-place editables
 if ("undefined" != typeof(page_id))
 {
  $("#page_name").editable(base_url + "/portfolio_pages/save_field/" + page_id + "/name", { loadurl: base_url + "/portfolio_pages/load/" + page_id + "/name", type: "text", style: "inherit", tooltip: "click to edit", indicator : "Saving...", cancel : "cancel", submit : "save", onblur: "submit", cssclass: "jeditable" });
  $("#page_body").editable(base_url + "/portfolio_pages/save_field/" + page_id + "/body", { cancel : "cancel", loadurl: base_url + "/portfolio_pages/load/" + page_id + "/body", submit : "save", type: "textarea", style: "inherit", tooltip: "click to edit", indicator : "<img src='/img/indicator.gif'>", onblur: "submit", cssclass: "jeditable" });
 }

 // Code to make section's name and description in-place editables
 if ("undefined" != typeof(section_id))
 {
  $("#section_name").editable(base_url + "/sections/save_field/" + section_id + "/name", { loadurl: base_url + "/sections/load/" + section_id + "/name", type: "text", style: "inherit", tooltip: "click to edit", indicator : "Saving...", cancel : "cancel", submit : "save", onblur: "submit", cssclass: "jeditable" });
  $("#section_description").editable(base_url + "/sections/save_field/" + section_id + "/description", { cancel : "cancel", loadurl: base_url + "/sections/load/" + section_id + "/description", submit : "save", type: "textarea", style: "inherit", tooltip: "click to edit", indicator : "<img src='/img/indicator.gif'>", onblur: "submit", cssclass: "jeditable" });
 }

 // Fade out any messages after 5 seconds
 window.setTimeout('$(".message").fadeOut("slow")', 5000);

 // Code to make list of section's works as sortable to save order
 make_section_works_sortable();

 // Code to revert selected template's CSS (needed on portfolio edit page)
 $("#revert_to_default").click(function()
 {
  // By default set selected template as 'false'
  var selected_template = false;

  // Loop through template's radio buttons to check if any template is selected
  $(".template_radio_buton").each(function()
  {
   if (this.checked)
   {
    selected_template = this.value;
   }
  });

  // If any selected template then get its CSS and store it in needed textarea
  if (selected_template)
  {
   $.get(base_url + "/templates/get_css/" + selected_template, function(response)
   {
    $("#cssLoadedMessage").show();
    $("#PortfolioCustomCss").val(response);
    window.setTimeout('$("#cssLoadedMessage").fadeOut("slow")', 5000);
   });
  }
 });

 // Code to save selected template's custom CSS (needed on portfolio edit page)
 $("#save_custom_css_button").click(function()
 {
  $.post(base_url + "/portfolios/save_field/" + portfolio_id + "/custom_css", { custom_css: $("#PortfolioCustomCss").val() }, function(response)
  {
   eval(response);
  });
 });

 // Code to display/hide 'edit stylesheet' link on 'edit portfolio' page as per 'use custom CSS' checkbox status
 $("#PortfolioUseCustomCss").click(function()
 {
  $("#edit_stylesheet_link").toggle();
 });

 // Attach click event for thickbox links
 $("a.thickbox").click(function()
 {
  // Display thickbox window
  tb_show(this.title , this.href, false);

  // Blur focus from current link
  this.blur();

  // By default return false
  return false;
 });

});

// Function used to send AJAX get request to remove image for work
function remove_image()
{
 // First confirm from user about image removal
 if (confirm("Do you really want to delete this image?"))
 {
  // Send AJAX get request to remove image for a work then put fetched response text in needed box and attach remove image function on click event for needed links
  $.get(this.href, function(response_text)
  {
   $("#work_images").html(response_text);
   $(".remove_image_link").click(remove_image);
   window.setTimeout('$(".message").fadeOut("slow")', 5000);
  });
 }

 // By default return false
 return false;
}

// Function used to send AJAX get request to remove logo for portfolio
function remove_portfolio_logo(portfolio_id)
{
 // First confirm from user about logo removal
 if (confirm("Do you really want to delete portfolio's logo?"))
 {
  // Send AJAX get request to remove logo for a portfolio then put fetched response text in needed box
  $.get(base_url + "/portfolios/delete_logo/" + portfolio_id, function(response_text)
  {
   $("#portfolio_logo").html(response_text);
   window.setTimeout('$(".message").fadeOut("slow")', 5000);
  });
 }
}

// Function used to make section's works draggable/dropable
function make_section_works_sortable()
{
 // Code to make list of section's works as sortable to save order
 $("#section_works").sortable({
  placeholder: "ui-selected-section-works",
  handle: '.title',
  update: function(e, ui) {
   $.get(base_url + "/works/save_order?" + $(this).sortable('serialize'));
  }
 });
}