<?php
/*
        max: 10,
        delay: 100,
        cacheLength: 1,
        scroll: false,
        highlight: false           
*/
class test_autocomplete
{
    function test_autocomplete()  
    {
        $g=&$GLOBALS;
  $g[HEADER]->addscriptfile(AM_PLUGIN_URL.'/jquery/js/jquery-1.6.1.min.js');
  $g[HEADER]->addscriptfile(AM_PLUGIN_URL.'/jquery/js/jquery-ui-1.8.13.custom.min.js');  
  $g[HEADER]->addscriptfile(AM_PLUGIN_URL.'/jquery/am_autocomplete_jquery.js');
  $g[HEADER]->addstylesheet(AM_PLUGIN_URL.'/jquery/css/ui-lightness/jquery-ui-1.8.13.custom.css');  
  $g[HEADER]->addstylesheet(AM_PLUGIN_URL.'/jquery/css/am_autocomplete_jquery.css'); 
  $g[HEADER]->addfunction('autocomplete',"
  \$(document).ready(function(){
      am_autocomplete('state','test');

     \$('#btn').click(function(){      
     \$('#dv').effect('scale',{percent:110, direction: 'both'});
     
     });
     \$('#dv').width(100);
     \$('#dv').hover(function(){ 
            if(\$('#dv').width()<=400)
            {
                \$('#dv').effect('size',{ to: { width: 400, height: 120 }, scale: 'content'  },300);  
            }
        },
        function(){
            \$('#dv').effect('size',{ to: { width: 200, height: 60 },scale: 'content'  },200);  
        }
      );
 });

 
  ");
        
    }
    function index()
    {
        ?>        
        <form>
        <input type='text' name='state' id='state' value='' />
        <input type='button' name='btn' id='btn' value='Btn' />        
        </form>
        <div id='dv' style="border:10px solid red;overflow:hidden;">
                   This is some div
        </div>
        <?        
    }
        function autocomplete_state()
        {
            $g=&$GLOBALS;
            $g[CODEPLATE]=$g[AM_SITE][themefolder].'/null_layout.php';
            
            //dbug($_REQUEST);
            /**
            * Add Categories in the suggestion list too
            * 
            */
            $f=fopen('cache/amit.txt','a');
            fwrite($f,implode(',',array_keys($_REQUEST))."\n".implode(',',$_REQUEST)."\n".date('M d,Y H:i:s').'------------'."\n");
            fclose($f);
            if(empty($g[q])) return;
            $query="SELECT category FROM mz_categories WHERE category RLIKE '$g[q]' 
            LIMIT 7";
            $rows=am_DB::getRows($query);
            $cnt=0;
            foreach($rows as $row)
            {
                $row[item]=$row[category];
                //clear commas in each item so that they don't appear as multiple suggestions
                $row[item]=str_replace(',',' ',$row[item]);

                //clean single quotes to avoid JSON error
                $row[item]=str_replace("'","\'",$row[item]);

                 $data[$cnt]['label']=$row['item'];
                $data[$cnt]['value']=$row['item'];
                $cnt++;
            }


            /**
            *Add companies in suggestion list
            * 
            */
            $query="SELECT company_name, categories
            FROM mz_profiles 
            WHERE company_name RLIKE '$g[q]' OR 
            categories LIKE '%$g[q]%' 

            LIMIT 7";
            $rows=am_DB::getRows($query);            
            foreach($rows as $row)
            {
                $row[item]="$row[company_name]";
                //clear commas in each item so that they don't appear as multiple suggestions
                $row[item]=str_replace(',',' ',$row[item]);

                //clean single quotes to avoid JSON error
                $row[item]=str_replace("'","\'",$row[item]);

                //$data[$cnt]['label']=str_replace($g[q],"<strong>$g[q]</strong>",$row[item]);
                $data[$cnt]['label']=$row['item'];
                $data[$cnt]['value']=$row['item'];
                $cnt++;
            }
            
            /*echo '[';
            foreach($data as $d)
            {                
                $out.="{\"label\":$d[label],\"value\":$d[value]},";
            }         
            echo substr($out,0,-1).']';
            */
            echo json_encode($data);
        }
 
}
