<?php
class MyWeb_JavaScript_Popup_Slave
{
	// je nutne aby nastaveni bylo provazane s nastavenim table view helperu
	
	
	private $_idLink;				//musi mit index protoze jich je hodne
	private $_idInputs;				//musi mit index protoze jich je hodne
	private $_idHidden = array();
	//const childMsg0				// index bude doplnen na zaklade pole get
	private $_getParam;
	
	private $_stringWrite;
	private $_nameFile = "";
	
	
	public function setNameFile($name)
	{
		$this->_nameFile = $name;
	}
	
	public function getNameFile()
	{
		return $this->_nameFile;
	}
	
	
	public function setIdLink($value)
	{
		$this->_idLink = $value;
	}
	public function getIdLink()
	{
		return $this->_idLink;
	}
	
	
	public function setIdHidden($value)
	{
		$this->_idHidden = $value;
	}
	public function getIdHidden()
	{
		return $this->_idHidden;
	}
	public function addIdHidden($value)
	{
		$this->_idHidden[] = $value;
	}
	
	public function setIdInput($value)
	{
		$this->_idInputs = $value;
	}
	public function getIdInput()
	{
		return $this->_idInputs;
	}
	
	private function validationSettings($method = 'js')
	{
		// pole hidden neni potreba kontrolovat protoze neni nutne
		$request=Zend_Controller_Front::getInstance()->getRequest();
		$this->_getParam = $request->getParam('idInput');
		//echo "<p>hhhhhhhhhhhhh".$this->_getParam."</p>";
		
		//kontrola predane hodnoty z pole get
		if (empty($this->_getParam) && ($this->_getParam != 0)){
			$zprava = "MyWeb_JavaScript_Popup_Main: Neni nastaveno vystupni pole v poli get: "."\n";
        	throw new Exception($zprava);
		}
		
		if (strcmp($method, 'writeFileJs') === 0){
			if (empty($this->_nameFile)){
				$zprava = "MyWeb_JavaScript_Popup_Slave: Neni nastaveno jmeno souboru pro ulozeni: "."\n";
        		throw new Exception($zprava);
			}
		}
	}
	
	
	
	public function js()
	{
		$js = "";
		try {
			$this->validationSettings();
			$js = $this->generateJs();
		}
		
		catch (Exception $e){
    		echo $e->getMessage();
    	}
		return $js;
	}
	
	
	/**
	 * 
	 * Matoda generuje kod javascriptu pro stranky odkazujici se na popup window
	 */
	private  function generateJs()
	{
		$this->_stringWrite = "";
		$this->_stringWrite .= 
						"\t".		'$(function(){'.					"\n" .
						"\t"."\t".		'// don\\\'t forget this init!'.	"\n" .
						"\t"."\t".		'$.initWindowMsg();'.			"\n" .
						
						
						"\t"."\t".		'// maximalni pocet odkazu v list2 pro odeslani hodnoty do formulare je:'.	"\n" .
        				"\t"."\t".		'// 		maximalni pocet radku tabulky list2 = 30'.						"\n" .
        				"\t"."\t".		'//		maximalni pocet sloupcu tabulky s odkazy = 4'.						"\n" .
        				"\t"."\t".		'//		celkem = 30 * 4 = 120'.												"\n"."\n"."\n".
						"\t"."\t".		'// upon click of a button, send corresponding text input value to parent window'."\n";
		
		$this->_stringWrite .= 
		"\t".		'//funkce slouzi k overeni existence substringu'.							"\n" .
		"\t".		'jQuery.isSubstring = function(haystack, needle) {'.						"\n" .
		"\t".		'return haystack.indexOf(needle) !== -1; };'.								"\n" ."\n" .
		
		"\t".		'var pathname;'.															"\n" .
		"\t".		'// funkce naplni promenou pathname hodnotou url'.							"\n" .
		"\t".		'$(document).ready(function() {'.											"\n" .
		"\t"."\t".		'pathname = window.location.pathname;'.									"\n" .
		"\t"."\t".		'//document.write(pathname);'.											"\n" .
		"\t".	'});'.																			"\n"."\n";
		
		
		
		
		
		
		$cell = 0;
		for ($row = 0; $row < 30; $row++){
			
			
			//hodnota pole (to co se objevi ve formulari)
			$this->_stringWrite .= 
							"\n"."\n".
							"\t".		'$(\'#'.$this->_idLink.$row.'\').click(function(){'.				"\n" .
							"\t"."\t".		'value = $(\'#'.$this->_idInputs.$row.'\').val();'.					"\n" .
							"\t"."\t".		'$.triggerParentEvent("childMsg'.$this->_getParam.'", value);'. 	"\n" .
							"\t".		'});';
			$cell++;
				
			foreach ($this->_idHidden as $key => $value){
				//echo "<p>kkkkkkkkkkkkkkk".$key."</p>";
				$this->_stringWrite .= 
							"\n"."\n".
							"\t".		'$(\'#'.$this->_idLink.$row.'\').click(function(){'.				"\n" .
							"\t"."\t".		'value = $(\'#'.$value.$row.$key.'\').val();'.					"\n" .
							"\t"."\t".		'$.triggerParentEvent("childHidden'.$this->_getParam.$key.'", value);'. 	"\n" .
							"\t".		'});';
				$cell++;
			}
		}
		
		
		$this->_stringWrite .= 
				"\n" ."\n" .
				'/*'.																								"\n" .
        		"\t".		'// set up the message handlers, listen for events \'parentMsg1\' and \'parentMsg2\''.	"\n" .
        		"\t".		'$.windowMsg("parentMsg1", function(message) {'.										"\n" .
        		"\t"."\t".		'$(\'#in1\').val(message);'.														"\n" .
        		"\t"."\t".		'});'.																				"\n" .
        		"\t".		'$.windowMsg("parentMsg2", function(message) {'.										"\n" .
        		"\t"."\t".		'$(\'#in2\').val(message);'.														"\n" .
        		"\t".		'});'.																					"\n" .
        		'*/'.																								"\n" .
				'});';
		return $this->_stringWrite;
	}
	
	/**
	 * 
	 * Metoda zajisti ulozani javascriptu do souboru
	 */
	public function writeFileJs()
	{
		$js = "";
		try {
			$this->validationSettings('writeFileJs');
			$js = $this->generateJs();
			
			$filename = $this->_nameFile.'.js';
			$file = fopen($filename, 'w');
			
			fwrite($file, $js);
			fclose($file);
		}
		
		catch (Exception $e){
    		echo $e->getMessage();
    	}
	}
}