<?php
    include_once('_dirinfo.php');

    // Matej, 2015-08-26
    $checkJquery .= "
        window.resetCustomCheckboxes = function() {};
        window.resetCustomRadios = function() {};
        ";

 //replace all checboxes with images
        if($rowBP['custom_checkbox_enable'] == "1")
        {
            $imagesArray = explode(';',$rowBP['checkbox_images']);
            if(count($imagesArray) == 3)
            {
                $checkJquery .= "
                $('input[type=\"checkbox\"]').each(function(obj, element){
                    var checkbox = $(element);

                    //var id = checkbox.context.id.split('_')[1] + '_' + checkbox.context.id.split('_')[2];
                    var id = checkbox.context.id;       // Matej, 2015-07-21, see below for more changes and info
                    id = id.slice(id.indexOf('_') + 1); //

                    //if(id.indexOf('undefined') > -1) return;
                    if (checkbox.context.id.split('_')[2] === undefined)
                        return; // Also Matej, certain checkboxes must remain as they are

                    checkbox.css('display', 'none');
                    checkbox.attr('data-custom-flag', 1);
                    if(checkbox.prop('checked'))
                    {
                        checkbox.after('<img id=\"img_' + id +'\" src=\" " . PATH_TO_ROOT . $imagesArray[0] . "\"\>');
                    }
                    else 
                    {
                        checkbox.after('<img id=\"img_' + id +'\" src=\" " . PATH_TO_ROOT . $imagesArray[1] . "\" \>');
                    }
                    
                    if(checkbox.prop('disabled'))
                    {
                        $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray[2] . "');
                    }
                });
                ";
                
                $checkJquery .= "
                window.resetCustomCheckboxes = function() {
                    $('input[type=\"checkbox\"]').each(function(obj, element){
                        var checkbox = $(element);
                        var id = checkbox.context.id;
                        id = id.slice(id.indexOf('_') + 1);

                        if (checkbox.context.id.split('_')[2] === undefined)
                            return;

                        $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray[1] . "');

                        if (checkbox.prop('disabled')) {
                            $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray[2] . "');
                        }
                    });
                }
                ";
            }
        }
        //replace all radiobuttons with images
        if($rowBP['custom_radio_enable'] == "1")
        {
            $imagesArray2 = explode(';',$rowBP['radio_images']);
            if(count($imagesArray2) == 3)
            {
                $checkJquery .= "
                $('input[type=\"radio\"]').each(function(obj, element){
                    var radio = $(element);

                    //var id = radio.context.id.split('_')[1] + '_' + radio.context.id.split('_')[2];
                    var id = radio.context.id; // Matej, 2015-07-21, see below for more changes and info
                    id = id.slice(id.indexOf('_') + 1);

                    //if(id.indexOf('undefined') > -1) return;
                    if (radio.context.id.split('_')[2] === undefined) return;

                    radio.css('display', 'none');
                    radio.attr('data-custom-flag', 1);
                    if(radio.prop('checked'))
                    {
                        radio.after('<img id=\"img_' + id +'\" src=\" " . PATH_TO_ROOT . $imagesArray2[0] . "\"\>');
                    }
                    else 
                    {
                        radio.after('<img id=\"img_' + id +'\" src=\" " . PATH_TO_ROOT . $imagesArray2[1] . "\" \>');
                    }
                    
                    if(radio.prop('disabled'))
                    {
                        $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray2[2] . "');
                    }
                });
                ";
                
                $checkJquery .= "
                window.resetCustomRadios = function() {
                    $('input[type=\"radio\"]').each(function(obj, element){
                        var radio = $(element);
                        var id = radio.context.id;
                        id = id.slice(id.indexOf('_') + 1);

                        if (radio.context.id.split('_')[2] === undefined)
                            return;

                        $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray2[1] . "');

                        if (radio.prop('disabled')) {
                            $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray2[2] . "');
                        }
                    });
                }
                ";
            }
        }

        if ($rowBP['custom_checkbox_enable'] == "1") {
            $checkJquery .= "
               window.customInputNogoUncheck = function(id) {
                   $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray[1] . "');    
               }
               ";
        }
        else {
            $checkJquery .= "
               window.customInputNogoUncheck = function(id) {
                   // Custom checkbox inputs disabled
               }
               ";
        }

       /*if we click on the row containing checbox or radio we have to change image to checked/unchecked
        * we also have to set value of the actual(hidden) checbox or radio
        */ 
        $checkJquery .= "
               $('tr[id^=\"tr_\"], td[id^=\"td_\"]').click(function(event){
                   // Matej, 2015-08-26, added stopPropagation()
                   event.stopPropagation();

                   //var id = event.currentTarget.id.split('_')[1] + '_' + event.currentTarget.id.split('_')[2] ;
                   var id = event.currentTarget.id;    //
                   id = id.slice(id.indexOf('_') + 1); // Matej, 2015-07-21: ct_Kakovost_1_1 => Kakovost_1_1

                   var clickedElement = $(event.target);
                   var MultiLegende = $('#MultiWithLegend').val();
                   if(MultiLegende && !clickedElement.prop('id')) return;

                   if (id != undefined)
                   {
                       var input = $('#ct_' + id);
                       if(input.prop('disabled')) return;
                       if(!$('#ct_' + id).prop('checked'))
                       {
                           $('#ct_' + id).prop('checked', true);";

                           if($rowBP['custom_checkbox_enable'] == "1")
                           {
                               $checkJquery .= "
                                    if(input.prop('type') == 'checkbox')
                                    {
                                        $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray[0] . "');    
                                    }
                                    ";
                           }
                           if($rowBP['custom_radio_enable'] == "1")
                           {
                               
                               // ko izberemo radio in ga nastavimo checked moramo vse ostale slikice spremeniti na unchecked
                               $checkJquery .= "
                                    if(input.prop('type') == 'radio')
                                    {
                                        // Matej, 2015-07-21, modified ids (Scale was not working)
                                        //
                                        var idSlice = event.currentTarget.id;
                                        idSlice = idSlice.slice(idSlice.indexOf('_') + 1, idSlice.lastIndexOf('_'));
                                        //idSlice = idSlice.slice(0, idSlice.lastIndexOf('_')); // ct_Kakovost_1_1 => Kakovost_1,  ct_Q2_3 => Q2
                                        
                                        $('img[id^=\"img_' + idSlice + '\"]').each(function(obj, element){
                                            var currentElement = $(element);
                                            if(!currentElement.prop('disabled'))
                                            {
                                                if(currentElement.context.id != 'img_' + id)
                                                {
                                                   currentElement.attr('src', '" . PATH_TO_ROOT . $imagesArray2[1] . "');
                                                }
                                            }   
                                        });

                                        $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray2[0] . "');
                                    }
                               ";
                           }
                           
                           
      $checkJquery .= "}
                       else
                       {
                            // Matej, moved from inside, 2015-07-31
                            if(input.prop('type') == 'checkbox')
                            {
                                $('#ct_' + id).prop('checked', false);
                            }
                         ";
                           if($rowBP['custom_checkbox_enable'] == "1")
                           {
                                $checkJquery .= "
                                if(input.prop('type') == 'checkbox')
                                {
                                    $('#img_' + id).attr('src', '" . PATH_TO_ROOT . $imagesArray[1] . "');  
                                }
                                ";
                           }
      $checkJquery .=  "}

                        // Matej, 2015-07-20
                        recodeClickEvents(input[0],1);
                   }                    
                   
               }) ;";
            // This script extracts url of background image from css and add it to anystretch. we have to use anystretch becouse you can't use css for bacground image with IE8
            $startPos = strpos($bodyStyle, "background-image");
            $anystretch = "";
            if($startPos != FALSE)
            {
                $endPos = strpos($bodyStyle,";", $startPos);
                $images = extract_css_urls(substr($bodyStyle, $startPos, (intval($endPos) -  intval($startPos)) + 1));
                $url = $images['property'][0];
                $bodyStyle = substr_replace($bodyStyle, "", $startPos, (intval($endPos) -  intval($startPos)) + 1);
                $anystretch = "<script type=\"text/javascript\">
                $.anystretch('" . $url ."', {speed: 0});
                    </script>";
            }

?>

