/******************************************************************************
 *     Jon's crazy javascript thingy                                          *
 *                                                                            *
 *                                                                            *
 ******************************************************************************/
(function(){

   /* configure */
   var best_bets_url    = 'http://www.tvpassport.com/tvmds/best_bets/best_bets.php';

   // defaults
   var default_subid    = '';    // leaving them blank so they can be set in the PHP script 
   var default_date     = '';
   var default_items    = '';
   var default_tz       = '';
   var default_width    = '';
   var default_height   = '';
   var default_mode     = 'iframe';
   var default_css_url  = '';
   var default_tvlink   = '';


/******************************************************************************/

   /* functions */

   function tvmds_best_bets_write_html(query) {
      document.write('<script type="text/javascript" src="' + best_bets_url + query + '"></script>');
   }

function tvmds_best_bets_write_iframe(query) {
      document.write('<iframe '
                   + 'name="tvmds_bb_frame" '
                   + 'id="tvmds_bb_frame" '
                   + 'width="' + width  + '" '
                   + 'height="' + height + '" '
                   + 'src="' + best_bets_url + query + '" '
                   + 'frameborder="0" '
                   + 'marginwidth="0" '
                   + 'marginheight="0" '
                   + 'vspace="0" '
                   + 'hspace="0" '
                   + 'allowtransparency="true" '
                   + 'scrolling="false"></iframe>');
   }

   function extractParameterString(search_string, param_name) {
      var start_index = search_string.indexOf("?"+param_name+"=");
      if (start_index < 0) {
         start_index = search_string.indexOf("&"+param_name+"=");
      }
      if (start_index < 0) { 
         return '';
      }
      var end_index = search_string.indexOf("&",start_index+1);
      if (end_index < 0) {
         end_index = search_string.length;
      }
      return search_string.substring(start_index, end_index);
   }

   function getArgsFromParameterString(string) {
      var args = new Object();
      var start_index = string.indexOf("=");
      string = string.substring(start_index+1, string.length);
      var pairs = string.split(",");                // Break at comma
      for(var i = 0; i < pairs.length; i++) {
          var pos = pairs[i].indexOf('=');          // Look for "name=value"
          if (pos == -1) continue;                  // If not found, skip
          var argname = pairs[i].substring(0,pos);  // Extract the name
          var value = pairs[i].substring(pos+1);    // Extract the value
          args[argname] = unescape(value);          // Store as a property
      }
      return args; 
   }


   /* program start */

   // get options set from calling page
   var subid   = window.tvmds_best_bets_subscription_id;
   var date    = window.tvmds_best_bets_for_date;
   var items   = window.tvmds_best_bets_show_num_items;
   var tz      = window.tvmds_best_bets_time_zone;
   var width   = window.tvmds_best_bets_width;
   var height  = window.tvmds_best_bets_height;
   var mode    = window.tvmds_best_bets_mode;
   var css_url = window.tvmds_best_bets_iframe_stylesheet_url;
   var tvlink  = window.tvmds_best_bets_listings_link;

   // assign default values to any unassigned options
   if (subid == null)   subid    = default_subid;
   if (date == null)    date     = default_date;
   if (items == null)   items    = default_items;
   if (tz == null)      tz       = default_tz;
   if (width == null)   width    = default_width;
   if (height == null)  height   = default_height;
   if (mode == null)    mode     = default_mode;
   if (css_url == null) css_url  = default_css_url;
   if (tvlink == null)  tvlink   = default_tvlink;


   // get grid options encoded url query string
   // important: these values override those set by the subscriber in the calling page
   // security: clever subscribers could rewrite window.location somehow? 
   // fix: could have the control php script include a checksum parameter containing the 
   //      checksum of the parameters combined. This could then be decoded here to make
   //      sure the subscriber isn't cheating!
   // this will only return values if the calling page is using an inline grid
   string = extractParameterString(window.location.search, 'tvmds_best_bets');
   args = getArgsFromParameterString(string);
   var date = '';
   if (args.date) date = args.date;

   // build query string
   var query = '?subid='   + subid
             + '&date='    + date
             + '&items='   + items
             + '&tz='      + tz
             + '&width='   + width
             + '&height='   + height
             + '&mode='   + mode
	   		 + '&css_url=' + escape(css_url)
			 + '&tvlink=' + escape(tvlink)
             + '&loc='     + escape(window.location.href);

// Object.dpDump(args);
// Object.dpDump(query);

   // generate
     // send the build query string to the appropriate function
   if (mode =='iframe') {
         tvmds_best_bets_write_iframe(query);
   } else {    // choose or default to 'inline' 
      tvmds_best_bets_write_html(query);
   }


})()