<?php
$currentLanguage = $this->language;
$fallbackLanguage = $currentLanguage;
$languages = $this->languages;
$baseLangFile = APPLICATION_PATH . DS . 'language' . DS . '%s' . DS . 'lang.php';
?>
<script type="text/javascript">
function changeLang(lang)
{
    document.location.href = '<?php echo $this->baseUrl(); ?>/install/index/language/' + lang + '/';
}
var langUrl = <?php
echo json_encode($this->url(array(
    'controller' => 'install',
    'action' => 'ajax',
    'lang' => '',
)));
?>;
var throbberImgSrc = '<?php echo $this->baseUrl(); ?>/css/msd/icons/ajax-loader.gif';
var okImgSrc = '<?php echo $this->getIconSrc('Ok', 16); ?>';
var failImgSrc = '<?php echo $this->getIconSrc('Attention', 16); ?>';
var installedText = new Array();
var failedText = new Array();
var updateText = new Array();
<?php
foreach ($languages as $langId => $language) {
    if (file_exists(sprintf($baseLangFile, $langId))) {
        $this->lang->loadLanguage($langId);
    } else {
        $this->lang->loadLanguage($fallbackLanguage);
    }
?>
installedText['<?php echo $langId; ?>'] = '<?php echo $this->lang->L_INSTALLED; ?>';
failedText['<?php echo $langId; ?>'] = '<?php echo $this->lang->L_ERROR; ?>';
updateText['<?php echo $langId; ?>'] = '<?php echo $this->lang->L_UPDATE; ?>';
<?php
}
$this->lang->loadLanguage($currentLanguage);
?>
var oldHtml = '';
function dlLanguageFile(language)
{
    $('#status-icon-' + language).attr('src', throbberImgSrc);
    $('#status-' + language).html(updateText[language]);
    $('#update-button-' + language).attr('disabled', 'disabled');
    $.getJSON(langUrl + language, function(objResult) {
        if (objResult.success) {
            $('#status-icon-' + objResult.language).attr('src', okImgSrc);
            $('#status-' + objResult.language).html(installedText[objResult.language]);
            $('#lang-' + objResult.language).removeAttr('disabled');
            changeLang(objResult.language);
        } else {
            $('#status-icon-' + objResult.language).attr('src', failImgSrc);
            $('#status-' + objResult.language).html(failedText[objResult.language]);
            $('#lang-' + objResult.language).attr('disabled', 'disabled');
        }
        $('#update-button-' + objResult.language).removeAttr('disabled');
        if (objResult.error.message) {
            $('#container').append(objResult.error.message);
            $('#update-message').dialog({"modal": 1,"dialogClass": "error","buttons": {"OK": function() {$(this).dialog('close');}},"position": {"0": "center","1": "center"},"title": "<?php echo $this->lang->L_INSTALL; ?>"});
        }
    });
}
</script>
<form id="langform" action="<?php
echo $this->url(
    array(
        'controller' => 'install',
        'action' => 'index',
    ),
    null,
    true
);?>" method="post" enctype="multipart/formdata">
<table cellpadding="0" cellspacing="0" border="0" class="bdr" style="width: 700px;" summary="Select language">
    <tr class="thead">
        <td colspan="5" style="text-align:center;">
            <button type="submit" class="Formbutton" style="margin:12px;"><?php echo $this->getIcon('save');?> <?php echo $this->lang->L_SAVEANDCONTINUE;?></button>
        </td>
    </tr>
    <tr class="thead">
        <th class="right">#</th>
        <th><?php echo $this->lang->L_LANGUAGE;?></th>
        <th colspan="2"><?php echo $this->lang->L_STATUS;?></th>
        <th><?php echo $this->lang->L_ACTION;?></th>
    </tr>
<?php
$counter = 1;
asort($languages);
foreach ($languages as $langId => $language) {
    if (file_exists(sprintf($baseLangFile, $langId))) {
        $this->lang->loadLanguage($langId);
    } else {
        $this->lang->loadLanguage($fallbackLanguage);
    }
    if ($langId == $this->language) {
        $checked = ' checked="checked"';
    } else {
        $checked = '';
    }
?>
    <tr class="row-<?php echo (($counter % 2) == 0) ? 'odd' : 'even'; ?>">
        <td class="right"><?php echo $counter; ?>.</td>
        <td class="nowrap"><div id="lang_line_<?php echo $counter; ?>">
            <input type="radio" class="radio" name="language" value="<?php echo $langId; ?>" id="lang-<?php echo $langId; ?>"<?php echo $language['installed'] ? '' : ' disabled="disabled"'; ?> onclick="changeLang('<?php echo $langId; ?>');"<?php echo $checked; ?> />
            <label for="lang-<?php echo $langId; ?>"><img src="<?php echo $this->baseUrl(); ?>/flag.php?langId=<?php echo $langId; ?>" alt=""/> <?php echo $language['langName']; ?></label>
        </div></td>
        <td><img src="<?php
        echo $language['installed'] ? $this->getIconSrc('Ok', 16) : $this->getIconSrc('Attention', 16);
        ?>" id="status-icon-<?php echo $langId; ?>" alt=""/></td>
        <td id="status-<?php echo $langId; ?>"><?php
        echo $language['installed'] ? $this->lang->L_INSTALLED : $this->lang->L_NOTAVAIL;
        ?></td>
        <td style="text-align:center;"><button id="update-button-<?php echo $langId; ?>" type="button" class="Formbutton" onclick="dlLanguageFile('<?php echo $langId; ?>');" style="width:150px;"><?php echo $this->getIcon('Sync', '', 16); ?> <?php echo $this->lang->L_UPDATE; ?></button></td>
    </tr>
<?php
    $counter++;
}
$this->lang->loadLanguage($currentLanguage);
?>
    <tr class="thead">
        <td colspan="5" style="text-align:center;">
            <button type="submit" class="Formbutton" style="margin:12px;"><?php echo $this->getIcon('save');?> <?php echo $this->lang->L_SAVEANDCONTINUE;?></button>
        </td>
    </tr>
</table>
<div id="langDl" title="Language Download">
</div>
</form>
