$('document').ready(function(){
	$('form#advanced-search-form .items_per_page a').click(function(e){
		e.preventDefault();
		var itemsPerPage = $(this).attr('rel');
		$('input[name=results_per_page]').val(itemsPerPage);	
		$('form#advanced-search-form').submit();		
	});
	
	
	$('.accordion').accordion({
		collapsible: true,
		active: false,
		autoHeight: false
	});
	
	$('#search_input_wrapper input[name=query], #search_input_wrapper input[name=search], #client_login input[name=email_login], #client_login input[name=password_login], #search_box input[name=postcode]').focus(function(){
		$(this).val('');
		$(this).unbind('focus');
	});
	
	$('#date_of_birth').datepicker({
		dateFormat: 'yy-mm-dd',
		maxDate: '0d',
		defaultDate: '-20y',
		changeYear: true,
		yearRange: 'c-100:c+20',
        changeMonth: true
	});

	
	if ($("#start_date").length > 0)
	{
		$('#start_date').datepicker({
			dateFormat: 'yy-mm-dd',
			maxDate: '31d',
			minDate: '0d'
		});
	}
	
	$('#today_button').click(function(){
		var today = new Date();
		var dateString = (today.getFullYear() + '-' + pad((today.getMonth() + 1), 2) + '-' + pad(today.getDate(), 2));
		$('#start_date').val(dateString);
	});
	if ($("#search_box select#category_id").length > 0) {
		$("#category_id").change(function(){
			var option = $("#category_id :selected").val();
			$.ajax({
				type: "POST",
			 	dataType: "json",
			 	data: {category_id: option},
			 	url: "/search/category",
			 	success: function(data){
			 		$('#search_box select#sub_category_id').empty();
			 		$(data).each(function(key, value){
			 			$('#search_box select#sub_category_id').append('<option value="' + value.id + '">' + value.name + '</option>');
			 		})	 			
			 	}
			})
		});
	}	
});

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return false;
}



function pad(number, length)
{
	   var str = "" + number;
	   while(str.length<length)
	   {
		   str = "0" + str;
	   }
	   return str;
}
function configureColorBox(obj)
{
	/*obj.click(function(){
	  var location = $(this).attr('href');
	  $.get(location, function(data){
	      $.fn.colorbox({
	        html: data,
	        opacity: 0.7,
	        height: "650",
	        width: "780",
	        onClosed: function(){
	    	  window.location.href = window.location.href
	      	}
	      });
	      initColorBox(location);
	   });
	   return false;
	});*/			  
}


function initColorBox(path) 
{
	// re-run the document load initialiser, this will append any JS events required to the pages 
	// loaded within a colorbox
	/*publicInit();*/
	/*$('#back_button').click(function(){
	   $.fn.colorbox.close();
	   return false;
	});
	$('#submit').click(function(){
		submitColorBox(path);
	    return false;
	});*/
}	


function submitColorBox(path) 
{
	/*var variables = $('#ajax_wrapper form').serialize();
	$.post(path, variables,
	   function(data){
		// callback
		 $('#ajax_wrapper').replaceWith(data);
		 initColorBox(path);
	});*/
}

function initRegistrationBox()
{
	//alert('yoyoy');
	
	  /*var location = '/';
	  $.get(location, function(data){
	      $.fn.colorbox({
	        html: data,
	        opacity: 0.7,
	        height: "450",
	        width: "580",
	        onClosed: function(){
	    	  window.location.href = window.location.href
	      	}
	      });
	      initColorBox(location);
	   });*/
	return;


}
