/**
 * Returns the javascript needed for a remote function.
 * Takes the same arguments as 'link_to_remote()'.
 *
 * Example:
 *   <select id="options" onchange="<?php echo remote_function(array('update' => 'options', 'url' => '@update_options')) ?>">
 *     <option value="0">Hello</option>
 *     <option value="1">World</option>
 *   </select>
 */
function ls_remote_function($options)
{
    sfContext::getInstance()->getResponse()->addJavascript(sfConfig::get('sf_prototype_web_dir') . '/js/prototype');
    $javascript_options = _options_for_ajax($options);
    $update = '';
    if (isset($options['update']) && is_array($options['update'])) {
        $update = array();
        if (isset($options['update']['success'])) {
            $update[] = "success:'" . $options['update']['success'] . "'";
        }
        if (isset($options['update']['failure'])) {
            $update[] = "failure:'" . $options['update']['failure'] . "'";
        }
        $update = '{' . join(',', $update) . '}';
    } else {
        if (isset($options['update'])) {
            $update .= "'" . $options['update'] . "'";
        }
    }
    $function = !$update ? "new Ajax.Request(" : "new Ajax.Updater({$update}, ";
    $function .= '\'' . url_for($options['url']) . '\'';
    //ADDED TO ALLOW FOR APPENDING HASHES TO URLS FOR AJAX
    if (isset($options['posturl'])) {
        $function .= ' + ' . $options['posturl'];
    }
    $function .= ', ' . $javascript_options . ')';
    if (isset($options['before'])) {
        $function = $options['before'] . '; ' . $function;
    }
    if (isset($options['after'])) {
        $function = $function . '; ' . $options['after'];
    }
    if (isset($options['condition'])) {
        $function = 'if (' . $options['condition'] . ') { ' . $function . '; }';
    }
    if (isset($options['confirm'])) {
        $function = "if (confirm('" . escape_javascript($options['confirm']) . "')) { {$function}; }";
        if (isset($options['cancel'])) {
            $function = $function . ' else { ' . $options['cancel'] . ' }';
        }
    }
    return $function . ';';
}
/**
 * Includes an AJAX navigation.
 * If you provide a sfPager object, the helper automatically stops the periodical call
 * wen reaching the last page
 *
 * <b>Options:</b>
 * - url - 'module/action' or '@rule' of the AJAX action
 * - update - id of the paginated list
 * - page_name - name of the page request parameter, defaults to 'name'
 * - frequency - number of seconds between each position check, defaults to 1 second
 * - trigger - height in pixels, calculated from the bottom of the page, which triggers the AJAX call
 * ...as well as the usual remote_ helpers options
 *
 * @param  array Ajax options
 * @param  object optional sfPager object of the current pager 
 * @return string XHTML code containing links
 */
function remote_pager($options = array(), $pager = null)
{
    if ($pager && ($pager->getNextPage() == $pager->getPage() || $pager->getPage() != 1)) {
        return;
    }
    // name of the page request parameter (default 'page')
    $options['page_name'] = isset($options['page_name']) ? $options['page_name'] : 'page';
    // frequency of the scroll check (default 1 second)
    $options['frequency'] = isset($options['frequency']) ? $options['frequency'] : 1;
    // scroll offset (in pixels, from the bottom) triggering the remote call (default 30px)
    $options['trigger'] = isset($options['trigger']) ? $options['trigger'] : '30';
    $options['position'] = isset($options['position']) ? $options['position'] : 'before';
    use_helper('Javascript');
    $javascript_callback = 'ajax_pager_semaphore = 0; ajax_pager_page++;';
    if ($pager) {
        // build in the stop of the PeriodicalExecuter when the pager reaches the last page
        $javascript_callback .= 'if(ajax_pager_page>' . $pager->getLastPage() . ') { pager_watch.callback = function () {}; };';
    }
    $options['success'] = isset($options['success']) ? $options['success'] . $javascript_callback : $javascript_callback;
    return javascript_tag("\r\n  var ajax_pager_semaphore = 0;\r\n  var ajax_pager_page = 2;\r\n\r\n  function sf_ajax_next_page()\r\n  {\r\n    if (ajax_pager_semaphore == 0)\r\n    {\r\n      ajax_pager_semaphore = 1;\r\n      new Ajax.Updater(\r\n        '" . $options['update'] . "', \r\n        '" . url_for($options['url']) . '?' . $options['page_name'] . "='+ajax_pager_page,\r\n        " . _options_for_ajax($options) . "\r\n      );\r\n    }\r\n  } \r\n\r\n  pager_watch = new PeriodicalExecuter(function() \r\n  {\r\n     var scrollpos = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;\r\n     var windowsize = window.innerHeight || document.documentElement.clientHeight;\r\n     var testend = document.body.clientHeight - (windowsize + scrollpos);\r\n     \r\n     if ( (testend < " . $options['trigger'] . ") )\r\n     {\r\n       sf_ajax_next_page();\r\n     }\r\n  }, " . $options['frequency'] . ");");
}
function remote_function($options)
{
    $obj =& get_instance();
    $obj->response->enqueue_js('prototype');
    $javascript_options = _options_for_ajax($options);
    $update = '';
    if (isset($options['update']) && is_array($options['update'])) {
        $update = array();
        if (isset($options['update']['success'])) {
            $update[] = "success:'" . $options['update']['success'] . "'";
        }
        if (isset($options['update']['failure'])) {
            $update[] = "failure:'" . $options['update']['failure'] . "'";
        }
        $update = '{' . join(',', $update) . '}';
    } else {
        if (isset($options['update'])) {
            $update .= "'" . $options['update'] . "'";
        }
    }
    $function = !$update ? "new Ajax.Request(" : "new Ajax.Updater({$update}, ";
    $function .= '\'' . system_url($options['url']) . '\'';
    $function .= ', ' . $javascript_options . ')';
    if (isset($options['before'])) {
        $function = $options['before'] . '; ' . $function;
    }
    if (isset($options['after'])) {
        $function = $function . '; ' . $options['after'];
    }
    if (isset($options['condition'])) {
        $function = 'if (' . $options['condition'] . ') { ' . $function . '; }';
    }
    if (isset($options['confirm'])) {
        $function = "if (confirm('" . escape_javascript($options['confirm']) . "')) { {$function}; }";
        if (isset($options['cancel'])) {
            $function = $function . ' else { ' . $options['cancel'] . ' }';
        }
    }
    return $function;
}