//Put this code in "functions.js" to be really slick
$(document).ready( function(){

	setExtraSession = function () {
		var dt = new Date();
		var place = $('#filter_plaats').val();
		place = place.replace(" ","_"); 
		var tref = $('#filter_trefwoorden').val();
		tref = tref.replace(" ","_"); 
		var pc = $('#filter_postcode').val();
		pc = pc.replace(" ","_"); 
		var pro = $('#filter_provincie').val();
		pro = pro.replace(" ","_"); 
		
		$('#LoadSetSession').load('/ajax_setSessionSearch.asp?place='+place+'&tref='+tref+'&prov='+pro+'&pc='+pc+"&time="+dt.getTime());
	}
	
	resetSession = function(){
		$.ajax({
			async: true,
			type: 'get',
			cache: false,
			data: {
				type: 'reset'
			},
			url: "/ajax_setSessionSearch.asp",
			success: function(){window.location.reload(true)}
		});
	}

	setSession = function(){    
		var catIds = '';       
		$('#categoriesFilter input:checked').each(function() {            
			catIds = catIds + $(this).val() + ",";       
		});      
		//alert(catIds);
		var dt = new Date();
		$('#LoadSetSession').load('/ajax_setSession.asp?catIds='+catIds+"&time="+dt.getTime());
		// now names contains all of the names of checked checkboxes      
		// do something with it    
	} 


	setContent = function(data){
		$('#companyListing').html(data.companys);
		//alert(data.activeEndCategories[0]);
		var endCategoriesArray = data.activeEndCategories;

		$(".catidsCheckbox").each(function(){
			$(this).attr('disabled',false)
		});	

		for (var i=0; i < endCategoriesArray.length; i++){ 
			$('#catid'+endCategoriesArray[i]+':not(:checked)').attr('disabled',true)
			//alert(endCategoriesArray[i]);
		}

	}

	loadCompanyResults = function(catId,iStart,iOffset)
	{


		var place = $('#filter_plaats').val();
		place = place.replace(" ","_"); 
		var tref = $('#filter_trefwoorden').val();
		tref = tref.replace(" ","_"); 
		var pc = $('#filter_postcode').val();
		pc = pc.replace(" ","_"); 
		var pro = $('#filter_provincie').val();
		pro = pro.replace(" ","_"); 

		$.ajax({
			async: true,
			type: 'get',
			cache: false,
			data: {
				place: place,
				tref: tref,
				pro: pro,
				pc: pc
			},
			url: "/ajax_setSessionSearch.asp",
			success: function(data){$('#LoadSetSession').html(data);},
			complete: function(){
				$.ajax({
					async: true,
					type: 'get',
					cache: false,
					dataType: "json",
					data: {
						catId: catId,
						start: iStart,
						offset: iOffset,
						json: 'true'
					},
					url: "/includes/ajax_CompanyResults.asp",
					success: function(data){setContent(data);}
				});	
			}
		});			
	}
							
	loadSearch = function (myObject,url,searchString,start,offset,searchAddress,searchKeywords)
	{

	    var searchCategories = [];
	    $('#categoryFilter :checked').each(function () {								 
	        searchCategories.push($(this).val());
	    });
		
		$.ajax({
			url: url,
			data: 'searchtxt=' + searchString + '&searchAdres=' + searchAddress + '&searchKeywords=' + searchKeywords + '&start=' + start + '&offset=' + offset + '&searchCategories=' + searchCategories,
			cache: false,
			beforeSend: function(data){
				$(myObject).html('<img src="/img/ajax-loader.gif">');
			},
			success: function(data){
				$('html,body').scrollTop(0);
				$(myObject).html(data);
			}
		});
	}

	$("a.ajaxLink").fancybox({
		'hideOnContentClick': false
	});
	
	$(function() {
		$('.companyInfoSectionList').disableTextSelect();
		$('.companyList').disableTextSelect();
	});
	
	addthis.init();
	
	$(".scrollable").scrollable({circular: true}).autoscroll({ autoplay: true,interval: 5000 });
	
	$("table.zebra").find("tr:nth-child(even)").each(
		function(i) {
		  if( 0 == $(this).find("th").length ) {
			$(this).addClass("even");
		  }
		}
	);
	
	if(!Modernizr.input.placeholder){

		$('[placeholder]').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();
		$('[placeholder]').parents('form').submit(function() {
		  $(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
			  input.val('');
			}
		  })
		});
	
	}
});


//Auto-Fill function accepts id of input and fills it with the given value.
//Written by Joe Sak http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click/
function autoFill(id, v){
	$(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "#333" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#b2adad" }).val(v);
		}
	});
	
}
