/**
 * @author sasha
 * Frontend	events
 */
$(document).ready(function(){
$("#port-all").click(function(event){
    event.preventDefault();
});
timedMsg();
$('#notification-message').click(function(event){
    $('#notification-message').fadeOut('slow');
});
$(".faq_answer").hide();

$("#secondary-menu li:first-child a").addClass("first");
$("#secondary-menu li:last-child a").addClass("last");
$(".box-tools div:last-child").addClass("last");

$(".MsoNormal").attr('style', '').removeClass('MsoNormal');
$("a[href='#']").attr("href", "javascript:void(0)");

var UsernameLogin = $("#UsernameLogin").val();
$("#UsernameLogin").focus(function(event){
    if ($(this).val() == UsernameLogin)
    {
        $(this).val('');
    }
});
$("#UsernameLogin").blur(function(event){
    if ($(this).val() == '')
    {
        $(this).val(UsernameLogin);
    }
});
     var $radios = $("input[type=radio]");
     if($radios.not('.radio')) {
              $radios.addClass("radio");
     }

$(".faq_link").click(function(event){
    var $answer = $(this).next();
    $answer.slideToggle("fast");
    if ($(this).is(".activated")) 
    {
        $(this).removeClass("activated")
    } else {
        $(this).addClass("activated")
        
    }
});

/**
 * Schimba varstele, inaltimile si greutatile in functie de valoare cealalta
*/

bindRanges('SearchAge');
bindRanges('SearchHeight');
bindRanges('SearchWeight');

if($('#UserProfileChildren101').attr('checked')){
         $('#UserProfileChildrenNo').attr({'disabled':'disabled'});
         $('#UserProfileChildrenLiving').attr({'disabled':'disabled'});
}

$('#UserProfileChildren101').click(function(e){
         if($(this).attr('checked')){
                  $('#UserProfileChildrenNo').attr({'disabled':'disabled'});
                  $('#UserProfileChildrenLiving').attr({'disabled':'disabled'});
         }         
});

$('#UserProfileChildren100').click(function(e){
         if($(this).attr('checked')){
                  $('#UserProfileChildrenNo').attr({'disabled':''});
                  $('#UserProfileChildrenLiving').attr({'disabled':''});
         }
});

$('#UserProfilePets5').click(function(e){
         $('.user-profile-pets').attr({'checked': ''});
         $(this).attr({'checked': 'checked'});
});
$('.user-profile-pets').click(function(e){
         if($(this).attr('id') != 'UserProfilePets5'){
                $('#UserProfilePets5').attr({'checked': ''});
         }
});

$('#UserProfileCountryCode').change(function(e){
if($(this).val() != 'ROM'){
    $('#UserProfileCityId').parent().hide();
    $("#UserProfileCityId").rules("remove", "required");
}else{
    $('#UserProfileCityId').parent().show();
    $("#UserProfileCityId").rules("add", "required");
}
});


$('.recommend-modal-link').click(function(e){
	e.preventDefault();
	Boxy.load('/messages/recommend/', {'title': 'Recomanda prietenilor tai', 'id': 'BoxyRecommend'})
});

});

function submit_recommend(event){
	event.preventDefault();
	var dataStr = '';
	$('.UserRecommendationEmail').each(function(){
		dataStr += $(this).attr('name') + "=" + $(this).val() + '&';
	});
	$.ajax({  
	  type: "POST",  
	  url: "/messages/recommend",
	  data: dataStr,
	  success: function(data) {
		if(data == 'OK')
			window.location = '/';
		else
			$('form.boxy-content').replaceWith(data);
	  }
	});
}

function bindRanges(fieldName) {
	$('#' + fieldName + 'Start').change(function(){
			 var start = $(this).val();
			 var end = $('#' + fieldName + 'End').val();
			 if(start >= end){
					  new_val = parseInt(start) + 1;
					  if($('#' + fieldName + 'End option[value=\''+ new_val +'\']').length){
							   $('#' + fieldName + 'End option[value='+ new_val +']').attr({'selected':'selected'});
					  }else{
							   $("#" + fieldName + "End option[value='']").attr({'selected':'selected'});
					  }
			 }
	});
	$('#' + fieldName + 'End').change(function(){
			 var end = $(this).val();
			 var start = $('#' + fieldName + 'Start').val();
			 if(start >= end){
					  new_val = parseInt(end) - 1;
					  if($('#' + fieldName + 'Start option[value=\''+ new_val +'\']').length){
							   $('#' + fieldName + 'Start option[value=\''+ new_val +'\']').attr({'selected':'selected'});
					  }else{
							   $("#" + fieldName + "Start option[value='']").attr({'selected':'selected'});
					  }

			 }
	});
}

function closemodal() {
	$('#modal-background').fadeOut('fast');
	$('.modal-window').fadeOut('fast');
	$('select').show();
}
