<?php

class sfMyWebResponse extends sfWebResponse
{
  protected $standAloneJavascripts = array();
  protected static $javascriptCommand = '';

  public function addJavascript($js, $position='', $merge=true) {
    if (substr($js, strlen($js)-3)!='.js') $js .= '.js';
    if ($merge) parent::addJavascript($js, $position);
    else $this->standAloneJavascripts[] = $js;
  }

  public function addJavascriptCommand($command) {
    if (sfContext::getInstance()->getRequest()->isXmlHttpRequest()) {
      echo '<script>', $command, '</script>';
    } else self::$javascriptCommand .= $command;
  }

  public function getJavascriptCommand() {
    $include = '';
    foreach ($this->standAloneJavascripts as $js) {
    	$include .= javascript_include_tag($js);
    }
    return $include.'<script>'.self::$javascriptCommand.'</script>';
  }
}
