var CategoryPopup = {
	showPopup : function(anchorElement) {
		var verticalPositionValue = findPosY(anchorElement) + anchorElement.offsetHeight-1;
		var verticalPosition = 'top: ' + verticalPositionValue + 'px;';
		var horizontalPosition = 'left: '+ findPosX(anchorElement)+ 'px;';
		var viewallLink = anchorElement.href;
		var popupInnerHtml= '<div id="innerTagPopup" class="popup tag-popup" style="display:none; z-index: 25550; position:absolute; '
				+ verticalPosition + ' ' +  horizontalPosition+' ">';
		popupInnerHtml += '<div class="popup-wrapper">';
		popupInnerHtml += '<p class="tag-popup-close"><a href="javascript:void(0);" onclick="CategoryPopup.hidePopup();return false;"></a></p>';
		popupInnerHtml += '<div id="categoryPopupFeedback" style="display: none">';
		popupInnerHtml += '<img id="categoryPopupImg" src="' + contextPath + 'images/miniglobe.gif" />';
		popupInnerHtml += '<small class="red" id="categoryPopupMsg"> ' + busyMessage + '</small>';
		popupInnerHtml += '</div>';
		popupInnerHtml += '<A id="popupViewAll" HREF="' + viewallLink + '">'+ JSResource.categoryManagement_showpopup_viewAllLink_viewAllPrefix +' '+ anchorElement.innerHTML + JSResource.categoryManagement_showpopup_viewAllLink_viewAllSuffix + '</A>';
		popupInnerHtml += '<BR/><SMALL>' + viewallCategoryMessage + '</SMALL>';
		if(includeExcludeLink) {
			popupInnerHtml += '<BR/>';
			if(anchorElement.excluded!=true){
				popupInnerHtml += '<A id="popupIncludeExclude" HREF="javascript:void(0);" onClick="excludeCategory(\'' + anchorElement.id + '\',\'' + anchorElement.name + '\'); return false;">'+JSResource.categoryManagement_showpopup_includeExcludeLink_Exclude+'</A>';
				popupInnerHtml += '<BR/><SMALL id="popupIncludeExcludeMsg">' + excludeHint + '</SMALL>';
			} else {
				popupInnerHtml += '<A id="popupIncludeExclude" HREF="javascript:void(0);" onClick="includeCategory(\'' + anchorElement.id + '\',\'' + anchorElement.name + '\'); return false;">'+JSResource.categoryManagement_showpopup_includeExcludeLink_Include+'</A>';
				popupInnerHtml += '<BR/><SMALL id="popupIncludeExcludeMsg">' + includeHint + '</SMALL>';
			}
		}
		popupInnerHtml += '</div></div>';

		$('#tagPopup').html(popupInnerHtml);				
		$('#tagPopup').show();
		$('#innerTagPopup').show();
	},
	
	hidePopup : function() {
	   if(document.getElementById('innerTagPopup')){
	       $('#innerTagPopup').hide();
	   }
	   if(document.getElementById('tagPopup')){
	       $('#tagPopup').hide();
	   }
	}
};

//this behaviour must be enforced for both header and footer widgets simultaneously
function markExcluded(name, id){
	var anchors = document.getElementsByTagName('a');
	for(var i=0;i<anchors.length;i++){
		//if the hyperlink is in the navigation bar(it is one of the links from the personas bar) we do not want to affect its apearance
		if ($(anchors[i]).parents("div.navigation").get(0) != null){
			continue;
		}		
		
		var text = null;
		if(anchors[i].text) text = anchors[i].text;
		if(anchors[i].innerText) text = anchors[i].innerText;
		if(text == name){
			anchors[i].style.textDecoration='line-through';
			anchors[i].excluded = true;
		}		
	}
	//update the personas widget (in header and footer as well) 
	var element = document.getElementById("community"+id);
	var headerElement = document.getElementById("hcommunity"+id);
	//alert("We are in markExcluded and the element is: " + element + " the id is: " + id + " the headerelement is " + headerElement);
	//alert("element checked is: " + element.checked);
	//alert("headerElement checked is: " + headerElement.checked);
	if(element){
		element.disabled = false;
		element.checked=false;
	}
	if(headerElement){
		headerElement.disabled = false;
		headerElement.checked=false;
	}
	//synchronize visual appearance of the checkboxes from header and footer 
	Custom.clear();	
}

//this behaviour must be enforced for both header and footer widgets simultaneously
function markIncluded(name, id){
	var anchors = document.getElementsByTagName('a');
	for(var i=0;i<anchors.length;i++){
		//if the hyperlink is in the navigation bar(it is one of the links from the personas bar) we do not want to affect its apearance
		if ($(anchors[i]).parents("div.navigation").get(0) != null){
			continue;
		}
				
		var text = null;
		if(anchors[i].text) text = anchors[i].text;
		if(anchors[i].innerText) text = anchors[i].innerText;
		if(text == name){
			anchors[i].style.textDecoration='none';
			anchors[i].excluded = false;
		}		
	}
	//update the personas widget (in header and footer as well) 
	var element = document.getElementById("community"+id);
	var headerElement = document.getElementById("hcommunity"+id);
	//alert("We are in markIncluded and the element is: " + element + " the id is: " + id + " the headerelement is " + headerElement);
	//alert("element checked is: " + element.checked);
	//alert("headerElement checked is: " + headerElement.checked);
	if(element){
		element.disabled = false;
		element.checked=true;
	}
	if(headerElement){
		headerElement.disabled = false;
		headerElement.checked=true;
	}
	//synchronize visual appearance of the checkboxes from header and footer 
	Custom.clear();	
}
		
function excludeCategory(categoryId, categoryName){
	var feedback = document.getElementById('categoryPopupFeedback');
	feedback.style.display = 'block';
	categoryId = categoryId.replace(/,/,"");
	categoryId = categoryId.replace(/\./,"");
	UserCategoryUtil.excludeCategory(categoryId, updateExcludedCategories);
}

function excludeCommunity(categoryId){
	categoryId = categoryId.replace(/,/,"");
	categoryId = categoryId.replace(/\./,"");
	UserCategoryUtil.excludeCategory(categoryId, updateExcludedCategories);
}

var updateExcludedCategories = function(name) {
	CategoryPopup.hidePopup();
	DWRErrorHandlerUtil.resetErrorHandler();
	//alert("We are in updateExcludedCategories and name is: " + name)
	var catInfo = name.split("-");
	if(catInfo){
		markExcluded(catInfo[0], catInfo[1]);
	}
}

function includeCategory(categoryId, index){
	var feedback = document.getElementById('categoryPopupFeedback');
	feedback.style.display = 'block';
	categoryId = categoryId.replace(/,/,"");
	categoryId = categoryId.replace(/\./,"");
	UserCategoryUtil.includeCategory(categoryId, updateIncludedCategories);
}
		
function includeCommunity(categoryId){
	categoryId = categoryId.replace(/,/,"");
	categoryId = categoryId.replace(/\./,"");
	UserCategoryUtil.includeCategory(categoryId, updateIncludedCategories);
}

var updateIncludedCategories = function(name) {
	CategoryPopup.hidePopup();
	DWRErrorHandlerUtil.resetErrorHandler();
	//alert("We are in updateIncludedCategories and name is: " + name)
	var catInfo = name.split("-");
	if(catInfo){
		markIncluded(catInfo[0], catInfo[1]);
	}
}
				
function includeExcludeCommunity(checkbox, isHeaderWidget) {
    //alert("We are here");
    if(!checkbox.disabled){
        //alert("Checkbox is not disabled");
		var commId = checkbox.id;
		if (isHeaderWidget){
		    //alert("We are in the header!");
		    commId = commId.replace(/hcommunity/,"");
		}else{
		    //alert("We are in the footer!");
			commId = commId.replace(/community/,"");
		}			
		checkbox.disabled = true;
		DWRErrorHandlerUtil.setNoErrorReporterHandler();
		if(checkbox.checked){
			includeCommunity(commId);
		} else {
			excludeCommunity(commId);
		}
		var communitiesMessageArea = document.getElementById("communitiesMessage");
		if(communitiesMessageArea){
			communitiesMessageArea.innerHTML = '<small style="color: grey;">'+communityChangeMessage+'</small>';
		}
	}
}
