// JavaScript function for replacing pictures in the image gallery
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function preparePlaceholder() {
  if (!document.createElement) return false;
  if (!document.createTextNode) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("imagegallery")) return false; // If there is no #imagegallery list on the page, the gallery won't function
  var placeholder = document.createElement("img");
  var page_title = document.getElementById("page_title");
  var defaultimageID = document.getElementById("thumbnails");
  var defaultimage = defaultimageID.getAttribute("name");
//  var defaultimage_src = "./uploads/images/" + defaultimage + ".jpg";
  var defaultimage_src = "./uploads/images/" + defaultimage + ".jpg";
  placeholder.setAttribute("id","placeholder");
  placeholder.setAttribute("src",defaultimage_src);
  placeholder.setAttribute("alt","Image gallery");
  placeholder.style.maxWidth = "500px";
  placeholder.style.maxHeight = "375px";
  var description = document.createElement("p");
  description.setAttribute("id","description");
  var desctext = document.createTextNode("Choose an image");
  description.appendChild(desctext);
  insertAfter(placeholder,page_title);
  insertAfter(description,placeholder);
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("imagegallery")) return false;
  var gallery = document.getElementById("imagegallery");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
    links[i].onclick = function() {
      return showPic(this);
	}
  }
}


function showPic(whichpic) {
  if (!document.getElementById("placeholder")) return false;
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  if (!document.getElementById("description")) return false;
  if (whichpic.getAttribute("title")) {
    var text = whichpic.getAttribute("title");
  } else {
    var text = "";
  }
  var description = document.getElementById("description");
  if (description.firstChild.nodeType == 3) {
    description.firstChild.nodeValue = text;
  }
  return false;
}

// menuPrepare() -  hides the parent and position input boxes on the Edit Site page if the user has javascript enabled

	function menuPrepare() { 
		$('div.pageInfo').css("display", "none");
	}

// editSitemapInstructions() -  updates the sitemap editing instructions if the user has javascript enabled

	function editSitemapInstructions() { 
		var container = document.getElementById("admin_content");
		var message = document.getElementById("nojavascript");
		var para = document.createElement("p");
		var txt = document.createTextNode("Click and drag page titles to reorder the main menu. You can view a page by clicking its title.");
		para.appendChild(txt);
		if (container) { //Only runs if container element exists on page
		container.replaceChild(para,message);
		}
	}

// menuSort() -  sets up the sortable stuff for the Edit Site page

	function menuSort() { // Could do with inserting all of the javascript checks from showPic function.
		$(".sortable").sortable({ revert: 'true', cancel: 'h3', axis: 'y',//connectWith: '.sortable',// handle: 'div',
								update: function(event, ui) {
								var pageOrder = $(this).sortable('toArray');//.toString();
								for (var i=0; i<pageOrder.length; i++) {
									var page = "ppinput_";
									page += pageOrder[i];
									var pageID = document.getElementById(page);
									pageID.value = i+1; 
									// Live update the main nav would go here
								}
								}
								//revert: 'true', axis: 'y', cancel: 'h3', connectWith: '.sortable2'
								});
		$(".sortable").disableSelection();
	};
	
// hideNav() -  Closes up the nav menu apart from the section of the site the user is in. Based on the current section's UL having .currentSection class applied with PHP.
	
		function hideNav() {
		//alert("wango");
		$('div#primary_nav_panel>ul>li>ul:not(.currentSection)').css("display", "none");
		$('div#primary_nav_panel>ul>li>ul>li>ul:not(.currentSubsection)').css("display", "none");
		}
// When the page is ready
   $(document).ready(function(){
     $(".module .module_overview").hide();
//		$(".module h2").replaceWith("<h2><a href='' title='View course modules'>"+not_title+"</a></h2>");	 
     $(".module h3")
	    $(".module a").click(function(event){
       $(this).parent("h3").next(".module_overview").slideToggle(200);
       // Stop the link click from doing its normal thing
       event.preventDefault();
     });

   });

//		$(".module h2").replaceWith("<h2><a href='' title='View course modules'>"+not_title+"</a></h2>");	 

addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);

addLoadEvent(menuSort);
addLoadEvent(menuPrepare);
addLoadEvent(hideNav);
addLoadEvent(editSitemapInstructions);






