var search_default	=	[];

jQuery(function( $ ){
	
	$('input.search-input-q').each(function (e) {
		search_default[this.id]	=	this.value;
	});
	
			
	$('input.search-input-q').focus(function (e) {		
		if (this.value == search_default[this.id]) {
			this.value	=	'';
		}			
	});	
	
	$('input.search-input-q').blur(function (e) {		
		if (this.value == '') {
			this.value = search_default[this.id];
		}
	});	
	
	$('form.search-submit').submit(function (e) {
		
		myid	=	this.id.split('-').pop();
		var returnvalue	=	true;
		$('input.search-input-q').each(function (e) {
			inputID	=	this.id.split('-').pop();
			
			if (myid == inputID && search_default[this.id] == this.value) {
				returnvalue	=	false;
				return;
			}
		});
				
		return returnvalue;		
	});
	
	
	
	if (typeof performSearch != 'undefined') {
		
		if (performSearch == 'index') {
			_SearchBuildIndex();
		}
		else {
			_SearchPerformSearch();
		}
	}
});

var	totalItems	=	0;

function _SearchBuildIndex ( ) {
	
	var	url			=	serverpath + '/cms/ajax/';

	$.ajax({
		type:		"GET",
		url:	url,
		data: {			
			nodes_ID	:	nodes_ID,
			sockets_ID	:	sockets_ID,			
			action		:	'buildIndexPrepare'
				
		},
		success: function (html) {

			$('#result').html(html);
		
			$('.items').each(function (e) {
				totalItems	+=	parseInt($(this).html());
			});
						
			$(SearchItems).each(function(e) {
				_SearchBuildIndexPlugin(this, 1);
			});
			
		}
	});	

}

function _SearchBuildIndexPlugin ( data, page ) {

	var	url			=	serverpath + '/cms/ajax/';
	
	var	plugin		=	data.plugin;
	var	mode		=	data.mode;
	var	type		=	data.type;

	$.ajax({
		type		:	'GET',
		url			:	url,
		dataType	:	'json',
		data: {			
			nodes_ID	:	nodes_ID,
			sockets_ID	:	sockets_ID,	
			action		:	'buildIndexPlugin',
				
			plugin		:	plugin,
			mode		:	mode,
			type		:	type,
			position	:	page						
		},
		success: function (mydata) {
			
			var items	=	parseInt($('#items-' + plugin + '-' + mode).html()) - mydata.items;
			
			$('#items-' + plugin + '-' + mode).html(items);	
			
			if (items > 0) {
				
				
				_SearchBuildIndexPlugin (data, page + 1);
			}
						
			var remaningItems	=	0;
			$('.items').each(function (e) {
				remaningItems	+=	parseInt($(this).html());
			});
			
			
			var percent	=	Math.floor(remaningItems * 100 / totalItems);
			
			$('#result').css('background', 'none');
			
				
			$('#searchResult').html($('#searchResult').html() + '<br />' + percent + ' %');
			$("#progressbarValue").animate({
				width	:	(100 - percent) + '%'
			});
			
			if (remaningItems < 1) {
				_SearchPerformSearch();				
			}
		}
	});
}

function _SearchPerformSearch ( ) {
	
	var	url			=	serverpath + '/cms/ajax/';
	
	$.ajax({
		type		:	'GET',
		url			:	url,
		dataType	:	'html',
		data: {			
			nodes_ID	:	nodes_ID,
			sockets_ID	:	sockets_ID,	
			action		:	'search',
							
			q			:	searchQuery						
		},
		success: function (html) {
			$('#searchResultIndexing').remove();
			$('#resultReceiver').html(html);
			
			if (typeof Cufon != 'undefined') {
				Cufon.refresh();
			}
			
			$('#resultReceiver').css('opacity', 0);							
			$('#resultReceiver').animate({
				opacity	:	1
			}, 400, 'linear');
		}
	});
	
}
