/*
* WAAPA Website Main Javascript Library
* Grant Heggie [Squiz.net.au]
* August 2009
* 
* Modification history:
* 
* 2009-11-26: Scott Hall
*             - Reassign jquery alias $ to use $j to avoid prototype alias conflicts.
* 2009-11-20: Scott Hall
*             - Dynamic radio selection to change search results cx hidden input values.
* 2009-11-19: Scott Hall
*             - Added conditions to dynamically hide empty right column wrappers.
* 2009-11-11: Scott Hall
*             - Removed login condition to remove pop up link, now controlled via login and simple edit css.
* 2009-11-10: Scott Hall
*             - Updated pop up box code.
* 2009-10-01: Andrew Dunbar
*             - Removed old form validation code.
*             - Updated library file to new formatting.
*
* 
*/

/* 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%2Fwaapa.ecu.edu.au">Logout</a></p>');
   }

   // searchbox on blur change
   $j("#search-site #searchbox").focus(function() {
      if( this.value == this.defaultValue ) {
         this.value = "";
      }
   }).blur(function() {
      if( !this.value.length ) {
         this.value = this.defaultValue;
      }
   });

   $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;
   });

   // 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();
      }
   }
	
   // lets avoid editing the paint layouts and asset listings for related content
   // by setting their empty wrappers to display none dynamically instead this
   // will avoid h2 premature line wrapping problems
   if($j("#floated-image-area").children().length < 1){ $j("#floated-image-area").css("display","none"); }
   if($j("#related-content-wrapped").children().length < 1){ $j("#related-content-wrapped").css("display","none"); }

   // set the radio button to ecu if ecu results exist
   if($j("#cse-search-results iframe[src*='018305793595511556410']").length > 0){
      $j("#search-output input#radioSearchWAAPA").removeAttr("checked");
      $j("#search-output input#radioSearchECU").attr("checked", "checked");
   }
   // dynamically set hidden input cx value on search results page to search waapa
   $j("#search-output input#radioSearchWAAPA").click( function() {
      $j("#search-output input[name='cx']").val("016802087585493462159:38qbewqfhpq");
   });
   // dynamically set hidden input cx value on search results page to search ecu
   $j("#search-output input#radioSearchECU").click( function() {
      $j("#search-output input[name='cx']").val("018305793595511556410:gafzrokm2kc");
   });

});

/* 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 ******************************************** */
