if (typeof YAHOO.Edmunds == 'undefined') {
  YAHOO.Edmunds = {};
}

YAHOO.Edmunds.PDPAd = function(id, make, styleid, usein, pagecode, dealer_specials_uri, author) {
  this.id = id;
  this.make = make;
  this.styleid = styleid;
  this.usein = usein;
  this.pagecode = pagecode;
  this.dealer_specials_uri = dealer_specials_uri;
  this.author = author;
  
  YAHOO.util.Event.addListener(window, 'load', this.handleOnContentReady, this); 
}; 

YAHOO.Edmunds.PDPAd.prototype = { 
  
  handleSuccess: function(o) {

    if(o.responseText !== undefined) {
    try {
      var data = YAHOO.lang.JSON.parse(o.responseText); 
      var template_html = '<img width="1" height="1" src="http://dl.edmunds.com/media/trans1x1.gif?app=dl_pdp&pagecode={pagecode}&zip={zipcode}&styleid={styleid}&fid={pdp_dealer_id}&usein={usein}&dlts={timestamp}"></img><br /><table valign="top" class="pdp-promo-ad" border="0" cellpadding="0" cellspacing="0"><tbody><tr><th><img src="http://a332.g.akamai.net/f/332/936/12h/www.edmunds.com/media/pdp/logo_pdp.gif" alt="Premier Dealer" border="0"><br></th></tr><tr><td><dl><dt><strong id="pdp-ad-dealer-name">{pdp_dealer_name}</strong><br><span id="pdp-ad-dealer-phone">{pdp_dealer_phone}</span></dt><dd><a id="pdp-ad-dealer-link-1" target="_blank" href="{pdp_ad_dealer_link1_href}" onclick="{pdp_ad_dealer_link1_onclick}">{pdp_ad_dealer_link1}</a></dd><dd><a id="pdp-ad-dealer-link-2" target="_blank" href="{pdp_ad_dealer_link2_href}" onclick="{pdp_ad_dealer_link2_onclick}">{pdp_ad_dealer_link2}</a></dd><dd><a target="_blank" id="pdp-ad-dealer-specials" href="{pdp_ad_dealer_specials_href}" onclick="{pdp_ad_dealer_specials_onclick}"><img src="http://a332.g.akamai.net/f/332/936/12h/www.edmunds.com/media/pdp/moreAboutTheDealer.gif" alt="More About This Dealer" border="0"></a></dd></dl></td></tr></tbody></table>';
      var template = new YAHOO.ext.DomHelper.Template(template_html);
      var current_page = window.top.location.href;
      var dealer_specials_url = this.dealer_specials_uri != null && this.dealer_specials_uri.length > 0 ? this.dealer_specials_uri + '?stepPage=dladvform' : 'undefined';
      var zip = YAHOO.Edmunds.Util.getQueryString('zip');

      if(zip == null || zip == 'undefined' || zip.length <= 0) {
        zip = YAHOO.Edmunds.Util.getQueryString('setzip');
      }

      if(zip == null || zip == 'undefined' || zip.length <= 0) {
        zip = YAHOO.Edmunds.UserTrack.getZipCookie();
      }

      template.append(this.id, {
      zipcode: zip,
      styleid: this.styleid,
      usein: this.usein,
      pagecode: this.pagecode,
      timestamp: new Date().getTime(),
      pdp_dealer_id: data.outputParameters.dealers[0].id,
      pdp_dealer_name: data.outputParameters.dealers[0].name, 
      pdp_dealer_phone: data.outputParameters.dealers[0].contactInfo.displayPhone,
      pdp_ad_dealer_link1_href: data.outputParameters.dealers[0].displayInfoPrimary.URL,
      pdp_ad_dealer_link1_onclick: "s_objectID='edmunds." + this.usein + ".dealerpricing." + data.outputParameters.dealers[0].id + ".pdpbox.1." + data.outputParameters.dealers[0].make + "*'; setTID('edmunds.n.dealerpricing." + data.outputParameters.dealers[0].id + ".pdpbox.1." + data.outputParameters.dealers[0].make + "*');",
      pdp_ad_dealer_link1: data.outputParameters.dealers[0].displayInfoPrimary.description,
      pdp_ad_dealer_link2_href: data.outputParameters.dealers[0].displayInfoSecondary.URL,
      pdp_ad_dealer_link2_onclick: "s_objectID='edmunds." + this.usein + ".dealerpricing." + data.outputParameters.dealers[0].id + ".pdpbox.2." + data.outputParameters.dealers[0].make + "*'; setTID('edmunds.n.dealerpricing." + data.outputParameters.dealers[0].id + ".pdpbox.2." + data.outputParameters.dealers[0].make + "*');",
      pdp_ad_dealer_link2: data.outputParameters.dealers[0].displayInfoSecondary.description,
      pdp_ad_dealer_specials_href: dealer_specials_url,
      pdp_ad_dealer_specials_onclick: "s_objectID='edmunds." + this.usein + ".dealerpricing." + data.outputParameters.dealers[0].id + ".pdpboxmore.1g." + data.outputParameters.dealers[0].make + "*'; setTID('edmunds.n.dealerpricing." + data.outputParameters.dealers[0].id + ".pdpboxmore.1g." + data.outputParameters.dealers[0].make + "*');"
      });

      var container = document.getElementById(this.id);
      container.style.display='';
      
      if(data.outputParameters.dealers[0].contactInfo.displayPhone && data.outputParameters.dealers[0].contactInfo.displayPhone == 'undefined') {
        document.getElementById("pdp-ad-dealer-phone").style.display='none';
      }
      if(data.outputParameters.dealers[0].displayInfoPrimary.description && data.outputParameters.dealers[0].displayInfoPrimary.description == 'undefined') {
        document.getElementById("pdp-ad-dealer-link-1").style.display='none';
      }
      if(data.outputParameters.dealers[0].displayInfoSecondary.description && data.outputParameters.dealers[0].displayInfoSecondary.description == 'undefined') {
        document.getElementById("pdp-ad-dealer-link-2").style.display='none';
      }
      if(dealer_specials_url && dealer_specials_url == 'undefined') {
        document.getElementById("pdp-ad-dealer-specials").style.display='none';	
      }
    } catch(e) {
      var container = document.getElementById(this.id);
      container.style.display='none';
    }
    }

  },
  
  handleFailure: function(o) {
    var container = document.getElementById(this.id);
    container.style.display='none';
  },  

  handleOnContentReady: function(event, me) { 
    var callback = {
      cache:false,
      success: me.handleSuccess,
      failure: me.handleFailure,
      scope: me
    };

    // Start the transaction. 
    var zip = YAHOO.Edmunds.Util.getQueryString('zip');

    if(zip == null || zip == 'undefined' || zip.length <= 0) {
      zip = YAHOO.Edmunds.Util.getQueryString('setzip');
    }

    if(zip == null || zip == 'undefined' || zip.length <= 0) {
      zip = YAHOO.Edmunds.UserTrack.getZipCookie();
    }
    
    var dealerType = 'PREMIUM';
    var post_data = "make=" + me.make + "&zip=" + zip + "&dealerType=" + dealerType;
    var data_service = "/dealerlocator/dealerservice.html";
    
    if(me.author) {
      data_service = "/author/edmunds2/dealerlocator/dealerservice.html";
    } 

    if(me.styleid != null && me.styleid.length > 0) {
      post_data += "&styleId=" + me.styleid;
    }

    YAHOO.util.Connect.setDefaultPostHeader(false);
    YAHOO.util.Connect.initHeader('Content-Type', 'application/x-www-form-urlencoded-ajax');
    YAHOO.util.Connect.asyncRequest('POST', data_service, callback, post_data);
  } 
};