if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

$(function() {
	var COOKIE_NAME = 'rentiesa';
	var options = { path: '/', expires: 10 };
	var screen_counter_container = document.getElementById("saved_count_container");
	var change_size = false;
	$('.slink').click(function(){
		var $this = $(this);
		var screen_counter = document.getElementById("saved_count");
		var extra = '';
		if ($this.is('.save'))
		{
			var classes = $(this).attr('class').split(' '); 
			var ad_id = 0;
			// Get the ID of the selected ad
			for(var i =0; i<classes.length; i++)
			{
				if(strstr(classes[i], 'id_'))
				{
					ad_id = classes[i].substring(3);
				}
				if(classes[i] == "smi")
				{
					change_size = true;		
				}
			}
			var $text = $('#text_' + ad_id);
			// Pull array of ids from cookie
			var ad_ids = Array();

			var ad_str = $.cookie(COOKIE_NAME);
			
			if(null!=ad_str)
			{
				if(ad_str.length > 0)
				{
					ad_ids = ad_str.split(",");
				}
			}
			// Stick this ad id in, but make sure it's unique in the array
			ad_ids[ad_ids.length] = ad_id;
			//Increase the counter, if empty(zero), add span/brackets

			if(screen_counter_container.innerHTML == '')
			{
				screen_counter_container.innerHTML = ' (<span id="saved_count">1</span>)';
			} else {
				screen_counter.innerHTML++;
			}

			var ad_str = (ad_ids.unique()).join(",");
			$.cookie(COOKIE_NAME, ad_str, options);
			// Update link settings
			$text.html('Remove Saved Ad');
			

			$(this).removeClass('save');
			$(this).addClass('remove');
			if(change_size){
				$(this).parent().addClass('narrow');
				$(this).parent().removeClass('wide');
			}
		}
		else
		{
			// On saved ads page, this will be set to true so removal triggers disappearance of div
			var hide_div = false;

			var classes = $(this).attr('class').split(' '); 
			var ad_id = 0;
			// Get the ID of the selected ad
			for(var i =0; i<classes.length; i++)
			{
				if(strstr(classes[i], 'id_'))
				{
					ad_id = classes[i].substring(3);
				}
				// On the saved ads page, so hide effect applicable
				if(classes[i] == "sp")
				{
					hide_div = true;		
				}
				if(classes[i] == "smi")
				{
					change_size = true;		
				}
			}
			var $text = $('#text_' + ad_id);
			// Pull array of ids from cookie
			var ad_ids = Array();
			var ad_str = $.cookie(COOKIE_NAME);
			if(null!=ad_str)
			{
				if(ad_str.length > 0)
				{
					ad_ids = ad_str.split(",");
				}
			}

			// Remove this ad id from the list of saved ads
			var t = ad_ids.indexOf(ad_id);
			var ad_type = ad_id.substring(0, 2);
			var type_count = 0;
			for(var i = 0; i < ad_ids.length; i++) {
				if(ad_type == ad_ids[i].substring(0, 2)) {
					type_count++;
				}
			}
			
			var ad_type_string = 'rental';
			if(ad_type == 's_')
				ad_type_string = 'sharing';
			if(ad_type == 'h_')
				ad_type_string = 'holiday home';
			if(ad_type == 'st')
				ad_type_string = 'short let';
			if(ad_type == 'p_')
				ad_type_string = 'parking';

			ad_ids.splice(t, 1);			
			
			//Decrease the counter, if 1, remove span/brackets

			if(screen_counter.innerHTML == '1')
			{
				screen_counter_container.innerHTML = '';
			} else {
				screen_counter.innerHTML--;
			}
			
	
			var ad_str = (ad_ids.unique()).join(",");
			$.cookie(COOKIE_NAME, ad_str, options);

			var close_string = '<div class="showMe" style="display:none;margin-bottom:10px;"><div id="content_main_top"><span><span></span></span>' +
				'<h1>Your saved ' + ad_type_string + ' ads</h1>' +
				'</div><div class="standard_bottom"><div class="saved_ads">' +
				'<h2 class="title failed">You have no saved ' + ad_type_string + ' ads</h2>'+
				'</div><span><span></span></span></div></div>';

			if(hide_div)
			{
				// Hide the parent div containing all property info, do text change as a call back so animation is complete
				$(this).parent().parent().slideUp('400', function() {
						$(this).html('Save This Ad');
						
						$(this).removeClass('remove');
						$(this).addClass('save');
						if(type_count == 1) {
							$(close_string).insertAfter($('#searchresults_container').find('#sresult_banner'))
							$('.showMe').slideDown('1000');
						}	
					}
				);
			} else {
				// Link settings
				
				$text.html('Save This Ad');
				
				
				$(this).removeClass('remove');
				$(this).addClass('save');
			}
			if(change_size){
				$(this).parent().removeClass('narrow');
				$(this).parent().addClass('wide');
			}
		}
		// Don't trigger page change
		return false;
	});
});

function strstr( haystack, needle, bool ) {
	// http://kevin.vanzonneveld.net
	// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// + bugfixed by: Onno Marsman
	// * example 1: strstr('Kevin van Zonneveld', 'van');
	// * returns 1: 'van Zonneveld'
	// * example 2: strstr('Kevin van Zonneveld', 'van', true);
	// * returns 2: 'Kevin '

	var pos = 0;
	haystack += '';
	pos = haystack.indexOf( needle );
	if( pos == -1 ){
		return false;
	} else {
		if( bool ){
			return haystack.substr( 0, pos );
		} else{
			return haystack.slice( pos );
		}
	}
}
Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}
$(document).ready(function(){
	$('.underline').hover(
		function () {
			$(this).css({cursor: 'pointer', textDecoration: 'underline'});
			$(this).children('div').css({cursor: 'pointer', textDecoration: 'underline'});
		},
		function () {
			$(this).css({textDecoration: 'none'});
			$(this).children('div').css({textDecoration: 'none'});
		}
	)
});