/*****************************
**     Popup message
******************************/

//close pop-up box
function closePopup()
 {
   $('#opaco').toggleClass('hidden').removeAttr('style');
   $('#popup').toggleClass('hidden');
   return false;
 }

//open pop-up
function showPopup()
 {
   //when IE - fade immediately
   if($.browser.msie)
   {
     $('#opaco').height($(document).height()).toggleClass('hidden');
   }
   else
   //in all the rest browsers - fade slowly
   {
     $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7);
   }

   $('#popup')
     .html($('#popup_bug').html())
     .alignCenter()
     .toggleClass('hidden');
   return false;
 }

$(document).ready(function(){
   //align element in the middle of the screen
   $.fn.alignCenter = function() {
      //get margin left
      var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
	  marginLeft = '0px';
      //get margin top
      var marginTop = Math.max(40, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
	  marginTop = '40px';
      //return updated element
      return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
   };
});



function checkSF() {
	if ($("#s").val() == 'Բառը, հետո սեղմեք Enter')  $("#s").val(""); 
}

function checkSB() {
	if ($("#s").val() == '') $("#s").val("Բառը, հետո սեղմեք Enter");
}

function CopyReg() {
$("#LoginDiv").hide("slow");
$("#RegDiv").show("slow");
$("#ForgotDiv").hide("slow");
}
function CopyEnter() {
$("#LoginDiv").show("slow");
$("#RegDiv").hide("slow");
$("#ForgotDiv").hide("slow");
}
function CopyForgot() {
$("#LoginDiv").hide("slow");
$("#RegDiv").hide("slow");
$("#ForgotDiv").show("slow");
}

//ajax login
function ajaxEnter() {

	var userLogin = $("#userLogin").val();
	var userPass = $("#userPass").val();
	
	var LoginDiv = $("#LoginDiv").html();
	$("#LoginDiv").html("<img src='/s/i/loading.gif' style='float:right;margin:10px 20px;'/>");

   $.post("/ajax/ajaxEnter.php", { login: userLogin, pass: userPass },
   function(data) {
	   if (data == "ok") {
		$("#LoginDiv").html("Դուք բարեհաջող մուտք եղաք:");
		setTimeout(function() { location.reload(); }, 1000);
	} else {
     alert(data);
	 $("#LoginDiv").html(LoginDiv);
	}
	 
   });

return false;
}



//ajax Reg
function ajaxReg() {

	var userRegLogin = $("#userRegLogin").val();
	var userRegMail = $("#userRegMail").val();
		
	var RegDiv = $("#RegDiv").html();
	$("#RegDiv").html("<img src='/s/i/loading.gif' style='float:right;margin:10px 20px;'/>");

   $.post("/ajax/ajaxReg.php", { userRegLogin: userRegLogin, userRegMail: userRegMail },
   function(data) {
	   if (data == "ok") {
		$("#RegDiv").html(RegDiv);
		$("#RegDiv .loginForm").html("Ստուգեք Ձեր E-mail-ը, այնտեղ կա նամակ, որով ուղարկել ենք ժամանակավոր գաղտնաբառը, որը կարող եք փոխել այդ գաղտնաբառով <a href=\"#\" onclick=\"CopyEnter();\">&quot;Մուտք&quot;</a> լինելուց հետո:");
		//setTimeout(function() { location.reload(); }, 1000);
	} else {
     alert(data);
	 $("#RegDiv").html(RegDiv);
	}
	 
   });

return false;
}



//ajax Reg
function ajaxRemind() {

	var userRemindLogin = $("#userRemindLogin").val();
	var userRemindMail = $("#userRemindMail").val();
		
	var RemindDiv = $("#ForgotDiv").html();
	$("#ForgotDiv").html("<img src='/s/i/loading.gif' style='float:right;margin:10px 20px;'/>");

   $.post("/ajax/ajaxRemind.php", { userRemindLogin: userRemindLogin, userRemindMail: userRemindMail },
   function(data) {
	   if (data == "ok") {
		$("#ForgotDiv").html(RemindDiv);
		$("#ForgotDiv .loginForm").html("Ձեր գաղտնաբառը փոփոխված է, ժամանակավոր գաղտնաբառը ուղարկված է նամակով Ձեր E-mail-ին: Դուք կարող եք փոխել այդ գաղտնաբառը նրանով <a href=\"#\" onclick=\"CopyEnter();\">&quot;Մուտք&quot;</a> լինելուց հետո:");
		//setTimeout(function() { location.reload(); }, 1000);
	} else {
     alert(data);
	 $("#ForgotDiv").html(RemindDiv);
	}
	 
   });

return false;
}

//ajax logout
function ajaxLogout() {
	$("#cpLogout").html("<img src='/s/i/loadingExit.gif' style='margin-top:5px;' />");

   $.post("/ajax/ajaxLogout.php", { login: "cpUser" },
   function(data) {
		setTimeout(function() { location.reload(); }, 1000)
   });

return false;
}


function commentReplay(id) {
	if(id) {

		$(".smilesT").css("width","430");
		$("#comment").css("width","420");
		$("#commentParentID").val(id);

		$("#commentReplayDiv")
			.appendTo("#comment-"+id);
		$("#imgReplay").css("display","inline");

	} else {

		$(".smilesT").css("width","460");
		$("#comment").css("width","450");
		$("#commentParentID").val('0');

		$("#commentReplayDiv")
			.appendTo("#commentReplayDivDefoult");
		$("#imgReplay").css("display","none");
	}
}


function leaveComment() {

	if($('#comment').val()) {

		var comVar = $('#comment').val();
		var parVar = $('#commentParentID').val();
		var postVar = $('#commentPostID').val();
		
		$('#comLoading').css('display','inline');

		$.post("/ajax/newComment.php", { comment: comVar, parentID: parVar, postID: postVar },
		function(data) {
			if(data && data!="error") {

				$('#comment').val('');

				if(parVar>0) {
					$(""+data+"").appendTo("#comment-"+parVar);
				} else {
					$(""+data+"").appendTo("#commentReplayDivDefoult");
				}
				commentReplay(0);
				$('#comLoading').css('display','none');

			} else if(data=="error") {
				alert("Մեկնաբանությունը չի ընդունվել:");
			}
		});

	}

}




// userDetails
function MyDetails(){
	showPopup();
	$('#popup .bug').css('width','700px');
	$('#popup .bug').css('height','560px');
	$('#popup .bug').css('margin','auto');
	$('#popup .bug').append("<div id='popupTh2'><h2>Իմ անձնական տվյալները</h2></div>");
	$('#popup .bug').append("<div style='width:700px;height:500px;'><p style='text-align:center;'><iframe id='DetailsFrame' name='DetailsFrame' src='/ajax/userDetails.php' style='width:660px;height:500px;border:0px solid #fff;'></iframe></p></div>");
return false;
}


// Add NewPost
function ajaxPost(pID){
	showPopup();
	$('#popup .bug').css('width','840px');
	$('#popup .bug').css('height','560px');
	$('#popup .bug').css('margin','auto');
	if(pID>0) $('#popup .bug').append("<div id='popupTh2'><h2>Փոփոխել գրառումը</h2></div>");
		else $('#popup .bug').append("<div id='popupTh2'><h2>Նոր գրառում</h2></div>");
	$('#popup .bug').append("<div style='width:840px;height:510px;'><p style='text-align:center;'><iframe id='NewPost' name='DetailsFrame' src='/ajax/ajaxPost.php?id="+pID+"' style='width:840px;height:510px;border:0px solid #fff;'></iframe></p></div>");
return false;
}


// Edit Comment
function EditComment(cID){
	showPopup();
	$('#popup .bug').css('width','540px');
	$('#popup .bug').css('height','340px');
	$('#popup .bug').css('margin','auto');
	$('#popup .bug').append("<div id='popupTh2'><h2>Փոփոխել մեկնաբանությունը</h2></div>");
	$('#popup .bug').append("<div style='width:540px;height:300px;'><p style='text-align:center;'><iframe id='EditComment' name='DetailsFrame' src='/ajax/ajaxComment.php?id="+cID+"' style='width:840px;height:510px;border:0px solid #fff;'></iframe></p></div>");
return false;
}


// Copy SMS 
function CopySMS(uID, uDN, act){
	showPopup();
	$('#popup .bug').css('width','520px');
	$('#popup .bug').css('height','350px');
	$('#popup .bug').css('margin','auto');
	$('#popup .bug').append("<div id='popupTh2'><h2>Copy SMS &#187; "+uDN+"</h2></div>");
	$('#popup .bug').append("<div style='width:540px;height:320px;'><p style='text-align:center;'><iframe id='CopySMS' name='DetailsFrame' src='/ajax/copySMS.php?id="+uID+"&act="+act+"' style='width:500px;height:320px;border:0px solid #fff;'></iframe></p></div>");
return false;
}


// Favorite List 
function FavList(){
	showPopup();
	$('#popup .bug').css('width','520px');
	$('#popup .bug').css('height','350px');
	$('#popup .bug').css('margin','auto');
	$('#popup .bug').append("<div id='popupTh2'><h2>Դուք ֆավորիտ եք նրանց մոտ :)</h2></div>");
	$('#popup .bug').append("<div style='width:540px;height:320px;'><p style='text-align:center;'><iframe id='FavList' name='DetailsFrame' src='/ajax/authorFavorite.php' style='width:500px;height:320px;border:0px solid #fff;'></iframe></p></div>");
return false;
}


// Add to favorite 
function myFavorite(uID){
	$('.authorFavorite').html("<img src='/s/i/loadingExit.gif' style='margin:0px;'/>");

	$.post("/ajax/authorFavorite.php", { id: uID },
	function(data) {
		if(data == "ok") {
			$('.authorFavorite').html("<span style='color:green'>Գործողությունը կատարված է: Շուտով կտեսնեք նրան ձեր ֆավորիտներում:</span>");
		} else {
			$('.authorFavorite').html("<span style='color:red'>"+data+"</span>");
		}
	});
}


// Delete from my favorite 
function myFavoriteDel(uID){
	$('.authorFavorite').html("<img src='/s/i/loadingExit.gif' style='margin:0px;'/>");

	$.post("/ajax/authorFavorite.php", { idDel: uID },
	function(data) {
		if(data == "ok") {
			$('.authorFavorite').html("<span style='color:green'>Գործողությունը կատարված է: Շուտով նա կանհետանա ձեր ֆավորիտներից:</span>");
		} else {
			$('.authorFavorite').html("<span style='color:red'>"+data+"</span>");
		}
	});
}



// New SMS Notifications
function checkNotifications() {
	
	$.post("/ajax/notifications.php", { login: "cpUser" },
	function(data) {
		if(data > 0)
			$('#NewSMS').html("("+data+")");
		else 
			$('#NewSMS').html("");
		setTimeout(function() { checkNotifications(); }, 30000);
	});
	
}


// Set cookie
function setCookieTop(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
	document.location.reload();
}
