if (typeof($j)=='undefined') throw 'jQuery library is required';
function writeKey() {
	var key;
  if (location.href.indexOf('hf.atomicmotion.com') > 0) key = 'ABQIAAAA1I6kEX8dPdH_9H3PyAIRBxQpTeFYbF4SavVJBY28KS6UJkNTjxR-qHAq5JZ_lBJZotTuNWHyhJz5sQ';
  else if (location.href.indexOf('homefocus.com') > 0)  key = 'ABQIAAAA1I6kEX8dPdH_9H3PyAIRBxS8We1PYI27TmevhnE0sbpunVggKBR5Ov1av7Xvsz0zxbUkiUkTrQIn-w';
  document.write('<scr'+'ipt src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='+key+'"></scr'+'ipt>');
}
function expandCats()	{
	$j("#directory.categories-block.category-expand").hide().css({ 'border':'none', 'height':'100%', 'overflow':'none' });
}
function setReservation( name, id )	{
	$j('#reservation-name').html(name);
	$j('#txt_reserve_title').attr("value", name);
	$j('#txt_reserve_id').attr("value", id);
	$j('#txt_reserve_security2').attr("value", "amsec");
}
function setQuote( name, id )	{
	$j('#quote-name').html(name);
	$j('#txt_quote_title').attr("value", name);
	$j('#txt_quote_id').attr("value", id);
	$j('#txt_quote_security2').attr("value", "amsec");
}
function setContact( name, id )	{
	$j('#contact-name').html(name);
	$j('#txt_contact_title').attr("value", name);
	$j('#txt_contact_id').attr("value", id);
	$j('#txt_contact_security2').attr("value", "amsec");
}
function changeCategory( obj ) {
	document.location = '?category=' + obj.options[obj.selectedIndex].value;
}
function changeRegion(category, obj ) {
	document.location = '?category=' + category + '&region=' + obj.options[obj.selectedIndex].value;
}
function getAjaxRegions( province, selectedRegion ) {
	$j.get( "/dir_regions.ashx", { p:province }, function( data ) {
		var str_regions = data.split(";");
		var obj_city = $j("#sel_city");
		obj_city.removeOption(/./);
		var toSelect = "";
		for( var i = 0; i < str_regions.length; i++ ) {
			var str_region = str_regions[i].split(":");
			obj_city.addOption( str_region[2], str_region[0] );
			if( selectedRegion == str_region[1] ) toSelect = str_region[2];
		}
		obj_city.selectOptions( toSelect , true );
	});   
}
function addToFavorites( id, title ) {
	var str_guid = addFavourite(id);	
	$j("#img_fav_" + id ).attr("src","/images/icon-star-on.gif");
	$j("#href_fav_" + id ).attr("href","?view=favorites").html("Favorite");	
	$j.get( "?view=favorite&id=" + id  + "&guid=" + str_guid );
	alert( "The listing has been added to your favourites." );
	return;
}
function removeFromFavorites( id, title ) {
	if( confirm( "Are you sure you want to remove this favorite?" ) )	{
		var str_guid = removeFavourite(id);		
		$j("#img_fav_" + id ).attr("src","/images/icon-star.gif");
		$j("#href_fav_" + id ).attr("href","javascript:addToFavorites( " + id + ", '" + title.replace("'","\\'") + "' );").html("Add to Favorites");		
		$j("#removefavorite_" + id ).hide();		
		if( str_guid != "" ) $j.get( "?view=favorite&action=remove&id=" + id + "&guid=" + str_guid);
	}
	return;
}
function addFavourite( id ) {
	var str_guid = generateGuid();
	if( $j.cookie("favourites_guid") == null ) $j.cookie("favourites_guid",str_guid, { expires:365 });
	else str_guid = $j.cookie("favourites_guid");	
	if( $j.cookie("favourites") == null ) $j.cookie("favourites",":" + id + ":" , {expires:365} );
	else {
		var contains_cookie = false;
		var str_cookie = $j.cookie("favourites").split( ":" );
		var str_newcookie = ":";
		for( var i = 0; i < str_cookie.length; i++ ) {
			if( str_cookie[i] != null && str_cookie[i] != "" && str_cookie[i] == id ) contains_cookie = true;
		}
		if( !contains_cookie ) $j.cookie("favourites",$j.cookie("favourites")+id+":", {expires:365} );
	}
	return str_guid;
}
function removeFavourite( id ) {
	var str_guid = "";
	var str_cookie = $j.cookie("favourites");
	if( $j.cookie("favourites_guid") != null ) str_guid = $j.cookie("favourites_guid");
	if( str_cookie != null && str_cookie != "" ) {
		str_cookie = str_cookie.split( ":" );
		var str_newcookie = ":";
		for( var i = 0; i < str_cookie.length; i++ )
			if( str_cookie[i] != null && str_cookie[i] != "" && str_cookie[i] != id ) str_newcookie += str_cookie[i] + ":";
		$j.cookie("favourites", str_newcookie, {expires:365});
	}
	return str_guid;
}
function generateGuid() {
	var result = '', i, j;
	for( j = 0; j < 32; j++ )	{
		if( j == 8 || j == 12|| j == 16|| j == 20 ) result = result + '-';
		i = Math.floor( Math.random() * 16 ).toString( 16 ).toUpperCase();
		result = result + i;
	}
	return result;
}