{include file="$admingentemplates/admin_top.tpl"}
<div class="back">
    <a href="{$site_url}{$site_root}/admin/admin_modules.php">{$lang.button.back}</a>
</div>
<div class="cr"></div>
<div class="show_help" style="display: none;">&nbsp;</div>
<div class="red_header">{$lang.admin_poll.razdel_name}</div>
<div class="cr">&nbsp;</div>
<div class="cr">&nbsp;</div>
<div class="help_text" style="display:block;">
    <div class="close_help">
        <img src="{$site_root}{$template_root}/images/close_help.png" alt="">
    </div>
    <div class="help_body">
        <span class="help_title">{$lang.help}:</span>{$lang.admin_help.poll_cats}
    </div>
</div>
<div class="cr">&nbsp;</div>
<ul class="tabs">
    <li>
        <a class="current">{$lang.admin_menu.poll_list}</a>
    </li>
    <li>
        <a href="admin_poll.php?sel=settings">{$lang.admin_menu.settings}</a>
    </li>
</ul>
<div class="user_operation_line" style="height: 4px;font-size:0px;"></div>
<div class="cr"></div>
<div id="tooltip" onclick="hideToolTip()" style="text-align:center; background-color: #FFFFFF; border: 1px dotted; text-align: {$default_left}; position:absolute; display:none; padding:5px;"></div>
{literal}
<script type="text/javascript">
$(function() {
$('#tool_tip_4 *').tooltip();
});
</script>
{/literal}

{if $err}
<div class="sep"></div>
<div class="error_msg">{$err}</div>
{/if}
{*<div class="sep"></div>
<font class="main_header_text">{$lang.settings.allow_users_create_poll}:</font>&nbsp;<input type="checkbox" name="allow_users_create_poll" onclick="setSetting($(this).attr('name'), $(this).attr('checked')?1:0,'input[name=allow_users_create_poll]')" {if $settings.allow_users_create_poll}checked="checked"{/if}>
*}<div class="sep"></div>
<div style="float:left;">
	<table>
        <tr>
            <form name="add_cat_form" action="./admin_poll.php?sel=add_cat" method="post">
        	<td>{$lang.admin_poll.add_category}:&nbsp;</td>
        	<td><input type="text" name="cat_name">&nbsp;</td>
        	<td><a href="#" class="admin_button_major" onclick="addCategory($('input[name=cat_name]').val()); return false;">{$lang.button.add}<span>&nbsp;</span></a></td>
        	</form>
         </tr>
    </table>
</div>
<div>
    <table align="right" width="50%">
        <tr>
            <td>
                <table align="right">
                    <tr>
                        <td>{$lang.admin_poll.show_created_by}:</td>
                        <td>
                        	<select name="created_by" onchange="if (this.value=='user') $('input[name=username]').attr('disabled',false); else $('input[name=username]').attr('disabled',true);">
                        		<option value="admin">{$lang.admin_poll.created_by.admin}</option>
                        		<option value="users">{$lang.admin_poll.created_by.users}</option>
                        		<option value="user">{$lang.admin_poll.created_by.user}</option>
                        	</select>
                        </td>
                    	<td><input type="text" name="username" disabled="disabled"></td> 
                    	<td><a class="admin_button" onclick="showAllPolls();return false;">{$lang.button.go}<span>&nbsp;</span></a></td>
                    </tr>
                 </table>
            </td>
        </tr>
        <tr>
            <td>
                <table align="right">
                    <tr>
                        <td>{$lang.admin_poll.cat_jump}:</td>
                    	<td>
                            <select name="category_id" onchange="if (this.value>0) location.href='./admin_poll.php?sel=view_polls&category_id='+this.value;">
                        		<option value="0">---</option>
                        		{foreach from=$all_categories item=item}
                        		<option value="{$item.category_id}">{$item.name}</option>
                        		{/foreach}
                    	   </select>
                        </td>
                     </tr>
                 </table>
            </td>
        </tr>
    </table>
</div>
	
<div style="clear:both;"></div>
<div class="sep"></div>
<table cellpadding="0" cellspacing="0" width="100%" {*style="border-left: 1px solid #29B43D; border-right: 1px solid #29B43D;"*}>
	<tr class="table_header">
		<td align="center" width="10%">№</td>
		<td width="25%" align="left">{$lang.admin_poll.category}</td>
		<td width="35%" align="left">{$lang.admin_poll.stats}</td>
		<td width="" align="right" style="padding-right:20px;">{$lang.admin_poll.actions}</td>
	</tr>
	{counter start=$from print=false}
	{foreach from=$categories item=item key=key}
	<tr id="tr_category{$item.category_id}" {if $key is div by 2}class="odd"{else}class="even"{/if}>
		<td align="center" valign="top" style="padding-top: 5px; padding-bottom: 5px;">{counter}</td>
		<td align="left" valign="top" style="padding-top: 5px; padding-bottom: 5px;">
			<div id="cat_name{$item.category_id}">
				<div id="inform_div{$item.category_id}" class="error_msg" style="display:none;float:right;"></div>
				<span id="link{$item.category_id}">{$item.name}</span>
				<input id="text_input{$item.category_id}" type="text" value="{$item.name}" style="display:none"/>
			</div>
			<div id="cat_name_input{$item.category_id}" style="display:none;">
				<input type="text" name="cat_name{$item.category_id}" value="{$item.name}">&nbsp;
				<a href="#" class="admin_button" onclick="saveCategory('{$item.category_id}',$('input[name=cat_name{$item.category_id}]').val());return false;">{$lang.button.save}<span>&nbsp;</span></a>
			</div>
		</td>
		<td align="left" valign="top" style="padding-top: 5px; padding-bottom: 5px;">
			<font class="main_header_text">{$lang.admin_poll.polls}: {$item.all_polls_count}</font><br>
			{$lang.admin_poll.by_users}: {$item.users_polls_count}<br>
			{$lang.admin_poll.by_admin}: {$item.admin_polls_count}
		</td>
		<td align="right" style="padding-right:10px;padding-top: 5px; padding-bottom: 5px;" valign="top">
					{*<div class="block_action fr" style="display: none;">
						<div class="action_del" style="display: block;cursor:pointer;">				
							<img width="16" alt="" src="{$site_root}{$template_root}/images/photo_delete.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.delete}');return false" onclick="deleteCategory('{$item.category_id}'); return false;">
						</div>
					</div>
					<div style="width:10px;float:right;" class="fl">&nbsp;</div>
					<div class="block_action fr" style="display: none;">
						<div class="action_edit" style="display: block;cursor:pointer;">
							<img width="16" alt="" src="{$site_root}{$template_root}/images/edit.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.edit}');return false" onclick="showCatForm('{$item.category_id}'); return false;">
						</div>
					</div><div style="width:10px;float:right;" class="fl">&nbsp;</div>
					<div class="block_action fr" style="display: none;">
						<div class="action_edit" style="display: block;cursor:pointer;">
							<img alt="" src="{$site_root}{$template_root}/images/icn_view.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.view}');return false" onclick="document.location.href='./admin_poll.php?sel=view_polls&category_id={$item.category_id}'">
						</div>
					</div>			<div id="loading{$item.category_id}" style="display: inline;"></div>*}
					<div class="block_action" >  
						<img alt="" height="13" style="cursor: pointer;" src="{$site_root}{$template_root}/images/icn_view.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.view}');return false" onclick="document.location.href='./admin_poll.php?sel=view_polls&category_id={$item.category_id}'">
						<img id="edit_button{$item.category_id}" src="{$site_root}{$template_root}/images/edit.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.edit}');return false" alt="" height="13" style="cursor: pointer;" onclick="javascript: editAction({$item.category_id});" />&nbsp;
						<img id="save_button{$item.category_id}" src="{$site_root}{$template_root}/images/success.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.save}');return false" alt="" height="13" style="cursor: pointer; display:none;" onclick="javascript: saveAction({$item.category_id});" />&nbsp;
						<img src="{$site_root}{$template_root}/images/photo_delete.png" alt="" height="13" style="cursor: pointer;" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.delete}');return false" onclick="deleteCategory('{$item.category_id}'); return false;" />&nbsp;&nbsp;
					</div>
		</td>
	</tr>
	{foreachelse}
	<tr>
		<td class="main_error_text" align="left"colspan="4">{$lang.admin_poll.no_categories}</td>
	</tr>
	{/foreach}
    {*if $links*}
	<tr  class="table_header">
		<td height="20" colspan=4 align="left" >
		  {foreach item=item from=$links.pages}
		    <a class="page_link" href="{$item.link}" 
			 {if $item.selected}style=" font-weight: bold; text-decoration: none;"{/if}>
			 {$item.name}</a>

		  {/foreach}
		</td>
	</tr>
	{*else}
	<tr  class="table_header" style="height:2px">
		<td height="2px" colspan=4 align="left" >
		</td>
	</tr>
	{/if*}
</table>
<div style="width:20px">&nbsp;</div>
<script type="text/javascript">
{literal}
lang = '{/literal}{$lang.country.saved}{literal}';
function editAction(id){
	link_obj = getById('link'+id);
	text_input_obj = getById('text_input'+id);
	edit_button_ogj = getById('edit_button'+id);
	save_button_ogj = getById('save_button'+id);

	link_obj.style.display = 'none';
	text_input_obj.style.display = '';
	edit_button_ogj.style.display = 'none';
	save_button_ogj.style.display = '';

	text_input_obj.focus();
}

function saveAction(id){
	inform_div_obj = getById('inform_div'+id);
	link_obj = getById('link'+id);
	text_input_obj = getById('text_input'+id);
	edit_button_ogj = getById('edit_button'+id);
	save_button_ogj = getById('save_button'+id);
	value = text_input_obj.value;

	file_name = 'admin_poll.php';
	str = 'sel=save_cat&category_id='+id+'&name='+value+'&e=1';
	destination_odj = inform_div_obj;
	tmp_text = 'Saving...';
	anisochronous = false;

	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
	
	ShowAjaxError(lang, '');
	
	inform_div_obj.style.display = '';

	saved_obj = getById('saved'+id);
	saved = saved_obj.value;
	if (saved == '1'){
		link_obj.style.display = '';
		link_obj.innerHTML = value;
		text_input_obj.style.display = 'none';
		edit_button_ogj.style.display = '';
		save_button_ogj.style.display = 'none';
		edit_button_ogj.focus();
	}
}
		function showToolTip(e,text){
			if(document.all)e = event;
			var obj = document.getElementById('tooltip');
			obj.innerHTML = text;
			obj.style.display = 'block';
			var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
			if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
			var leftPos = e.clientX - $('#tooltip').width()-10;
			if(leftPos<0)leftPos = 0;
			obj.style.left = leftPos + 'px';
			obj.style.top = e.clientY - obj.offsetHeight -10 + st + 'px';
		}
		function hideToolTip(){
			document.getElementById('tooltip').style.display = 'none';
		}
{/literal}		
</script>

<script type="text/javascript">
	{literal}
	function addCategory(name){
		if (name==''){
			alert("{/literal}{$lang.poll_errors.empty_cat_name|escape:javascript}{literal}");
			return false;
		}
		document.forms.add_cat_form.submit();
	}

	function saveCategory(category_id,name){
		if (category_id==''){
			alert("{/literal}{$lang.poll_errors.empty_cat_id|escape:javascript}{literal}");
			return false;
		}
		if (name==''){
			alert("{/literal}{$lang.poll_errors.empty_cat_name|escape:javascript}{literal}");
			return false;
		}
		ajaxLoadingImage(1, 'tr_category'+category_id);
		str = "ajax=1&category_id="+category_id+"&name="+name;

		$.post("./admin_poll.php?sel=save_cat",str,function(data){
			ajaxLoadingImage(0, 'tr_category'+category_id);
			resp = getJQData(data);
			if (resp["code"]=='1'){
				showCatForm(category_id);
				$("#cat_name"+category_id).text(resp["new_val"]);
				showNoticeOnEl(resp["msg"],$("#cat_name"+category_id));
			}else{
				showNoticeOnEl(resp["msg"],$("#cat_name_input"+category_id));
			}
		});
	}

	function showCatForm(category_id){
		$("#cat_name"+category_id).toggle();
		$("#cat_name_input"+category_id).toggle();
		return;
	}

	function deleteCategory(category_id, jq_obj_el){
		ajaxLoadingImage(1, 'tr_category'+category_id);
		str = "ajax=1&category_id="+category_id;
		$.post("./admin_poll.php?sel=del_cat",str,function(data){
			ajaxLoadingImage(0, 'tr_category'+category_id);
			ShowAjaxError(data);
			$("#tr_category"+category_id).remove();
		});
	}

	function showAllPolls(){
		filter = $("select[name=created_by]").val();
		username = $("input[name=username]").val();

		if (filter=='user' && username==''){
			alert("{/literal}{$lang.poll_errors.empty_username|escape:javascript}{literal}");
			return false;
		}
		location.href="./admin_poll.php?sel=view_polls&filter="+filter+(filter=="user"?"&username="+username:"");
	}

	{/literal}
</script>
{include file="$admingentemplates/admin_bottom.tpl"}