var siteDomain = "http://www.mongoliatourism.gov.mn/";
jQuery.fn.limitMaxlength = function(options){

  var settings = jQuery.extend({
    attribute: "maxlength",
    onLimit: function(){},
    onEdit: function(){}
  }, options);
  
  // Event handler to limit the textarea
  var onEdit = function(){
    var textarea = jQuery(this);
    var maxlength = parseInt(textarea.attr(settings.attribute));

    if(textarea.val().length > maxlength){
      textarea.val(textarea.val().substr(0, maxlength));
      
      // Call the onlimit handler within the scope of the textarea
      jQuery.proxy(settings.onLimit, this)();
    }
    
    // Call the onEdit handler within the scope of the textarea
    jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
  }

  this.each(onEdit);

  return this.keyup(onEdit)
        .keydown(onEdit)
        .focus(onEdit);
}

$().ready(function(){
			
  var onEditCallback = function(remaining){
    $('.' + this.className + 'Char').text(remaining + " тэмдэгт");
    
    if(remaining > 0){
      $(this).css('background-color', 'white');
    }
  }
  
  var onLimitCallback = function(){
    $(this).css('background-color', 'red');
  }
  
  $('textarea[maxlength]').limitMaxlength({
    onEdit: onEditCallback,
    onLimit: onLimitCallback,
  });
  
$(".oneComment").mouseenter(function() {
    $(this).addClass('oneCommentOn');
  }).mouseleave(function() {
    $(this).removeClass('oneCommentOn');
});
  
$(".replyDivBtn").mouseenter(function() {
	
	if(this.className=='replyDivBtn')
	    $(this).addClass('replyDivBtnM');
		
  }).mouseleave(function() {
	if(this.className=='replyDivBtn replyDivBtnM')
	    $(this).removeClass('replyDivBtnM');
  });

$(".mouseOverDiv").mouseenter(function() {
	
	if(this.className=='mouseOverDiv')
	    $(this).addClass('mouseOverDivOn');
		
  }).mouseleave(function() {
	if(this.className=='mouseOverDiv mouseOverDivOn')
	    $(this).removeClass('mouseOverDivOn');
  });
  
  
});


function replyMaxFeild(field) {
	
	maxlimit = 300;
	if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
	thisCharCount = maxlimit - field.value.length;
	
	$('.' + field.className + 'Char').text(thisCharCount + " тэмдэгт");
	
}

function ajaxLoadPage(url,methotType,divID,loadDivID)
{
	
	$('#' + loadDivID).css('display','');
	$.ajax({
		type: "POST",
		url: url,
		data: 'page='+url,
		dataType: "html",
		success: function(msg){
			
			if(parseInt(msg)!=0)
			{
				$('#' + divID).html(msg);
				$('#' + loadDivID).css('display','none');
			}
		}
		
	});
}
function ajaxFromPage(url,divID,loadDivID,frm)
{
	$('#' + loadDivID).css('display','');
	
	$.post(  
            url,
           $(frm).serialize(),  
           function(data){ 
               $('#' + divID).html(data);
			   $('#' + loadDivID).css('display','none');
           }  
       );  
	
}
function changeLocalnews(localID){
	
	var thisLink = siteDomain+"module.php?action=localnews&locID="+localID;
	ajaxLoadPage(thisLink,'','localNewsDivID','localNewsDivLoad')
}
function ajaxCommentPost(url,divID,loadDivID,frm,txtAr)
{	
	
	$('#' + loadDivID).css('display','');
	
	var allComNum = parseInt($('.allComNum').text());
	var newComNum = allComNum + 1;
	
	$.post(  
            url,
           $(frm).serialize(),  
           function(data){ 
		   
		   	   $('#' + txtAr).val("");
			   
			   $('.allComNum').text(newComNum);
			   $('.cmtCount').html(newComNum);
			   
               $('#' + divID).html(data);
			   $('#' + loadDivID).css('display','none');
			   
           }  
       ); 
	
		  
}
function ajaxReCommentPost(url,divID,loadDivID,frm,mainCmtID)
{		
	frm.table_type.value = document.cmtAddFrm.table_type.value
	frm.feild_id.value = document.cmtAddFrm.feild_id.value;
	frm.adm_id.value = document.cmtAddFrm.adm_id.value;
	
	var allComNum = parseInt($('.allComNum').text());
	var newComNum = allComNum + 1;
	
	$('#' + loadDivID).css('display','');
	
	$.post(  
            url,
           $(frm).serialize(),  
           function(data){ 
		   	   
		   	   replyCancel(mainCmtID);
			   $('.allComNum').text(newComNum);
			   $('.cmtCount').html(newComNum);
               $('#' + divID).append(data);
			   $('#' + loadDivID).css('display','none');
           }  
       ); 
	   
		  
}
function replyCmnt(mainCmtID){
	
	url = siteDomain+'smodule.php?action=commentReply&mainCmdID='+mainCmtID;
	
	$('#replyCmntLoad'+mainCmtID).css('display','');
	
	$.ajax({
		type: "POST",
		url: url,
		data: 'page='+url,
		dataType: "html",
		success: function(msg){
			
			if(parseInt(msg)!=0)
			{
				$('#replyCmntDiv'+mainCmtID).html(msg);
				$('#replyCmntLoad'+mainCmtID).css('display','none');
				
				$('#' + 'replyDivID' + mainCmtID).removeClass('replyDivBtnM');
				$('#' + 'replyDivID' + mainCmtID).addClass('replyDivBtnOn');
				
				$('#comTxtAr' + mainCmtID).focus();
				
	
			}
		}
		
	});	
}
function replyCancel(mainCmtID){
	$('#' + 'replyCmntDiv' + mainCmtID).html('');
	$('#' + 'replyDivID' + mainCmtID).removeClass('replyDivBtnOn');
	$('#' + 'replyDivID' + mainCmtID).addClass('replyDivBtn');
}

function ajaxFaqPost(url,divID,loadDivID,frm)
{	
	
	$('#' + loadDivID).css('display','');
	
	$.post(
            url,
           $(frm).serialize(),  
           function(data){ 
		   
		   	   $('#frmName').val("");
			   $('#frmMail').val("");
			   $('#frmAsq').val("");
			   $('#frmCode').val("");
			   
			   $('.' + divID).html(data);

			   $('#' + loadDivID).css('display','none');
			   $('#capImgID').attr('src',siteDomain+'index.php?staticLink=securityImg&sid='+Math.random());			   
           }  
       ); 
	
		  
}


