$(document).ready(function(){

	$('.captcha img').click(function(){
		
		$(this).attr('src', '/site_img/google_captcha.php?rand='+Math.random()*1000);
	
	});
	
	
	$('#add_wall_post').toggle(
		function(){
			$('#wall_post_form').slideDown('fast');
		}, 
		function(){
			$('#wall_post_form').slideUp('fast');
		}
	);
	
	
	$('.question_title').click(function(){
	
		answer = $(this).parent('li').find('.answer');
	
		if(answer.css('display') == 'none'){
			
			answer.slideDown('fast');
			
			$(this).css('background-image','url(../site_img/close_faq.gif)');
			
		}else{
			
			answer.slideUp('fast');
			
			$(this).css('background-image','url(../site_img/open_faq.gif)');
			
		}
	
	});
	
	
	if(window.location.href.indexOf('user_wall_post') > 0){
		$('#wall_post_form').css('display', 'block');
		$('#comment_message').focus();
	}
	
});



function video_vote(video_id, vote_num){
	
	$.ajax({
		url: '/video/video_vote/',
		type: 'POST',
		data: ({videoId : video_id, voteNum: vote_num}),
		success: function(data) {
			if(data == 2){
				showDisable({id: 'auth', width: 350, height: 100, closeButton: 1})
			}else if(data == 1){
				showDisable({id: 'alert', width: 200, height: 70, closeButton: 0});
				
				temp = $('#vide_rait_'+video_id).text()*1;
				
				$('#vide_rait_'+video_id).text(temp+vote_num);
				
			}else if(data == 3){
				showDisable({id: 'alert_already_vote', width: 200, height: 85, closeButton: 0})
			}
		}
	});	

}


function removeDisable(){
	$('.disable_screen').remove();
	$('.layer_content_wrap').remove();
}

function showDisable(options){
	
	if($('.layer_content_wrap').length >= 1) return false;
	
	if(options.width)
		popUpWidth = options.width;
	else
		popUpWidth = 100;
		
	if(options.height)
		popUpHeight = options.height;
	else
		popUpHeight = 100;	
	
	if(options.closeButton){
		if(options.closeButton == 1)
			closeButton = true;
		else
			closeButton = false;
			
	}else
		closeButton = false;
	
	
	popUpLeft = (getDocumentWidth()/2)-(popUpWidth/2);
	popUpTop = (getDocumentHeight()/2)-(popUpHeight/2);
	
	content = '';
	content = $('#'+options.id).html();
	
	//
	
	tempContent = '<div class="layer_content_wrap" style="width:'+popUpWidth+'px;height:'+popUpHeight+'px; left:'+popUpLeft+'px; top:'+popUpTop+'px">';
	if(closeButton == true) tempContent += '<div class="close_window"><span onclick="removeDisable()">Закрыть</span></div>';
	tempContent += content+'</div>';
	
	$('body').prepend('<div class="disable_screen" onclick="removeDisable()"></div>');
	
	$('.disable_screen').css('height', document.documentElement.scrollHeight+'px');
	
	$('body').prepend(tempContent);
	
	$('.disable_screen').animate({opacity: 0.4}, 50);
	//$('body').css('overflow','hidden');
}

function getDocumentHeight(){
	
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	
}

//Размер документа по горизонтали
function getDocumentWidth(){
	
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;

}



function addVideoPopUp2(){

	window.open('/dev_profile/add_video/', 'add_video','width=400,height=300,toolbar=0')  

}


function emoticon(text) {  
	
	var txtarea = document.getElementById('comment_message');  
	
	text = ' ' + text + ' ';  
	
	cursorPos = getCaretPos(txtarea);
	
	first = txtarea.value.slice(0,cursorPos)+' ';
	second = ' '+txtarea.value.slice(cursorPos);
	
	txtarea.value = first+text+second;
	
	
	txtarea.focus(); 
	pos = first.length+1+text.length;
	if (txtarea.createTextRange)
	{
		// IE
		var range = txtarea.createTextRange();
		range.collapse();
		range.move('character', pos);
		range.select();
	}
	else if (txtarea.selectionEnd)
	{
		// Gecko
		//txtarea.selectionStart = pos;
		txtarea.selectionEnd = pos;
	}

}  

function getCaretPos(obj)
{
  obj.focus();
  
  if(obj.selectionStart) return obj.selectionStart;//Gecko
  else if (document.selection)//IE
  {
	 var sel = document.selection.createRange();
	 var clone = sel.duplicate();
	 sel.collapse(true);
	 clone.moveToElementText(obj);
	 clone.setEndPoint('EndToEnd', sel);
	 return clone.text.length;
  }
  
  return 0;
}


function openFullRules(){
	
	window.open('/rules/full/','rules','width=940,height=500,toolbar=0,scrollbars=1')  

}


function checkUserNick(){
	
	if($('input[name="usr_login"]').val() == ''){
		$('#ckeck_nick_res').html('&nbsp;&nbsp;&nbsp;Введите Ник для проверки');	
		$('#ckeck_nick_res').css('color', '#6c080a');
		return false;
	}
	
	$.post("/registration/check_nick/", { value: $('input[name="usr_login"]').val() },
	  function(data){
			if(data == 0){
				$('#ckeck_nick_res').html('&nbsp;&nbsp;&nbsp;Этот Ник свободен');	
				$('#ckeck_nick_res').css('color', '#109506');
			}else{
				$('#ckeck_nick_res').html('&nbsp;&nbsp;&nbsp;Этот Ник аанят');	
				$('#ckeck_nick_res').css('color', '#6c080a');
			}
	  });

}

function checkUserEmail(){
	
	if($('input[name="usr_email"]').val() == ''){
		$('#ckeck_email_res').html('&nbsp;&nbsp;&nbsp;Введите E-mail для проверки');	
		$('#ckeck_email_res').css('color', '#6c080a');
		return false;
	}
	
	if(!isValidEmail($('input[name="usr_email"]').val())){
		$('#ckeck_email_res').html('&nbsp;&nbsp;&nbsp;Введите корректный E-mail для проверки');	
		$('#ckeck_email_res').css('color', '#6c080a');
		return false;
	}
	
	$.post("/registration/check_email/", { value: $('input[name="usr_email"]').val() },
	  function(data){
			if(data == 0){
				$('#ckeck_email_res').html('&nbsp;&nbsp;&nbsp;Этот E-mail свободен');	
				$('#ckeck_email_res').css('color', '#109506');
			}else{
				$('#ckeck_email_res').html('&nbsp;&nbsp;&nbsp;Этот E-mail аанят');	
				$('#ckeck_email_res').css('color', '#6c080a');
			}
	  });

}


function isValidEmail (email)
{
	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}



function deleteMessage(id)
{

	if(confirm('Вы уверены что хотите удалить сообщение?'))
	{
		
		currentMessage = id;
	
		$.post("/profile/delete_message/", { id: id },
			function(data){
				
				if(data == 0){
					alert('Error');
				}else{
					showDisable({id: 'delete_message', width: 250, height: 100, closeButton: 1});
				}
				
			});
		
	}

}

function cancelDeleteMessage()
{

	$.post("/profile/cancel_delete_message/", { id: currentMessage },
		function(data){
			
			currentMessage = 0;
			
			removeDisable();
			
		});

}

function confirmDeleteMessage()
{
	
	removeDisable();
	
	window.location.reload();

}


