jQuery(document).ready(function() {
	$("div.multiselect").each(function() {
		attachEventHandlers($(this));
		
		$(".multiselect > p").each(function() {
		$(this).text(getCheckedBoxLabel(this));
		});
	});
	$("div.multiselect.single").each(function() {
		var that = this;
		$("li",this).click(function() {
			$(".multiselect-wrapper",that).slideUp(200);
			$("input[type=hidden]",that).val($(this).find('label').attr('for'));
			$("p",that).html($(this).find('label').html());
			return false;
		});
	});

});

attachEventHandlers = function(e)
{
		
		var that = e;
		$(that).click(function() {
			
			if ($(that).attr("id") == 'salary') {
				
				checked = $("div#job_type input:checked");
				if (checked.length == 1)
				{
					var raw_value = checked.attr('name');
					raw_value = raw_value.match(/\[[^\]]*\]/g);
					var value = raw_value[0].replace(/\[|\]/g, '');
//					console.log(value);
					hideOtherSalaryTypes(value);
				}
			}
			
			$(".multiselect-wrapper",that).slideDown(200);
			
			slideUpOthersThan(that);
			
			chekd = $("div.multiselect-wrapper  input:checked", that).length;
//			console.log(chekd);
			
			if(!$(".multiselect-wrapper",that).data('checked_boxes'))
			{
				$(".multiselect-wrapper",that).data('checked_boxes', chekd);
			}

		});
		

		
		
		$("input:checkbox", that).click(function(event){
			$(".multiselect-wrapper",that).data('dirty', true);
			event.stopPropagation();
			
		});
		
	

		$("button",that).click(function() {
			$(".multiselect-wrapper",that).slideUp(200);
			
//			console.log($(".multiselect > p", that));
//			console.log(that);
		
			$("p", that).each(function() {
//				console.log('label update');
				$(this).text(getCheckedBoxLabel(this));
			});
			
//			console.log('after:');
//			console.log($(".multiselect-wrapper",that).data('checked_boxes'));
//			console.log($("div.multiselect-wrapper  input:checked", that).length);
			if(($(".multiselect-wrapper",that).data('dirty') 
					|| $(".multiselect-wrapper",that).data('checked_boxes') != $("div.multiselect-wrapper  input:checked", that).length ) 
					&& $(".multiselect-wrapper").length > 1)
			{
				$(".multiselect-wrapper",that).removeData('checked_boxes');
				updateForm();
				
			}
			return false;
		});
		
	
}

getCheckedBoxLabel = function(e) {
	
	var mselect = $(e).parent().get(0);
	
	var oldtitle = "All"; 
	mselect_id = $(mselect).attr('id');
	
	if ($(".multiselect-wrapper").length == 1)
	{
		oldtitle = 'Select '
		var label = $("label[for='" +mselect_id+ "']").text();
		if (label != '' )
		{
			oldtitle += label;
		}
		else 
		{
			oldtitle += mselect_id;
			oldtitle = oldtitle.replace(/_/g, " ");
		}
	}
	
	var title = '';
	
	
	$("div.multiselect-wrapper  input:checked + label", mselect).each(function(){
		
		if (title != '') title += " | ";
		title += $(this).text();
	});
	
	if(title != "") 
	{
		len = 24;
		if (title.length > len)
		{
			title = title.substring(0, len);
			title += "...";
		}
		return title;
	}
	else
	{
		return oldtitle;
	}
	

}

slideUpOthersThan = function(e) 
{
	var that = e;
	$("div.multiselect").each(function(){
		if($(this).attr('id') != $(that).attr('id'))
		{
			var mselect = this;
			 $(".multiselect-wrapper",mselect).slideUp(200);
		}
	});
}

hideOtherSalaryTypes = function(type)
{
	if (type == 'contract') {
		identifier = 'k]';
	}
	else {
		identifier = 'h]';
	}
	$("div#salary li:has(input[name$='" +identifier+ "'])").hide();
}

updateForm = function()
{
	
	var qs =''; 
	$(".multiselect-wrapper").each(function(){
		var that = this;
		var key = $("input:first", that).attr('name');
		var values = '';
		key = key.replace(/\[[^\]]*]/g,'');
		
		$("input:checked", that).each(function(){
			raw_value = $(this).attr('name');
			raw_value = raw_value.match(/\[[^\]]*\]/g);
			values += raw_value[0].replace(/\[|\]/g, '') + ',';
		});
		
		if (values != '')
		{
			qs += key + "=" + values.replace(/,$/g, '') + "&";	
		}
	});
		

	$.ajax({
		  url: "/search",
		  cache: false,
		  data: qs,
		  processData: false,
		  success: function(html){
		    $("#job-search").replaceWith(html);
		    $("div.multiselect").each(function() {
				attachEventHandlers($(this));
				
				$(".multiselect > p").each(function() {
				$(this).text(getCheckedBoxLabel(this));
				});
		    });
		  }
	});
		
}

	
