$(function() {
	var COOKIE_NAME = 'dpl_props';
	var SS_COOKIE_NAME = 'dpl_search';
	var options = { path: '/', expires: 10 };
	// Track the value of saved properties
	var prop_count = 0;

	$('.slink').click(function(){
		var $this = $(this);
		// Current value of saved properties?
		prop_count = $('#count_prop').html();

		if ($this.is('.save'))
		{
			var classes = $(this).attr('class').split(' '); 
			var prop_id = 0;
			// Get the ID of the selected ad
			for(var i =0; i<classes.length; i++)
			{
				if(strstr(classes[i], 'id_'))
				{
					prop_id = classes[i].substring(3);
				}
			}

			// Pull array of ids from cookie
			var prop_ids = Array();
			var prop_str = $.cookie(COOKIE_NAME);
			if(null!=prop_str)
			{
				if(prop_str.length > 0)
				{
					prop_ids = prop_str.split(",");
				}
			}
			// Stick this ad id in, but make sure it's unique in the array
			prop_ids[prop_ids.length] = prop_id;
			var prop_str = (prop_ids.unique()).join(",");
			$.cookie(COOKIE_NAME, prop_str, options);

			// Update link settings
			$(this).html("usuń z zapisanych ogłoszeń");
			$(this).removeClass('save');
			$(this).addClass('remove');
		}
		else
		{
			var classes = $(this).attr('class').split(' '); 
			var prop_id = 0;
			// Get the ID of the selected ad
			for(var i =0; i<classes.length; i++)
			{
				if(strstr(classes[i], 'id_'))
				{
					prop_id = classes[i].substring(3);
				}

				// On the saved ads page, so hide effect applicable
				if(classes[i] == "sp")
				{
					hide_div = true;		
				}
			}

			// Pull array of ids from cookie
			var prop_ids = Array();
			var prop_str = $.cookie(COOKIE_NAME);
			if(null!=prop_str)
			{
				if(prop_str.length > 0)
				{
					prop_ids = prop_str.split(",");
				}
			}

			// Remove this ad id from the list of saved ads
			var t = prop_ids.indexOf(prop_id);
			prop_ids.splice(t, 1);
			var prop_str = (prop_ids.unique()).join(",");
			$.cookie(COOKIE_NAME, prop_str, options);

			// Link settings
			$(this).html("dodaj do zapisanych ogłoszeń");
			$(this).removeClass('remove');
			$(this).addClass('save');
		}
		prop_count = prop_ids.length;
		set_count('#prop_count', prop_count);
		// Don't trigger page change
		return false;
	});



	// handle saved searches
	$('.ssearch').click(function(){
		var $this = $(this);
		// Current value of saved searches?
		prop_count = $('#search_prop').html();

		if ($this.is('.save'))
		{
			var classes = $(this).attr('class').split(' '); 
			var search_url = '';
			// Get the ID of the selected ad
			for(var i =0; i<classes.length; i++)
			{
				if(strstr(classes[i], 'ss_'))
				{
					search_url = classes[i].substring(3);
				}
			}

			// Pull array of ids from cookie
			var ss_urls = Array();
			var ss_url = $.cookie(SS_COOKIE_NAME);
			if(null!=ss_url)
			{
				if(ss_url.length > 0)
				{
					ss_urls = ss_url.split("|");
				}
			}
			// Stick this search in, but make sure it's unique in the array
			ss_urls[ss_urls.length] = search_url;
			// Sort urls alphabetically
			ss_urls.sort();			
			var ss_str = (ss_urls.unique()).join("|");
			$.cookie(SS_COOKIE_NAME, ss_str, options);

			// Update link settings
			$(this).html("Usuń kryteria wyszukiwania");
			$(this).removeClass('save');
			$(this).addClass('remove');

			// Update "saved searches" dropdown
			populate_sv_searches(ss_urls);
		}
		else
		{
			var classes = $(this).attr('class').split(' '); 
			var search_url = 0;
			// Get the ID of the selected ad
			for(var i =0; i<classes.length; i++)
			{
				if(strstr(classes[i], 'ss_'))
				{
					search_url = classes[i].substring(3);
				}

				// On the saved ads page, so hide effect applicable
				if(classes[i] == "sp")
				{
					hide_div = true;		
				}
			}

			// Pull array of ids from cookie
			var ss_urls = Array();
			var ss_url = $.cookie(SS_COOKIE_NAME);
			if(null!=ss_url)
			{
				if(ss_url.length > 0)
				{
					ss_urls = ss_url.split("|");
				}
			}

			// Remove this ad id from the list of saved ads
			var t = ss_urls.indexOf(search_url);
			ss_urls.splice(t, 1);
			// Sort urls alphabetically
			ss_urls.sort();
			var ss_url = (ss_urls.unique()).join("|");
			$.cookie(SS_COOKIE_NAME, ss_url, options);

			// Link settings
			$(this).html("Zapisz kryteria wyszukiwania");
			$(this).removeClass('remove');
			$(this).addClass('save');

			// Update "saved searches" dropdown
			populate_sv_searches(ss_urls);
		}

		var srch_count = ss_urls.length;
		set_count('#search_count', srch_count);
		// Don't trigger page change
		return false;
	});


	$('.ss_list').mouseenter(function(){
		$('#ss_viewer').toggle();

	});
});


/**
 * Sets the value of id to val
 * Expect id in the form "#id_name"
 */
function set_count(id, val)
{
	$(id).html("("+val+")");
}


/**
 * Easiest way of ensuring that saved search is kept
 * current is to blow it away & rebuild each time
 */
function populate_sv_searches(searches)
{
	// Wipe the list contents
	$('ul#ss_vl').html('');

	// Cycle through the links
	// Format each
	// Then append li element
	for(var t=0; t<searches.length; t++)
	{
		var search_url = searches[t];

		// First, trim off host
		var display_url = search_url.replace('http://','');

		var display_parts = display_url.split('/');
		display_url = '/';
		for(var j=1; j< display_parts.length; j++)
		{
			// Don't add empty strings (when trailing '/' is present
			if(display_parts[j].length > 0)
			{
				display_url += display_parts[j]+'/';
			}
		}
		// Now, make sure it's not longer than 60 chars
		if(display_url.length>60)
		{
			display_url = display_url.substring(0,57)+"...";
		}

		$('<li class="'+search_url+'"><a href="'+search_url+'">'+display_url+'</a></li>').appendTo('ul#ss_vl');
	}
}