var baseURL   = getBaseLoc();
var imagesURL = getImagesLoc();
var ajaxURL   = getAjaxLoc();

var AdView = {
  getContactForm: function(adKey) {
  	$j.ajax({
  		type: "GET",
  		url: ajaxURL+"ad/get_contact_form.php",
  		data: ({ 
  			'k': adKey
  		}),
  		success: function(msg){
  		 $j("#adContactFormParent").html(msg);
  		}
  	});
  },

  displayContactForm: function(adKey) {
  	if($j("#adContactFormParent").html().length == 0) {
  	  $j("#adContactFormParent").html("<img src='"+imagesURL+"spinny_green.gif' />");
  		AdView.getContactForm(adKey);
  	}
  	$j("#viewContactFormLink").css( {width: 'auto'} );
    $j("#adContactFormParent").show();
  	
//  	if($j("#adContactFormParent"+id).css("display") == "none") {
//  	   $j("#viewContactFormLink"+id).animate(
//  	     {
//    	     width: '300px',
//    	     height: '285px'
//  	     }, 
//  	     250, 
//  	     function() {
//  	       $j("#adContactFormParent"+id).fadeIn(400);
//  	     }
//  	   );
//  	}
  },
  
  hideContactForm: function() {
    $j("#adContactFormParent").fadeOut(400, function() {
  	  $j("#viewContactFormLink").animate( { width: '150px'}, 500);
  	});
  },
  
  submitContactForm: function() {
    AdView.toggleContactFormButtons();
    
  	$j.ajax({
  		type: "POST",
  		url: ajaxURL+"ad/submit_contact_form.php",
  		data: $j("#adContactForm").serialize(),
  		success: function(msg){
  			$j("#contactFormSuccess").html(msg);
  			$j("#contactFormSuccess").show();
  			$j("#viewContactFormLink").hide();
  			$j("#adContactFormParent").hide();
  			AdView.toggleContactFormButtons();
  		},
  		// xhr = XMLHttpRequest object
  		error: function (xhr, ajaxOptions, thrownError) {
        if(xhr.status == 500) {
//          c("is 500 error responseText: " + xhr.statusText);
//          c("thrown error is " + thrownError);
//          c(xhr);
          
          $j("#contactFormAlert").html(xhr.statusText).show();
//  			  $j("#contactFormAlertRow").show();
        }
        else {
          $j("#contactFormAlert").html("Oops. An error has occurred.");
  			  $j("#contactFormAlertRow").show();
        }
        AdView.toggleContactFormButtons();
      } 
  	});
  },
  
  toggleContactFormButtons: function() {
    $j("#contactFormButtons").toggle();
    $j("#contactFormButtonsSpinny").toggle();
  },

  go: function(key) {
    window.location = baseURL+"ad/go.php?k=" + key;
  },
  
  confirmDeleteToggle: function() {
    if($j("#adDeleteLink").css("display") == "none") {
      $j("#adDeleteConfirm").hide("fast", function() {
        $j("#adDeleteLink").show("fast");
      });
    } else {
      $j("#adDeleteLink").hide("fast", function() {
        $j("#adDeleteConfirm").show("fast");
      });
    }
  }
}