
function ContentFilter(){
	this.ProcessedDocument = false;
	this.FilterPanelID = "";
	this.FilterKeys = new Array();
	this.FilterLabels = new Array();
	this.FilterControls = new Array();
	
	if(document.createStyleSheet)
		this.StyleSheet = document.createStyleSheet();
	else{
		var styleElement = document.createElement('style');
		styleElement.type = 'text/css';
		document.getElementsByTagName('head')[0].appendChild(styleElement); 
		this.StyleSheet = styleElement.sheet;
	}

	this.Bind = ContentFilter_Bind;
	this.AddFilterKey = ContentFilter_AddFilterKey;
	this.GetStyle = ContentFilter_GetStyle;
	this.SelectedFiltersChanged = ContentFilter_SelectedFiltersChanged;
}

function ContentFilter_Bind(sID){
	this.FilterPanelID = sID;
	this.FilterPanel = document.getElementById(sID);
	if(this.FilterPanel == null) return;
	this.FilterPanel.className = "CommonSidebarArea ContentFilterArea";
	
	var tempElement = document.createElement("h4");
	tempElement.className = "CommonSidebarHeader";
	tempElement.appendChild(document.createTextNode("Content Filter"));
	this.FilterPanel.appendChild(tempElement);
	tempElement = document.createElement("div");
	tempElement.className = "CommonSidebarContent";
	tempElement.ID =sID + "_ItemsPanel";
	this.FilterPanel.appendChild(tempElement);
	this.FilterItemsPanel = tempElement;

	if(this.FilterKeys.length == 0){
		this.FilterPanel.style.display = "none";
		return;
	}

	for(var x = 0; x < this.FilterKeys.length; x++){
		var checkbox = document.createElement("input");
		checkbox.type = "checkbox";
		checkbox.id = "filter_" + this.FilterKeys[x];
		this.FilterControls[x] = checkbox;
		this.FilterItemsPanel.appendChild(checkbox);
		checkbox.checked = true;
		checkbox.onclick = this.SelectedFiltersChanged;
		var label = document.createElement("label");
		label.htmlFor = checkbox.id;
		label.appendChild(document.createTextNode(this.FilterLabels[x]));
		this.FilterItemsPanel.appendChild(label);
		
		if(x+1 < this.FilterKeys.length)
			this.FilterItemsPanel.appendChild(document.createElement("br"));
	}
}

function ContentFilter_AddFilterKey(sFilterKey, sFilterLabel){
	for(var x = 0; x < this.FilterKeys.length; x++)
		if(this.FilterKeys[x] == sFilterKey)
			return;

	this.FilterKeys[this.FilterKeys.length] = sFilterKey;
	this.FilterLabels[this.FilterLabels.length] = sFilterLabel;
	
	if(this.StyleSheet.addRule)
		this.StyleSheet.addRule("." + sFilterKey, "{ }", -1);
	else if (this.StyleSheet.insertRule)
		this.StyleSheet.insertRule("." + sFilterKey + "{ }", this.StyleSheet.cssRules.length );
}

function ContentFilter_GetStyle(sClassName){
	var rules;
	if(this.StyleSheet.rules)			//IE
		rules = this.StyleSheet.rules;
	else if(this.StyleSheet.cssRules)	//FireFox
		rules = this.StyleSheet.cssRules;
	else
		return null;
		
	for(var y = 0; y < rules.length; y++){
		var rule = rules[y];
		if(rule.selectorText == "." + sClassName)
			return rule;
	}
}

function ContentFilter_SelectedFiltersChanged(clickEventArgs){
	if(clickEventArgs == null)
		clickEventArgs = window.event;
	
	var src;
	if(clickEventArgs.srcElement)
		src = clickEventArgs.srcElement;
	else if(clickEventArgs.target)
		src = clickEventArgs.target;
	
	var styleKey;
	for(var x = 0; x < window.ContentFilter.FilterControls.length; x++){
		if(window.ContentFilter.FilterControls[x] == src){
			styleKey = window.ContentFilter.FilterKeys[x];
			break;
		}
	}
	
	var style = window.ContentFilter.GetStyle(styleKey);
	if(style != null && style.style)
		style = style.style;
	if(src.checked)
		style.display = '';
	else
		style.display = "none";
}

window.ContentFilter = new ContentFilter();

if(!document.all){
eval("HTMLElement.prototype.innerHTML setter = function (str) {\n" +
"   var r = this.ownerDocument.createRange();\n" +
"   r.selectNodeContents(this);\n" +
"   r.deleteContents();\n" +
"   var df = r.createContextualFragment(str);\n" +
"   this.appendChild(df);\n" +
"   return str;\n" +
"}\n" +
"\n" +
"HTMLElement.prototype.outerHTML setter = function (str) {\n" +
"   var r = this.ownerDocument.createRange();\n" +
"   r.setStartBefore(this);\n" +
"   var df = r.createContextualFragment(str);\n" +
"   this.parentNode.replaceChild(df, this);\n" +
"   return str;\n" +
"}\n" +
"\n" +
"HTMLElement.prototype.innerHTML getter = function () {\n" +
"   return getInnerHTML(this);\n" +
"}\n" +
"\n" +
"HTMLElement.prototype.outerHTML getter = function () {\n" +
"   return getOuterHTML(this)\n" +
"}\n");
}