/**
*
* Show window for comments window
*
* @name showCommentsWindow
* @last 14.08.2009
*
**/
function showCommentsWindow(uid,fstat){

 if (!(fstat > 0)) {

   showPopUpWindow('guiAlertWindow');
  jQuery('#guiDivMessage').html('<p>Для написания Личного Сообщения Вам необходимо заполнить все обязательные поля в <a href="/user/profile-edit" title="Личный профиль">вашем&nbsp;личном&nbsp;профиле</a></p>');
  return false;
 }

 var dialog  = jQuery('#popUpWindow');
 var options = {
                containerId : 'modalBigContainer',
                onShow : function() {}
               };
jQuery('#persmsg').html('<a class="userLog-new-mail" style="visibility: hidden;" href="#" </a>');     
 jQuery('#popUpWindow').html(arrWindow['guiMessages']);
 dialog.modal(options);
 initCommentsWindow(uid);
}

jQuery(function($) {
 if (jQuery('#guiRbnDayOfTheWeddingNone').attr('id') == 'guiRbnDayOfTheWeddingNone') {
  var stat = jQuery("#profileForm input:radio[@name='guiRbnDayOfWedding']:checked").val();

  jQuery('#guiRbnDayOfTheWeddingNone').click(function(){
   jQuery('#guiWedInPast').animate({ opacity: "hide" }, 1500);
  
   jQuery('#guiWedInFuture').animate({ opacity: "hide" }, 1500);
  });

  jQuery('#guiRbnDayOfTheWeddingInFuture').click(function(){
   jQuery('#guiWedInPast').animate({ opacity: "hide" }, 500,function(){
    jQuery('#guiWedInFuture').animate({ opacity: "show" }, 500);
   });
  });

  jQuery('#guiRbnDayOfTheWeddingInPast').click(function(){
   jQuery('#guiWedInFuture').animate({ opacity: "hide" }, 500,function(){
    jQuery('#guiWedInPast').animate({ opacity: "show" }, 500);
   });
  });

  switch(stat){
   case '1':
    jQuery('#guiRbnDayOfTheWeddingNone').click();
    break;

   case '2':
    jQuery('#guiRbnDayOfTheWeddingInFuture').click();
    break;

   case '3':
    jQuery('#guiRbnDayOfTheWeddingInPast').click();
    break;
  }
 }
 
	$('#guiRbnWorkInIndustryAdd').click(function() {
		$('#guiRbnWorkInIndustrySection').animate({ opacity: "show" }, 500);
	});
	$('#guiRbnWorkInIndustryNone').click(function() {
		$('#guiRbnWorkInIndustrySection').animate({ opacity: "hide" }, 500);
	});
});


/**
*
* delete all messages
*
* @name deleteAllMessages
* @author Sergey Donchenko <sergey.donchenko@gmail.com>
* @last 27.08.2009 Sergey Donchenko
*
**/
function deleteAllMessages(uid){
 if (confirm("Вы действительно хотите удалить всю переписку с пользователем?")) {
  var option = {};

  option['userid'] = uid;

  jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/delete-personal-messages",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (data.code == 0) {
                      location.reload(true);
                     }
		            },
    	 error    : function (){}
		});
  }
}

/**
*
* show moderators fields for answer on question
*
* @name showModeratorTextArea
* @author Sergey Donchenko <sergey.donchenko@gmail.com>
* @last 29.08.2009 Sergey Donchenko
*
**/
function showModeratorTextArea(messageId){
 if (!messageId) {return;}

 var form = '<textarea rows="10" cols="90" id="guiStrModeratorAnswer'+messageId+'" name="guiStrModeratorAnswer'+messageId+'" class="textarea"></textarea>'
          + '<br /><br />'
		  + '<input type="button" value="Ответить" class="inputButton" onclick="answerModerator('+messageId+')"/>';

 jQuery('#guiDivAreaAnswer'+messageId).html(form);
 jQuery('#guiDivAreaAnswer'+messageId).show();
 jQuery('#guiStrModeratorAnswer'+messageId).focus();
}

/**
*
* answer moderators
*
* @name answerModerator
* @author Sergey Donchenko <sergey.donchenko@gmail.com>
* @last 29.08.2009 Sergey Donchenko
*
**/
 function answerModerator(mId){
  if (!mId) {return;}
  var option = {};
  var mess   = jQuery('#guiStrModeratorAnswer'+mId).attr("value");

  if (!mess.length) {
  	alert("Поле с ответом не должно быть пустым!");
  	jQuery('#guiStrModeratorAnswer'+mId).focus();
  	return;
  }

  option['messageid'] = mId;
  option['message']   = mess;

  jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/add-moderator-answer",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (data.code == 0) {
                      jQuery('#guiDivAreaAnswer'+mId).hide();
                      location.reload(true);
                     }
		            },
    	 error    : function (){}
		});
 }
/**
 *
 * initialization for comments window
 *
 * @name initCommentsWindow
 * @author Sergey Donchenko <sergey.donchenko@gmail.com>
 * @last 14.08.2009 Sergey Donchenko
 *
 **/
function initCommentsWindow(uid){
 // if user id not set's
 if (!(uid > 0)) {return;}

 var arrComments = {};

 jQuery('#commentsForm').validate({
      submitHandler: function(form) {
       var mess = jQuery('#guiTxtMessageBody').val();

       if (!mess) {return;}

       arrComments['messBody'] = mess;
       jQuery('#guiTxtMessageBody').val("");
       jQuery('#guiTxtMessageBody').focus();

       var options = {
                      /*timeout: 10000,*/
				      type: 'POST',
				      url: '/user/add-personal-message',
				      data: arrComments,
				      dataType: 'json',
				      success: function(data){
                        jQuery('#errorTr').hide(100);
				        if (data.code > 0) {
				          message = data.message;
				          jQuery('#guiMessage').html(message);
					      jQuery('#errorTr').show(500);
				        }else{
                              // alert('Все круто');

                              jQuery('#guiTxtMessageBody').val("");
                              jQuery('#guiTxtMessageBody').focus();
                              jQuery('#guiPersonalMessageList').html(data.list);
                              jQuery('#guiPersonalMessageList').scrollTop(jQuery('#messages').height()+10);
					         }


					//   setTimeout("closeModal();", 2500);

					},
				    error: function() {}
	   };

	   jQuery(form).ajaxSubmit(options);
	  }
  });
  arrComments['userid']          = uid;
  jQuery('#guiImgMessageStatus' + uid).attr('src','/public/img/mailGrayNoAnimated.gif');
  loadMessageList(uid);
}

/**
 * Load users comment
 *
 * @name loadMessageList
 * @author Sergey Donchenko <sergey.donchenko@gmail.com>
 * @last 14.08.2009 Sergey Donchenko
 * @params
 *         uid - user's identificator
 *
 */

function loadMessageList(uid){
 if (!parseInt(uid)) {return;}

 var option = {};

 option['userid'] = uid;

 jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/get-personal-messages",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (!(data.code > 0)) {
                       jQuery('#guiPersonalMessageList').html(data.list);
					   //set user block
					   jQuery('#guiTdUserBlock').animate({ opacity: "hide" }, 1500, function(){
					    jQuery('#guiTdUserBlock').html(data.block);
					    jQuery('#guiTdUserBlock').animate({ opacity: "show" }, 1500);
					   });

                     }
                     jQuery('#guiPersonalMessageList').scrollTop(jQuery('#messages').height()+10);
		            },
    	 error    : function (){}
		});

}

/**
 * Check interest person and add to own profile
 *
 * @name addInterestingPerson
 * @author Sergey Donchenko <sergey.donchenko@gmail.com>
 * @last 13.08.2009 Sergey Donchenko
 * @params
 *         uid - user's identificator
 *
 */
function addInterestingPerson(uid){
  if (!parseInt(uid)) {
   return;
  }

  jQuery('img[name=guiDivGrayHeart' + uid + ']').attr('disabled',true);
  var option = {};

  option['userid'] = uid;

  jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/add-intersting-person",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (!(data.code > 0)) {
                       jQuery('form img[name=guiDivGrayHeart' + uid + ']').attr('src','/img/red-heart-comment.gif');
                       jQuery('form img[name=guiDivGrayHeart' + uid + ']').removeAttr('onclick').unbind("click");
                       jQuery('form img[name=guiDivGrayHeart' + uid + ']').bind('click',function(){
                                                                       removeFromInterestingPerson(uid);
					                                                 });
                       jQuery('form img[name=guiDivGrayHeart' + uid + ']').css({cursor:'hand', cursor:'pointer'});
                       jQuery('img[name=guiDivGrayHeart' + uid + ']').attr('disabled',false);
                     }
		            },
    	 error    : function (){}
		});
}

/**
 * Check interest person and add to own profile
 *
 * @name addInterestingPerson
 * @author Sergey Donchenko <sergey.donchenko@gmail.com>
 * @last 13.08.2009 Sergey Donchenko
 * @params
 *         uid - user's identificator
 *
 */
function removeFromInterestingPerson(uid){
 if (!parseInt(uid)) {
  return;
 }

 jQuery('img[name=guiDivGrayHeart' + uid + ']').attr('disabled',true);
 var option = {};

 option['userid'] = uid;

 jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/remove-from-intersting-person",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (!(data.code > 0)) {
                       jQuery('img[name=guiDivGrayHeart' + uid + ']').attr('src','/img/gray-heart-comment.gif');
                       jQuery('form img[name=guiDivGrayHeart' + uid + ']').removeAttr('onclick').unbind("click");
                       jQuery('form img[name=guiDivGrayHeart' + uid + ']').css({cursor:'hand', cursor:'pointer'});
					   jQuery('form img[name=guiDivGrayHeart' + uid + ']').bind('click',function(){
                                                                       addInterestingPerson(uid);
					                                                 });
                      jQuery('img[name=guiDivGrayHeart' + uid + ']').attr('disabled',false);
                     }
		            },
    	 error    : function (){}
		});
}

/**
 *
 * Show window for complaint to moderators
 *
 * @name showComplaintWindow
 * @author Sergey Donchenko <sergey.donchenko@gmail.com>
 * @last 13.08.2009 Sergey Donchenko
 *
 */
function showComplaintWindow(uid,corn){
 var dialog  = jQuery('#popUpWindow');
 var options = {};

 jQuery('#popUpWindow').html(arrWindow['guiComplaint']);

 dialog.modal(options);
 initComplaintWindow(uid,corn);
}

/**
 * initialization for complaint window
 *
 * @name initComplaintWindow
 * @author Sergey Donchenko <sergey.donchenko@gmail.com>
 * @last 13.08.2009 Sergey Donchenko
 *
 */
function initComplaintWindow(id_user,corn){
 var arrCompl = {};

 jQuery('#complaintForm').validate({
                 submitHandler: function(form) {
                    arrCompl['body'] = jQuery('#guiTxtBodyCompl').attr("value");
                    arrCompl['page'] = jQuery('#guiHrefUrlPage').attr("href");

					if (parseInt(corn) > 0) {
                     arrCompl['page'] += '#corn'+corn;
					}
					arrCompl['uid']  = id_user;

                    var options = {
                          /*timeout: 10000,*/
				          type: 'POST',
				          url: '/user/add-complaint',
				          data: arrCompl,
				          dataType: 'json',
				          success: function(data){
				           if (data.code > 0) {
				           	message = data.message;
				           }else{
				                 message = "Ваша жалоба принята и в ближайшее время будет расмотрена.";
						        }

						   jQuery('#guiMessage').html(message);
						   jQuery('#errorTr').show(100);

						   setTimeout("closeModal();", 1500);
						  },
				          error: function() {}
					};

                    jQuery(form).ajaxSubmit(options);
				 }
 });
}


/**
 * Show confirm window for for delete from InterestingPerson
 *
 * @name showConfirmWindow
 * @author Batechko Dmitry <ReptileDB@bigmir.net>
 * @last 14.08.2009 Batechko Dmitry
 *
 */
function showConfirmWindow(uid){
 var dialog  = jQuery('#popUpWindow');
 var options = {};


 jQuery('#popUpWindow').html(arrWindow['guiConfirmForm']);

 jQuery('#id_record').val(uid)
 dialog.modal(options);
}
/**
 * submit confirm window for for delete from InterestingPerson
 *
 * @name submitConfirmWindow
 * @author Batechko Dmitry <ReptileDB@bigmir.net>
 * @last 14.08.2009 Batechko Dmitry
 *
 */
function submitConfirmWindow(){

 jQuery('#id_rec').val(jQuery('#id_record').val());
 jQuery('#guiFrmFavoritePerson').submit();

}

/**
 *
 * @access public
 * @return void
 **/
function setUpBallParams(uid){
 if (!(parseInt(uid) > 0)) {
  return false;
 }

 var tp = jQuery('form :radio[name=guiAdminSetBalls]:checked').val();

 if (tp == 0) {
  //Добавление баллов
  setupuserbonusballs(uid);
 }else{
        setupuserstatus(uid);
      }
}
/**
 * set user status
 *
 * @name ssetupuserstatus
 * @author Donchenko Sergey <sergey.donchenko@gmail.com>
 * @last 16.08.2009 Donchenko Sergey
 *
 */
function setupuserstatus(uid){
 if (!parseInt(uid)) {
   return;
  }

  var option = {};

  option['userid'] = uid;
  option['status'] = jQuery('select[@name=guiSelSetAdminStatus] option:selected').val();
  jQuery('#guiBtnSetBonusBalls').attr("disabled", true);

  jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/set-status-for-user",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (!(data.code > 0)) {
                       jQuery('#guiImgStatusBlock').attr('src','/img/'+data.newstat);
                       alert('Статус успешно установлен!');
                       location.reload(true);
                     }
                   //  jQuery('#guiBtnSetBonusBalls').attr("disabled", false);
		            },
    	 error    : function (){}
		});
}

/**
 * set bonus balls
 *
 * @name setupbonusballs
 * @author Donchenko Sergey <sergey.donchenko@gmail.com>
 * @last 16.08.2009 Donchenko Sergey
 *
 */
function setupuserbonusballs(uid){

 if (!parseInt(uid)) {
   return;
 }

 var option = {};

 option['userid']      = uid;
 option['bonus_balls'] = jQuery('#guiStrBonus').val();

 jQuery('#guiBtnSetBonusBalls').attr("disabled", true);

 jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/set-bonus-balls",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (!(data.code > 0)) {
                       jQuery('#guiStrBonus').val(option['bonus_balls']);
                       alert('Баллы успешно установлены.');
                      // jQuery('#guiBtnSetBonusBalls').attr("disabled", false);
                       location.reload(true);
                     }
		            },
    	 error    : function (){}
		});
}

/**
*
* Set up user show  gallery status
*
* @name setUpUserShowGalleryStatus
* @author Sergey Donchenko <sergey.donchenko@gmail.com>
* @last 14.08.2009 Sergey Donchenko
*
**/
function setUpUserShowGalleryStatus(obj){
 var option = {};

 if (jQuery(obj).attr('checked')) {
 	option['status'] = '1';
 }else{
       option['status'] = '0';
      }

 jQuery(obj).attr("disabled", true);

 jQuery.ajax({
    	 type     : "POST",
    	 url      :  "/user/user-gallery-show-status",
    	 async    : false,
    	 data     : option,
    	 dataType : "json",
    	 success  : function(data){
                     if (!(data.code > 0)) {
                       jQuery(obj).attr("disabled", false);
                     }
		            },
    	 error    : function (){}
		});

}
/* ========================================================================= */

/**
* Client-side validation
*/
/**
* Location select inputs
*/
jQuery( '#guiTownSelect, #guiTownSelectCat' ).live( 'change', function () {
	if( jQuery(this).val() == 0 ) {
		jQuery(this).closest('table').parent().prev().prev().find('.top_red_star').show();
		jQuery(this).closest('table').parent().prev().css('padding-top', '0px');
	}
	else {
		jQuery(this).closest('table').parent().prev().prev().find('.top_red_star').hide();
		if( jQuery(this).attr('id') == 'guiTownSelectCat' ) {
			jQuery(this).closest('table').parent().prev().css('bottom', '0px');
		}
		else {
			//jQuery(this).closest('table').parent().prev().css('padding-top', '21px');
		}
	}
});
/**
* Simple inputs
*/
jQuery( '#guiStrEmployment, #guiDtBirth, #guiStrEmploymentLife, #guiStrHobby, #guiStrFirmDescr, #guiChapterSelectCat, #guiStrShortDescr,#editorTextArea, #guiStrPhone, #guiStrWeb, #guiStrEMail, #guiFirstnameSrch, #guiLastnameSrch, #guiStrKeyWords, #guiFirmnameSrch,#minSum, #midleSum, #maxSum, #guiSelSigment' ).live( 'change', function () {
	if( jQuery(this).val() == '' ) {
		jQuery(this).parent().prev().prev().find('.top_red_star').show();
		jQuery(this).parent().prev().prev().find('.annot').css('bottom', '15px');
	}
	else {
		jQuery(this).parent().prev().prev().find('.top_red_star').hide();
		jQuery(this).parent().prev().prev().find('.annot').css('bottom', '2px');
		jQuery(this).parent().prev().prev().css('padding-top', '25px');
	}

});
jQuery(' #guiChapterSelectCat').live('change', function() {
	if( jQuery(this).val() == '' ) {
		jQuery("#guiChapterSelectCat").closest('table').parent().prev().find('.top_red_star').show();
		//jQuery(this).closest('table').parent().prev().css('padding-top', '0px');
	}
	else {
		jQuery("#guiChapterSelectCat").closest('table').parent().prev().find('.top_red_star').hide();
		//jQuery(this).parent().parent().prev().find('.annot').css('bottom', '2px');
		//jQuery(this).parent().parent().prev().css('padding-top', '25px');
	}
});

jQuery(' #guiRbnWorkInIndustryNone').live('click', function() {
	if( jQuery(this).val() == '' ) {
		jQuery("#guiChapterSelectCat").closest('table').parent().prev().find('.top_red_star').show();
		//jQuery(this).closest('table').parent().prev().css('padding-top', '0px');
	}
	else {
		jQuery("#guiChapterSelectCat").closest('table').parent().prev().find('.top_red_star').hide();
		//jQuery(this).parent().parent().prev().find('.annot').css('bottom', '2px');
		//jQuery(this).parent().parent().prev().css('padding-top', '25px');
	}
});
jQuery(' #guiRbnWorkInIndustryAdd').live('click', function() {	
	if( jQuery('#guiChapterSelectCat').val() == 0 ) {
		jQuery("#guiChapterSelectCat").closest('table').parent().prev().find('.top_red_star').show();	
	}
	else {
		jQuery("#guiChapterSelectCat").closest('table').parent().prev().find('.top_red_star').hide();		
	}
});

jQuery('#editorTextArea').live( 'change', function() {
    if( jQuery(this).val() == '' ) {
		jQuery('#editorTextArea').closest('table').prev().prev().show();
	}
	else {
		jQuery('#editorTextArea').closest('table').prev().prev().hide();		
	}
});
//jQuery('#guiDtBirth').live('change', function() {alert('asdasda');});


