// Common js for IDTLP site

/* --------------------------------------------------------------------------------
   IMMEDIATELY RUNNING CODE
   -------------------------------------------------------------------------------- */

addLoadEvent(function() {
	windowLinks();	// run our new function as soon as the page loads. 
	//otherFunctions();
	//goHere();
	});


/* --------------------------------------------------------------------------------
   OTHER FUNCTIONS
   -------------------------------------------------------------------------------- */

// addLoadEvent function allows you to stack up 'window.onload' 
// events without them stepping on each other's toes. 
// It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578
//
function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      oldonload(); 
      func(); 
    } 
  } 
}


/* ---------------------------------------------------------------------------------------
   EDIT-IN-PLACE FUNCTIONS -- allows administrators to edit text with class "eip_editable"
   --------------------------------------------------------------------------------------- */

$(document).ready(function(){
  eip_setClickable();
});
/*
    var textarea = '<div><textarea rows="1" cols="10">'+$(this).html()+'</textarea>';
    var button	 = '<div><input type="button" value="SAVE" class="saveButton" />  <input type="button" value="CANCEL" class="cancelButton" /></div></div>';
*/
function eip_setClickable() {
  $('.eip_editable').click(function() {
    var textarea = '<span class="eip_editing"><input value="'+$(this).html()+'" size="10" /> ';
    var button	 = '<span><input type="button" value="&#10004;" class="savebutton" /> <input type="button" value="&#10008;" class="cancelbutton" /></span></span>';
    var revert = $(this).html();
    $(this).after(textarea+button).remove();
    $('.savebutton').click(function(){eip_saveChanges(this, false);});
    $('.cancelbutton').click(function(){eip_saveChanges(this, revert);});
  })
  .mouseover(function() {
    $(this).addClass("eip_editable2");
  })
  .mouseout(function() {
    $(this).removeClass("eip_editable2");
  });
};

function eip_saveChanges(obj, cancel) {
  if(!cancel) {
    var t = $(obj).parent().siblings(0).val();
    $.post("test2.php",{
      content: t
    },function(txt){
      alert( txt);
    });
  }
  else {
    var t = cancel;
  }
  if(t=='') t='(click to add text)';
  $(obj).parent().parent().after('<span class="eip_editable">'+t+'</span>').remove();
  eip_setClickable();
}


/* --------------------------------------------------------------------------------
   POPUP WINDOW FUNCTIONS
   -------------------------------------------------------------------------------- */

// Popup window function
//
// usage:
//   <a href="blah.html" rel="popup|320|240">popup window</a> - link css is .popup
//   <a href="blah.html" rel="external">external window that loads in a new window</a> - link css is .external
//

/* new accessible, unobtrusive popup code */

function windowLinks() {                      // create a new function called windowLink(); 
  if(!document.getElementsByTagName) {      // Only run the function on browsers that
    return;                              // understand 'getElementsByTagName' - new browsers
  }

  var anchors = document.getElementsByTagName("a"); // grab all links and pop them in an array called 'anchors'
  for (var i = 0; i < anchors.length; i++) {        // start a loop to work our way through 
    var anchor = anchors[i];                        // grab the next link & copy it to 'anchor' for working
    var relIndex = anchor.rel;                      // get the value of it's 'REL' attribute
    if (relIndex){                                  // does it have a value for REL?...
      var relSplit = relIndex.split("|");          
      /* if it does, look for '|' to use to split the value into an 
      array - relSplit[0], relSplit[1], relSplit[2], etc . If it doesn't 
      find any '|', the whole value gets transferred to relSplit[0] */
	 
/* XHTML compliant target attribute */

      if (relSplit[0] == "external") {       // If the relSplit[0] is 'external'...
        anchor.target = "_blank";            // then set it's 'target' attribute to '_blank'
        anchor.className = "external";       // then attach a CSS class to it 
        anchor.title = "Load in new window: "+ anchor.href;  
        // And give it a new title attribute to warn users a new window is launching

/* Elegantly degrading window code */

      } else if (relSplit[0] == "popup") {        // else if relSplit[0] is 'popup'...
        anchor.className = "popup";               // attach a CSS class to it 
        anchor.title = "Loads in a Popup Window"; // Give it a helpful title attribute
        anchor.popupWidth = relSplit[1];          // set the popup's width
        anchor.popupHeight = relSplit[2];         // set the popup's height
        anchor.scroll = relSplit[3];              // set the popup's scrollbars on or off ('scroll' or '')
        anchor.onclick = function() {acpopup(this.href,'console',this.popupWidth,this.popupHeight,this.scroll);return false;};
        // plug all this information into our original window launch function (acpopup) 
      }
    }
  }
} 

/* This is our current popup function - parts of the site still use it, so I need to keep it */

function acpopup(strURL,strType,strWidth,strHeight,strScroll) {
  var strOptions="";
  if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
  if (strScroll=="scroll") strOptions=strOptions+",scrollbars=yes";
  window.open(strURL, '', strOptions);
}



/*
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/

/*  specify all rollover images with class="imgover"
    off image: image-name.jpg
     on image: image-name_o.jpg
*/
function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

window.onload = initRollovers;
