<!-- BEGIN {$smarty.template} -->

{include file="delete_person.tpl.js"}
{include file="delete_file.js"}
{include file="upload.tpl.html"}

{assign var=files value=$person.files}

<input type="hidden" name="curryear" id="curryear" value="{$curryear}">
<input type="hidden" name="currmonth" id="currmonth" value="{$currmonth}">

{literal}
<script type="text/javascript">
    $(document).ready(function() {
        {/literal}{if $person.pe_duration > 0}{literal}
        var curryear = $('#curryear').val();
        var currmonth = $('#currmonth').val();

        var ocplot = null;
        function resize_oppeprj() {
            pos = $("#oppetab").position();
            w = $("#oppetab").width();

            x = 60 + pos.left + w;
            winw = $('#tab_container').width() - 50;
            opw = $("#oppeprj").width();

            avail = winw - x;
            if (avail < 400)
                avail = winw;

            if (avail < opw) {

                $("#oppeprj").width(avail);

                $("#oppeprj").prepend('<div id="content-slider"><\/div>');
                $("#content-slider").width(avail - 10);
                
                $("#oppeprjtab").wrap('<div id="content-scroll"><div id="content-holder">');
                $("#content-scroll").width(avail);

                $("#content-slider").slider({ animate: true, change: handleSliderChange, slide: handleSliderSlide });
            }
        }

        $.getJSON("data_service.php?req=opcat&pe_id={/literal}{$person.pe_id}{literal}",
            function(data){  ocplot = get_opcat_plot('peopcat', data); }
        );


        /* Navigate through years and months */

        $('#mhr_prev').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            year--;
            $('#year').html(year);
            set_navbuttons();
            replot_mhrs(year, month, true);
            return false;
        });

        $('#mhr_next').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            year++;
            if ((year==curryear) && (month>currmonth)) {
                month=currmonth;
                $('#month').html(month);
            }
            $('#year').html(year);
            set_navbuttons();
            replot_mhrs(year, month, true);
            return false;
        });

        $('#mhr_prevm').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            var replot_year = false;
            if (month == 1) {
                year--;
                month = 12;
                replot_year = true;
            } else
                month--;

            $('#year').html(year);
            $('#month').html(month);

            set_navbuttons();
            
            replot_mhrs(year, month, replot_year);
            return false;
        });

        $('#mhr_nextm').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            var replot_year = false;
            if (month == 12) {
                year++;
                month = 1;
                replot_year = true;
            } else
                month++;

            $('#year').html(year);
            $('#month').html(month);

            set_navbuttons();
            
            replot_mhrs(year, month, replot_year);
            return false;
        });

        function set_navbuttons() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());

            if ((year < curryear) || ((year == curryear) && (month < currmonth)))
                $('#mhr_nextm').show();

            if ((year == curryear) && (month >= currmonth))
                $('#mhr_nextm').hide();

            if (year < curryear)
                $('#mhr_next').show();
            if (year >= curryear)
                $('#mhr_next').hide();
        }

        /* Manhours chart */

        function replot_mhrs(year, month, replot_year) {
            if (replot_year == true) {
                $('#mhchartyear').empty();
                $.getJSON("data_service.php?req=mhrs&pe_id={/literal}{$person.pe_id}"+
                        "&type={$smarty.const.PHPAGA_MANHOURS_DISPLAY_YEAR}{literal}&date="+
                        year+"-01-01",  
                        function(data) { mhrsplot = get_mhrsplot_year('mhchartyear', data); });
            }

            $('#mhchartmonth').empty();
            $.getJSON("data_service.php?req=mhrs&pe_id={/literal}{$person.pe_id}"+
                    "&type={$smarty.const.PHPAGA_MANHOURS_DISPLAY_MONTH}{literal}&date="+
                    year+"-"+month+"-01", 
                    function(data) { mhrsplot = get_mhrsplot_month('mhchartmonth', data); });
        }

        replot_mhrs($('#curryear').val(), $('#currmonth').val(), true);

        set_navbuttons();

        {/literal}{/if}{literal}
        $("#tab_container").tabs();
        {/literal}{if $person.pe_duration > 0}{literal}
        $('#tab_container').bind('tabsshow', function(event, ui) {
            if (ui.panel.id == "person-task-categories") {
                ocplot.replot();
                resize_oppeprj();
            }
        });
        {/literal}{/if}{literal}
    });
</script>
{/literal}

{$STATUSMSG}

<h2>{t}Person{/t}: {$person.pe_person|wash}</h2>

{if ($perm_persons) || ($person.pe_pe_id == $smarty.session.auth_user.pe_id)}
<div class="actions">
    [ <a class="ptr" onclick="confirm_delete_person({$person.pe_id}, '{$person.pe_person|phpaga_popup_sanitizestr}');">{t}Delete{/t}</a>
    | <a href="edit_person.php?id={$person.pe_id}">{t}Edit{/t}</a>
    | <a id="uplink" class="ptr" onclick="show_box('uplink', 'upload');">{t}Add files{/t}</a>]
</div>
{/if}

<div id="tab_container">
    <ul class="tab_items">
        <li><a href="#person-details" rel="person-details">{t}Person{/t}</a></li>
        {if (isset($opcats) && (count($opcats) > 0))}
        <li><a href="#person-task-categories" rel="person-task-categories">{t}Task categories{/t}</a></li>
        {/if}
        {if (isset($person.projects) && (count($person.projects) > 0))}
        <li><a href="#person-projects" rel="person-projects">{t}Projects{/t}</a></li>
        {/if}
        {if count($files) > 0}
        <li><a href="#person-files" rel="person-files">{t}Files{/t}</a></li>
        {/if}
    </ul>

    <div id="person-details" class="tab_content">

        <div class="floatcontainer">
            <table class="detail flleft" cellspacing="0">

                <caption>
                    <em>{$person.pe_person|wash}</em>
                </caption>
                <tr>
                    <th>{t}Person{/t}: </th>
                    <td colspan="3"> {$person.pe_person|wash} </td>
                    <th>{t}Category{/t}: </th>
                    <td colspan="3"> {$person.pecat_title|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}Address{/t}: </th>
                    <td colspan="7"> {$person.pe_address|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}Address 2{/t}: </th>
                    <td colspan="7"> {$person.pe_address2|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}ZIP{/t}: </th>
                    <td> {$person.pe_zip|wash|default:"&nbsp;"} </td>
                    <th>{t}City{/t}: </th>
                    <td> {$person.pe_city|wash|default:"&nbsp;"} </td>
                    <th>{t}Work phone{/t}: </th>
                    <td colspan="3"> {$person.pe_phonework|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}Region{/t}: </th>
                    <td colspan="3"> {$person.pe_region|wash|default:"&nbsp;"} </td>
                    <th>{t}Mobile{/t}: </th>
                    <td colspan="3"> {$person.pe_phonemobile|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}Country{/t}: </th>
                    <td colspan="3"> <img src="img/flags/{$person.ctr_code|strtolower|wash}.png" alt="{$person.ctr_name|wash|default:"&nbsp;"}" title="{$person.ctr_name|wash|default:"&nbsp;"}" /> {$person.ctr_name|wash|default:"&nbsp;"} </td>
                    <th>{t}Home phone{/t}: </th>
                    <td colspan="3"> {$person.pe_phonehome|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}Tax number{/t}: </th>
                    <td colspan="3"> {$person.pe_taxnr|wash|default:"&nbsp;"} </td>
                    <th>{t}Fax{/t}: </th>
                    <td colspan="3"> {$person.pe_fax|wash|default:"&nbsp;"} </td>
                </tr>

                <tr>
                    <th>{t}Url{/t}: </th>
                    <td colspan="3"> {if strlen($person.pe_url)}{$person.pe_url|wash|phpaga_urlmodifier}{else}&nbsp;{/if} </td>
                    <th>{t}Company{/t}: </th>
                    <td colspan="3"> <a href="company.php?id={$person.cpn_id}"> {$person.cpn_name|wash} </a> </td>
                </tr>
                <tr>
                    <th>{t}Email{/t}: </th>
                    <td colspan="3">
                        {if (isset($person.pe_email) && strlen($person.pe_email))}
                        <a href="mailto:{$person.pe_email|wash}"> {$person.pe_email|wash} </a>
                        {else}
                        &nbsp;
                        {/if}
                    </td>
                    <th>{t}Job category{/t}: </th>
                    <td colspan="3"> {$person.jcat_title|wash|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th class="recinfo"> {t}Record owner{/t}:</th>
                    <td class="recinfo" colspan="3"> {$person.owner_pe_person|wash|default:"&nbsp;"} </td>
                    <th{if (!$perm_rate)} class="greyed"{/if}> {t}Default hourly rate{/t}: </th>
                    <td{if (!$perm_rate)} class="greyed"{/if} colspan="3"> {if ($perm_rate)}{$person.pe_hourlyrate|wash|default:"&nbsp;"}{else}{t}hidden{/t}{/if} </td>
                </tr>
            </table>

            {if $person.pe_duration > 0}
            <div class="flleft">
                <div class="dcaption year"><em>{$MANHOURS_HEADER} {if strlen($mhr_year)}
                        <a href="#" id="mhr_prev"><img src="img/phpaga/resultset_previous.png" width="16" height="16"></a>
                        <span id="year">{$mhr_year}</span> 
                        <a href="#" id="mhr_next"><img src="img/phpaga/resultset_next.png" width="16" height="16"></a> | 
                        <a href="#" id="mhr_prevm"><img src="img/phpaga/resultset_previous.png" width="16" height="16"></a>
                        <span id="month">{$currmonth}</span> 
                        <a href="#" id="mhr_nextm"><img src="img/phpaga/resultset_next.png" width="16" height="16"></a>
                        {/if}</em>&nbsp;
                </div>
                <div id="mhchartyear" class="mhchart"></div>
                <div id="mhchartmonth" class="mhchart"></div>
            </div>
            {/if}
            <div class="spacer"></div>
        </div>
    </div>

    {if (isset($opcats) && (count($opcats) > 0))}
    <div id="person-task-categories" class="tab_content">
        <div class="dcaption"><em>{t}Task categories{/t}</em>&nbsp;</div>

        <div class="floatcontainer">

            <div id="peopcat" class="opcatchart flleft"></div>

            <table id="oppetab" class="flleft" cellspacing="0">
                <caption><em>{t}Overall{/t}</em></caption>
                <tbody>
                    {section name=opcdet loop=$opcats}
                    <tr>
                        <td>{$opcats[opcdet].opcat_title|wash}:</td>
                        <td class="number">{$opcats[opcdet].opcat_duration|phpaga_minutes2duration}</td>
                    </tr>
                    {/section}
                </tbody>
                <tfoot>
                    <tr class="summary">
                        <td class="label">{t}Total duration{/t}: </td>
                        <td class="data number"> {$TOTAL_DURATION|phpaga_minutes2duration} </td>
                    </tr>
                </tfoot>
            </table>

            <div class="flleft" id="oppeprj">
                <table id="oppeprjtab" cellspacing="0">
                    <caption><em>{t}Per project{/t}</em></caption>
                    <thead>
                        <tr class="r">
                            <th>&nbsp;</th>
                            {foreach item=prj from=$opcats_project.projects}
                            <th><a href="project.php?id={$prj.prj_id}">{$prj.prj_title|wash}</a></th>
                            {/foreach}
                            <th>{t}Total{/t}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach key=opckey item=opc from=$opcats_project.opcats}
                        <tr>
                            <td> {$opc.opcat_title|wash} </td>
                            {foreach item=row from=$opcats_project.rows[$opckey]}
                            <td class="time"> {if strlen($row.opcat_duration)}{$row.opcat_duration|phpaga_minutes2duration}{else}&nbsp;{/if} </td>
                            {/foreach}
                            <td class="time"> {$opc.duration|phpaga_minutes2duration} </td>
                        </tr>
                        {/foreach}
                    </tbody>
                    <tfoot>
                        <tr class="summary">
                            <td>&nbsp;</td>
                            {foreach item=pe from=$opcats_project.projects}
                            <td class="time">{$pe.duration|phpaga_minutes2duration}</td>
                            {/foreach}
                            <td class="time">{$project.prj_duration_minutes|phpaga_minutes2duration}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>


            <div class="spacer"></div>
        </div>
    </div>
    {/if}

    {if (isset($person.projects) && (count($person.projects) > 0))}
    <div id="person-projects" class="tab_content">
        <table cellspacing="0">
            <caption><em>{t}Projects{/t}</em></caption>
            {foreach name=fpowns item=pown from=$person.projects}
            <tr>
                <td><a href="project.php?id={$pown.prj_id}">{$pown.prj_title|wash}</a></td>
                <td>{$pown.relation}</td>
            </tr>
            {/foreach}
        </table>
    </div>
    {/if}

    {if count($files) > 0}    
    <div id="person-files" class="tab_content">
        {include file="listfiles.tpl.html"}
    </div>
    {/if}
</div>

<!-- END {$smarty.template} -->
