/**
 * @projectname     Diana Trnas
 * @version         1.0 2010.03.18
 * @copyright       Copyright (C) 2008 - 2010 All rights reserved.
 * @license         Commercial
 * @author          Székely Csaba / csaba@szekely.ro / http://www.csaba.szekely.ro
 *
 * @desc            Script for search display
 */




// on document ready runing page
jQuery(document).ready(function(){
    // prevent browser from starting the autofill function
    jQuery('#search_text').attr('autocomplete', 'off');
    // getting default value of searchbox
    var search_def_value = jQuery('#search_text').val();

    // when click on search box select text in box
    jQuery('#search_text').focus(function(){
       // only select if the text has not changed
         if(this.value == search_def_value){
            //this.select();
            jQuery('#search_text').val('');
         } // end if searchbox is default value
    });  // end focus

    // cheking for input change
    jQuery('#search_text').keyup(function(){
       // only select if the text has not changed
         if(this.value != search_def_value){
            // initializing search suggestion
            loadSuggest();
         } // end if
    }); // end keyup

    // hidding sugest no out
    jQuery('#search_text').blur(function(){
		// hide the suggestions
		setTimeout('jQuery(".search_suggest").hide()',1000);
    }); // end blur

	// displaying search box options
	jQuery('.search_show_opt_link_container').click(function(){
		// hidding open link
		jQuery('.search_show_opt_link_container').hide();
		// displaying options
		jQuery('.search_opt_container').show();
	}); // end disp. search option

	// hide search box options
	jQuery('.search_hide_opt_link_container').click(function(){
		// hide options
		jQuery('.search_opt_container').hide();
		// show display option button
		jQuery('.search_show_opt_link_container').show();
	}); // end hide search option
}); // end on ready

// initializing sugest
function loadSuggest(){
    // assigning object to variable
    var search_input = jQuery('#search_text');
    // retrieve the keyword object
    var keyword = search_input.val();

	// hide options
	jQuery('.search_opt_container').hide();
	// show display option button
	jQuery('.search_show_opt_link_container').show();

    // check to see if the keyword is empty
    if(keyword.length < 3 ){
        // hide the suggestions
        jQuery(".search_suggest").hide() ;
        // reset the keywords
        userKeyword="";
    } else {
        if(jQuery(".search_suggest").is(":hidden")){
            // displaying
            jQuery(".search_suggest").show();
        }; // end if
        // showing loading gif
        jQuery('.search_suggest').html(jQuery('.ajax_small_loading_img_container').html());
        // form input data
		var form_input_data = jQuery('#search_frm').serialize();
        // loading data from php
        jQuery(".search_suggest").load('./modules/pageSearch/suggestHandler.php?action=suggest&'+form_input_data, function(){
            // linking location suggestin hover
            linkElementHover(".search_suggest .location_result_list_element_container", 'loc_sug_location_id');
            // linking county suggesting hover
            linkElementHover(".search_suggest .county_result_list_element_container", 'loc_sug_county_id');
            // linking county suggesting hover
            linkElementHover(".search_suggest .unit_result_list_element_container", 'loc_sug_unit_id');
            // linking region suggesting hover
            linkElementHover(".search_suggest .region_result_list_element_container", 'loc_sug_region_id');
        }); // end load
    } // end if else
} // end function snist suggest


// linking hover to map over
function linkElementHover(hover_item, item_id){
    // after data loads
    jQuery(hover_item).hover(
        function(){
			// if not empty string
			if(jQuery(this).attr(item_id).length > 1){
				var id_array_in = jQuery(this).attr(item_id).split(',');
				// inserting on hower for each id
				jQuery.each(id_array_in, function(index, value) {
					jQuery("#romania " + "area#" + value).mouseover();
				}); // end foreach
			} // end if not empty string
        }, // end hover function
        function(){
			// if not empty string
			if(jQuery(this).attr(item_id).length > 1){
				var id_array_out = jQuery(this).attr(item_id).split(',');
				// inserting on hower for each id
				jQuery.each(id_array_out, function(index, value) {
					jQuery("#romania " + "area#" + value).mouseout();
				}); // end foreach
			} // end if not empty string
        } // end callback function
    ); // end hover
} // end function


































