<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Stylish Controls helper
 *
 * @version     1.0
 * @package        qs_ci_addons
 * @subpackage    helpers
 * @category    helpers
 * @author        Tommy Lacroix <tlacroix quantiksolutions com>
 * @license        BSD
 * @copyright   Copyright (c) 2006-2008 Quantik Solutions
 */

// ------------------------------------------------------------------------

/**
 * Global variable that contains wheter or not the global checkbox and radio scripts
 * have been sent.
 */
$stylish_scriptSent = array();

/**
 * Global variable that contains used unique identifiers to avoid collisions.
 */
$stylish_ids = array();



/**
 * Create a stylish checkbox, with no javascript compatibility
 *
 * @param    string     HTML form element name
 * @param     string    HTML form element value
 * @param     bool    Initial checked state
 * @param     string    Class when checked
 * @param     string    Class when unchecked
 * @param     string    JavaScript code for onChange event (escape your quotes!)
 * @return    string    HTML/script code
 */
function stylishCheckbox($name$value$checked$classChecked$classUnchecked$onChange=false) {
    global 
$stylish_ids$stylish_scriptSent;
    
    
// Find a unique identifier
    
if (!is_array($stylish_ids)) $stylish_ids = array();
    do {
        
$checkId uniqid('stylishCheckbox_');
    } while (
in_array($checkId,$stylish_ids));
    
$stylish_ids[] = $checkId;
    
    
// Create code
    
$out '';
    
$out .= '<script type="text/javascript" language="javascript">';
    
$out .= 'document.write(\'<a id="'.$checkId.'" href="#" onclick="toggleStylishCheckbox(\\\''.$checkId.'\\\', \\\''.$value.'\\\', \\\''.$classChecked.'\\\', \\\''.$classUnchecked.'\\\'); '.($onChange!==false?$onChange:'').'" class="'.($checked $classChecked $classUnchecked).'"></a>\');';
    
$out .= 'document.write(\'<input type="hidden" name="'.$name.'" id="'.$checkId.'_val" value="'.($checked?$value:'').'" />\');';
    
$out .= '</script>';
    
$out .= '<noscript><input type="checkbox" name="'.$name.'" value="'.$value.'"'.($checked?' checked="checked"':'').($onChange!==false?' onchange="'.$onChange.'"':'').' /></noscript>';
    
    
// Add script if unsent
    
if (!isset($stylish_scriptSent['checkbox'])) {
        
$out .= stylishCheckboxScript();
    }
    return 
$out;
}

/**
 * Get the common checkbox script
 *
 * @return string
 */
function stylishCheckboxScript() {
    global 
$stylish_scriptSent;

    
$stylish_scriptSent['checkbox'] = true;
    
$out '';
    
$out .= '<script type="text/javascript" language="javascript">';
    
$out .= 'function toggleStylishCheckbox(id, value, classOn, classOff) {';
    
$out .=     'if (document.getElementById(id+"_val").value != "") {';
    
$out .=            'document.getElementById(id+"_val").value = "";';
    
$out .=            'document.getElementById(id).className = classOff;';
    
$out .=        '} else {';
    
$out .=            'document.getElementById(id+"_val").value = value;';
    
$out .=            'document.getElementById(id).className = classOn;';
    
$out .=        '}';
    
$out .=    '}';
    
$out .= '</script>';
    return 
$out;
}

/**
 * Create a stylish radio, with no-javascript compatibility
 *
 * @param    string     HTML form element name
 * @param     string    HTML form element value
 * @param     bool    Initial checked state
 * @param     string    Class when checked
 * @param     string    Class when unchecked
 * @return    string    HTML/script code
 */
function stylishRadio($name$value$checked$classChecked$classUnchecked) {
    global 
$stylish_ids$stylish_scriptSent;
    
    
// Find a unique identifier
    
if (!is_array($stylish_ids)) $stylish_ids = array();
    do {
        
$checkId uniqid('stylishRadio_');
    } while (
in_array($checkId,$stylish_ids));
    
$stylish_ids[] = $checkId;
    
    
// Find group unique identifier
    
$groupId "radioGroup_".md5($name);
    
    
// Create code
    
$out '';
    
$out .= '<script type="text/javascript" language="javascript">';
    
$out .= 'var '.$groupId.';';
    
$out .= 'if ('.$groupId.' == undefined) '.$groupId.' = ["'.$checkId.'"]; else '.$groupId.'.push("'.$checkId.'");';
    
$out .= 'document.write(\'<a id="'.$checkId.'" href="#" onclick="toggleStylishRadio(\\\''.$checkId.'\\\', \\\''.$groupId.'\\\', \\\''.$value.'\\\', \\\''.$classChecked.'\\\', \\\''.$classUnchecked.'\\\');" class="'.($checked $classChecked $classUnchecked).'"></a>\');';
    
$out .= 'if (!document.getElementById("'.$groupId.'")) document.write(\'<input type="hidden" name="'.$name.'" id="'.$groupId.'" value="'.($checked?$value:'').'" />\');';
    
$out .= '</script>';
    
$out .= '<noscript><input type="radio" name="'.$name.'" value="'.$value.'"'.($checked?'checked="checked"':'').' /></noscript>';
    
    
// Add script if unsent
    
if (!isset($stylish_scriptSent['radio'])) {
        
$out .= stylishRadioScript();
    }
    return 
$out;
}

/**
 * Get the common radio script
 *
 * @return string
 */
function stylishRadioScript() {
    global 
$stylish_scriptSent;
    
    
$stylish_scriptSent['radio'] = true;
    
$out =  '';
    
$out .= '<script type="text/javascript" language="javascript">';
    
$out .= 'function toggleStylishRadio(id, groupName, value, classOn, classOff) {';
    
$out .=     'var group = eval(groupName);';
    
$out .=     'for (var i=0;i<group.length;i++) {';
    
$out .=            'if (group[i] == id) var myClass=classOn; else var myClass=classOff;';
    
$out .=            'document.getElementById(group[i]).className = myClass;';
    
$out .=        '}';
    
$out .=        'document.getElementById(groupName).value=value;';
    
$out .=    '}';
    
$out .= '</script>';
    return 
$out;
}