/**
 *           ********************************** COMMUNICATION **********************************
 */
 


/************ wrappers for doRequest ************/
	
function getContentQueryShows(ftquery, andit, whole) {
  var ftflags = "";
  ftflags += ( andit ) ? "1" : "0";
  ftflags += ( whole ) ? "1" : "0";
  doRequest ( 1, ftquery, ftflags, false, false, false, false, false ); 
}

function getGuestShows(guest_id) {
  doRequest ( 2, false, false, guest_id, false, false, false, false );	
}

function getCronologicalShows(startdate, enddate, subject) {
  doRequest ( 3, false, false, false, startdate, enddate, subject, false ); 
}

function getSinglemodeShow(show_id) {
  doRequest ( 4, false, false, false, false, false, false, show_id ); 
}


/************ CS CallBack's for gethtmlcontent ************/

function cacheHtml(shows_array) {

  //this CB is called when no error has occured to chache html on client
  error = { has_error: false, error_string: '' };
  
  // request not pending anymore...
  top.req_pending = false;
  top.lib_hideLoadingIcon();
  
  if ( shows_array.length == 0 ) {
    top.clearChannel();
    return;
  }
  
  if (top.shows_cache[shows_array[0].index] != null) {
    for (i=0;i<shows_array.length;i++) {
      // replace back single quotes!
      top.shows_cache[shows_array[i].index].html = "" + shows_array[i].html.replace(/&sq;/g, "'");
    }	
  }
  top.clearChannel();
}

function clearRequestedHtml(null_entries, the_error) {

  //this CB is called in case eof an error, set error and reset html entries if any
  error = {has_error:true,error_string:the_error};
  
  // request not pending anymore...
  top.req_pending = false;
  top.lib_hideLoadingIcon();
	
  if ( null_entries.length != 0 ) {
    // reset all html properties requested, for when loding indicator has been set...
    if (top.shows_cache != null) {
      for (e=0; e<null_entries.length; e++) {
        if (top.shows_cache[null_entries[e]] != null) {
          top.shows_cache[null_entries[e]].html = null;
          top.shows_cache[null_entries[e]].maximized = false;
        }
      }
    }
  }	
  top.clearChannel();
}


/************ CS CallBack's for queryresults ************/

function cacheEntries(aShowsArray, anError, anErrorString, aQuery, aSortorder, anRSLength) {
  
  //set the error object and exit if one occured
  top.error = { has_error:anError, error_string:anErrorString };
  
  // request not pending anymore...
  top.req_pending = false;
  top.lib_hideLoadingIcon();
  
  // make sure correct tab is active on search when opened
  setSearchModeFromQuery(aQuery);
  
  if (!anError) {
    for (i=0;i<aShowsArray.length;i++) {

      // needs to be replaced here as well for singlemode (see cacheHtml())
      if ( aShowsArray[i].html )
        aShowsArray[i].html = aShowsArray[i].html.replace(/&sq;/g, "'");
        
      top.shows_cache[aShowsArray[i].index] = 
      {
        showid:aShowsArray[i].showid,
        title:aShowsArray[i].title,
        descr:aShowsArray[i].descr,
        guestids:aShowsArray[i].guestids,
        modids:aShowsArray[i].modids,
        subjectids:aShowsArray[i].subjectids,
        bbn:aShowsArray[i].bbn,
        audio:aShowsArray[i].audio,
        video:aShowsArray[i].video,
        link:aShowsArray[i].link,
        datetime:aShowsArray[i].datetime,
        duration:aShowsArray[i].duration,
        html:aShowsArray[i].html,
        showtype:aShowsArray[i].showtype,
        mp3:aShowsArray[i].mp3,
        ra:aShowsArray[i].ra,
        chat:aShowsArray[i].chat,
        maximized:false
      };
    }
    
    //set total number of returns
    top.rs_length = anRSLength;
    
    // check if new query and show navigation row if so
    if ( !top.lib_isSameQuery ( aQuery ) )
      top.showNavigation();
      
    // set the query object and searchform
    top.current_query = aQuery;
    top.tmp_searchform = {
                           ftquery: aQuery.ftquery,
                           andit: ( aQuery.ftflags.substring ( 0, 1 ) == "1" ) ? true : false,
                           whole: ( aQuery.ftflags.substring ( 1, 2 ) == "1" ) ? true : false,
                           guestid: aQuery.guestid,
                           startdate: lib_prepareDateForUI ( aQuery.startdate ),
                           enddate: lib_prepareDateForUI ( aQuery.enddate ),
                           subject: aQuery.subject
                         };
                           
    // set the global sort_order
    top.sort_order = aSortorder;
        
    // store current_query in cookie
    top.storeQueryCookie();        
  } 
  top.clearChannel();
}


/************ communication functions ************/

function doRequest(aMode, anFtquery, someFTFlags, aGuestid, aStartdate, anEnddate, aSubject, aShowid) {
	/* Set query object and do request, ONLY for initial requests.
	 * For paging getShows is used directly!
	 * If last param is present properties of query as params are ignored!
	 */
	
	storeCurrentQuery();
    
    // current query will be set to new query by request
	tmp_query = {
                      mode:aMode,
                      ftquery: (anFtquery)? anFtquery + '' : top.default_query.ftquery,
                      ftflags: (someFTFlags)? someFTFlags + '' : top.default_query.ftflags,
                      guestid: (aGuestid)? aGuestid + '' : top.default_query.guestid,
                      startdate: (aStartdate)? aStartdate + '' : top.default_query.startdate,
                      enddate: (anEnddate)? anEnddate + '' : top.default_query.enddate,
                      subject: (aSubject)? aSubject + '' : top.default_query.subject,
                      showid: (aShowid)? aShowid + '' : top.default_query.showid
                };
	
	getShows(tmp_query, top.getDefaultEntriesString());
}

 
function doDefaultQuery() {
	/* execute the default query */

	storeCurrentQuery();

	// paranoid...
	clearClientCache();
	
	// execute default query (constant)  
	getShows(top.default_query, getDefaultEntriesString());
}

function getShows(query, entries) {
	/* request missing entries from server for caching */
	
	top.deactivateAppLinks();
	top.lib_showLoadingIcon();
	
	// check if query has changed and clear cache if so!
	if ( !lib_isSameQuery ( query ) )
	  top.clearClientCache();

	// add parameters from query object supplied
	var query_params = "";
	for (var prop in query) {
		query_params += prop + "=" + query[prop] + "&";
	}
	
	// set sortorder (not a part of prop object!)
	query_params += "sortorder=" + top.sort_order + "&";

	// request guest list (1st request should have taken care)
	if (top.guests_cache == null) {
		query_params += "guestcache=false&";
	} else {
		query_params += "guestcache=true&";
	}
		
	// request subject list (1st request should have taken care)
	if (top.subjects_cache == null) {
		query_params += "subjectcache=false&";
	} else {
		query_params += "subjectcache=true&";
	}

	top.comFrame.location.replace ( host_name + "/cropfm/jsp/queryresults.jsp?" + 
								    query_params +
								    "entries=" + entries );
}

function getHtml(entries) {
	/* request missing html from server for caching */

	top.deactivateAppLinks();
	top.lib_showLoadingIcon();

	// add parameters from query object
	var query_params = "";
	for (var prop in top.current_query) {
		query_params += prop + "=" + top.current_query[prop] + "&";
	}

	// set sortorder (not a part of prop object!)
	query_params += "sortorder=" + top.sort_order + "&";

	top.comFrame.location.replace ( host_name + "/cropfm/jsp/gethtmlcontent.jsp?" + 
								    query_params +
								    "entries=" + entries );
}