var months;
var years;

var currentMonth;
var currentYear;
var currentCat;
var timerID = 0;

document.observe("dom:loaded", function() {
	currentCat = $('category').value;

	$('search-button').observe('click', doSearch);
	$('keywords-text').observe('focus', keywordsSelected);
	$('keywords-text').observe('blur', keywordsUnselected);
	$('keywords-text').observe('keyup', setKeywords);
	keywordsUnselected();

	years = $$('#dates a.year');
	years.each(function(obj, index) {
		obj.observe('click', setYear);
	});

	months = $$('#dates a.month');
	months.each(function(obj, index) {
		obj.observe('click', setMonth);
	});
//$('keywords-text').removeClassName("empty");
//		$('keywords-text').addClassName("not_empty");
	$('category').observe('click', setCategory);
});

function keywordsSelected(event) {
	if($('keywords-text').value == "Enter Keywords") {
		$('keywords-text').value = "";
		$('keywords-text').removeClassName("empty");
		$('keywords-text').addClassName("not_empty");
	}
}

function keywordsUnselected(event) {
	if($('keywords-text').value == "" || $('keywords-text').value == "Enter Keywords") {
		$('keywords-text').value = "Enter Keywords";
		$('keywords-text').removeClassName("not_empty");
		$('keywords-text').addClassName("empty");
	} else {
		$('keywords-text').removeClassName("empty");
		$('keywords-text').addClassName("not_empty");
	}
}

function doSearch(event) {
	event.stop();
	//document.search_form.submit();
	$('search_form').submit();
	/*
	var k = "";
	if($('keywords-text').value != "Enter Keywords") {
		k = $('keywords-text').value;
	}
	new Ajax.Updater('news-list', '/content/news/search?c=' + $('category').value + '&filter');
	*/
}

function setMonth(event) {
	event.stop();
	month = event.element().innerHTML;
	months.each(function(obj, index) {
		thisMonth = obj.innerHTML;
		if(thisMonth == month) {
			obj.removeClassName("unselected");
			obj.addClassName("selected");
		} else {
			obj.removeClassName("selected");
			obj.addClassName("unselected");
		}
	});
	timeUpdate('/content/news/search?m=' + month + '&filter');
	//$('month').value = $m;
}

function setYear(event) {
	event.stop();
	year = event.element().innerHTML;
	years.each(function(obj, index) {
		thisYear = obj.innerHTML;
		if(thisYear == year) {
			obj.removeClassName("unselected");
			obj.addClassName("selected");
			$('months-' + thisYear).setStyle("display: inline;");
		} else {
			obj.removeClassName("selected");
			obj.addClassName("unselected");
			$('months-' + thisYear).setStyle("display: none;");
		}
	});
	months.each(function(obj, index) {
		obj.removeClassName("selected");
	});

	if(year == "All Dates") {
		year = 0;
	}

	timeUpdate('/content/news/search?y=' + year + '&filter');
}

function setCategory(event) {
	if($('category').value != currentCat) {
		currentCat = $('category').value;
		timeUpdate('/content/news/search?c=' + currentCat + '&filter');
		$('land-img-news').setStyle('background-image: url(/sites/core/images/standard_default/news-article/land-img-' + currentCat + '.jpg);');
	}
}


function setKeywords(event) {
	timeUpdate('/content/news/search?k=' + $('keywords-text').value + '&filter');
}

function timeUpdate(url) {
	if(timerID > 0) {
		clearTimeout(timerID);
	}
	timerID = setTimeout("doUpdate('" + url + "')", 500);
}

function doUpdate(url) {
	theAjax = new Ajax.Updater('news-list', url);
}


/** Show a loading gif for any pages that take longer than a second */
Ajax.Responders.register({
	onCreate : function(){
		// Needs if no loading box completed.  create div and insert img in it.
		if(!$("loading-box")){
			//loadingB = Builder.node("img",{src:"/sites/core/images/standard_default/ajax-loader.gif", id:"loading_box", style: ""});
			//$("id-where-you-will-insert-the-image").appendChild(loadingB);
			//l = $("loadingBox"); //fix for IE so we can hide
			//l.hide();
			//new Effect.Appear("loading_box");
		} else {
			$('search-results').hide();
			new Effect.Appear("loading-box");
		}
	},
	onComplete : function(){
		l = $("loading-box"); //fix for IE so we can hide
		new Effect.Fade(l);
		$('search-results').show();
	}
});
