

$(document).ready(function(){

	//$("#categories").columnize({columns: 2, balanced: false});
	//$("#subcategories").columnize({columns: 2, balanced: false});
	
	document.forms.searchForm.what.focus()

  // override some default options
  // http://plugins.learningjquery.com/expander/index.html#options
  $('div.expander').expander({
    slicePoint:       120,  // default is 100
    expandText:         'read more', // default is 'read more...'
    collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    userCollapseText: 'collapse text'  // default is '[collapse expanded text]'
  });
  
jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};





$("a.expcoll").click(function(){

	var togglelink = $(this);
	var togglebox = $("#" + this.rel);
	var toggleboxdisplay = $("#" + this.rel).css("display");

	if ( toggleboxdisplay == 'none' ){
		togglebox.slideFadeToggle();
		togglelink.html("collapse");
    togglelink.removeClass('expand');
    togglelink.addClass('collapse');
 		$.cookie(this.rel, 'expanded', {expires: 14, path: '/'});
	};

	if ( toggleboxdisplay == 'block' || toggleboxdisplay == 'inline-block' ){

		togglebox.slideFadeToggle();
		togglelink.html("expand");
    togglelink.removeClass('collapse');
    togglelink.addClass('expand');
		$.cookie(this.rel, 'collapsed', {expires: 14, path: '/'}); 
	};
	return false;
});

var cookiesubcategories = $.cookie('subcategories');
var cookierelatedcategories = $.cookie('relatedcategories');
var cookieneighbourcategories = $.cookie('neighbourcategories');
 
if (cookiesubcategories == 'collapsed') {
	$("#subcategories").hide();
	$("#expcollsubcategories").html("expand");
  $("#expcollsubcategories").removeClass('collapse');
  $("#expcollsubcategories").addClass('expand');
	}; 

if (cookierelatedcategories == 'collapsed') {
	$("#relatedcategories").hide();
	$("#expcollrelatedcategories").html("expand");
  $("#expcollrelatedcategories").removeClass('collapse');
  $("#expcollrelatedcategories").addClass('expand');
	}; 

if (cookieneighbourcategories == 'collapsed') {
	$("#neighbourcategories").hide();
	$("#expcollneighbourcategories").html("expand");
  $("#expcollneighbourcategories").removeClass('collapse');
  $("#expcollneighbourcategories").addClass('expand');
	}; 




//clickcount
$('a').click(function() {

  	if ( this.id != '' ) {

	 		var tp = this.id.charAt(0);
	 		link = $(this).attr('href');
	 
	 		if(tp != 'l' &&  tp != 'c')
			{
				return true;
			}
			
			if(this.id.length < 2)
			{
				return true;
			}
			
			var itemid = this.id.substring(1);
			if(parseInt(itemid.charAt(0)) < 1 || itemid.match(/\D/))
			{
				return true;
			}
			
			var h = Math.random();
			$.get("/click-count.php", { id:itemid, type:tp, h:h }, function()
				{ 
					if(tp == 'c') { window.location = link; }
					if(tp == 'l') { window.open(link); }
				});
				return false; 
   	}
});


// track outgoing links with google analytics

$('a').each(function() {
  var $a = $(this);
  var href = $a.attr('href');
 
  // see if the link is external
  if ( (href.match(/^http/)) && (! href.match(document.domain)) ) {
 
    // if so, add the GA tracking code
    $a.click(function() {
      pageTracker._trackPageview('/outgoing/' + href);
    });
  }
 
});




// same page scrolls



function filterPath(string) {
    return string
      .replace(/^\//,'')  
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')  
      .replace(/\/$/,'');
  }
  $('a[href*=#]').each(function() {
    if ( filterPath(location.pathname) == filterPath(this.pathname)
    && location.hostname == this.hostname
    && this.hash.replace(/#/,'') ) {
      var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
       if ($target) {
         var targetOffset = $target.offset().top;
         $(this).click(function() {
           $('html, body').animate({scrollTop: targetOffset}, 400);
           return false;
         });
      }
    }
  });






});























function reportBrokenListing(id, msg)
{
	var answer = confirm(msg);

	if(answer)
	{
		$.get("report-listing.php", {id: id, report: 1}, function(data)
		{
			$("#tdlisting" + id).after(data);
		});
	}
}

function actionFavorites(listing_id, account_id, action, refresh, remove_phrase, add_phrase)
{
	var msg = ('add' == action) ? add_phrase : remove_phrase;
	var conf = confirm(msg);
	
	if (conf)
	{
		$.post("favorites.php", {account_id: account_id, listing_id: listing_id, action: action, refresh: refresh}, function(data)
		{
			$("#af_" + listing_id).text();
			$("#af_" + listing_id).html(data);
		});
	}

	return false;
}