var ac = null;


function search()
{

	this.dest = '/engine/ajax/search_ajax.php';

	this.init = function()
	{
		this.searchMenu();
		this.searchTabs();
		this.searchAdvanced();
		this.autocomplete();
		this.leftTabs();
	}
	
	
	this.searchMenu = function()
	{
		$(".select_artikel").click(function() {
			if($('#countryInput').attr('rel') == '')
			{
				alert('Selecteer eerst een land uit de beschikbare opties, indien het door u gezochte land niet gevonden kan worden dan kunt u het proberen via trefwoord');
				return;
			}
			
			$("#menu_keuze").fadeIn();
		});
		
		
		$(".specificeer_artikel").click(function() {
			if($('#countryInput').attr('rel') == '')
			{
				alert('Selecteer eerst een land uit de beschikbare opties, indien het door u gezochte land niet gevonden kan worden dan kunt u het proberen via trefwoord');
				return;
			}
		
			$("#menu_keuze").fadeIn();
		});
		
		
		$("#menu_keuze").mouseleave(function() {
			$("#menu_keuze").fadeOut();
		});
		
		
		$(".sluit").click(function() {
			$("#menu_keuze").fadeOut();
			
			search.byCountry($(this).attr('rel'));
		});		
		
		
		$(".toon_spec").click(function() {
			search.setTypes($(this).attr('rel'));
			$(".niv3").fadeIn();
		});		
		
		
		$(".niv2").click(function(event) {
			$(".niv2 a").removeClass('active');
			$(".niv3 a").removeClass('active');
			$(event.target).addClass('active');
		});
 
		// specificatie tonen
		$(".niv3").click(function(event) {
			$("#menu_keuze").fadeOut();
			$(".niv3 a").removeClass('active');
			$(event.target).addClass('active');
		});
		
		//$(".niv3").hide();
	}
	
	
	this.searchTabs = function()
	{
		// tab land
		$(".tab_op_land").click(function() {
			search.request('f=tab&name=search&value=land', null);
		
			$(".zoekoptrefwoord").hide();
			$(".zoekopnummer").hide();			
			$(".zoekopland").show();
			
			$(".tab").removeClass('active');
			$(this).parent().addClass('active');
		});
		
		this.inputActions('countryInput', 'Type een land...');
 
		// tab trefwoord
		$(".tab_op_trefwoord").click(function() {
			search.request('f=tab&name=search&value=trefwoord', null);
		
			$(".zoekopland").hide();
			$(".zoekopnummer").hide();						
			$(".zoekoptrefwoord").show();
			
			$(".tab").removeClass('active');
			$(this).parent().addClass('active');
		});
		
		this.inputActions('keywordInput', 'Type een trefwoord...');
		
		$('#keywordInput').keyup(function(e) {
			if(e.keyCode == 13) search.byKeyword();
		});
		
		$('#btnKeyword').click(function() {
			search.byKeyword();
		});
		
		$('#btnAdvancedSearch').click(function() {
			window.location = '/zoeken/uitgebreid';
		});
 
		// tab nummer
		$(".tab_op_nummer").click(function() {
			search.request('f=tab&name=search&value=nummer', null);
		
			$(".zoekopland").hide();
			$(".zoekoptrefwoord").hide();
 
			$(".zoekopnummer").show();
			
			$(".tab").removeClass('active');
			$(this).parent().addClass('active');
		});
		
		this.inputActions('eanInput', 'Type een isbn/ean nummer...');
		
		$('#eanInput').keyup(function(e) {
			if(e.keyCode == 13) search.byEAN();
		});
		
		$('#buttonEAN').click(function() {
			search.byEAN();
		});
		
	}
	
	
	this.searchAdvanced = function()
	{
		$('#slctContinent').change(function() {
			search.continentSelected($(this).val());
		});
		
		$('#slctCountry').change(function() {
			search.countrySelected($(this).val());
			search.typeSelected($(this).val());
		});
		
		search.validateSearchForm();
	}
	
	
	this.leftTabs = function()
	{
		$(".streek_tab").click(function() {
			search.request('f=tab&name=geo&value=streek', null);
			$(".streek .tab").removeClass('active');
			$(this).parent().addClass('active');
			$(".steden_lijst").hide();
			$(".streken_lijst").show();
		});
		
		$(".stad_tab").click(function() {
			search.request('f=tab&name=geo&value=stad', null);
			$(".streek .tab").removeClass('active');
			$(this).parent().addClass('active');
			$(".steden_lijst").show();
			$(".streken_lijst").hide();
		});
		
		$(".kaart_tab").click(function() {
			search.request('f=tab&name=type&value=kaart', null);
			$(".kaart .tab").removeClass('active');
			$(this).parent().addClass('active');
			$(".boeken_lijst").hide();
			$(".kaarten_lijst").show();
		});
		
		$(".boek_tab").click(function() {
			search.request('f=tab&name=type&value=boek', null);
			$(".kaart .tab").removeClass('active');
			$(this).parent().addClass('active');
			$(".boeken_lijst").show();
			$(".kaarten_lijst").hide();
		});

		
		$(".boeken_lijst").hide();
		$(".steden_lijst").hide();
	}
	
	
	this.inputActions = function(id, text)
	{
		$('#' + id).click(function() {
			if($(this).val() == text) $(this).val('');
		});
		
		$('#' + id).focusout(function() {
			if(($(this).val()).length < 1) $(this).val(text);
		});
	}


	this.byCountry = function(cat)
	{
		country = $('#countryInput').attr('rel');
		req = 'f=strippedCountry&country=' + country;
		$.get(this.dest, req, function(data){
			country = data;
			window.location = '/' + cat + '/alle/' + data;
		});
	}
	
	
	this.byKeyword = function()
	{
		q = $('#keywordInput').val();
		
		if(q == 'Type een trefwoord...') return;
		
		q = search.stripKeyword(q);
		window.location = '/zoek/' + q;
	}
	
	
	this.stripKeyword = function(q)
	{
		q = q.replace(/<\/?[^>]+>/gi, '');
		q = q.replace(/\&+/g, "");
		q = q.replace(/\s+/g, "+");
		q = q.replace(/\%+/g, "");
		
		return q;
	}
	
	
	this.byEAN = function()
	{
		if($('#eanInput').val().match(/^\d{10,}$/))
			window.location = '/ean/' + $('#eanInput').val();
		else
			alert('U dient minimaal 10 cijfers in te voeren in dit invoerveld');
	}
	
	
	this.setTypes = function(range)
	{
		range = range.split(',');
	
		req = 'f=types&min=' + range[0] + '&max=' + range[1];
		this.request(req, 'specArticles');
	}
	

	this.autocomplete = function()
	{
		$('#countryInput').autocomplete({
			source: '/engine/ajax/search_ajax.php?f=matchCountry',
    	delay:10,
			minLength:2,
			select:function(event, ui) {
				search.setCountry(ui.item.value, ui.item.id);
				$("#menu_keuze").fadeIn();
			}
  	});
  	
  	$('#keywordInput').autocomplete({
			source: '/engine/ajax/search_ajax.php?f=matchKeywords',
    	minLength: 3,
    	delay: 300,
    	focus: function(event, ui) {
    		return false;
    	},
			select: function(event, ui) {
				search.setKeyword(ui);
			}
  	})
  	.data("autocomplete")._renderItem = function(ul, item) {
			return $("<li></li>")
				.data("item.autocomplete", item)
				.append("<a>" + item.label + "<br><span class='lblAutocomplete'>" + item.desc + "</span></a>")
				.appendTo(ul);
		};
  }
  
  
  this.setCountry = function(value, data)
  {
  	if($('#countryInput').attr('rel') == value) return;
  
  	$('#countryInput').val(value);
    $('#countryInput').attr('rel', value);
    search.request('f=country&id=' + data + '&value=' + value, null);
  }
  
  
  this.setKeyword = function(ui)
	{
		window.location = '/artikel/' + ui.item.id + '/';
	}
	
	
	this.continentSelected = function(continent)
	{
		req = 'f=selectCountries&continent=' + continent;
		
		$.get(this.dest, req, function(data){
			$('#slctCountry').html(data);
		});
	}
	
	
	this.countrySelected = function(country)
	{
		req = 'f=countrySpecification&country=' + country;
		
		$.get(this.dest, req, function(data){
			$('#trPlace').html(data);
			search.searchAdvanced();
		});
	}
	
	
	this.typeSelected = function(place)
	{
		prefix = place.substring(0,1);
		def = 'country';
		if(prefix == 'a') def = 'area';
		else if(prefix == 'c') def = 'city';
		
		if(prefix == 'a' || prefix == 'c') place = place.substr(1);
	
		req = 'f=selectTypes&' + def + '=' + place;
		
		$.get(this.dest, req, function(data){
			$('#slctType').html(data);
		});
	}
	
	
	this.advancedSearch = function()
	{
		var results = $('#frmAdvancedSearch').serialize();
		results = decodeURI(results);
		
		q = $('#advKeywordInput').val();
		country = $('#slctCountry').val();
		type = $('#slctType').val();
		
		if(country == 0 && type == 0)
		{
			$('#keywordInput').val(q);
			search.byKeyword();
			return;
		}
		
		req = 'f=retrieveLink&' + results;
		$.get(this.dest, req, function(data){
			if(q.length < 3)
				window.location = data;
			else
				window.location = data + '/zoek/' + search.stripKeyword(q);
		});
	}
	
	
	this.validateSearchForm = function()
	{
		// validate signup form on keyup and submit
		$("#frmAdvancedSearch").validate({
		submitHandler: function(form) {
   		search.advancedSearch();    	
   	},
		rules: {

		},
		messages: {

		}
		});
	}
  
  
  this.request = function(req, target)
	{
		$.ajax({
			url: this.dest,
			type: 'GET',
			cache: false,
			data: req,
			success: function(data)
			{
				$('#' + target).html(data);
			}
		});
	}

}

$(document).ready(function() {
	search = new search();
	search.init();
});
