protected function overrideScript($script)
    {
        $noConflict = $this->getNoConflictMode();
        if (!$noConflict) {
            return $script;
        }

        $jQ = ($this->getNoConflictHandler())
            ? $this->getNoConflictHandler()
            : 'jQuery';

        // clear immediate functions from script
        // we don't want noConflict handler inside immediate functions
        preg_match_all(
            '/\([\s\n]*function[\s\n]*\([\s\n]*\$[\s\n]*\)[\s\n]*{([\w|\D]*)}[\s\n]*\)[\s\n]*\([\s\n]*jQuery[\s\n]*\)/',
            $script,
            $matches
        );

        $immScripts = array();
        if (isset($matches[1])) {
            foreach ($matches[1] as $i => $mtch) {
                $repVar = '$__var_'.$i;
                $immScripts[$repVar] = $mtch;
                $script = str_replace($mtch, $repVar, $script);
            }
        }

        // replace $ with noConflict handler
        $script = preg_replace_callback (
            '/\$\s*[\.|\(]\s*(\"|\'|\w[\w\d]*)/',
            function ($matches) use ($jQ) {
                return str_replace('$', $jQ, $matches[0]);
            },
            $script
        );

        // get back immediate functions body to script
        foreach($immScripts as $repVar => $scr) {
            $script = str_replace($repVar, $scr, $script);
        }

        return $script;
    }