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

   /* configure */
   var search_url    = 'http://www.tvpassport.com/tvmds/search/search.php';

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


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

   /* functions */

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

	function tvmds_search_write_iframe(query) {
      document.write('<iframe '
                   + 'name="tvmds_search_frame" '
                   + 'id="tvmds_searcg_frame" '
                   + 'width="' + width  + '" '
                   + 'height="' + height + '" '
                   + 'src="' + search_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_search_subscription_id;
   var tz      = window.tvmds_search_time_zone;
   var width   = window.tvmds_search_width;
   var height  = window.tvmds_search_height;
   var mode    = window.tvmds_search_mode;
   var css_url = window.tvmds_search_iframe_stylesheet_url;
   var tvlink  = window.tvmds_search_listings_link;

   // assign default values to any unassigned options
   if (subid == null)   subid    = default_subid;
   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_search');
   args = getArgsFromParameterString(string);

   // build query string
   var query = '?subid='   + subid
             + '&tz='      + tz
             + '&width='   + width
             + '&height='   + height
             + '&mode='   + mode
			 + '&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_search_write_iframe(query);
   } else {    // choose or default to 'inline' 
      tvmds_search_write_html(query);
   }


})()