//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var availablePopUp="";
//0 means disabled; 1 means enabled;
var popupStatus2 = 0;
var availablePopUp2="";
//loading popup with jQuery magic!
function loadPopup(objPopup){
	//loads popup only if it is disabled
		$("#other_pop").css({
			"display": "inline"
		});
	
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(availablePopUp).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
		$("#other_pop").css({
			"display": "none"
		});

	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(availablePopUp).fadeOut("slow");
		popupStatus = 0;
	}
}

//loading popup with jQuery magic!
function loadPopup2(objPopup){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		//$("#backgroundPopup").css({
			//"opacity": "0.7"
		//});
		//$("#backgroundPopup").fadeIn("slow");
		$(availablePopUp2).fadeIn("slow");
		popupStatus2 = 1;
	}
}

//disabling popup with jQuery magic!

function disablePopup2(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		//$("#backgroundPopup").fadeOut("slow");
		$(availablePopUp2).fadeOut("slow");
		popupStatus2 = 0;
	}
}

//centering popup
function centerPopup(objPopup,w,h){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	if (objPopup)
	{
		myObj = document.getElementById(objPopup);
		availablePopUp = myObj;

	}
	var popupHeight =h;//$(myObj).height();
	var popupWidth = w;//$(myObj).width();
	var top = (windowHeight/2)-(popupHeight/2)-10;
	var left = (windowWidth/2)-(popupWidth/2)-10;
	//centering
	$(myObj).css({
		"position": "absolute",
		"top": top,
		"left": left
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//centering popup2
function centerPopup2(objPopup,w,h){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	if (objPopup)
	{
		myObj = document.getElementById(objPopup);
		availablePopUp2 = myObj;

	}
	var popupHeight =h;//$(myObj).height();
	var popupWidth = w;//$(myObj).width();
	var top = (windowHeight/2)-(popupHeight/2)-10;
	var left = (windowWidth/2)-(popupWidth/2)-10;
	//centering
	$(myObj).css({
		"position": "absolute",
		"top": top,
		"left": left
	});
	
}
var   saveScoreField = 0;
var	  enterKeyCode	 = 0;		
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	//CLOSING POPUP
	//Click the x event!
	$("#imgcloswindow").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
//		disablePopup();
		movePopup(availablePopUp);

	});
	//Press Escape event!
	$(document).keypress(function(e){
		//alert('ddd');
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
		if(e.keyCode==13){
			//alert('ddd');
			if(saveScoreField == 10){			
				
				//addProjectScore();
				//////////////////////////////////////////////////////
						id = jQuery("#score_list").getGridParam("selrow");
						if(id > 0){
							confirmSaveObjective();
							saveScoreField =0;
							//jQuery("#score_list").saveRow (id, false);
						setTimeout('refresh_score()',500);
						}
				///////////////////////////////////////////////////////////		
				saveScoreField =0;
			}
			
			switch (enterKeyCode)
			{
			case 11:
			//Add objective
			  saveObjective();
			  break;
			case 12:
			//Add backlog
			 object_save_backlog(); 
			  break;
			case 13:
			//Add product feature
			  save_backlog_product_feature();
			  break;
			case 14:
			//Add dev tasks
			 save_iterations_product_feature();
			  				  
			  break;
			case 15:
			//Add dev tasks
			  loginSubmit();
			  break;
			case 16:
			//Add sign up
			  signupSubmit();		  
			  break;
			case 17:
			//save results
			  updateResults();			  
			  break;
			case 18:
			//save project
			checkRequiredFields(editRowSatatus);  
			  break;
			case 19:
			//close popup
			  disablePopup();
			  enterKeyCode	=	0;	  
			  break;
			case 20:
			//save product features
				updateProductFeatures()
			  break;
			case 21:
				//save Objectives
				id = jQuery("#list").getGridParam("selrow");
				if(id > 0){
					checkRequiredFields(id);
				}
			  break;
			case 22:
			//save iteration
				checkRequiredFields(editRowSatatus);  
			  break;

			case 23:
				//save iteration
					checkRequiredFields(editRowSatatus);  
				  break;
	
			case 24:
				//delete objective
					confirm_delete_objective();  
				  break;
	
			case 25:
				//delete backlog
					confirm_delete_backlog();
				  break;
	
			case 26:
				//mark complete backlog
					confirm_mark_complete_project();
				  break;
			case 27:
				//delete backlog feature
					confirm_delete_backlog_placeHolder();
				  break;
	
			case 28:
				//delete backlog feature
					confirm_delete_backlog_placeHolder();
				  break;

			case 29:
				//delete Iterations
					confirm_delete_iterations();
				  break;
			case 30:
				//Complete Iterations
					confirm_mark_complete_iterations();
				  break;
			case 31:
				//Mark Complete Dev Task
					toggleDevelopmentTask();
				  break;
			case 32:
				//delete Iterations Dev Task
					confirm_delete_iterations_placeHolder();
				  break;
			case 33:
				//delete WT3 Tasks
					confirm_delete_weekly();
				  break;
		    case 34:
				//Mark Complete WT3
					confirm_mark_complete_wt3();
				  break;
			case 35:
				//delete team_site_users
				  id = jQuery("#list").getGridParam("selrow");
				  //removeUserFromTeamSite(id);
				  removeUserFromTeam();
				  break;
			case 36:
				//Mark Update Profile
					confirmUpdateProfile();
				  break;
			case 37:
				//Update Profile
					sendAFriendSubmit();
				  break;
			case 38:
				//Update Team Settings
					confirmChangeTeamSiteSettings();
				  break;
			case 39:
				//Update Contact us 
					confirmContactUsSubmit();
				  break;
			case 40:
				//Update Contact us 
					confirmFeedBackSubmit();
				  break;
			case 41:
				//Send WT3 Email 
					wt3MailSubmit();
				  break;
			case 42:
				//Invite user add 
					inviteUserSubmit();
				  break;
			default:
			  enterKeyCode	=	0;				  
			  break;
			  
			}
		}
	});

});


function openNewPopup(obj,w,h)
{
	//centering with css
		centerPopup(obj,w,h);
		//load popup
		loadPopup(obj);
}

function openNewPopup2(obj,w,h)
{
	//centering with css
		centerPopup2(obj,w,h);
		//load popup
		loadPopup2(obj);
}


function movePopup()
{
	window.setTimeout('shakemodel(5)',100);
	window.setTimeout('shakemodel(-10)',200);
	window.setTimeout('shakemodel(10)',300);
	window.setTimeout('shakemodel(-10)',400);
	window.setTimeout('shakemodel(5)',500);

}
function shakemodel(xadd)
{
availablePopUp.style.left = eval(parseInt(availablePopUp.style.left) + xadd )+"px";
}