var popupStatus = 0;

function loadPopup(){

	$("#popup .clear").show();
	$("#write_message .textarea").show();


	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").show();
		$("#popup").show();
		popupStatus = 1;
	}
}

function disablePopup(){

	$(".select").removeClass("on");

	if(popupStatus==1){
		$("#backgroundPopup").hide();
		$("#popup").hide();
		popupStatus = 0;
	}
	$(".sub_popup, #backgroundSubPopup").hide();
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
	var windowScroll = $(window).scrollTop();
	$("#popup").css({
		"position": "fixed",
		"top": (windowHeight/2-popupHeight/2),
		"left": windowWidth/2-popupWidth/2
	});
}


function initPopup(){

	$("#link_register, #link_login, #socw_login").click(function(){
		var id = $(this).attr("id").substr(5);
		$(".popup").hide();
		$("#" + id).show();
		centerPopup();
		loadPopup();
		return false;
	});
	
	$(".material a").click(function(){
		var id = $(this).attr("rel").substr(5);
		$(".popup").hide();
		$("#" + id).show();
		centerPopup();
		loadPopup();
		return false;
	});
	

	/*
	$(".photos_head a").click(function(){
		var id = $(this).attr("rev");
		$(".popup").hide();
		$("#" + id).show();
		centerPopup();
		loadPopup();
		return false;
	});
	*/

	$(".link_feedback").click(function(){
		var id = $(this).attr("class").substr(5);
		$(".popup").hide();
		$("#" + id).show();
		centerPopup();
		loadPopup();
		return false;
	});
	
	$("#popup .forgot").click(function(){
		disablePopup();
		var id = $(this).attr("class");
		$(".popup").hide();
		$("#" + id).show();
		centerPopup();
		loadPopup();
		return false;
	});
	
	$(".link_forgot_message").click(function(){
		disablePopup();
		var id = $(this).attr("class").substr(12);
		$(".popup").hide();
		$("#" + id).show();
		centerPopup();
		loadPopup();
		return false;
	});

	$("#backgroundPopup, .popup_close, .cancel").click(function(){

		disablePopup();
	});
	
	$("#backgroundSubPopup, .sub_popup_close, .cancel").click(function(){
		$(".sub_popup, #backgroundSubPopup").hide();
	});
	
	$(document).keydown(function(e){
		if(e.which==27 && popupStatus==1){
			disablePopup();
		}
	});


}

function showPopup(id){

	$(".popup").hide();			
	$("#"+id+".popup").show();
	
	centerPopup();
	loadPopup();
	return false;	
}

function alertPopup( text, title ){
	
	if( !text ){
		return false;
	}
	
	title = title || 'Внимание!';
	
	// Если есть открытый попап то показываем sub_popup
	if( popupStatus === 1 ){ 
	
		$(".sub_popup").hide();
		$("#backgroundSubPopup").show().css({
			"opacity": "0.7"
		});
		
		$(".sub_popup .head span#sub_popup_title").html(title);
		$(".sub_popup .popup_body span.name").html(text);
		
		
		$(".sub_popup").show();
		$(".sub_popup").ahAlign();
		$(".sub_popup").avAlign();
	
	} else {
		
		//Если нет открытого попапа показываем простой попап с текстом
		$('#simplePopup .head span#sub_popup_title').html(title);
		$("#simplePopup .popup_body span.name").html(text);
		showPopup('simplePopup');
	}
	
}


$(document).ready(function(){

	initPopup();
		
});
