{include file="$admingentemplates/admin_top.tpl"}
<div class="back">
    <a href="./admin_poll.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="padding:5px;">
	<div class="close_help">
	  <img src="../templates/admin/images/close_help.png" alt="">
	</div>
	 <span class="help_title">{$lang.help}:</span>{$lang.admin_help.polls}
</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 {if !$curr_category}style="float: left;"{/if}>
{if $curr_category}
{$lang.admin_poll.category}: {$curr_category.name}
</div>
<div class="sep"></div>
<div class="user_status_elem_active" style="float: left;" align="center" id="tab_list" onclick="lists();">
    {$lang.settings.lists}
</div>
<div class="user_status_elem" style="float: left;" align="center" id="tab_add" onclick="add();">
    <a>{$lang.button.add}</a>
</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>
<div id="add_poll_form" style="display:none;">
<table width="100%" class="panel_subtabs">
 <tr>
  <td>
  <div style="padding:10px;overflow:hidden;">
	<!--add poll form-->
	<form name="add_poll_form" method="post" action="./admin_poll.php?sel=add_poll">
		<input type="hidden" name="poll_id" value="0">
		<font id="form_header" class="main_header_text">{$lang.admin_poll.add_poll}</font>
		<div id="add_new_poll_div" style="display:none;">&nbsp;&nbsp;<input type="button" value="{$lang.admin_poll.add_new_poll}" onclick="resetAddForm();"></div>
		<div class="sep"></div>
		<div style="width:70px;float:left;">{$lang.admin_poll.status}:</div>
		<select name="status">
			<option value="1">{$lang.admin_poll.status_vals.1}</option>
			<option value="0">{$lang.admin_poll.status_vals.0}</option>
		</select>&nbsp;
		{$lang.admin_poll.expiration}:<input type="text" name="expiration" size="3">&nbsp;
		<label for="never">{$lang.admin_poll.never_exp}</label><input id="never" type="checkbox" name="never_expire" value="1" checked="checked" onclick="initExpiration($(this).attr('checked'));">&nbsp;
		<label for="comments">{$lang.admin_poll.allow_comments}</label><input id="comments" type="checkbox" name="allow_comments" value="1" checked="checked">
		<div class="sep"></div>
		<div style="width:120px;float:left;">{$lang.admin_poll.question}:</div><input type="text" name="question" style="width:350px;">
		<div class="sep"></div>
		<div style="width: 100%; display: table;">
		<div id="answers" style="float:left;width:51%;">
		</div>
		<div style="float:right;width:49%;text-align:left;" id="add_img"><img src="{$site_root}{$template_root}/images/add_listing.gif" alt="" style="margin-left: 10px; cursor: pointer;" onclick="addAnswer(); return false;" /></div>
		</div>{*<a class="admin_button" href="#" onclick="addAnswer(); return false;">{$lang.admin_poll.add_answer}<span>&nbsp;</span></a>*}
		<div class="sep"></div>
		<div style="width:70px;float:left;">{$lang.admin_poll.category}:</div>
		<select name="category_id">
			{foreach from=$all_categories item=item}
			<option value="{$item.category_id}" {if $curr_category.category_id==$item.category_id}selected{/if}>{$item.name}</option>
			{/foreach}
		</select>
		<div class="sep"></div>
		<div style="float: left;">{$lang.admin_poll.show_groups}:</div>
		<div style="float:left;padding-left:10px;">
			<table cellpadding="0" cellspacing="0">
			{foreach from=$groups item=item}
			<tr>
				<td>{$item.name}</td>
				<td><input type="checkbox" name="group_ids[]" value="{$item.group_id}" checked="checked"></td>
			</tr>
			{/foreach}
			</table>
		</div>
	</form>
	<!--/add poll form-->
  </div>
  </td>
 </tr>
</table> 
		<div>
			<a class="admin_button_major" onclick="addPoll();return false;">{$lang.button.add}<span>&nbsp;</span></a>
		</div>
</div>
<div id="answer_source" style="display: none;">
	<div class="div_answer" style="padding-bottom:10px;">
		<div style="width:120px;float:left;">{$lang.admin_poll.answer}<span class="answer_counter"></span>:</div><input type="text" name="answer[]" style="width:280px;">
		#<input type="text" name="color[]" class="colorpick" size="7" style="padding-left:3px;">&nbsp;{*<a href="#" onclick="removeAnswer($(this)); return false;">{$lang.admin_poll.crop_answer}</a>*}<img width="16" alt="" src="{$site_root}{$template_root}/images/photo_delete.png" onmouseout="javascript:hideToolTip();" style="cursor:pointer;" onmouseover="javascript: showToolTip(event,'{$lang.button.delete}');return false" onclick="removeAnswer($(this)); return false;">
	</div>
</div>
<div class="sep"></div>
{/if}
<table cellpadding="0" id="main_table" cellspacing="0" width="100%" {*style="border-left: 1px solid #29B43D; border-right: 1px solid #29B43D;"*}>
	<tr class="table_header">
		<td align="center">
			№
		</td>
		{if $show_category}
		<td align="left">{$lang.admin_poll.category}</td>
		{/if}
		<td align="left">{$lang.admin_poll.owner}</td>
		<td align="left">{$lang.admin_poll.question}</td>
		<td align="left">{$lang.admin_poll.created}</td>
		<td align="left">{$lang.admin_poll.expirate}</td>
		<td align="left">{$lang.admin_poll.stats}</td>
		<td align="left">{$lang.admin_poll.status}</td>
		<td align="left">{$lang.admin_poll.allow_comments}</td>
		<td align="right">{$lang.admin_poll.actions}</td>
	</tr>
	{counter start=$from print=false}
	{foreach from=$polls item=item}
	 
	<tr id="tr{$item.poll_id}" param="poll_tr" {if  $smarty.foreach.index is div by 2}class="odd"{else}class="even"{/if}>
		<td align="center" valign="top">
			{counter}
		</td>
		{if $show_category}
		<td align="left" valign="top"><a href="./admin_poll.php?sel=view_polls&category_id={$item.category_id}">{$item.cat_name}</a></td>
		{/if}
		<td align="left" valign="top">
			{if $user_id == $item.user_id}
			{$item.login}
			{else}
			<a href="#" onclick="window.open('admin_users.php?sel=edituser&editpage=1&page=1&id={$item.user_id}','info','width=600, height=800, resizable=yes, scrollbars=yes, menubar=no,status=no');">{$item.login}</a>
			{/if}
		</td>
		<td align="left" valign="top">{$item.question}</td>
		<td align="left" valign="top">{$item.date_add}</td>
		<td align="left" valign="top">{if $item.date_until}{$item.date_until}{else}{$lang.admin_poll.never_expire}{/if}</td>
		<td class="main_header_text" align="left" valign="top">
			{$lang.admin_poll.votes_count}: {$item.votes_count}<br>
			{$lang.admin_poll.comments_count}: {$item.comments_count}
		</td>
		<td align="left" valign="top">
			{$lang.admin_poll.status_vals[$item.status]}
		</td>
		<td align="left" width="10%">
			{if $item.allow_comments}
			+
			{else}
			-
			{/if}
		</td>
		<td style="width: 136px;" align="right" 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="deletePoll('{$item.poll_id}'); return false;">
						</div>
					</div>			
			{if $user_id == $item.user_id}
					<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="editPoll('{$item.poll_id}'); return false;">
						</div>
					</div>			
			{/if}
					<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/admin_marketing_view1.gif" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.statistic}');return false" onclick="document.location.href='admin_poll.php?sel=view_poll&poll_id={$item.poll_id}&category_id={$curr_category.category_id}&filter={$filter}&username={$username}&page={$page}'">
						</div>
					</div>			
			{if $user_id != $item.user_id}
					<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/communicate.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.comunicate.editform}');return false" onclick="javascript:window.open('./admin_comunicate.php?id={$item.user_id}&commit=1','comunicate', 'height=800, resizable=yes, scrollbars=yes,width=600, menubar=no,status=no');">
						</div>
					</div>			
			{/if}
		</td>
	</tr>
	{foreachelse}
	<tr>
		<td class="main_error_text" align="left" colspan="8">{$lang.admin_poll.no_polls}</td>
	</tr>
	{/foreach}
	<tr  class="table_header">
		<td height="20" colspan={if $show_category}10{else}9{/if} 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>
</table>
<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>
<script type="text/javascript">
{literal}
		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">

	page = '{$page}';
	category_id = '{$curr_category.category_id}'
	filter='{$filter}';
	username='{$username}';

	{literal}
	min_answers = 2;
	$(document).ready(function(){
		resetAddForm();

	});


	function resetAddForm(){
		$("#add_poll_form #form_header").html('{/literal}{$lang.admin_poll.add_poll}{literal}');
		$("#add_poll_form #add_new_poll_div").hide();
		$("#add_poll_form input[name=poll_id]").val('0');
		$("#add_poll_form select[name=status]").val('1');
		$("#add_poll_form input[name=expiration]").val('');
		$("#add_poll_form input[name=never_expire]").attr('checked',true);
		initExpiration();
		$("#add_poll_form input[name=allow_comments]").attr('checked',true);
		$("#add_poll_form input[name=question]").val('');
		$("#add_poll_form #answers").html('');
		initAnswers();
		$("#add_poll_form select[name=category_id]").val(category_id);
		$("#add_poll_form input[name='group_ids[]']").attr('checked',true);
		$("#add_poll_form #save_button").val('{/literal}{$lang.button.add}{literal}');
	}

	function initExpiration(){
		checked_never = $("input[name=never_expire]").attr('checked');
		if (checked_never){
			$("input[name=expiration]").val('');
			$("input[name=expiration]").attr('disabled',true);
		}else{
			$("input[name=expiration]").attr('disabled',false);
		}
	}

	function initAnswers(){
		for(i=1;i<=min_answers;i++){
			addAnswer();
		}
		return;
	}

	function addAnswer(text,color){
		count = $("#answers .div_answer").length;
		$("#answer_source .answer_counter").html(count+1);
		source = $("#answer_source").html();
		$("#answers").append(source);
		initColorPickers();
		$("#answers input[name='answer[]']:last").focus();
		$("#answers input[name='answer[]']:last").val(text);
		$("#answers input[name='color[]']:last").val(color);
		$('#add_img').css('padding-top', $('#answers').height()-$('#add_img').height()-8);
		$('#add_img').css('padding-bottom', '8');		
	}

	function initColorPickers(){
		setColors();
		inpt=[];
		$('.colorpick').each(function(i){
			inpt[i] = $(this);
			inpt[i].ColorPicker({
					color: '#'+(inpt[i].val()?inpt[i].val():getRandColor()),
					onShow: function (colpkr) {
						$(colpkr).fadeIn(500);
						return false;
					},
					onHide: function (colpkr) {
						$(colpkr).fadeOut(500);
						return false;
					},
					onChange: function (hsb, hex, rgb) {
						inpt[i].val(hex);
					}
			});
		});
	}

	function setColors(){
		$("#answers .colorpick").each(function(){
			if (!$(this).val())
				$(this).val(getRandColor());
		});
	}

	function removeAnswer(link_remove){
		if ($("#answers .div_answer").length <= min_answers){
			$("#answers input[name='answer[]']:last").focus();
			return false;
		}
		link_remove.parent().remove();
		$("#answers .div_answer").length = $("#answers .div_answer").length-1;
		$("#answers .answer_counter").each(function(i){
			$(this).html(i+1);
		});
		$("#answers input[name='answer[]']:last").focus();
		$('#add_img').css('padding-top', $('#answers').height()-$('#add_img').height()-8);
		$('#add_img').css('padding-bottom', '8');		
	}

	function addPoll(){
		question = $("input[name=question]").val();
		count_with_vals=0;
		$("#answers input[name='answer[]']").each(function(i){
			$(this).val()?count_with_vals++:'';
		});
		if (question==''){
			alert("{/literal}{$lang.poll_errors.empty_question|escape:javascript}{literal}");
			return false;
		}
		if (count_with_vals < min_answers){
			alert("{/literal}{$lang.poll_errors.min_answers|escape:javascript}{literal}"+min_answers);
			return false;
		}
		if (!$("input[name='group_ids[]']:checked").val()){
			alert("{/literal}{$lang.poll_errors.addPoll_empty_groups|escape:javascript}{literal}");
			return false;
		}

		document.forms.add_poll_form.submit();
	}

	function editPoll(poll_id){
		str = "poll_id="+poll_id;
		$.post("./admin_poll.php?sel=get_poll_edit_info",str,function(data){
			if (!data){
				return false;
			}
			data = getJQData(data);
			setEditFormData(data["poll_info"]);
		});
	}

	function setEditFormData(poll_info){
		$("#add_poll_form #form_header").html('{/literal}{$lang.admin_poll.edit_poll}{literal}');
		$("#add_poll_form #add_new_poll_div").show();
		$("#add_poll_form #add_new_poll_div").css("display","inline");
		$("#add_poll_form input[name=poll_id]").val(poll_info.poll_id);
		$("#add_poll_form select[name=status]").val(poll_info.status);
		if (poll_info.expire_days){
			$("#add_poll_form input[name=expiration]").val(poll_info.expire_days);
			$("#add_poll_form input[name=never_expire]").attr('checked',false);
		}else{
			$("#add_poll_form input[name=expiration]").val('');
			$("#add_poll_form input[name=never_expire]").attr('checked',true);
		}
		initExpiration();
		$("#add_poll_form input[name=allow_comments]").attr('checked',poll_info.allow_comments=='1'?true:false);
		$("#add_poll_form input[name=question]").val(poll_info.question);
		$("#add_poll_form #answers").html('');
		for(i=0;i<poll_info["answers"].length;i++){
			addAnswer(poll_info["answers"][i]["text"],poll_info["answers"][i]["color"]);
		}
		$("#add_poll_form select[name=category_id]").val(poll_info.category_id);
		
		$("#add_poll_form input[name='group_ids[]']").attr("checked",false);
		if (poll_info["groups"]){
			for(i=0;i<poll_info["groups"].length;i++){
				$("#add_poll_form input[name='group_ids[]'][value="+poll_info["groups"][i]+"]").attr("checked",true);
			}
		}
		$('#add_button').hide();$('#add_poll_form').show();
		$("#add_poll_form #save_button").val('{/literal}{$lang.button.save}{literal}');
	}

	function deletePoll(poll_id){
		str = 'poll_id='+poll_id;

		$.post('./admin_poll.php?sel=delete_poll',str,function(data){
			data = getJQData(data);
			if (data['success']=='1'){
				location.href="./admin_poll.php?sel=view_polls&category_id="+category_id+"&filter="+filter+"&username="+username+"&page="+(page-1);
			}else{
				showNoticeOnEl(data['msg'],$("#delete_link"+poll_id));
			}
		});
	}
	function lists(){
        $('#tab_add').html("<a>"+$('#tab_add').text()+"<\/a>");
        $('#tab_list').text($('#tab_list').text());
        $('#tab_add').removeClass('user_status_elem_active');
        $('#tab_add').addClass('user_status_elem');
        $('#tab_list').removeClass('user_status_elem');
        $('#tab_list').addClass('user_status_elem_active');
		$('#main_table').show();
		$('#add_poll_form').hide();
		return false;
	}
	function add(){
        $('#tab_list').html("<a>"+$('#tab_list').text()+"<\/a>");
        $('#tab_add').text($('#tab_add').text());
        $('#tab_list').removeClass('user_status_elem_active');
        $('#tab_list').addClass('user_status_elem');
        $('#tab_add').removeClass('user_status_elem');
        $('#tab_add').addClass('user_status_elem_active');
		$('#add_poll_form').show();
		$('#main_table').hide();
		$('#add_img').css('padding-top', $('#answers').height()-$('#add_img').height()-8);
		$('#add_img').css('padding-bottom', '8');
		return false;
	}
{/literal}
</script>
{include file="$admingentemplates/admin_bottom.tpl"}