﻿//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var SignInPopupStatus = 0;
var SignUpPopupStatus = 0;
var EditProfilePopupStatus = 0;
var SubmitProjectPopupStatus = 0;
var ContactUsPopupStatus = 0;
var RequestPopupStatus = 0;

//loading sign in popup
function loadSignInPopup(){
	//loads popup only if it is disabled
	if(SignUpPopupStatus==0)
	{
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("fast");
		$("#popupSignIn").fadeIn("fast");
		SignInPopupStatus = 1;
	}
}

//disabling sign in popup
function disableSignInPopup(){
	//disables popup only if it is enabled
	if(SignInPopupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupSignIn").fadeOut("fast");
		SignInPopupStatus = 0;
	}
}

//centering sign in popup
function centerSignInPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSignIn").height();
	var popupWidth = $("#popupSignIn").width();
	var newTop = windowHeight / 2 - popupHeight / 2;
	if (newTop < 10)
	  newTop = 10;
	  
	//centering
	$("#popupSignIn").css({
		"position": "absolute",
		"top": newTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//loading signup popup
function loadSignUpPopup(){
	//loads popup only if it is disabled
	if(SignUpPopupStatus==0)
	{
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("fast");
		$("#popupSignUp").fadeIn("fast");
		SignUpPopupStatus = 1;
	}
}

//disabling signup popup
function disableSignUpPopup(){
	//disables popup only if it is enabled
	if(SignUpPopupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupSignUp").fadeOut("fast");
		SignUpPopupStatus = 0;
	}
}

//centering signup popup
function centerSignUpPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSignUp").height();
	var popupWidth = $("#popupSignUp").width();
	var newTop = windowHeight / 2 - popupHeight / 2;
	if (newTop < 10)
	  newTop = 10;

	//centering
	$("#popupSignUp").css({
		"position": "absolute",
		"top": newTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//loading edit profile popup
function loadEditProfilePopup(){
	//loads popup only if it is disabled
	if(EditProfilePopupStatus==0)
	{
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("fast");
		$("#popupEditProfile").fadeIn("fast");
		EditProfilePopupStatus = 1;
	}
}

//disabling edit profile popup
function disableEditProfilePopup(){
	//disables popup only if it is enabled
	if(EditProfilePopupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupEditProfile").fadeOut("fast");
		EditProfilePopupStatus = 0;
	}
}

//centering edit profile popup
function centerEditProfilePopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupEditProfile").height();
	var popupWidth = $("#popupEditProfile").width();
	var newTop = windowHeight / 2 - popupHeight / 2;
	if (newTop < 10)
	  newTop = 10;

	//centering
	$("#popupEditProfile").css({
		"position": "absolute",
		"top": newTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//loading submit project popup
function loadSubmitProjectPopup(){
	//loads popup only if it is disabled
	if(SubmitProjectPopupStatus==0)
	{
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("fast");
		$("#popupSubmitProject").fadeIn("fast");
		SubmitProjectPopupStatus = 1;
	}
}

//disabling submit project popup
function disableSubmitProjectPopup(){
	//disables popup only if it is enabled
	if(SubmitProjectPopupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupSubmitProject").fadeOut("fast");
		SubmitProjectPopupStatus = 0;
	}
}

//centering submit project popup
function centerSubmitProjectPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSubmitProject").height();
	var popupWidth = $("#popupSubmitProject").width();
	var newTop = windowHeight / 2 - popupHeight / 2;
	if (newTop < 10)
	  newTop = 10;

	//centering
	$("#popupSubmitProject").css({
		"position": "absolute",
		"top": newTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//loading contact us popup
function loadContactUsPopup(){
	//loads popup only if it is disabled
	if(SubmitProjectPopupStatus==0)
	{
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("fast");
		$("#popupContactUs").fadeIn("fast");
		ContactUsPopupStatus = 1;
	}
}

//disabling contact us popup
function disableContactUsPopup(){
	//disables popup only if it is enabled
	if(ContactUsPopupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupContactUs").fadeOut("fast");
		ContactUsPopupStatus = 0;
	}
}

//centering contact us popup
function centerContactUsPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContactUs").height();
	var popupWidth = $("#popupContactUs").width();
	var newTop = windowHeight / 2 - popupHeight / 2;
	if (newTop < 10)
	  newTop = 10;

	//centering
	$("#popupContactUs").css({
		"position": "absolute",
		"top": newTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//loading request form popup
function loadRequestFormPopup(){
	//loads popup only if it is disabled
	if(RequestPopupStatus==0)
	{
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("fast");
		$("#popupRequest").fadeIn("fast");
		RequestPopupStatus = 1;
	}
}

//disabling request form popup
function disableRequestFormPopup(){
	//disables popup only if it is enabled
	if(RequestPopupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupRequest").fadeOut("fast");
		RequestPopupStatus = 0;
	}
}

//centering request form popup
function centerRequestFormPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupRequest").height();
	var popupWidth = $("#popupRequest").width();
	var newTop = windowHeight / 2 - popupHeight / 2;
	if (newTop < 10)
	  newTop = 10;

	//centering
	$("#popupRequest").css({
		"position": "absolute",
		"top": newTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//open request form popup - center and load
function OpenRequestFormPopup()
{
  // Move the popup into the body
  var panel = $("#popupRequest");
  var form = $(document.forms[0]);
  form.prepend(panel);

	//centering with css
	centerRequestFormPopup();
	
	//load popup
	loadRequestFormPopup();
}

$(document).ready(function(){
	
	//SIGN IN POPUP
	//Click the sign up button event
	$("#SignInButton").click(function(){
	
	  // Move the popup into the body
    var panel = $("#popupSignIn");
    var form = $(document.forms[0]);
    form.prepend(panel);

		//centering with css
		centerSignInPopup();
		
		//load popup
		loadSignInPopup();
	});
				
	//Click the sign up x event
	$("#SignInClose").click(function(){
		disableSignInPopup();
	});
	
	//SIGN UP POPUP
	//Click the sign up button event
	$("#SignUpButton").click(function(){
	
	  // Move the popup into the body
    var panel = $("#popupSignUp");
    var form = $(document.forms[0]);
    form.prepend(panel);

		//centering with css
		centerSignUpPopup();
		
		//load popup
		loadSignUpPopup();
	});
				
	//Click the sign up x event
	$("#SignUpClose").click(function(){
		disableSignUpPopup();
	});
	
	//EDIT PROFILE POPUP
	//Click the edit profile button event
	$("#EditProfileButton").click(function(){
	
	  // Move the popup into the body
    var panel = $("#popupEditProfile");
    var form = $(document.forms[0]);
    form.prepend(panel);

		//centering with css
		centerEditProfilePopup();
		
		//load popup
		loadEditProfilePopup();
	});
				
	//Click the edit profile x event
	$("#EditProfileClose").click(function(){
		disableEditProfilePopup();
	});
	
	//SUBMIT PROJECT POPUP
	//Click the edit profile button event
	$("#SubmitProjectButton").click(function(){
	
	  // Move the popup into the body
    var panel = $("#popupSubmitProject");
    var form = $(document.forms[0]);
    form.prepend(panel);

		//centering with css
		centerSubmitProjectPopup();
		
		//load popup
		loadSubmitProjectPopup();
	});
				
	//Click the submit project x event
	$("#SubmitProjectClose").click(function(){
		disableSubmitProjectPopup();
	});
	
	//CONTACT US POPUP
	//Click the contact us button event
	$(".ContactUsButton").click(function(){
	
	  // Move the popup into the body
    var panel = $("#popupContactUs");
    var form = $(document.forms[0]);
    form.prepend(panel);

		//centering with css
		centerContactUsPopup();
		
		//load popup
		loadContactUsPopup();
	});
				
	//Click the contact us x event
	$("#ContactUsClose").click(function(){
		disableContactUsPopup();
	});
	
	//REQUEST POPUP
	//Click the request form button event
	
	$(".RequestButton").click(function(){
	  OpenRequestFormPopup();
	});
				
	//Click the request form x event
	$("#RequestClose").click(function(){
		disableRequestFormPopup();
	});
	
	// GLOBAL POPUP EVENTS
	//Click out event
	$("#backgroundPopup").click(function(){
		if(SignInPopupStatus==1){
			disableSignInPopup();
		}
		if(SignUpPopupStatus==1){
			disableSignUpPopup();
		}
		if(EditProfilePopupStatus==1){
		  disableEditProfilePopup();
		}
		if(SubmitProjectPopupStatus==1){
		  disableSubmitProjectPopup();
		}
		if(ContactUsPopupStatus==1){
		  disableContactUsPopup();
		}
		if(RequestPopupStatus==1){
		  disableRequestFormPopup();
		}
	});
	
	//Press Escape event
	$(document).keypress(function(e){
		if(e.keyCode==27 && SignInPopupStatus==1){
			disableSignInPopup();
		}
		if(e.keyCode==27 && SignUpPopupStatus==1){
			disableSignUpPopup();
		}
		if(e.keyCode==27 && EditProfilePopupStatus==1){
		  disableEditProfilePopup();
		}
		if(e.keyCode==27 && SubmitProjectPopupStatus==1){
		  disableSubmitProjectPopup();
		}
		if(e.keyCode==27 && ContactUsPopupStatus==1){
		  disableContactUsPopup();
		}
		if(e.keyCode==27 && RequestPopupStatus==1){
		  disableRequestFormPopup();
		}
	});

});