//SETTING UP OUR POPUP	//0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({	"opacity": "0.7"	});
		$("#backgroundPopup").show();//fadeIn("slow");
		$("#popupSelectQuestionCategory").show();//fadeIn("slow");
		//$("#popupSelectQuestionCategoryArea").innerHTML='...Loading Content...';
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").hide();//fadeOut("slow");
		$("#popupSelectQuestionCategory").hide();//fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth; 
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSelectQuestionCategory").height();
	var popupWidth = $("#popupSelectQuestionCategory").width();
	var total= (windowWidth - popupWidth)/2;   
	//alert(total); 
	//centering
	$("#popupSelectQuestionCategory").css({
		"position": "absolute",
		"top": "145px",
		"left":total+"px"   //"178.5px"
		//"left":"240px"
	});
	//only need force for IE6
	$("#backgroundPopup").css({		"height": windowHeight	});
	
}
function LoadSubCategories(whichCategory,categoryName) {
	var content_show = whichCategory; //retrieve category of link so we can load subcategories 
	document.getElementById('popupFinalCategory').innerHTML = categoryName; 
		if($("#popupFinalCategory").text()==""){
			$("#selectQuestionCategoryTxt").text("Select Category:");
		}else{
			$("#selectQuestionCategoryTxt").text("(edit)");
		}
		document.getElementById("popupFinalCategoryId").value = content_show;
		disablePopup();
}
$(document).ready(function(){
	// Loading Popup	// On Click selectQuestionCategory
	$("#selectQuestionCategory").click(function(){	//load popup
			document.getElementById("catErr").style.display="none"												
			if(document.getElementById("popupFinalCategoryId")){
			whichCategory=document.getElementById("popupFinalCategoryId").value; 
			}else{ 
			whichCategory=0;
			}
			centerPopup();		loadPopup();		
		if(document.getElementById('SpltPgResSelBoxTwo')!=null){
			document.getElementById('SpltPgResSelBoxTwo').style.display='none';
		}			
			//LoadSubCategories(whichCategory);
	
	});
	$("#selectQuestionCategory").hover(	/* Binding Hover Event */
		function(e){
			targetElement = e.target; 
						if(targetElement.childNodes.length>0){
							/*if(targetElement.childNodes[1].nodeName=="IMG"){
								temp = targetElement.childNodes[1].src;
								targetElement.childNodes[1].src = targetElement.childNodes[1].lowsrc;
								targetElement.childNodes[1].lowsrc=temp;
							}*/
						};
					}, 
		function(e){	targetElement = e.target; 
						/*if(targetElement.childNodes.length>0){
							if(targetElement.childNodes[1].nodeName=="IMG"){
								temp = targetElement.childNodes[1].src;
								targetElement.childNodes[1].src = targetElement.childNodes[1].lowsrc;
								targetElement.childNodes[1].lowsrc=temp;
						
							}
						};*/
					}
    );
	//CLOSING POPUP	//Click the x event!
	$("#popupSelectQuestionCategoryClose").click(function(){				if(document.getElementById('SpltPgResSelBoxTwo')!=null){
			document.getElementById('SpltPgResSelBoxTwo').style.display='block';
		};disablePopup();	});
//	$("#categoryClose").click(function(){		disablePopup();	});
	$("#popupSelectQuestionCategoryDone").click(function(){				if(document.getElementById('SpltPgResSelBoxTwo')!=null){
			document.getElementById('SpltPgResSelBoxTwo').style.display='block';
		}disablePopup();	});
	//Click out event!
	$("#backgroundPopup").click(function(){					if(document.getElementById('SpltPgResSelBoxTwo')!=null){
			document.getElementById('SpltPgResSelBoxTwo').style.display='block';
		}disablePopup();	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});
