var ac = null;

function search()
{

	this.init = function()
	{
		this.searchMenu();
		this.searchTabs();
		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');
		});
		
		$('#countryInput').click(function() {
			if($(this).val() == 'Type een land...') $(this).val('');
			search.lookupAjax();
		});
		
		$('#countryInput').focusout(function() {
			if(($(this).val()).length < 1) $(this).val('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();
		});
		
		$('#buttonKeyword').click(function() {
			search.byKeyword();
		});
 
		// 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.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)
	{
		window.location = '/' + cat + '/alle/' + $('#countryInput').attr('rel');
	}
	
	
	this.byKeyword = function()
	{
		q = $('#keywordInput').val();
		q = q.replace(/<\/?[^>]+>/gi, '');
		q = q.replace(/\&+/g, "");
		q = q.replace(/\s+/g, "+");
		q = q.replace(/\%+/g, "");
		window.location = '/zoek/' + 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.lookupAjax = function()
	{
		var oSuggest = $("#countryInput")[0].autocompleter;
		oSuggest.findValue();
 
		return false;
	}
	

	this.autocomplete = function()
	{
		var ac = $('#countryInput').autocomplete(
		'/engine/ajax/search_ajax.php', 
		{
			extraParams: {f:'matchCountry'},
    	delay:10,
			minChars:2,
			cacheLength:10,
			onItemSelect:function(li) {
				search.findValue(li);
				$("#menu_keuze").fadeIn();
			},
			onFindValue:function(li) {
				search.findValue(li);
			},
			selectFirst:true,
			autoFill:true
  	});
  }
  
  
  this.findValue = function(li)
  {
  	if( li == null ) return;
  	
 		search.setCountry(li.selectValue, li.extra[0]);
  }
  
  
  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.request = function(req, target)
	{
		$.ajax({
			url: '/engine/ajax/search_ajax.php',
			type: 'GET',
			cache: false,
			data: req,
			success: function(data)
			{
				$('#' + target).html(data);
			}
		});
	}

}

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