function Search()
{
	this.searchPageCount = 0;
	this.customSearchPageCount = 0;
	this.lastQuery = false;
	
	this.isNewQuery = function(query)
	{
		if(query == this.lastQuery)
		{
			return false;
		} else {
			this.lastQuery = query;
			return true;
		}
	}
	
	this.setLastQuery = function(query)
	{
		this.lastQuery = query;
	}
	
	this.search = function(from)
	{
		var filterChecked = new Array();
		if(!$('#filter1:checked').val()) { filterChecked.push(1); }
		if(!$('#filter2:checked').val()) { filterChecked.push(2); }
		if(!$('#filter3:checked').val()) { filterChecked.push(3); }
		if(!$('#filter4:checked').val()) { filterChecked.push(4); }
		if(!$('#filter5:checked').val()) { filterChecked.push(5); }
		if(!$('#filter6:checked').val()) { filterChecked.push(6); }
		if(!$('#filter7:checked').val()) { filterChecked.push(7); }
		if(!$('#filter8:checked').val()) { filterChecked.push(8); }
		if(!$('#filter9:checked').val()) { filterChecked.push(9); }

		var self = this;
		$.post(applicationObject.rootPath+'index.php?controller=Content&action=results', { f: from, 'filter[]': filterChecked }, function(data)
		{
			var returnedData = data.split('||');
			$('#searchResult').html(returnedData[0]);
		});
	}
	
	this.searchFilter = function()
	{
		var filterChecked = new Array();
		if(!$('#filter1:checked').val()) { filterChecked.push(1); }
		if(!$('#filter2:checked').val()) { filterChecked.push(2); }
		if(!$('#filter3:checked').val()) { filterChecked.push(3); }
		if(!$('#filter4:checked').val()) { filterChecked.push(4); }
		if(!$('#filter5:checked').val()) { filterChecked.push(5); }
		if(!$('#filter6:checked').val()) { filterChecked.push(6); }
		if(!$('#filter7:checked').val()) { filterChecked.push(7); }
		if(!$('#filter8:checked').val()) { filterChecked.push(8); }
		if(!$('#filter9:checked').val()) { filterChecked.push(9); }
	
		var self = this;
		$.post(applicationObject.rootPath+'index.php?controller=Content&action=results', { f: 0, 'filter[]': filterChecked }, function(data)
		{
			var returnedData = data.split('||');
			self.searchPageCount = parseInt((returnedData[1] / 15));
			$("#searchPager").pager({ pagenumber: 1, pagecount: self.searchPageCount, buttonClickCallback: searchPageClick });
			$('#searchResult').html(returnedData[0]);
		});
	}

	this.openCustomSearch = function(site, query)
	{
		$.prompt('<iframe src="'+applicationObject.rootPath+'index.php?controller=Content&action=customSearch&site='+site+'&sq='+escape(query)+'" width="540" height="450" frameborder="0">');
	}

	this.customSearch = function(from, site)
	{
		var self = this;
		$.post(applicationObject.rootPath+'index.php?controller=Content&action=customresults', { f: from, site: site }, function(data)
		{
			var returnedData = data.split('||');
			$('#customSearchResult').html(returnedData[0]);
		});
	}

	this.insertAlert = function()
	{
		var error = false;
		var errorMessage = '';
		var query = $('#query').val();
		var name = $('#name').val();
		var company_name = $('#company_name').val();
		var competitor1 = $('#competitor1').val();
		var competitor2 = $('#competitor2').val();
		var competitor3 = $('#competitor3').val();
		var email = $('#email').val();
		var phone = $('#phone').val();
		//var frequency = $("input[name='frequency']:checked").val();
		var noresults = 0;
		if ($('#noresults:checked').val() !== null) { noresults = 1; }
		if(!validationAlert())
		{		
			$.post(applicationObject.rootPath+'index.php?controller=Alerts&action=subscribe', { query: query, name: name, company_name: company_name, competitor1: competitor1, competitor2: competitor2, competitor3: competitor3, email: email, phone: phone, noresults: noresults }, function(str)
			{
				if(str == 'saved') {
					$.prompt('Een bevestigingsmail is aan je verzonden.',{ callback: goHome });
				} else {
					//$.prompt('Er is iets misgegaan met je inschrijving.',{ callback: goHome });
				}
			});
		}
	}
}

function goHome()
{
	document.location.href=applicationObject.rootPath;
}
