(function($) {
	$.fn.firstElement = function()
	{
	    $(":input[value='']:not(.nofocus):visible:enabled:first").focus();
	    return this;
	};
	$.fn.log = function (msg) {
	    // if (MyOptions.logs) {
	        console.log("%s: %o", msg, this);
	        return this;
	    // }
	};

	modalLoader = function() {
	    var fbOptions = {
	        modal: true,
	        opacity: true,
	        href: '#divFancyBox',
	        scrolling: 'yes',
	        centerOnScroll: true,
			titlePosition: 'inside'
	    };
	    $.fancybox(fbOptions);
	};
	modalClose = function() {
		//jQuery.fancybox.close();
		//alert(stop);
	    setTimeout("jQuery.fancybox.close()",300);
	};

	commentReply = function() {
		replyId = this.id;
		replyId = replyId . substring(9);
		$('form#formComments input[name=parent]').val(replyId)
	};
    commentAdd = function() {
    	modalLoader();
    	$('.commentStatus').html('');
    	/*
    	$('#loader').ready(function() { $.blockUI({ overlayCSS:  { 
        	opacity:         0.8 
    	},message: $('#loader'),baseZ: 9999999,  showOverlay: true}) });
    	$('#loader').ready(function() { 
    	$.blockUI({ 
    	    overlayCSS:  { 
	        	opacity:         0.7 
	    	},
    		message: $('#loader') ,
	    	showOverlay: true, 
    		css: {
    			backgroundColor: '#cecece', 
    			color: '#dedede' 
			}
	    });
    	});
    	*/
        $.ajax({
        	type: "post",
        	dataType: 'json',
        	url: myindo.site_url('dc/comments'),
        	data: {
        	    dtype:'json',
        	    parent: $('form#formComments input[name=parent]').val(),
        	    module: $('form#formComments input[name=module]').val(),
        	    module_id: $('form#formComments input[name=module_id]').val(),
        	    name: $('form#formComments input[name=name]').val(),
        	    email: $('form#formComments input[name=email]').val(),
        	    url: $('form#formComments input[name=url]').val(),
        	    comment: $('form#formComments textarea[name=comment]').val(),
        	    captcha_word: $('form#formComments input[name=captcha_word]').val(),
        	    captcha_id: $('form#formComments input[name=captcha_id]').val(),
        	},
            success: function(d) {
        		modalClose();
        		if (d.is_success) {
        			$('.commentStatus').html(d.status);
        			$('form#formComments textarea[name=comment]').val('');
        			if (d.reload) {
        				setTimeout('window.location.reload();',500);
        			}
        			$('form#formComments').reset();
        		} else {
        			$('.commentStatus').html(d.status);
        			$('form#formComments textarea[name=comment]').focus();
        			if (d.captcha.length) {
	        			$('form#formComments div#captcha_html').html(d.captcha.html);
	        			$('form#formComments input#captcha_id').val(d.captcha.id);
	        			$('form#formComments input#captcha_word').val('');
        			}
        		}
            }  
        });
   };
})(jQuery);

jQuery(document).ready(function($) {
	//$(document).ajaxStop(modalClose());
	$('.commentReply').bind('click',commentReply);
	$('#formComments').bind('submit',commentAdd);
	//$.fn.firstElement();
});