function setPriceAlert(productName, productImage, productUrl, productPrice, category, duration) {
  var dialog = new priceAlertDialog(
      productName, productImage, productUrl, productPrice, category, duration);
  dialog.show();
}

var priceAlertDialog = function(name, imageUrl, url, price, category, duration) {
  this.maskingDiv;
  this.contentFrame;
  this.timer;
  
  // These values are set by the calling script.
  this.productImage = imageUrl;
  this.productUrl = url;
  this.productName = name;
  this.productPrice = price;
  this.productCategory = category;
  this.duration = !duration || Number(duration) == NaN ? 30 : duration;
  
  this.createInternalStructure();
  this.addEventListeners();
};

priceAlertDialog.baseZIndex = 1000;
priceAlertDialog.contentWidth = 430;
priceAlertDialog.contentHeight = 380;
priceAlertDialog.pollIntervalMs = 200;

priceAlertDialog.ZOrdering = {
  MASKING_DIV: 1,
  CONTENT_FRAME: 2  
};

priceAlertDialog.Id = {
  MASKING_DIV: 'pp_md',
  CONTENT_FRAME: 'pp_cf'  
};


priceAlertDialog.prototype.createInternalStructure = function() {
  if (!document.getElementById(priceAlertDialog.Id.MASKING_DIV)) {
	  var maskingDiv = document.createElement('div');
	  maskingDiv.setAttribute('id', priceAlertDialog.Id.MASKING_DIV);
	  var style = maskingDiv.style;
	  style.position = 'absolute';
      style.top = '0';
      style.left = '0';
	  style.zIndex = priceAlertDialog.baseZIndex + priceAlertDialog.ZOrdering.MASKING_DIV;
	  style.opacity = '.40';
	  style.backgroundColor = '#000';
	  style.filter = 'alpha(opacity=40)';
	  style.display = 'none';
	  document.body.appendChild(maskingDiv);

	  var contentFrame = document.createElement('iframe');
	  contentFrame.setAttribute('id', priceAlertDialog.Id.CONTENT_FRAME);
	  style = contentFrame.style;
	  style.position = 'absolute';
	  style.width = priceAlertDialog.contentWidth + 'px';
	  style.height = priceAlertDialog.contentHeight + 'px';
	  style.zIndex = priceAlertDialog.baseZIndex + priceAlertDialog.ZOrdering.CONTENT_FRAME;
	  style.border = '2px solid #649d0b';
	  style.display = 'none';
	  document.body.appendChild(contentFrame);
  }
  
  this.maskingDiv = document.getElementById(priceAlertDialog.Id.MASKING_DIV);
  this.contentFrame = document.getElementById(priceAlertDialog.Id.CONTENT_FRAME);
  this.contentFrame.src = this.getIframeUrl();
};


priceAlertDialog.prototype.getIframeUrl = function() {

  var src = 'http://www.priceprotectr.com/widget/pricealert/v1/pricealert.jsp';
  //var src = 'http://www.testpp.com:81/PP-Beta-Svn/widget/pricealert/v1/pricealert.jsp';
  src += '?name=' + escape(this.productName);
  src += '&url=' + escape(this.productUrl);
  src += '&duration=' + escape(this.duration);
  if (this.productImage) {
    src += '&image=' + escape(this.productImage);
  }
  if (this.productPrice) {
    src += '&price=' + escape(this.productPrice);
  }
  if (this.productCategory) {
    src += '&cat=' + escape(this.productCategory);
  }
  var thisPage = top.location.href;  
  if (thisPage) {   
    var referringDomain = thisPage.match(/:\/\/(.[^/]+)/)[1];  
    src += '&source=' + escape(referringDomain);
  }
  
  return src;
};


priceAlertDialog.prototype.addEventListeners = function() {
  this.addEventListener(this.maskingDiv, 'click', this.hide.bind(this), false);
};


priceAlertDialog.prototype.addEventListener = function(elt, event, fn, useCapture) {
  if (elt.addEventListener){
    elt.addEventListener(event, fn, useCapture); 
  } else if (elt.attachEvent){
    elt.attachEvent('on' + event, fn);
  }
};


priceAlertDialog.prototype.show = function() {
  var windowWidth = (typeof window.innerWidth != 'undefined' ? 
      window.innerWidth : document.body.offsetWidth);
  var windowHeight = (typeof window.innerHeight != 'undefined' ? 
      window.innerHeight : document.body.offsetHeight);

  var scrollTop = document.body.scrollTop ? document.body.scrollTop
    : (window.pageYOffset ? window.pageYOffset
        : (document.body.parentElement ? document.body.parentElement.scrollTop : 0));

  var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft 
    : (window.pageXOffset ? window.pageXOffset
        : (document.body.parentElement ? document.body.parentElement.scrollLeft : 0));

  this.maskingDiv.style.height = 
      Math.max(windowHeight, document.documentElement.offsetHeight) + 'px';
  this.maskingDiv.style.width = document.documentElement.offsetWidth + 'px';

  this.contentFrame.style.left = 
      scrollLeft + (windowWidth - priceAlertDialog.contentWidth) / 2 + 'px';
  this.contentFrame.style.top = 
      scrollTop + (windowHeight - priceAlertDialog.contentHeight) / 2 + 'px';

  this.maskingDiv.style.display = 'block';
  this.contentFrame.style.display = 'block';
  
  this.startPolling();
};


priceAlertDialog.prototype.startPolling = function() {
  this.timer = setInterval(this.poll.bind(this), priceAlertDialog.pollIntervalMs);
};


priceAlertDialog.prototype.stopPolling = function() {
  clearInterval(this.timer);
};


priceAlertDialog.prototype.poll = function() {
  var hash = window.location.hash;
  if (hash == '#close') {
    this.hide();
    window.location.hash = '';
  }
};


priceAlertDialog.prototype.hide = function() {
  this.stopPolling();
  this.maskingDiv.style.display = 'none';
  this.contentFrame.style.display = 'none';
  this.contentFrame.src = '';
};


if (!Function.prototype.bind) {
  Function.prototype.bind = function(obj) {
    var method = this,
    temp = function() {
      return method.apply(obj, arguments);
    };
 
    return temp;
  }
}
