<?php namespace ProcessWire;

class JqueryUI extends ModuleJS { 

	public static function getModuleInfo() {
		return array(
			'title' => 'jQuery UI',
			'version' => 196,
			'summary' => 'jQuery UI as required by ProcessWire and plugins',
			'href' => 'http://ui.jquery.com', 
			'permanent' => true, 
		);
	}
	
	public function __construct() {
		$debug = $this->wire('config')->debug;
		$this->addComponents(array(
			'modal' => $debug ? 'modal.js' : 'modal.min.js',
			'panel' => $debug ? 'panel.js' : 'panel.min.js', 
			'touch' => 'touch.js' 
			));
	}
	
	public function init() {
		parent::init();
		if($this->wire('session')->touch) $this->use('touch'); 
	}

	public function ___use($name) {
		if($name == 'panel') {
			// also add stylesheet when panel component requested
			$this->config->styles->add($this->config->urls('JqueryUI') . "panel.css");
		} else if($name == 'vex') {
			// custom loader for vex
			static $vexLoaded = false;
			if(!$vexLoaded) {
				$vexLoaded = true;
				$url = $this->config->urls('JqueryUI') . 'vex/';
				$this->config->styles->add($url . 'css/vex.css');
				$this->config->styles->add($url . 'styles/vex-theme-default.css');
				$this->config->scripts->add($url . 'scripts/vex.combined.min.js');
				$adminTheme = $this->wire('adminTheme');
				if($adminTheme) $adminTheme->addExtraMarkup('head',
					"<script>" .
					"vex.defaultOptions.className='vex-theme-default';" .
					"vex.dialog.buttons.YES.text='" . __('Ok', 'common') . "';" . // Yes/Ok button text for Confirm dialog
					"vex.dialog.buttons.NO.text='" . __('Cancel', 'common') . "';" . // No/Cancel button text for Confirm dialog
					"</script>"
				);
			}
			return $this;
		}
		return parent::___use($name);
	}


}
