<script type="text/javascript">

jQuery(document).ready(function($){
    //create the main events and their details from json
    var userandguest_items = {
        getAllItems: function(){
            return <?=$elemek_json?>;
        },
        getItemByID: function(cu_id){
            var items = this.getAllItems();
            var len = items.length;
            
            for(var i = 0; i < len; i++){
                if(items[i].cu_id == cu_id){
                    return items[i];
                }
            }
            
            return false;
        }
    };
    
    //open and close events for the lists
    $('.open-closer').click(function(){
        $(this).parent('div').parent('li').next('ul').toggle();
    });
    
    $('.open-closer-all').click(function(){
        $('.registrations').toggle();
        $('.events').toggle();
    });
    
    $('.open-closer-allin').click(function(){
        $('.registrations').fadeIn();
        $('.events').fadeIn();
    });
    
    $('.open-closer-allout').click(function(){
        $('.registrations').fadeOut();
        $('.events').fadeOut();
    });
    
	
    $('#edit_event_dialog').dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        buttons: {
            '<?=$this->lang->line('save')?>': function(){
                $('#modositas_form').submit();
                $('#edit_event_dialog').dialog('close');
            },
            '<?=$this->lang->line('cancel')?>': function(){
                $('#edit_event_dialog').dialog('close');
            }
        }
    });
    
	
    //confirm the delete links
    $('.del_anchor').click(function(){
        var href = $(this).attr('href');
        var question = $(this).attr('question');
        
        //create the confirm dialog
        $('#confirm_dialog').dialog({
            autoOpen: false,
            modal: true,
            buttons: {
                'Törlés': function(){
                    window.location = href;
                },
                'Mégsem': function(){
                    $('#confirm_dialog').dialog('destroy');
                }
            }
        }).html(question).dialog('open');
        
        return false;
    });
    
    
    //edit the event details
    $('.edit_anchor').click(function(){
        //populate the dialog
        var con = $('#modositas_form');
        var event = $(this).parents('.event');
        
        $('#nev', con).val(event.attr('event_name'));
        $('#cp_id', con).val(event.attr('cp_id'));
		
		if($(this).attr('autodel') === '1'){
            $('#autodel', con).attr('checked', 'checked');
        } else {
            $('#autodel', con).attr('checked', '');
        }
        
        
        //open the dialog
        $('#edit_event_dialog').dialog('open');
        
        return false;
    });
	
	//edit the guest name
    $('.edit_guestname').click(function(){
        //populate the dialog
        var con = $('#form_gyarto_edit');
        var event = $(this).parents('.main_event');
        
        $('#name', con).val(event.attr('name'));
        $('#guestname', con).val(event.attr('name'));
        
        
        //open the dialog
        $('#guestname_dialog_edit').dialog('open');
        
        return false;
    });
	
	//
	$('#guestname_dialog_edit').dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        buttons: {
            '<?=$this->lang->line('save')?>': function(){
                $('#form_gyarto_edit').submit();
                $('#guestname_dialog_edit').dialog('close');
            },
            '<?=$this->lang->line('cancel')?>': function(){
                $('#guestname_dialog_edit').dialog('close');
            }
        }
    });
	
	// edit item/image
	$('.edit-item').click(function(){
		var con = $('#modositas_form_item');
		var cu_id = $(this).attr('cu_id');
		var elem = userandguest_items.getItemByID(cu_id);
		
		$('#cu_id', con).val(cu_id);
		$('#it_title', con).val(elem.title);
		$('#it_desc', con).val(elem.desc);
		$('#it_cp_id', con).val(elem.cp_id);
		
        
        //open the dialog
        $('#edit_item_dialog').dialog('open');
        
		return false;
    });
	
	$('#edit_item_dialog').dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        buttons: {
            '<?=$this->lang->line('save')?>': function(){
                $('#modositas_form_item').submit();
                $('#edit_item_dialog').dialog('close');
            },
            '<?=$this->lang->line('cancel')?>': function(){
                $('#edit_item_dialog').dialog('close');
            }
        }
    });
	
	$('.viewerstat').click(function(){
		jQuery('#statisztika').load('/admin/viewerstat').dialog('open');
	});
	
	$('#statisztika').dialog({
        autoOpen: false,
        modal: true,
        width: 400,
		minHeight: 500
    });
	
	//alert("ok");
});

</script>
<style type="text/css">

ul.main_events, ul.events, ul.registrations{
    list-style-type: none;
    padding: 0px;
}

ul.main_events, ul.events{
    margin: 0px 0px 0px 20px;
}

ul.registrations{
    margin: 5px 0px 5px 40px;
}

li.main_event{
    font-weight: bold;
	margin-top: 10px;
}

li.main_event, li.event{
    color: green;
    clear: both;
}

ul.events, ul.registrations{
    display: none;
}

li.full span.main_event_name, li.full span.event_date, li.full span.event_name{
    color: red !important;
}

li.inactive span.event_date, li.inactive span.main_event_name, li.inactive span.main_event_dates{
    color: gray !important;
    text-decoration: line-through;
}

li.new-link{
    margin: 5px 0px;
}

li.col_fajlok{
	clear: both;
}

li.main_event span.open-closer, span.main_event_name{
    font-size: 14px !important;
}

span.main_event_dates{
    font-weight: normal;
    margin: 0px 0px 0px 10px;
}

span.event_name{
    margin: 0px 10px 0px 0px;
}

span.open-closer, span.open-closer-all, span.open-closer-allout, span.open-closer-allin{
    margin-right: 10px;
    color: gray;
    cursor: pointer;
}

span.open-closer-space-holder{
    margin-right: 10px;
    color: white;
}

span.delete-link, span.edit-link, .edit_gyarto{
    margin: 0px 0px 0px 20px;
}

span.view_link_container{
    margin: 0px 0px 0px 10px;
}

span.reg_email{
	width: 50px;
}

div#edit_event_dialog table tr td, div#new_event_dialog table tr td, div#edit_main_event_dialog table tr td, div#new_main_event_dialog{
    text-align: left;
}

div.error{
    margin: 10px;
    color: red;
    font-weight: bold;
    text-align: center;
}

div.picture_link_container{
    margin: 0px 0px 10px 0px;
}

a.view_link{
    color: #003C70 !important;
    font-size: 10px !important;
}

tr.neccessary td{
    font-weight: bold;
}

tr.neccessary td input{
    font-weight: normal !important;
    border: 1px solid black;
    background-color: #fdb910;
}

div.col_szint1, div.col_szint2, div.col_szint3{
	float: left;
}

div.col_szint1{
	width: 200px;
}

div.col_szint2{
	width: 150px;
}

div.col_szint3{
	width: 192px;
}

.new-man, .new_anchor{
	clear: both;
	cursor: pointer;
	margin-top: 15px;
}

</style>
<?if($error):?>
<div class="error"><?=$error?></div>
<?endif;?>
<h2>Guests</h2>
<p>Néhány funkció még tesztelés alatt áll.</p>
<?if($guests):?>
<div id="statisztika" style="display: none;" title="Viewer használati statisztika"></div>
<div id="confirm_dialog" style="display: none;" title="Törlés megerősítés"></div>
<div id="guestname_dialog_edit" style="display: none;" title="Vendég átnevezése">
	<form action="/admin/viewguestnameedit" method="post" id="form_gyarto_edit">
    <input type="hidden" name="do" id="do" value="true" />
	<input type="hidden" name="guestname" id="guestname" value="" />
    <?=$this->lang->line('name')?>: <input type='text' name='name' id='name' value='' />
    </form>
</div>
<div id="edit_event_dialog" style="display: none;" title="Projekt szerkesztése">
	<form action="/admin/viewprojectnameedit" method="post" id="modositas_form">
    <input type="hidden" name="do" id="do" value="true" />
	<input type="hidden" name="cp_id" id="cp_id" value="" />
    <?=$this->lang->line('name')?>: <input type="text" name='nev' id='nev' value='' /><br />
	<label for="autodel">Automatikus törlés:</label> <input type="checkbox" name="autodel" id="autodel" /><br />
	<span style="font-size: 0.7em;">3 hónap után</span>
    </form>
</div>
<div id="edit_item_dialog" style="display: none;" title="Kép szerkesztése">
	<form action="/admin/???" method="post" id="modositas_form_item" enctype='multipart/form-data'>
	<input type="hidden" name="do" id="do" value="true" />
	<input type="hidden" name="cu_id" id="cu_id" value="" />
	<table style="text-align: left;">
	<tr>
		<td>Projekt:</td>
		<td>
			<select name="it_cp_id" id="it_cp_id">
			<?foreach($projektek as $projekt):?>
			<option value="<?=$projekt->cp_id?>"><?=$projekt->guestname." - ".$projekt->nev?></option>
			<?endforeach;?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Név:</td>
		<td><input type="text" name='it_title' id='it_title' value='' style="width: 280px;" /></td>
	</tr>
	<tr>
		<td>Leírás:</td>
		<td><textarea name="it_desc" id="it_desc" style="width: 280px; height: 30px;"></textarea></td>
	</tr>
	<tr>
		<td>Kép csere:</td>
		<td><input type="file" name='ujfajl' id='ujfajl' style="width: 280px;" /></td>
	</tr>
	</table>
	</form>
</div>
<p align="right">
	Mindet
	<span class="open-closer-allin">kinyit</span> / <span class="open-closer-allout">bezár</span>
</p>
<ul class="main_events">
    <?foreach($guests as $vendeg):?>
    <li class="main_event" name="<?=$vendeg->guestname?>">
        <div class="col_szint1">
        <span class="open-closer">+</span>
        <span class="main_event_name"><?=$vendeg->guestname?></span>
        <span class="main_event_dates">(<?=$vendeg->darab?>)</span>
        </div>
        <span class="edit-link"></span>
        <span class="view-link">
            <?=anchor($urlelotag.'mobile/guests/'.current(explode('/',$vendeg->mappa)), $this->lang->line('view'), 'target="_blank"')?>
			<a class="edit_guestname"><?=$this->lang->line('pages_h1_edit')?></a>
        </span>
		<span class="delete-link"><?=anchor('/admin/viewguestnamedel/'.$vendeg->cp_id, $this->lang->line('news_button_delete'), 'class="del_anchor" question="Biztosan törli a vendéget és projektjeit?"')?></span>
        <span class="delete-link"></span>
    </li>
        <ul class="events">
            <?if($vendeg->projektek):?>
            <?foreach($vendeg->projektek as $projekt):?>
            <li class="event" event_name="<?=$projekt->nev?>" cp_id="<?=$projekt->cp_id?>">
                <div class="col_szint2">
                <span class="open-closer">+</span>
                <span class="event_name"><?=$projekt->nev?></span>
                <span class="event_date">(<?=$projekt->darab?>)</span>
                </div>
                <span class="edit-link">
					<?=anchor($urlelotag.'mobile/guests/'.$projekt->mappa, $this->lang->line('view'), 'target="_blank"')?>
				</span>
                <span class="edit-link edit_anchor" event_id="<?=$projekt->c_id?>" autodel="<?=$projekt->autodel?>"><?=$this->lang->line('pages_h1_edit')?>
                </span>
                <span class="delete-link"><?=anchor('/admin/viewguestprojectdel/'.$projekt->cp_id, $this->lang->line('news_button_delete'), 'class="del_anchor" question="Biztosan törli a projektet?"')?></span>
            </li>
            <?if($projekt->fajlok):?>
            <ul class="registrations">
                <?foreach($projekt->fajlok as $fajl):?>
                <li class="col_fajlok">
                    <div class="col_szint3">
                    <span class="reg_email"><?=$fajl->title?></span>
                    </div>
                    <span class="edit-link edit-item" cu_id="<?=$fajl->cu_id?>"><?=$this->lang->line('pages_h1_edit')?></span>
                    <span class="delete-link"><?=anchor('admin/viewitemdel/'.$fajl->cu_id, $this->lang->line('news_button_delete'), 'class="del_anchor" question="Biztosan törli a képet?"')?></span>
                </li>
                <?endforeach;?>
            </ul>
            <?endif;?>
            <?endforeach;?>
            <?endif;?>
        </ul>
    <?endforeach;?>
</ul>
<?else:?>
<p>No events!</p>
<?endif;?>

<p><br /><a class="viewerstat" href="#">Felhasználók használati statisztikája</a></p>
