$(document).ready(function() {

  /* Bindings */
  
  var wt = PRIMEDIA.WebTrendsTag;

  $('.wt_lc_0005').click(function(){ // Check Availability button on detail page
    wt.listingid = wt.getListingId(this); //Dynamically adding property to wt reference - needed to populate listing id for lead form tag
    wt.fire({
      clicktype:"propertyoverview",
      position: "contact2",
      a_l_cp_click: wt.listingid,
      LISTINGID: wt.listingid
    });
  });
  
  $('.wt_lc_0007').click(function(){ // Check Availability button on search result page
    wt.listingid = wt.getListingId(this); //Dynamically adding property to wt reference - needed to populate listing id for lead form tag
    wt.fire({
      clicktype:"searchresult",
      position: "contact",
      a_l_cp_click: wt.listingid,
      LISTINGID: wt.listingid
    });
  });

  $('.wt_lc_0015').click(function(){ // View Slideshow button on details page
    wt.listingid = wt.getListingId(this); //Dynamically adding property to wt reference - needed to populate listing id for lead form tag
    wt.fire({
      clicktype: "photos",
      a_l_photos: wt.listingid
    });
  });

  $('.wt_lc_0042').click(function(){ // View Slideshow button on details page
    wt.listingid = wt.getListingId(this); //Dynamically adding property to wt reference - needed to populate listing id for lead form tag
    wt.fire({
      clicktype:"slideshow",
      a_l_slideshow: wt.listingid
    });
  });

  $('.wt_lc_0043').click(function(){ // View Slideshow button on details page
    wt.listingid = wt.getListingId(this); //Dynamically adding property to wt reference - needed to populate listing id for lead form tag
    wt.fire({
      clicktype:"propertyphoto",
      a_l_propphoto: wt.listingid
    });
  });

  $('.wt_lc_0044').click(function(){ // View Slideshow button on details page
    wt.listingid = wt.getListingId(this); //Dynamically adding property to wt reference - needed to populate listing id for lead form tag
    wt.fire({
      clicktype: "photos",
      a_l_photos: wt.listingid
    });
  });

  /************** Lead Form **************/
  
  $('.lead_submit').live('click',function(){
    var lead_form = $('form.lead_form').addClass($(this).parents().is('div#inline_leadform') ? 'wt_lc_000A' : 'wt_lc_002Y');
    $(lead_form).submit(function(){
      $(lead_form).unbind('ajaxSuccess'); //Remove any previously bound ajaxSuccess functions for other forms 
      
      $(this).ajaxSuccess(function(event, XMLHttpRequest, ajaxOptions){
        if(ajaxOptions.type == 'POST'){ //This conditional is needed to prevent ajaxSuccess from firing on page load 
          var listing_id = wt.getMetaAttr("DCSext.A_L_PV") || $('input.lead_entity_id').val();
          clicktype = 'LeadSubmitted';
          // Use clicktype of LeadSubmitted2 only on the popup on propertyoverview page
          if (!$(this).parents().is('div#inline_leadform') && (wt.getMetaAttr('DCSext.WT.cg_n') == 'propertyoverview') ) clicktype += '2';
          var wt_fire_params = { //Default parameters that get fired w/ every Lead submission
            LEADCOUNT: "1",
            LEADTYPE: "email",
            clicktype: clicktype,
            a_l_cp_click: listing_id,
            LISTINGID: listing_id,
            NAME: $(this).parent().find('input.lead_first_name').val(),
            EMAIL: $(this).parent().find('input.lead_email').val(),
            PHONE: $(this).parent().find('input.lead_phone').val()          
          };
          
          wt.fire(wt_fire_params); //Fire the tag
          
        }
      });  
    });
  });
  
});