<?php
  /**
   * Main
   *
   * @package Digital Downloads Pro
   * @author wojoscripts.com
   * @copyright 2010
   * @version $Id: comments.php, v2.00 2011-07-10 10:12:05 gewa Exp $
   */
  if (!defined("_VALID_PHP"))
      die('. دسترسی مستقیم به این محل مجاز نمی باشد');

  $counter = countEntries("comments", "pid", $item->pid."' AND active = '1");
  $config = $content->getCommentsConfig();
  $pages = ceil($counter/$config['perpage'])	  
?>
<!-- Start Comments-->
<div class="box2" style="position:relative">
<div id="loadbig" style="display:none;position: absolute; left:10px"> <img src="<?php echo SITEURL;?>/images/ajax-loader.gif" alt=""/></div>
<?php echo ($counter <> 0) ? "<h3>" . $counter." نـظـرات </h3>" : "<h4>...هنـوز نظـری داده نشـده اسـت</h4>";?>
<div id="comments"></div>
<?php if($counter > $config['perpage']):?>
<div class="pagination" style="margin-bottom:8px; float:left"><span class="inner">
  <?php for($j=1; $j<=$pages; $j++):?>
  <a href="javascript:void(0);" class="number" id="pg-<?php echo $j;?>"><?php echo $j;?></a>
  <?php endfor;?>
</span></div><br clear="right" />
<?php endif;?>
  <?php if($config['public_access']):?>
  <?php include("form.tpl.php");?>
  <?php elseif(!$config['public_access'] and $user->logged_in):?>
  <?php include("form.tpl.php");?>
  <?php else:?>
  <?php echo $core->msgInfo('<span>!تـوجـه</span>. با عرض پـوزش، تنهـا کـاربـران عضـو شـده می توانند نـظـر بدهنـد. لطـفـا وارد شویـد و یا ثـبـت نـام نمائیـد',false);?>
  <?php endif;?>
<div id="response" style="margin-top:5px"></div>
</div>
<script type="text/javascript">
// <![CDATA[
//Char Limiter
(function ($) {
    $.fn.limit = function (options) {
        var defaults = {
            limit: 200,
            id_result: false,
            alertClass: false
        }
        var options = $.extend(defaults, options);
        return this.each(function () {
            var characters = options.limit;
            if (options.id_result != false) {
                $("#" + options.id_result).append("...هنـوز <strong>" + characters + "<\/strong> کارکتـر باقی مـانـده");
            }
            $(this).keyup(function () {
                if ($(this).val().length > characters) {
                    $(this).val($(this).val().substr(0, characters));
                }
                if (options.id_result != false) {
                    var remaining = characters - $(this).val().length;
                    $("#" + options.id_result).html("...هنـوز <strong>" + remaining + "<\/strong> کارکتـر باقی مـانـده");
                    if (remaining <= 10) {
                        $("#" + options.id_result).addClass(options.alertClass);
                    } else {
                        $("#" + options.id_result).removeClass(options.alertClass);
                    }
                }
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    function showLoader() {
        $('#loadbig').fadeIn(200);
    }

    function hideLoader() {
        $('#loadbig').fadeOut(200);
    };

    function showsmLoader() {
        $('#smloading').fadeIn(200);
    }

    function hidesmLoader() {
        $('#smloading').fadeOut(200);
    };
	
    function loadList() {
        showLoader();
        $("#pg-1").addClass("current");
        $.ajax({
            url: SITEURL + "/plugins/comments/loadComments.php?pid=<?php echo $item->pid;?>",
            data: "pg=1",
            cache: false,
            success: function (html) {
                $("#comments").html(html);
            }
        });
        hideLoader();
    }

    loadList();
	
	//Pagination
    $("a.number").click(function (e) {
        e.preventDefault();
        $(".current").removeClass("current");
        $(this).addClass("current");
        showLoader();
        $.ajax({
            url: SITEURL + "/plugins/comments/loadComments.php?pid=<?php echo $item->pid;?>",
            data: "pg=" + this.id.replace('pg-', ''),
            cache: false,
            success: function (html) {
                $("#comments").html(html);
            }
        });
        hideLoader();
    });
		
    //Limiter
    $("#body").limit({
        limit: <?php echo $config['char_limit'];?>,
        id_result : "counter",
        alertClass : "char-alert"
    });
	
	//Post Comment
	$("#commentform").submit(function () {
		var str = $(this).serialize();
		showsmLoader();
		$.ajax({
			type: "POST",
			url: SITEURL + "/plugins/comments/processComment.php",
			data: str,
			success: function (msg) {
				$("#response").ajaxComplete(function (event, request, settings) {
					if (msg == 'OK1') {
						result = '<div class="msgOk"><span>!متشکـریـم</span> پست شما ارسال شـد<\/div>';
						$("#reply").hide();
					} else if (msg == 'OK2') {
						result = '<div class="msgOk"><span>!متشکـریـم</span> پست شما ارسال شـد و در انتظار تائید می باشد<\/div>';
						$("#reply").hide();
					} else {
						result = msg;
					}
					$(this).html(result);
					hidesmLoader();
				});
			}
		});
		return false;
	});
});
// ]]>
</script>
<!-- End Comments/--> 