function filter(type, on) {
	var displayStyle = '';
	if ( on ) {
		divDisplayStyle = 'block';
		spanDisplayStyle = 'inline';
	}
	else {
		divDisplayStyle = 'none';
		spanDisplayStyle = 'none';
	}
	
	// set up other types in an array
	var allTypes = Array("news", "guides", "features");
	var otherTypes = Array(2);
	var otherTypesOn = Array(2)
	var index = 0;
	for ( var l = 0; l < allTypes.length; l++ ) {
		if ( allTypes[l] != type ) {
			otherTypes[index] = allTypes[l];
			otherTypesOn[index] = document.getElementById('checkbox_' + allTypes[l] + '_' + allTypes[l]).checked;
			index++;
		}
	}
	
	var divClassName = "dulcinea_result_";
	var spanClassName = "dulcinea_result_tag_";
	
	var divs = document.getElementsByTagName('div'); 
	for ( var i = 0; i < divs.length; i++ ) {
		if ( divs[i].className.search(divClassName + type) != -1 ) {
			divs[i].style.display = divDisplayStyle;
		}
	}
	var spans = document.getElementsByTagName('span');
	for ( var i = 0; i < spans.length; i++ ) {
		var tmpSpanDisplayStyle = spanDisplayStyle;
		if ( spans[i].className.search(spanClassName + type) != -1 ) {
			if ( !on ) {
				var turnOff = true;
				for ( var j = 0; j < otherTypes.length; j++ ) {
					if ( (spans[i].className.search(spanClassName + otherTypes[j]) != -1) && otherTypesOn[j] ) {
						turnOff = false;
					}
				}
				if ( !turnOff )
					tmpSpanDisplayStyle = 'inline';
			}
			spans[i].style.display = tmpSpanDisplayStyle;
		}
	}
}

function typeFilter(type, on, page, method) {
	if ( method == 'refresh' ) {
		var tParam = getURLParam('t');
		var tagParam = getURLParam('tag');
		var timeParam = getURLParam('time');
		var typeParam = getURLParam('type');
		
		var url = page + '?';
		if ( tParam != '' ) {
			url += 't=' + tParam;
		}
		if ( tagParam != '' ) {
			url += '&';
			url += 'tag=' + tagParam;
		}
		if ( timeParam != '' ) {
			url += '&';
			url += 'time=' + period;
		}
		if ( on ) {
			if ( typeParam.indexOf(type) == -1 )
				if ( typeParam != '' )
					typeParam += ",";
				typeParam += type;
		}
		else {
			if ( typeParam == '' )
				typeParam = "guides,news,features";
			if ( typeParam.indexOf(type) != -1 ) {
				var types = typeParam.split(",");
				typeParam = "";
				for ( var i = 0; i < types.length; i++ ) {
					if ( types[i] != type ) {
						if ( typeParam != "" )
							typeParam += ",";
						typeParam += types[i];
					}
				}
			}
		}
		url += '&';
		url += 'type=' + typeParam;
		
		
		window.location = url;
	}
	else if ( method == 'js' ) {
		// ch will be the state of the checkbox BEFORE it is clicked
		var ch = document.getElementById("checkbox_" + type + "_" + type).checked;
		filter(type, !ch);
	}
	else alert("typeFilter: no method provided")
}

function getURLParam ( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function timePeriod(period, on, page) {
//	parameters:
//		period: 'week', 'month', 'year', 'custom'
//		start: for 'custom', start date
//		end: for 'custom', end date

	if ( period == 'custom' ) {
		alert("custom time period");
	}
	else {
		var tParam = getURLParam('t');
		var tagParam = getURLParam('tag');
		var typeParam = getURLParam('type');
		var url = page + '?';
		if ( tParam != '' ) {
			url += 't=' + tParam;
		}
		if ( tagParam != '' ) {
			url += '&';
			url += 'tag=' + tagParam;
		}
		if ( typeParam != '' ) {
			url += '&';
			url += 'type=' + typeParam;
		}
		if ( period != null && period != '' && on ) {
			url += '&';
			url += 'time=' + period;
		}
		window.location = url;
	}
}

// ajax for topics popup
function getArticles(tag, topic) {
	var url1 = 'docroot/dulcinea/jsp/topics/getArticlesShort.jsp';
	var url2 = 'docroot/dulcinea/jsp/topics/getArticles.jsp';
	var pars = "tag=" + tag;
	pars += "&";
	pars += "topic=" + topic;
	var myAjax = new Ajax.Request( url1, {method: 'get', parameters: pars, onComplete: showArticles } );
	var myAjax = new Ajax.Request( url2, {method: 'get', parameters: pars, onComplete: showArticles } );
}

// ajax for topics popup
function showArticles (originalRequest) {
	var html = originalRequest.responseText;
	document.getElementById("dulcinea_topics_popup").innerHTML = html;            
}

// for topics popup
function topicsPop(vis, tag, topic) {
	grayOut(vis);
	popDiv(vis);
	if ( tag ) {
		getArticles(tag, topic);
	}
}

function timePop(vis) {
	grayOut(vis);
	timeDiv(vis);
}

// for time popup
function timeDiv(vis) {
	var w = 429;
	var h = 477;
	var pageWidth = 0;
	var pageHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		pageWidth = window.innerWidth;
		pageHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	}

	var pop = document.getElementById('dulcinea_caldate_popup_bg');
	if ( vis ) {
		pop.style.left = ((pageWidth - w) / 2) + 'px';
		pop.style.top = ((pageHeight - h) / 2) + 'px';
		pop.style.display = 'block';
	}
	else {
		pop.style.display = 'none';
	}
	
	var pop = document.getElementById('dulcinea_caldate_popup');
	if ( vis ) {
		pop.style.left = ((pageWidth - w) / 2) + 'px';
		pop.style.top = ((pageHeight - h) / 2) + 'px';
		pop.style.display = 'block';
	}
	else {
		pop.style.display = 'none';
	}
}

// for topics popupdulcinea_caldate_popup
function popDiv(vis) {
	var w = 429;
	var h = 477;
	var pageWidth = 0;
	var pageHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		pageWidth = window.innerWidth;
		pageHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	}

	var pop = document.getElementById('dulcinea_topics_popup_bg');
	if ( vis ) {
		pop.style.left = ((pageWidth - w) / 2) + 'px';
		pop.style.top = ((pageHeight - h) / 2) + 'px';
		pop.style.display = 'block';
	}
	else {
		pop.style.display = 'none';
	}
	
	var pop = document.getElementById('dulcinea_topics_popup');
	if ( vis ) {
		pop.style.left = ((pageWidth - w) / 2) + 'px';
		pop.style.top = ((pageHeight - h) / 2) + 'px';
		pop.style.display = 'block';
		if ( navigator.userAgent.indexOf('Safari') != -1 ) {
			pop.style.width = "369px";
			pop.style.height = "426px";
		}
		else {
			pop.style.width = w + "px";
			pop.style.height = h + "px";
		}
	}
	else {
		pop.style.display = 'none';
	}
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 50;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}