/*
* ECU Website Main Javascript Library
* Scott Hall [Squiz.net.au]
* February 2009
* 
* Modification history:
* 
* 2009-11-27: Scott Hall
*             - Reassigned jQuery alias $ to use $j to avoid prototype conflicts
* 2009-11-13: Scott Hall
*             - Updated page info pop up function
* 2009-11-13: Scott Hall
*             - Updated page info pop up function
* 2009-11-11: Scott Hall
*             - Updated to new formatting
*             - Removed login condition to remove pop up link, now controlled via login and simple edit css.
*
* 
*/

/* START jQuery 'document ready' function for generic use *************************************** */

// Creates a different alias instead of $ to use in the rest of the script.
var $j = jQuery.noConflict(); 

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

   // get logged in cookie using cookie plugin and generate portal links 
   // set to 1 for testing only
   // $j.cookie('ecu_portal', 1, { path: '/' });
	
   var portalLoggedIn = $j.cookie('ecu_portal');
   if(portalLoggedIn=='1'){
      $j("#portal-links").html('<p>Signed into <a href="https://portal.ecu.edu.au">Portal</a> | <a href="https://sso.ecu.edu.au/pls/orasso/orasso.wwsso_app_admin.ls_logout?p_done_url=http%3A%2F%2Fwww.ecu.edu.au">Logout</a></p>');
   }

   $j("#nav-local > ul > li").each(function(i){
      if ($j(this).children().length > 1) {
         $j(this).children().eq(0).addClass("parent-with-subs");
      }
   });

   // ie6 fix for content headings
   $j("#content-main h2:first-child").addClass("content-title");

   // ie6 and Firefox 3 on blur fix
   $j("#searchbox").focus(function() {
      if( this.value == this.defaultValue ) {
         this.value = "";
      }
   }).blur(function() {
      if( !this.value.length ) {
         this.value = this.defaultValue;
      }
   });

   // fix breadcrumb trail if displays last greater than char
   if($j("#breadcrumb-links").length > 0){
      var linkHTML = $j("#breadcrumb-links").html() + "";
      var trimmedHTML = trim(linkHTML);
      var lastChar = trimmedHTML.charAt(trimmedHTML.length-1);
      if(lastChar==";"){
         var newHTML = trimmedHTML.substring(0,trimmedHTML.lastIndexOf("&gt;"));
         $j("#breadcrumb-links").html(newHTML);
      }
      // for safari
      if(lastChar==">"){
         var newHTML = trimmedHTML.substring(0,trimmedHTML.lastIndexOf(">"));
         $j("#breadcrumb-links").html(newHTML);
      }
   }

   // fix the width of the floated image area container based on image size */
   var imgWidth = $j("#floated-image-area img:first-child").width();
   $j("#floated-image-area").width(imgWidth);

   // Dynamically pull wrapped related content column up
   // by the height of the main content h2 if no floated image exists
   if($j("#related-content-wrapped").length > 0 && $j("#floated-image-area img").length == 0){
      //var headingHeight = $j("#related-content-wrapped").next("h2").height();
      //headingHeight = "-" + headingHeight + "px";
      $j("#related-content-wrapped").css("margin-top", "-22px");
   }

   // Dynamically submit the search website remote form
   // only submit if there are no children of cse-search-results
   if($j("form.remote-search-website").length > 0 && $j("#cse-search-results").length > 0){
      if($j("#cse-search-results").children().length < 1){
         $j("form.remote-search-website").submit();
      }
   }

   // show page info pop up
   $j('a.show-page-info').click(function(){
      var documentWidth = $j(document).width();
      var windowHeight = $j(window).height();
      var popupWidth = 496;
      var popupHeight = 650;
      var posTop = 0 + windowHeight / 2 - popupHeight / 2;
      var posLeft = 0 + documentWidth / 2 - popupWidth / 2;
      newWindow = window.open(this.href,"mywindow","location=0,status=0,scrollbars=1,width="+popupWidth+",height="+popupHeight+",left="+posLeft+",top="+posTop);
      return false;
   });
	
});

/* END jQuery 'document ready' function for generic use ***************************************** */


/* START Declaration of generic javascript functions ******************************************** */

function removeTokenFromString(s,t) {
        s = s + "";
	var i = s.indexOf(t);
	var r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + removeTokenFromString(s.substring(i + t.length), t);
	return r;
}

function getIndex(varArray, varValue){
	for(var i = 0; i < varArray.length; i++){
		if(varArray[i]==varValue){
			return i;
		}
	}
}

/* Read a page's GET URL variables and return them as an associative array. */
/* Example - var urlHash = getUrlVars(); alert(urlHash['hello']); */
function getUrlVars(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function trim(str){
    str = str + "";
    return str.replace(/^\s+|\s+$/g,"");
}

/* END Declaration of generic javascript functions ******************************************** */