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

var AdList = {
  showAdDetails: function(key, queryString) {
    $j("#dataFilter").hide();  
    $j("#adListContainer").hide();
    AdList.getAdDetails(key);
  },
  
  // test
  showAdDetailsPopup: function(el, key) {
//    var detailsPopup = $j("#adDetailsContainer");
//		var smallAdPosition = $j(el).offset();
//    detailsPopup.css( { 'position': 'absolute',
//    			              'top': smallAdPosition.top + $j(el).height() + 5,
//    			              'z-index': 99999
//    									});
    									
    AdList.getAdDetails(key);
//    detailsPopup.fadeIn("fast");
  },
  // end test
  
  getAdDetails: function(key) {
    //$j(document).unbind("endlessScroll");
  	$j.ajax({
  		type: "GET",
  		url: ajaxURL + "ad/get_ad_details.php",
      data: ({ 
  			'k': key
  		}),
  		beforeSend: function() {
//  		  $j("#adDetailsContainer").show("fast");
//  		  $j("#seeOlderPostsHighlight").hide();
  		  $j("#adDetails").html("<img src='"+imagesURL+"icon_loading.gif' />");
  		},
  		complete: function() {
//  		  $j("loading").html("&nbsp;");
  		},
  		success: function(msg){
  		  $j("#adDetails").html(msg);
  		  $j("#adDetailsContainer").dialog( { modal:     true,
        		                                resizable: false,
  		                                      width:     890,
  		                                      position: 'center'});
  		}
  	});
  },
  
  hideAdDetails: function() {
    $j("#adDetailsContainer").toggle("fast", function() {
      $j("#dataFilter").toggle("fast");
      $j("#adListContainer").toggle("fast");
      
      // cant toggle() here because the display might already be displayed
      $j("#olderPostsLink").css( {'display': 'block'} );
    });
  },
  
  adSearch: function(formName) {
  	$j.ajax({
  		type: "POST",
  		url: ajaxURL + "ad/ad_search.php",
  		data: $j("#"+formName).serialize(),
  		beforeSend: function() {
  		  $j("#getMoreArrow").hide();
  		  $j("#getMoreSpinny").show();
  		},
  		complete: function() {
  		  $j("#getMoreSpinny").hide();
  		  $j("#getMoreArrow").show();
  		},
  		success: function(msg){
//        $j("#adSmallContainer:last").after(msg);
        $j("#seeOlderPostsHighlight").before(msg);
        if(parseInt($j("#currentPage").val()) >= parseInt($j("#totalPages").val())) {
          $j("#seeOlderPostsHighlight").hide();
          $j("#noMorePosts").show();
        }
  		}
  	});
  },

  paginate: function(formName) {
//    AdList.currentPageNumber++;
    var currPageNum = parseInt($j("#currentPage").val());
    var totalPages  = parseInt($j("#totalPages").val());
//    c("currently have " + parseInt($j("#resultsPerPage").val()) * parseInt(currPageNum+1) + " of "+$j("#totalCount").val()+" total results");
//    c("currPageNum = " + parseInt(currPageNum+1));
//    c("totalPages = " + totalPages);
    
    if(currPageNum < totalPages) {
      $j("#currentPage").val(currPageNum + 1);
      AdList.adSearch(formName);
    }
  },
  
  highlightNewResults: function() {
    // gets all of the siblings after the selector (#adSmallHighlight);
    // this doesnt work quite right, as there seems to be a bug where 
    // animating from a color to "transparent" doesnt work.
    // Also apparently causes 100% CPU utilization, so that's not good
//    $j(".firstResult:last").nextAll(".adSmallHighlight").css('border', '1px solid red');
    $j(".firstResult:last").nextAll(".adSmallHighlight").stop().animate({ backgroundColor: '#EDFFDF' }, 300, function() { $j(this).stop().animate({ backgroundColor: '#E8E8E2' }, 500); c("done2") });
  },
  
  init: function() {
    // Using the .stop() method before the animate() fixes the animation queue 
    // buildup where the animation will loop repeatedly by moving your mouse back 
    // and forth over the item.
    $j(".adSmallHighlight").hover(
      function() {
  			$j(this).css({ 'background-color': '#f4f0e6' });
//  			$j(this).stop().animate({ 'background-color': '#EDFFDF' }, 300);
  		}, 
  		function() {
  		  $j(this).css({ 'background-color': '' });
//  		  $j(this).stop().animate({ 'background-color': 'transparent' }, 300);
  		}
    );
  }
}