<?php

class ffGFonts{
    static $instance;
    
    static function getInstance(){
        if( empty(self::$instance) ){
            self::$instance = new ffGFonts();
        }
        return self::$instance;
    }
    
    function printFontCSS(){
        if( fOpt::Get('fontfamily', 'use-defined-fonts') ){
            $this->printGoogleCSSOnFonts(
                array('menu','pagetitle','headings','text','button','blogpreviousnext','dropcap','sidemenu','faq','pricing'),
                get_template_directory().'/css/fonts_template.css'
            );
        }else{
            echo "<link href='".get_template_directory_uri()."/css/fonts_template.css' rel='stylesheet' type='text/css'>";
        }
    }

    protected $gFonts = array(
            'Abel' =>           array( 'type' => 'sans-serif', 'subtypes' => null),
            'Anton' =>          array( 'type' => 'sans-serif', 'subtypes' => null),
            'Arimo' =>          array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Arvo' =>           array( 'type' => 'serif',      'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Asap' =>           array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Bitter' =>         array( 'type' => 'serif',      'subtypes' => array( '400', '700', '400italic') ),
            'Black Ops One' =>  array( 'type' => 'cursive',    'subtypes' => null),
            'Cabin' =>          array( 'type' => 'sans-serif', 'subtypes' => array( '400', '500', '600', '700', '400italic', '500italic', '600italic', '700italic') ),
            'Cantarell' =>      array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Coming Soon' =>    array( 'type' => 'cursive',    'subtypes' => null),
            'Crafty Girls' =>   array( 'type' => 'cursive',    'subtypes' => null),
            'Cuprum' =>         array( 'type' => 'sans-serif', 'subtypes' => array( '400', '400italic', '700', '700italic') ),
            'Dancing Script' => array( 'type' => 'cursive',    'subtypes' => null),
            'Dosis' =>          array( 'type' => 'sans-serif', 'subtypes' => array( '200', '300', '400', '500', '600', '700', '800') ),
            'Droid Sans' =>     array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700') ),
            'Droid Serif' =>    array( 'type' => 'serif',      'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Exo' =>            array( 'type' => 'sans-serif', 'subtypes' => array( '400', '500', '600', '700', '400italic', '500italic', '600italic', '700italic') ),
            'Francois One' =>   array( 'type' => 'sans-serif', 'subtypes' => null),
            'Changa One' =>     array( 'type' => 'cursive',    'subtypes' => array( '400', '400italic') ),
            'Josefin Sans' =>   array( 'type' => 'sans-serif', 'subtypes' => array( '400', '600', '700', '400italic', '600italic', '700italic') ),
            'Lato' =>           array( 'type' => 'sans-serif', 'subtypes' => array( '100', '300', '400', '700', '900', '100italic', '300italic', '400italic', '700italic', '900italic') ),
            'Lobster' =>        array( 'type' => 'cursive',    'subtypes' => null),
            'Lora' =>           array( 'type' => 'serif',      'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Merriweather' =>   array( 'type' => 'serif',      'subtypes' => array( '400', '300', '700', '900') ),
            'Montserrat' =>     array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700') ),
            'Nobile' =>         array( 'type' => 'sans-serif', 'subtypes' => array( '400', '400italic', '700', '700italic') ),
            'Nunito' =>         array( 'type' => 'sans-serif', 'subtypes' => array( '400', '300', '700') ),
            'Open Sans' =>      array( 'type' => 'sans-serif', 'subtypes' => array( '300italic', '400italic', '600italic', '700italic', '800italic', '400', '300', '600', '700', '800') ),
            'Open Sans Condensed' =>
                                array( 'type' => 'sans-serif', 'subtypes' => array( '300', '300italic', '700') ),
            'Oswald' =>         array( 'type' => 'sans-serif', 'subtypes' => array( '400', '300', '700') ),
            'Oxygen' =>         array( 'type' => 'sans-serif', 'subtypes' => array( '400', '300', '700') ),
            'Pacifico' =>       array( 'type' => 'cursive',    'subtypes' => null),
            'Play' =>           array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700') ),
            'PT Sans' =>        array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'PT Sans Narrow' => array( 'type' => 'sans-serif', 'subtypes' => array( '400', '700') ),
            'PT Serif' =>       array( 'type' => 'serif',      'subtypes' => array( '400', '700', '400italic', '700italic') ),
            'Questrial' =>      array( 'type' => 'sans-serif', 'subtypes' => null),
            'Raleway' =>        array( 'type' => 'sans-serif', 'subtypes' => array( '400', '200', '300', '500', '600', '700', '800') ),
            'Righteous' =>      array( 'type' => 'cursive',    'subtypes' => null),
            'Rokkitt' =>        array( 'type' => 'serif',      'subtypes' => array( '400', '700') ),
            'Shadows Into Light' =>
                                array( 'type' => 'cursive',    'subtypes' => null),
            'Signika' =>        array( 'type' => 'sans-serif', 'subtypes' => null),
            'Source Sans Pro' =>array( 'type' => 'sans-serif', 'subtypes' => array( '200', '300', '400', '600', '700', '900', '200italic', '300italic', '400italic', '600italic', '700italic', '900italic') ),
            'Squada One' =>     array( 'type' => 'cursive',    'subtypes' => null),
            'The Girl Next Door' =>
                                array( 'type' => 'cursive',    'subtypes' => null),
            'Ubuntu' =>         array( 'type' => 'sans-serif', 'subtypes' => array( '300', '400', '500', '700', '300italic', '400italic', '500italic', '700italic') ),
            'Ubuntu Condensed' =>
                                array( 'type' => 'sans-serif', 'subtypes' => null),
            'Unkempt' =>        array( 'type' => 'cursive',    'subtypes' => array( '400', '700') ),
            'Vollkorn' =>       array( 'type' => 'serif',      'subtypes' => array( '400italic', '700italic', '400', '700') ),
            'Yanone Kaffeesatz' =>
                                array( 'type' => 'sans-serif', 'subtypes' => array( '400', '200', '300', '700') ),
    );
    
    protected $gFontsTypes = array(
                  'sans-serif' => 'Sans-serif',
                  'serif' => 'Serif',
                  'cursive' => 'Cursive',
    );
    
    function printBigCSSLink(){
        echo "<link href='http://fonts.googleapis.com/css?family=";
        foreach ($this->gFonts as $fontName=>$fontData) {
            echo $fontName;
            if( !empty($fontData['subtypes']) ){
                echo ":";
                echo implode(",",$fontData['subtypes']);
            }
            /*
            echo ":";
            echo implode(",", array( '100', '200', '300', '400', '500', '600', '700', '800', '900', '100italic', '200italic', '300italic', '400italic', '500italic', '600italic', '700italic', '800italic', '900italic' ) );
            // */
            echo "|";
        }
        echo "&subset=latin,latin-ext,cyrillic,cyrillic-ext,greek-ext,greek,vietnamese' rel='stylesheet' type='text/css'>";
    }
    
    function printGoogleCSSLink($fonts){
        echo "http://fonts.googleapis.com/css?family=";
        if( ! is_array($fonts) ){
            $fonts = array( $fonts );
        }
        foreach ($fonts as $key=>$font) {
            echo $font;
            if( !empty($this->gFonts[$font]) ){
                if( !empty($this->gFonts[$font]['subtypes']) ){
                    echo ":";
                    echo implode(",",$this->gFonts[$font]['subtypes']);
                }
            }
            echo "|";
        }
        echo "&subset=latin,latin-ext,cyrillic,cyrillic-ext,greek-ext,greek,vietnamese";
    }
    
    function printGoogleCSSOnFonts( $items, $css_template ){
        //$items = array('menu','headings','text','dropcap');
        //fOpt::Get($ns,$n.'_fontPickerIsGoogleFontChecker')

        $fonts_to_include = array();
        foreach ($items as $key=>$item) {

            if( ! fOpt::Get( 'fontfamily', $item . '_fontPickerIsGoogleFontChecker' ) ){
                continue;
            }
            
            $font = fOpt::Get('fontfamily', $item);

            $font_key = $font;
            
            if( FALSE !== strpos($font_key, ',') ){
                $font_key = explode(',',$font_key);
                $font_key = trim($font_key[0]);
            }
            $fonts_to_include[ $font_key ] = $font_key;
        }
        
        echo "<link rel='stylesheet' href='";
        $this->printGoogleCSSLink($fonts_to_include);
        echo "' type='text/css' media='all' />";

        $css = file_get_contents($css_template);
        foreach ($items as $key=>$item) {
            $fontfamily = fOpt::Get('fontfamily', $item);
            $fontfamily = explode(',', $fontfamily);
            $fontfamily[0] = "'".$fontfamily[0]."'";
            $fontfamily = implode(',',$fontfamily);
            $css = str_replace("/*font-family: $item;*/", "font-family: $fontfamily;", $css);

            $fontweight = fOpt::Get('fontfamily', $item . '_fontPickerWeight');
            $css = str_replace("/*font-weight: $item;*/", "font-weight: $fontweight;", $css);

            $fontstyle = fOpt::Get('fontfamily', $item . '_fontPickerCursive') ? 'italic' : 'normal';
            $css = str_replace("/*font-style: $item;*/", "font-style: $fontstyle;", $css);
        }

        echo "<style type='text/css' media='screen'>";
        echo $css;
        echo "</style>";

        //echo '<pre>'; echo $css; echo '</pre>';
        
    }

    function printComponent($id, $fontFamily, $isGoogleFont, $isCursive, $fontWeight){
        echo $this->getComponent($id, $fontFamily, $isGoogleFont, $isCursive, $fontWeight);
    }

    function getComponent($id, $fontFamily, $isGoogleFont, $isCursive, $fontWeight){

        $ret = '';

        // WRAPPER START
        $ret .= '<div ';
        $ret .= ' class="fontPickerWrapper" id="'.$id.'_fontPickerWrapper" data-font-field-id="'.$id.'" ';
        $ret .= ' >';

        // FONT EXAMPLE
        $ret .= '<div style="padding-bottom:20px">';
        $ret .= '<div class="fontPickerShowCase description" style="padding:20px 30px;font-size:15px">';
        $ret .= '<p style="margin:0;padding:0">Grumpy wizards make toxic brew for the evil Queen and Jack.</p>';
        $ret .= '</div>';
        $ret .= '</div>';


        $ret .= '<div style="overflow:hidden; padding-right:20px">';
        // CHECKBOX IN LABEL
        $ret .= '<p class="one_option_wrapper">';
        $ret .= '<label><input ';
        $ret .= ' class="fontPickerIsGoogleFontChecker checkbox" id="'.$id.'_fontPickerIsGoogleFontChecker" name="'.$id.'_fontPickerIsGoogleFontChecker" data-font-field-id="'.$id.'" ';
        $ret .= ' type="checkbox" ';
        if( $isGoogleFont ){
            $ret .= ' checked="checked"';
        }
        $ret .= ' />';
        $ret .= 'Google Font ';
        $ret .= ' </label> ';

        // GOOGLE SELECT TOP
        $ret .= '<select ';
        $ret .= ' class="fontPickerGoogleSelect" id="'.$id.'_fontPickerGoogleSelect" data-font-field-id="'.$id.'" ';
        $ret .= ' >';
        $ret .= '<option style="color:Silver" value="">- Another Google Font -</option>';
        foreach ($this->gFonts as $fontName=>$fontData) {
            $ret .= '<option value="'.$fontName.', '.$fontData['type'].'"';
            if( $fontName.', '.$fontData['type'] == $fontFamily ){
                $ret .= ' selected="selected"';
            }
            $ret .= '>'.$fontName.'</option>';
        }
        $ret .= '<select>';

        $ret .= '</p><p class="one_option_wrapper">';

        // FONT FAMILY
        $ret .= 'Font family: ';
        $ret .= '<input ';
        $ret .= ' class="fontPicker" id="'.$id.'" name="'.$id.'" ';
        $ret .= 'type="text" value="'.$fontFamily.'" placeholder="Arial, sans-serif" />';

        $ret .= '<a href="#" class="fontPickerShowCaseActivate button" data-font-field-id="'.$id.'">Refresh Example</a>';

        $ret .= '</p><p class="one_option_wrapper">';
        
        $ret .= '<label><input ';
        $ret .= ' class="fontPickerCursive checkbox" id="'.$id.'_fontPickerCursive" name="'.$id.'_fontPickerCursive" data-font-field-id="'.$id.'" ';
        $ret .= ' type="checkbox" ';
        if( $isCursive ){
            $ret .= ' checked="checked"';
        }
        $ret .= ' />';
        $ret .= 'Cursive ';
        $ret .= ' </label> ';

        $ret .= '</p><p class="one_option_wrapper">';

        $ret .= 'Font weight: ';

        $fontWeights = array(
            100 => '100',
            200 => '200',
            300 => '300',
            400 => '400 - normal',
            500 => '500',
            600 => '600',
            700 => '700 - bold',
            800 => '800',
            900 => '900',
        );
        
        $ret .= '<select ';
        $ret .= ' class="fontPickerWeight" id="'.$id.'_fontPickerWeight" name="'.$id.'_fontPickerWeight" ';
        $ret .= ' >';
        foreach ($fontWeights as $fontWeightValue=>$fontWeightDescription) {
            $ret .= '<option value="'.$fontWeightValue.'"';
            if( $fontWeightValue == $fontWeight ){
                $ret .= ' selected="selected"';
            }
            $ret .= '>'.$fontWeightDescription.'</option>';
        }
        $ret .= '<select>';

        $ret .= '</p>';

        // WRAPPER END
        $ret .= '</div>';
        
        return $ret;
    }
    
    static function printFontPickerJavascript(){
        ?><script type="text/javascript">
        jQuery(document).ready(function($) {

            $('.fontPickerGoogleSelect').change( function (){
                _id = $(this).attr('data-font-field-id');
                _val = $(this).val();
                if( ! _val ){
                    return;
                }
                $('#'+_id).val( _val );
                $('#'+_id+'_fontPickerWrapper .fontPickerShowCase p').css( 'font-family', _val );
            });

            $('.fontPickerGoogleSelect').click(  function (){ $('.fontPickerShowCaseActivate').click(); });
            $('.fontPickerGoogleSelect').change( function (){ $('.fontPickerShowCaseActivate').click(); });

            $('.fontPickerCursive').click(  function (){ $('.fontPickerShowCaseActivate').click(); });
            $('.fontPickerCursive').change( function (){ $('.fontPickerShowCaseActivate').click(); });

            $('.fontPickerWeight').click(  function (){ $('.fontPickerShowCaseActivate').click(); });
            $('.fontPickerWeight').change( function (){ $('.fontPickerShowCaseActivate').click(); });

            $('.fontPickerShowCaseActivate').click( function (){
                _id = $(this).attr('data-font-field-id');

                _fontFamily  = $('#' + _id).val();
                _fontWeight  = $('#' + _id + '_fontPickerWeight').val();
                _fontCursive = $('#' + _id + '_fontPickerCursive').is(':checked') ? 'italic' : 'normal';

                if( ! _fontFamily ){ _fontFamily = 'Arial'; }
                if( ! _fontWeight ){ _fontWeight = 400; }

                $('#'+_id+'_fontPickerWrapper .fontPickerShowCase p')
                    .css( 'font-family', _fontFamily )
                    .css( 'font-style', _fontCursive )
                    .css( 'font-weight', _fontWeight );
                return false;
            }).click();

            $('.fontPickerIsGoogleFontChecker').change( function (){
                _id = $(this).attr('data-font-field-id');
                _sel_id = _id + '_fontPickerGoogleSelect';

                //if( 'checked' == $(this).attr('checked') ){
                if( $(this).is(':checked') ){
                    $('#'+_sel_id).show();
                }else{
                    $('#'+_sel_id).hide();
                }
            }).change();
            
            $('#foptions-fontfamily-use-defined-fonts').change( function (){
                _use_defined_fonts = $('#foptions-fontfamily-use-defined-fonts').is(':checked') ? false : true;
                if( _use_defined_fonts ){
                    $('.fontPickerWrapper').each(function( index ) {
                        $(this).parents('tr:first').hide();
                    });
                }else{
                    $('.fontPickerWrapper').each(function( index ) {
                        $(this).parents('tr:first').show();
                    });
                }
            }).change();

        });
        </script><?php
    }
}
