<?php
    class js{
        
        static $instance , $i;
        public $json = array();
        
        public function __construct(){
            
            self::$instance = $this;
            self::$i = $this;
            
        }
        
        public function datapicker(){
            $this->put('jq-datapicker',true);
            css::$i->put('jquery-ui',true);
        }
        
        public function put( $srcname , $mapname = false , $useMap = true ){
         
            if( is_array( $srcname ) ){
                
                foreach( $srcname as $name => $file ){
                    
                    page::$instance->js[$file] = $file;
                }
                
            } else {
                
                page::$instance->js[$srcname] = $srcname; 
                
            }

            
        }
        
        /**
         * @param mixed $name - Json var name or array('name'=>'value')
         * @param mixed $value - Default: false
         */
        public function setJSON( $name , $value = false )
        {
            if( is_array( $name ) )
            {
                foreach( $name AS $Key => $Value )
                {
                    $this->json[$Key] = $Value;
                }
            } 
            else 
            {
                $this->json[$name] = $value;
            }
        }
        
        public function getJSON(){
            return json_encode( $this->json );
        }
        
        public function clearJSON(){
            $this->json = array();
        }
        
        /**
         * New functions
         */
         
         
        public static function RoutePoint(){
            
        	if(file_exists('ajax/'. core::$i->mode .'/'.core::$i->function.'.php')){
        	   
               include 'ajax/'. core::$i->mode .'/'.core::$i->function.'.php';
               
        	} else {
        	   
        	   js::$i->setJSON('status',false);
               js::$i->setJSON('error',l('Błąd podczas komunikacji z serwerem'));
        	
            }
        }
        
        public static function LoadForm( $form_location ){
            global $tpl;
            
            js::$i->setJSON('status',true);
            js::$i->setJSON('form',true);
            js::$i->setJSON('html',$tpl->fetch( $form_location ) );
        }
        
        /**
         * Inicjowanie kliknięcia w element po załadowaniu strony
         */
        public static function InitClick( $HtmlElement )
        {
             $html = '<script type="text/javascript">';
             $html .= "$(function(){ ";
             $html .= "$('".( $HtmlElement )."').click();";
             $html .= " });";
             $html .= '</script>';
             
                return $html;
        }
        
        public static function jqueryCode( $Code )
        {
             $html = '<script type="text/javascript">';
             $html .= "$(function(){ ";
             $html .= $Code;
             $html .= " });";
             $html .= '</script>';
             
                return $html;
        }
        
    }

?>