<?php
 
    class html{
        
        static $instance , $i;
        static $tpl_messages_dir = 'tpl/messages/';
        
        public $tmp_error = false;
        
        public function __construct(){
            self::$instance = $this;
            self::$i = $this;
        }
        
        /**
         * @param mix $value
         * @param string $tpl_prefix // tpl_messages_dir / error_$tpl_prefix.tpl
         */
        public static function error( $value , $add_params = false ,  $tpl_prefix = 'page' ){
            global $tpl;
            
            self::$i->tmp_error = $value;
            
            return '<div class="bg-danger p10">' . $value . '</div>';
        }

        /**
         * @param mix $value
         * @param string $tpl_prefix // tpl_messages_dir / error_$tpl_prefix.tpl
         */
        public static function warn( $value ){
            global $tpl;
            
            self::$i->tmp_error = $value;
            
            return '<div class="bg-warning p10">' . $value . '</div>';
        }
        
        public static function success( $value , $add_params = false ,  $tpl_prefix = 'page' ){
            global $tpl;
            
            self::$i->tmp_error = $value;
            
            return '<div class="bg-success p10">' . $value . '</div>';
        }
        
        public static function infobox( $value ){
            return '<div class="ContentInfoBox">' . $value . '</div>';
        }
                
        /**
         * 
         */
        public function ok( $value , $add_params = false , $tpl_prefix = 'page' ){
            global $tpl;

            $tpl->assign(array(
                'value' => $value,
                'params' => $add_params
            ));
            
             return $tpl->fetch( file_exists(self::$tpl_messages_dir . 'ok_' . $tpl_prefix . '.tpl') ? (self::$tpl_messages_dir . 'ok_' . $tpl_prefix . '.tpl') : (self::$tpl_messages_dir . 'ok_default.tpl') );
            
        }
        
        public static function field( $value = false , $default_value = false , $StripTags = true ){

            $FieldValue = '';
            
            if( $value AND !empty( $value ) )
            {
                $FieldValue = ( $StripTags ? strip_tags( $value ) : $value );
            } 
            else
            {
                if( $default_value !== false  )
                {
                    $FieldValue = ( $StripTags ? strip_tags( $default_value ) : $default_value );
                }
            }
            
                return $FieldValue;
            
        }
        
        public static function ValueSelect( $SelectValue , $UsedValue = false )
        {
            $html = ' value="' . $SelectValue . '"';
            
            if( !empty( $SelectValue ) AND $SelectValue == $UsedValue )
            {
                $html .= ' selected="true"';
            } 
            
                return $html;
            
        }
        
        public static function CreateSelect( $name , $values , $AutoSelectValue = false , $id = false , $class = 'form-control' , $SelectField = '- wybierz -' )
        {
            $html = '<select name="' . $name . '"';

            // HTML CLASS
            if( $class ) $html .= ' class="' . $class . '"';
            
            // HTML ID
            $html .= ' id="' . ( $id ? $id : ('select-'.md5(microtime(true).rand(0,99999))) ) . '">';
            
            // Default select option
            if( $SelectField ) $html .= '<option value="">' . l( $SelectField ) . '</option>';
            
            if( count( $values ) )
            {
                foreach( $values as $key => $value )
                {

                    $html .= '<option'.self::ValueSelect( $key , $AutoSelectValue ).'>' . $value . '</option>';
                }
            }
            
            $html .= '</select>';
            
                return $html;
        }

        public static function CreateSimpleSelect( $name , $values , $AutoSelectValue = false , $id = false , $class = 'form-control' , $SelectField = '- wybierz -' )
        {
            $html = '<select name="' . $name . '"';

            // HTML CLASS
            if( $class ) $html .= ' class="' . $class . '"';
            
            // HTML ID
            $html .= ' id="' . ( $id ? $id : ('select-'.md5(microtime(true).rand(0,99999))) ) . '">';
            
            // Default select option
            if( $SelectField ) $html .= '<option value="">' . l( $SelectField ) . '</option>';
            
            if( count( $values ) )
            {
                foreach( $values as $key => $value )
                {

                    $html .= '<option'.self::ValueSelect( $value , $AutoSelectValue ).'>' . $value . '</option>';
                }
            }
            
            $html .= '</select>';
            
                return $html;
        }        
        public function editor( $admin=true ){
            
			js::$i->put( 'tiny_mce' );
			
			if( $admin ){
			     page::$i->setHead( "
					<script language=\"javascript\" type=\"text/javascript\">
					  tinyMCE.init({
					    mode : \"textareas\",
					    theme : \"advanced\",
					    language: \"pl\",
					    editor_deselector : \"mceNoEditor\",
					    gecko_spellcheck : true,
					    plugins : \"noneditable\",
					    noneditable_leave_contenteditable : true,
						theme_advanced_toolbar_location : \"top\"

					  });
					</script>			
					" );			
			} else {
			     page::$i->setHead( "
				<script language=\"javascript\" type=\"text/javascript\">
				  tinyMCE.init({
				    mode : \"textareas\",
				    theme : \"advanced\",
				    language: \"pl\",
				    editor_deselector : \"mceNoEditor\",
				    gecko_spellcheck : true,
				    plugins: \"advimage\",
				    theme_advanced_toolbar_location : \"top\"
				  });
				</script>			
				" );
			}
        }     
        
        /**
         * Get country flag svg image
         * @param string $CountryCode - Like PL, DE, etc.
         * @param string $Format - Available: 4x3 (This is default) and 1x1 
         */
        public static function CountryFlag( $CountryCode , $Format = '4x3' )
        {
            return '<img src="flags/'.( $Format ).'/'.( strtolower( $CountryCode ) ).'.svg" alt="'.( $CountryCode ).'" class="CountryFlag'.( $Format ).'" />';
        }   
        
    }




?>