<?
/*
 * \php_lib\moduls\accords.class.php
 */
class AccordionHandler {

var $Accords= array();
var $AccordData= array();
var $First = true;
var $ClassName = "toggler";
var $Padding = 32;
var $sequence = 0;
var $Effects = array('slide','fade','show');
var $Speeds = array('slow','normal','fast');

function Add ($id,$caption,$auto,$effect=2,$link,$open="",$speed="normal")
    {
    global $wb;
	if ($effect=="") $effect = 2;
	if ($speed =="" || !is_numeric($speed)) $speed = 1;
    $wb->JQUERY_ONSITE = true;
    $wb->JQ_COOKIE = true;
    $wb->SENTRAJS_ONPAGE = true;
    $tmp = array();
    $tmp['id'] = $id;
		$tmp['speed'] =  $this->Speeds[$speed];
    $tmp['caption'] = $caption;
    $tmp['effect'] = $this->Effects[$effect];
    $tmp['auto'] = $auto;
    $tmp['link'] = $link;
    array_push ($this->Accords,$tmp);
    $tpl = file_get_contents(DESIGNRENDERSCRIPTS.'accordion.item.tpl');
    $tpl = str_replace("{{ID}}",$id,$tpl);    
    $tpl = str_replace("{{CAPTION}}",quote2entities($caption),$tpl);
    $tpl = str_replace("{{OPEN-STATE}}",$open,$tpl);
    $this->sequence  ++;
    if ($this->First)
        {
        $this->First = false;
        return $tpl;
        }
    else
        {
        $ret ="";

        if ($this->Accords[count($this->Accords)-2]['link'] == "True")
			$ret .= '<div class="spacer"><a href="#"
				onclick ="$.toggleElementsClose(\''.$this->Accords[count($this->Accords)-2]['id'].'\',\''.$this->Accords[count($this->Accords)-2]['effect'].'\',\''.$this->Accords[count($this->Accords)-2]['speed'].'\');return false" >'.$wb->RESOURCES['closeclick'].'</a></div>';

       else
            $ret .= "";
        return $ret.'</div>'.$tpl;
        }
    }
		
function AddData ($data) {
	array_push($this->AccordData, $data);
}
function GetData () {
	return implode("\n",$this->AccordData);
}

function CloseAccords()
    {
    global $wb;
    $auto = "";
    foreach ($this->Accords as $item)
        {
        if ($this->sequence == 1)
            $item['auto'] = "True";
        if ($item['auto'] == "True")
            $auto .= $item['id'].' ';
        }
    $auto = RemoveLastChar($auto);

    $ret = '';
    if ($this->Accords[count($this->Accords)-1]['link'] == "True")
		$ret .= '<div class="spacer"><a href="#" class="ileft" 
				onclick ="$.toggleElementsClose(\''.$this->Accords[count($this->Accords)-1]['id'].'\',\''.$this->Accords[count($this->Accords)-1]['effect'].'\',\''.$this->Accords[count($this->Accords)-1]['speed'].'\');return false" >'.$wb->RESOURCES['closeclick'].'</a></div>';

    else
        $ret .= "";
    $ret .= '</div>';
    $ret .= '<script>'.PHP_EOL;

    foreach ($this->Accords as $item) {

        $ret .=	'$(document).ready(function(){$(\'div#'.$item['id'].'\').toggleElements(  { fxAnimation:\''.$item['effect'].'\',fxSpeed: \''.$item['speed'].'\',autoClose:\''.$auto.'\'});	});'.PHP_EOL;
    }
		$ret .= '</script>'.PHP_EOL;		
    return $ret;
    }

function Dispose()
    {
    $this->Accords=array();
    }
}


