function getParameterByName( 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 checkIt( tags ) {
	$("#tagSelect input:checked").each(function(index) {
	    theTag = $(this).val();
		bTagFound = false;
		for ( var i=0, len=tags.length; i<len; ++i ) {
		  if (tags[i] == theTag) {
			bTagFound = true;
		  };
		};
		if (!bTagFound) {
			$(this).attr("checked","");
		};
	  });
}

function filter(tag) {
	// this function sets and removes a '.visible' class in ordert to determine what should show - doing it this way avoids flickering from hiding all and then showing only matches
	if (tag == "all") {
		$(".eventList > li").addClass("visible");
	} else {
		$(".eventList > li").removeClass("visible");
		$(".eventList > li." + tag).addClass("visible");			
	}
	$(".eventList > li:visible:not(.visible)").slideUp("fast");
	$(".eventList > li.visible:not(:visible)").slideDown("fast");

	$('.eventList > li').removeClass('stripeEven');
	$('.eventList > li').removeClass('stripeOdd');
    $('.eventList > li:visible:even').addClass('stripeEven');
    $('.eventList > li:visible:odd').addClass('stripeOdd');		
}

$(document).ready(function() {

    $('#features').cycle({
        fx: 'fade',
		timeout: 6000,
		speed: 800,
        pager: '#pager',
        pagerAnchorBuilder: function(idx, slide) {
            idxNUM = idx + 1;
            return '<a class="pause" href="#">' + idxNUM + '</a>';
        },
		height: '200px'
    });

	$('#pager .pause').click(function(){
		$('#features').cycle('pause');
	});
	
	$("#button").click(function () {
	      $("#topBarWrap").slideToggle("slow");
	    });
		
		$("#topBarWrap").slideToggle(1);
	
	
	$(".accordion li:first-child").addClass("select");
	
	if ($('#tagSelect').length != 0) {
		var tags = getParameterByName("tags").split(",");
		if (tags != "") {
			checkIt(tags);
			filter(tags[0]);
		};	
		
		$("#tagSelect a").click (function () {
			var href = $(this).attr("href");
			var tag = href.substr(href.indexOf('=')+1);
			filter(tag);
			
			return false;
		});
	};

	//zebra striped event lists
    $('.eventList > li:visible:even').addClass('stripeEven');
    $('.eventList > li:visible:odd').addClass('stripeOdd');
	
});

