
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

$(document).ready(function()
{
	$('a.add-to-fav').live('click', add_favorite);
	$('a.remove-fav').live('click', remove_favorite);
	
	
	$(".term ul li a").click(go_to_url);
	
	$(".term a").click(function() {
		$(".term ul").hide();
		
		var id = "#" + $(this).parent()[0].id;
		
		$(id + " ul").bind("mouseleave", menu_close_timer);
		
		$(id + " ul").show();
		return false;
	});
});

function buildSearchLink(type, loc, price)
{
	window.location = '/search/' + type + loc + price;
}

function check_fav_status(ix, el, evObj)
{
	var link = $(el);//$(evObj.currentTarget);
	var id = link.attr("p_id");
	var cookie_name = "photog" + id;
	
	if($.cookie(cookie_name) != null)
	{
		//exists!
		try{
			link = link.addClass("fav");
			link.text("Favorite");
		} catch(e){}
	}
}

function remove_favorite(evObj)
{
	//remove from my cookies!
	var link = $(evObj.currentTarget);
	var id = link.attr("p_id");
	var cookie_name = "photog" + id;
	var options = {path: '/', expires: 120};
	
	$.cookie(cookie_name, null, options);
	
	//hide obj
	$('#fav_' + id).hide('slow', function()
	{
		if($(".fav-item:visible").length == 0)
		{
			noFavMessage();
		}
	}
);
	
}


function add_favorite(evObj)
{
	//add this to my cookies!
	var link = $(evObj.currentTarget);
	var id = link.attr("p_id");
	var name = link.attr("p_name");
	var param_name = link.attr("p_param_name");
	var contents = id + "|" + name + "|" + param_name;
	
	var cookie_name = "photog" + id;
	var options = {path: '/', expires: 120};
	$.cookie(cookie_name, contents, options);
	
	link = link.addClass("fav");
	link.text("Favorite");
}

function go_to_url(evObj)
{
	try
	{
		$(evObj.currentTarget).addClass("sel");
		var sel = $(evObj.currentTarget).text();
		if(sel.indexOf('More locations') < 0)
		{
			$(evObj.currentTarget).parents('span').first().children('a').text(sel).toLowerCase();
		}
	}
	catch(ex)
	{}
	// URL: /type/location/price
}

function menu_close_timer(evObj)
{
	window.setTimeout(function() {
		$("#"+$(evObj.currentTarget).parent()[0].id + " ul").hide()
	}, 500);
	
}

function load_favorites()
{
	var cookies = { };

  if (document.cookie && document.cookie != '') {
      var split = document.cookie.split(';');
      for (var i = 0; i < split.length; i++) {
          var name_value = split[i].split("=");
          name_value[0] = name_value[0].replace(/^ /, '');
          cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
      }
  }

	var htmlList = "";
  for(var i in cookies)
  {
  	if(i.indexOf("photog") >= 0)
  	{
  		var parts = cookies[i].split("|");
  		var id = parts[0];
  		var name = parts[1];
  		var param_name = parts[2] == undefined ? "" : "-" + parts[2];
  		
  		$.getJSON("/" + id, function(id) { return function(data){
  				try
  				{
	  				var m = data;
	  				var imgUrl = "/system/pic1s/" + id + "/small/" + m["member"]["pic1_file_name"];
	  				$("#" + id + "_pic").attr("src", imgUrl);
					}catch(e){}
				} }(id)
			);
  		
	  	htmlList += "<li id='fav_"+id+"' class='fav-item'><a href='/"+ id + param_name +"' class='fav-name'><img id='"+ id +"_pic' src='/images/ajax-loader.gif' />"+ name +"</a>" +
	  		"<br />(<a href='#"+id+"' p_id='"+id+"' class='remove-fav'>Remove</a>)" +
	  		"</li>";
	  	
		}
		
  }
  
  if(htmlList == "")
  {
  	noFavMessage();
  }
  
  $("#favorites").append(htmlList);
}

function noFavMessage()
{
	msg = "<h3 class='thin'>You don't have any favorites! Click the star (<img src='/images/icons/fatcow/star.png' />) icon on a listing to add it to your favorites.</h3><a href='/'>&larr; Back to search</a><hr class='space' />";
	$("#favorites").append(msg);
}

