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

   /* configure */
   var show_list_url    = 'http://www.tvpassport.com/tvmds/show_list/show_list.php';

   // defaults
   var default_subid    = '';    // leaving them blank so they can be set in the PHP script 
   var default_date     = '';
   var default_time     = '';
   var default_filter1     = '';
   var default_filter2     = '';
   var default_items    = '';
   var default_tz       = '';
   var default_width    = '';
   var default_height   = '';
   var default_mode     = 'iframe';
   var default_css_url  = '';
   var default_show_type = '%';
   var default_link = '';


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

   /* functions */

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

	function tvmds_show_list_write_iframe(query) {
      document.write('<iframe '
                   + 'name="tvmds_show_list_frame" '
                   + 'id="tvmds_show_list_frame" '
                   + 'width="' + width  + '" '
                   + 'height="' + height + '" '
                   + 'src="' + show_list_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_show_list_subscription_id;
   var date    = window.tvmds_show_list_for_date;
   var time    = window.tvmds_show_list_for_time;
   var items   = window.tvmds_show_list_show_num_items;
   var filter1 = window.tvmds_show_list_filter1;
   var filter2 = window.tvmds_show_list_filter2;
   var tz      = window.tvmds_show_list_time_zone;
   var width   = window.tvmds_show_list_width;
   var height  = window.tvmds_show_list_height;
   var mode    = window.tvmds_show_list_mode;
   var css_url = window.tvmds_show_list_iframe_stylesheet_url;
   var show_type = window.tvmds_show_list_show_type;
   var tvlink = window.tvmds_show_list_listings_link;

   // assign default values to any unassigned options
   if (subid == null)   subid    = default_subid;
   if (date == null)    date     = default_date;
   if (time == null)    time     = default_time;
   if (filter2 == null)	filter1  = default_filter1;
   if (filter2 == null)	filter2  = default_filter2;
   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 (show_type == null) show_type = default_show_type;
   if (tvlink == null)	tvlink	 = default_link;


   // 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_show_list');
   args = getArgsFromParameterString(string);
   var date = '';
   if (args.date) date = args.date;
   if (args.filter1) filter1 = args.filter1;
   if (args.filter2) filter2 = args.filter2;
	if (args.time) time= args.time;

   // build query string
   var query = '?subid='	+ subid
             + '&date='		+ date
             + '&time='		+ time
             + '&filter1='  + filter1
             + '&filter2='  + filter2
             + '&items='	+ items
             + '&tz='		+ tz
             + '&width='	+ width
             + '&height='   + height
             + '&mode='		+ mode
			 + '&show_type='   + show_type
			 + '&tvlink='	+ escape(tvlink)
		     + '&css_url='	+ escape(css_url)
             + '&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_show_list_write_iframe(query);
   } else {    // choose or default to 'inline' 
      tvmds_show_list_write_html(query);
   }


})()