/* jQuery jcautare 1.0
 * http://www.webspider.ro/jquery-plugins/jquery.jcautare/
 *
 * Copyright (c) 2010 Ovidiu Pop
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function(jQuery){
 	jQuery.fn.extend({jcautare: function(options) {
		var defaults = {
			mod:'advanced', //simple
			doajax:false,
			mod_debug:true,
			up_icon:'img/button_up.gif',
			down_icon:'img/button_down.gif',
			str_search:'Search...',
			str_any: 'Any word',
			str_all: 'All words',
			str_exact: 'Exact',
			ajax_page : 'functions.php',
			ajax_function:'function=myfunction',

			atasare_modul:function(){
				 obj.html(options.mod == "advanced"? options.modul_avansat(): options.modul_simplu());
				isearch = jQuery(".input_cautare");
				jQuery('.div_toggle_avansata').click(function(){
					jQuery('.optiuni_cautare_avansata').toggle();
					if(jQuery('.optiuni_cautare_avansata').css("display") == "block"){
						jQuery('.div_toggle_avansata').html('<img src="'+options.up_icon+'" alt="sus" />');
					}else{
						jQuery('.div_toggle_avansata').html('<img src="'+options.down_icon+'" alt="down" />');
					}
				});
				options.clear_search();
			 },
			 
			 modul_simplu:function(){
				 var html = '<input class="input_cautare" type="text" value="'+options.str_search+'" />';
				 return html;
			 },
			 
			 modul_avansat:function(){
				var html = '';
				html += '<div class="search_inputs">';
					html += '<div class="div_input_search"><input class="input_cautare" type="text" value="'+options.str_search+'" /></div>';
					html += '<div class="div_toggle_avansata"><img src="'+options.down_icon+'" alt="down" /></div>';
				html += '</div>';
				html += '<div class="optiuni_cautare_avansata">';
					html += options.optiuni_avansate();
				html += '</div>';

				 return html;
			 },

			 optiuni_avansate:function(){
				var tipuri = ['any', 'all', 'exact'];
				var html ='';
				jQuery(tipuri).each(function(i,n){
					var checked = (i == 0) ? ' checked="checked"' : "";
					html += '<div class="row_optiunea_cautare"><input type="radio" name="osearch" value="' + n + '"' + checked + '/> '+eval("options.str_" + n)+'</div>';
				})

				return html;
			 },
			 
			 clear_search:function(){
				isearch.bind("focus keypress", function(){
					if(jQuery('.optiuni_cautare_avansata').css("display") == "block"){
						jQuery('.div_toggle_avansata').html('<img src="'+options.down_icon+'" alt="jos" />');
						jQuery('.optiuni_cautare_avansata').toggle();
					}
					isearch.val(isearch.val() == options.str_search ? "" : isearch.val());
				})
				
				isearch.blur(function(){
					isearch.val(isearch.val() == "" ? options.str_search : isearch.val());
				})
				
				isearch.keyup(function(event){
					if(event.keyCode == 27){
						isearch.val(options.str_search);
					}
					if(event.keyCode == 13){
						var optiune = jQuery('input[name=osearch]:checked').val();
						var termen = isearch.val();
						if(!termen){return false;}
						if(options.doajax){
							$.post(options.ajax_page, options.ajax_function+'&arg1='+termen+'&arg2='+optiune, function(rez){

								jQuery(".titlu").html('<h1>Rezultatele cautarii</h1>');
								jQuery(".continut").html(rez);
								
								if(options.mod_debug){
									alert(rez);
								}
							})
						}else{
							if(options.mod_debug){
								alert(termen +'::'+optiune);
							}
						}
					}
				})
			}
		}

		var options =  $.extend(defaults, options);
		var obj = '';
		var isearch = '';
		
    		return this.each(function() {
			obj = jQuery(this);
			options.atasare_modul();
		});
	}
});
})(jQuery);
