(function($) {
  $(document).ready(function() {

    // initiate treeview expandable/collapsable sub menus to subjects list

    // initiate toolboxToggle
    toolboxToggle();

  
    // Set default value for main search box
    $(".featureSearch-input").defaultValue("Search site...");

    // Rotating Feature box on homepage js
    if($('#rotating-feature-box').length) {
      setupHomepageFeatureBox();

    // dropdowns wohoooo
    $('#nav-pri-list').superfish({
	autoArrows: false
    })

   // Add confirm yes/no to delete Research updates (news) and delete asset with GET request
   confirmAndDeleteResearchUpdate();

   $('.hp-feature').cycle({ 
       delay:  3000, 
       speed:  500 
   }); 
 

  });
})(jQuery);

/*------- Toolbox Opening, Closing & Cookies ---------*/
function toolboxToggle() {
  if(! $("body.funnelback").length){
    // Assign click handlers to the open/close toolbox icons
    var COOKIE_NAME = 'toolbox_cookie';
    $("#se_toolbox-opener a").click(function() {
      $("#se_toolbox, #se_toolbox_spacer").slideDown();
      $("#se_toolbox").removeClass("close").addClass("open");
      $("#se_toolbox-opener").hide();
      $("#se_toolbox-closer").show();

      // set cookie to ensure that the toolbox is kept open until it is manually closed
      $.cookie(COOKIE_NAME, 'open', { path: '/' });
    });

    $("#se_toolbox-closer a").click(function() {
      $("#se_toolbox, #se_toolbox_spacer").slideUp();
      $("#se_toolbox").removeClass("open").addClass("close");
      $("#se_toolbox-closer").hide();
      $("#se_toolbox-opener").show();

      // delete the cookie
      $.cookie(COOKIE_NAME, null, { path: '/' });
    });

    // Make sure that the toolbox is shown when in simple edit mode. We do this by setting the cookie and letting the code following open the toolbox.
    if ($("body.simple-edit").length > 0) {
      $.cookie(COOKIE_NAME, 'open', { path: '/' });
    }

    // Check the toolbox cookie and show the toolbox if it is set
    if ($.cookie(COOKIE_NAME) == "open") {
      $("#se_toolbox, #se_toolbox_spacer").show();
      $("#se_toolbox").removeClass("close").addClass("open");
      $("#se_toolbox-opener").hide();

      // If in simple edit mode then don't show opener/closer as we need the user to select from one of the Save/Cancel options
      if ($("body.simple-edit").length > 0) {
        $("#se_toolbox-closer").hide();
      } else {
        $("#se_toolbox-closer").show();
      }
    };
  };
}; // End of Toolbox Opening, Closing & Cookies


function confirmAndDeleteResearchUpdate() {
	// When delete 'research update' link is clicked, hide 'delete' and show 'are you sure?'
	$(".list-research-updates a.delete").live("click", function() {
		$(this).hide();
		$(this).parents("td").find(".confirm-delete").show();
		return false;
	});

	// When "yes, delete" is clicked, access the asset's url in the background and reload the page
	$(".list-research-updates a.delete-yes").live("click", function() {
		$.get($(this).attr('href'), function(){
			window.location.reload();
		});
		return false;
	});

        // When "no", hide "are you sure" and show "delete" link again
	$(".list-research-updates a.delete-no").live("click", function() {
	   $(this).parents("span").hide();
	   $(this).parents("td").find(".delete").show();
           return false;
	});

}; // End confirmAndDeleteResearchUpdate


function setupHomepageFeatureBox() {

  var rand_no = Math.random();
  var currentImg = $("#features li a img").attr("id").substr(3);
  var rrn = $("#replaceroot").text()
  $.ajax({
     url : "http://" + top.location.host + "/_media/javascript/feature-listing-js" + "?rrn=" + rrn,
     data: "nocache=" + rand_no,
     success: function(data){
        $.each(data.imgs, function(i, img){
           if(img.id != currentImg) {
              $("#features").append('<li><a href="' + img.url + '" target="_blank"><img src="' + img.src + '" alt="' + img.alt + '" width="' + img.w + '" height="' + img.h + '" /></a></li>');
           }
        })
         var featureLength = $("#features li").length
         if (featureLength != 1)
         {
           $("#features")
           .css({"margin-left":"0"})
           .before('<a id="prevFeature" href="#" title="Previous"><img src="/__data/assets/image/0011/2153/Showcase-prev.png" alt="Previous" /></a>')
           .after('<a id="nextFeature" href="#" title="Next"><img src="/__data/assets/image/0012/2154/Showcase-next.png" alt="Next" /></a>');
            $('#sponsors').cycle({ 
               prev:   '#prevFeature', 
               next:   '#nextFeature', 
               timeout: 2000 
           });
         }
     }
  })
} // end setupHomepageFeatureBox

