window.addEvent('domready', function(){
	
	/* EVENTS */
	
	// Enter gedrückt	
	$('SearchPattern').addEvent('keypress', function(code){
		var c = kc(code);
		if(c==13){
			ValidateSearch();
		}
	});
	
	function goToPage(){
		try{
			var n = parseInt($('TextGoToPage').value) ? parseInt($('TextGoToPage').value) : 'fail';
			if(n!='fail'){
				var str = location.href;
				var i = str.indexOf('%5Bpage:', 0);
				var j = str.indexOf('%5D', i);

				if(i != -1){
					i += 3;
					str = str.replace(str.substr(i, j-i), 'page:' + n.toString());
					location.href = str;
				}else{
					str += '/[page:'+n .toString()+']';
					location.href = str;	
				}
			}else{
				SetStyle($('TextGoToPage'), false);
			}
		}
		catch(e){
			SetStyle($('TextGoToPage'), false);
		}
	}
	
	$('ButtonGoToPage').addEvent('click', function(){
		goToPage();
	});
	
	$('TextGoToPage').addEvent('keypress', function(code){
		var c = kc(code);
		if(c==13){
			goToPage();
		}
	});
	
	// Submit der Suche
	$('SearchSubmit').addEvent('click', function(){
		ValidateSearch();
	});
	
	
	// Clear
	$('SearchClear').addEvent('click', function(){
		// abschicken
		location.href=CJPath + 'cache/list/';
	});
	
	/* ANDERE FUNKTIONEN */
	
	// validate
	function ValidateSearch(){
		if($('SearchPattern').value.length<=2){
			SetStyle($('SearchPattern'), false);
		}else{
			SetStyle($('SearchPattern'), true);
			
			// abschicken
			location.href=CJPath + 'cache/list/[search:' + $('SearchPattern').value + ']/';
		}
	}
	
	// Style setzen
	function SetStyle(Element, valid){
		if(valid){
			Element.style.backgroundColor='#FFFFFF';
			Element.style.color='#4D610C';
		}else{
			Element.style.backgroundColor='#F89E00';
			Element.style.color='#FFF';
		}
	}
	
});