PTV.namespace("ajax");
/**
 * Periodically requests a url via ajax and processes the response.
 * 
 * @param {String} url a url to be used as the data source
 * @param {Function} renderHandler a function to process the response
 * @param {Integer} interval time between requests in millis
 * @param {Function} errorHandler Optional function to process errors
 * @author Mark Steyn
 * @depends /js/jquery-1.2.js, /js/ptv.js, /js/crossDomainAjax.js
 */
PTV.ajax.AjaxInterval = function(url, renderHandler, interval, errorHandler) {
   
  /**
   * Current url.
   */
  this.url = url;
	
  /**
   * Current interval. 
   */
  this.interval = interval;
  
  /**
   * Change the request url and process it immediately
   * @param {String} url The url to request.   
   */
  this.setUrl = function(url) {
   this.url = url;
   this.start();
  }

  /**
   * Request and process the url.
   */  
  this.start = function() {

    if (this.timeout) {
      clearTimeout(this.timeout);
    }

    //
    // generate unique request every 10 seconds
		//
		
		var timestamp = "?" + (new Date()).getTime();
		timestamp = timestamp.substring(0, timestamp.length - 4);
		
		$.ajax({
			url: this.url + timestamp,
			dataType: "xml",
			success: renderHandler,
			error: errorHandler
		});

    if(this.interval > 0) {
      var obj = this;
      this.timeout = setTimeout(function() {obj.start()}, this.interval);
    }

    return;
  };
  this.start();
	
  /**
   * Sets the interval.
   * @param {Object} interval
   */
  this.setInterval = function(interval) {
    this.interval = interval;
  }
	
  /**
   * Stops processing requests.
   */
  this.stop = function() {

    if (this.timeout) {
      clearTimeout(this.timeout);
    }		
  }
}

PTV.ajax.CrossDomainAjaxInterval = function(crossDomainFrameName, domain, url, renderHandler, interval, errorHandler) {
	
	this.crossDomainFrameName = crossDomainFrameName;
	
	this.domain = domain;
	  
	/**
   * Current url.
   */
  this.url = url;
	
  /**
   * Current interval. 
   */
  this.interval = interval;
	  
  /**
   * Change the request url and process it immediately
   * @param {String} url The url to request.   
   */
  this.setUrl = function(url) {
	 this.url = url;
   this.start();
  }

  /**
   * Request and process the url.
   */  
  this.start = function() {
  	if (this.timeout) {
      clearTimeout(this.timeout);
    }
  	
  	if (this.url != "") {
	  	try {
				this.getCrossDomainContent(this.domain, this.url, renderHandler, errorHandler);
			} catch (e) {
				this.getNonCrossDomainContent(this.url, renderHandler, errorHandler);
			}
  	}
		
		if (this.interval > 0) {
      var obj = this;
      this.timeout = setTimeout(function() {obj.start()}, this.interval);
    }
  };
	  
  this.getNonCrossDomainContent = function(path, renderHandler, errorHandler) {
	  try {
	  $.ajax({
			url: path,
			cache: false,
			dataType: "xml",
			success: renderHandler,
			error: errorHandler
		});
	  } catch (e) {;}
	}
	  
  this.getCrossDomainContent = function(domain, path, renderHandler, errorHandler) {
  	var crossDomainAjax = new PTV.ajax.CrossDomainAjax(this.crossDomainFrameName, domain, path, false, true);
  	crossDomainAjax.getContent(renderHandler, errorHandler);
	}
	  
  this.start();
		
  /**
   * Sets the interval.
   * @param {Object} interval
   */
  this.setInterval = function(interval) {
    this.interval = interval;
  }
		
  /**
   * Stops processing requests.
   */
  this.stop = function() {
  	if (this.timeout) {
      clearTimeout(this.timeout);
    }		
  }
}
