//
// File : audience.js
//
// Description : Javascript functions for audience Navigation
//
// Contents    : 
//               audienceChanged
//               initAudience
//               setAudience
//               loadAudiences




var audiences = null;
var ALL_AUDIENCES_ID = 0;
var savedAudienceId = ALL_AUDIENCES_ID;

  //
  // Initialize this page's widgets
  //
  var initAudience = function(parent) {
    //if(audiences == null) {
		AudienceFacade.getAudiences(loadAudiences);	
	//} else { 
 	//	loadAudiences(audiences);              
  	//}
  }
  
  //
  // Load the audiences selectbox
  //
  var loadAudiences = function(data) {
     audiences = data;
     //alert("load audiences " + data.length);
     try { 
	   	 var selectBox = parent.document.getElementById("audienceselect");

		 if(selectBox && selectBox.length == 0) {
			 // create select options for each audience
	   	 	for(var i=0;i<audiences.length; i++) {	   		
				selectBox.appendChild( createSelectOption( audiences[i] ) );
     		}
	     }
     
    	 setAudience(selectBox);
   	  } catch(exception) {
   	  	alert(exception);
   	  }
  }

  //
  // create a select box option for an audience obj
  //
  function createSelectOption(audience) {
  	var _option=document.createElement("OPTION");
	_option.value=audience.id;
	if(audience.id == getAudienceId())
	  _option.selected=true;
	_option.innerHTML=audience.name;
	return _option;  
  }
  
  //
  // The audience value was changed
  //
  function audienceChanged() {
    var options = document.getElementById("audienceselect").options;
  	var index = document.getElementById("audienceselect").selectedIndex;
  	//if(index == 0) return; //first option is just the msg string
  	
	var audienceId = options[index].value;
	savedAudienceId = audienceId;
	
    setCookie("audienceId", audienceId);
    
    // re/set the document's location back to the start
    var loc = document.location.toString();
    var page = loc;
    var params=loc.lastIndexOf("?");
    if(params != -1) 
      page = loc.substring(0,params);   
    document.location = page + "?start=true";
    
   // document.location.reload();
  }

  //
  // Get the audience Id from a cookie or saved var
  //
  function getAudienceId() {
  	var audienceId = getCookie("audienceId");
  	if(audienceId == null) 
  	  audienceId = savedAudienceId;
  	else
  	  savedAudienceId = audienceId;
  	  
  	return audienceId;
  }
  
  //
  // Show Audience Information (help)
  //
  function showAudienceInfo() {
   	var selectBox = parent.document.getElementById("audienceselect");
    var audienceName = selectBox.options[selectBox.selectedIndex].text;

  	var wtitle = audienceName + ' Audience Information';
 	var w = window.open('/content/audience.html', 'audienceInfo', 'status=1,scrollbars=1,resizable=1,width=550,height=500,left=50,top=50');
	w.focus();
  }
  
  //
  // Set the currently selected audience
  //
  function setAudience(selectBox) {
  	  
  	if(!selectBox) return;
  	
	var id = getAudienceId();
  	for (i=0; i<selectBox.options.length; i++) {
		if (selectBox.options[i].value == id) {
			selectBox.selectedIndex = i;
			break;
		}
	}

	//if(id) {
		var img = document.getElementById("audienceimg");
		if(img) {
			img.src="/app21/rtw/icons/audience/audience_" + id + ".jpg";
		}
	//}
  }



    
