var contenting = true;
var fadeactive = false;
var wtimer = false;

function fader(id,time) {
	if ( !$(id) ) return false;
	
	if ( false == fadeactive ) {
		$(id).appear();
		fadeactive = true;
		wtimer = window.setTimeout("fadeactive=false;$('"+id+"').fade();",time);
		return true;
	} 

	fadeactive = true;
	$(id).show();
	window.clearTimeout(wtimer);
	wtimer = window.setTimeout("fadeactive=false;$('"+id+"').fade();",time);

}

function sidderb(url) {
	url = 2;
	return url;
}

function sidder(url) {
	
	if ( sessioncookie ) return url;
	if ( url.indexOf(sessionname) <= 0 ) {		
		if ( url.indexOf('?') <= 0 ) {
			url += "?";
		} else {
			url += "&";		
		}
		
		url += sessionname+"="+sessionid;
	}
	
	return url;
}

function toBasket(obj) {	
	

	ajax_update('messageboxy',true,obj);	

	fader('messageboxy',3000);
	window.setInterval("$('messageboxy').style.top=document.viewport.getScrollOffsets()[1]+20+'px';",10);
	
	
	ajax_update('top_head',true);
	ajax_update('top_sub',true);
	
	//ajax_update('artikelpage',true);
	return false;
}

function searchvals(obj) {
	alert(obj);
}	

function handle_caller_false(gid) {
	$(gid).hide();
	return false;
}




function teaser_change(id,url) {
	
	if ( !url ) return false;
	
	var returned = cms_request(url);
	
	if ( !returned ) return false;
	$(id).href=windowbase+returned.link;
	$(id).update(returned.content);
}


function cms_request(url) {
	url += "/"+sessionname+"="+sessionid;
	//$('content').update(url);
	var xml = cms_getter(url);	
	var content = cms_xmlobject(xml);	
	if ( !content ) return false;	
	return content;
}

function cms_xmlobject(xml) {
	//return false;
	if ( !xml ) return false;
	var root = xml.getElementsByTagName('root')[0];
	if ( null == root ) return false;
	var items =  root.childNodes;
	if ( items == root ) return false;	
	
	//$('content').update("test");
	for ( var cnt = 0; cnt < items.length; cnt++ ) {
		var node = items[cnt];
		var childs = node.childNodes;
		if ( !childs ) return false;
		
		var out = new Object();		
		
		for ( var cnt2 = 0; cnt2 < childs.length; cnt2++ ) {
			var childItem = childs[cnt2];
			//return false;
			var content = ( childItem.text ) ? childItem.text : childItem.textContent;	
			out[childItem.nodeName] = content;
			continue;
			childItem['content'] = 5;
//return false;			
			childItem[content] = ( childItem.text ) ? childItem.text : childItem.textContent;	
			childs[cnt2] = childItem;			
			out[childItem.nodeName] = childItem.content;
			//$('content').update($('content').innerHTML+"<br>"+childItem.nodeName);
			
		}
		return out;
		$('content').update($('content').innerHTML+"<br>"+out.content);
		continue;
		var value 	= node.getElementsByTagName(field)[0];
		if ( !value ) return false;
		value = ( value.text ) ? value.text : value.textContent;
		return value;		
	}
	return false;
	//$('content').update(dump(items));
	return root;	
}

function cms_getter(url) {
	var content = false;

	
	var nA = new Ajax.Request(url, {
		method: 'post',
		evalScripts: true,
		asynchronous:false,		
		onSuccess: function(transport) {			
			content = transport.responseXML;
		}
	});

	return content;
}

function handle_caller(gid,func,call) {
	

	var data = call;
	
	//var data = handleRaws(call);
	//if ( null == data ) return false;
	//if ( NULL == data ) return false;
	
	//alert(call.getElementsByTagName('root'));
	

	
	var out = '';
	var bgc = new Array();
	bgc[true] = '#EEEEEE';
	bgc[false] = '#FFFFFF';
	var bg = true;	

	
	var root = call.getElementsByTagName('root')[0];
	if ( null == root ) return handle_caller_false(gid);
	
	var items =  root.childNodes;
	if ( items == root ) return handle_caller_false(gid);

	$(gid).show();
	$(gid).style.top = $('searchfield').viewportOffset()[1]+35+"px";
	$(gid).setOpacity(0.9);
	//$(gid).style.left = $('content').viewportOffset()[0]+"px";
	$(gid).update();	
	//$('content').update(dump(items));
	
	var count = items.length;
	var max = 50;
	if ( count > max ) count = max;
	
	
	for ( var cnt = 0; cnt <= count; cnt++ ) {
		var node = items[cnt];
		//var link 	= node.getElementsByTagName("link")[0].textContent;
		//var value 	= node.getElementsByTagName("value")[0].textContent;		
		var link 	= node.getElementsByTagName("link")[0];
		var value 	= node.getElementsByTagName("value")[0];
		
		var thelink = ( link.text ) ? link.text : link.textContent ;
		var thevalue = ( value.text ) ? value.text : value.textContent;
	
		thelink = sidder(thelink);
	
		out = '';
		out += '<div onClick="window.location=\''+thelink+'\';" onMouseOver="this.style.backgroundColor=\'#FFEECC\';" onMouseOut="this.style.backgroundColor=\''+bgc[bg]+'\';" style="background-color:'+bgc[bg]+';">';	

		out += thevalue;
		out += '</div>';
		
		$(gid).update($(gid).innerHTML+out);
		bg = !bg;	
	}	
	
	$(gid).update($(gid).innerHTML+out);
	return false;
	

}

function caller(func,gid,goal,params) {
	//var nA = new Ajax.Updater(gid, goal, {
	
	var nA = new Ajax.Request(goal, {
		method: 'post',
		evalScripts: true,
		asynchronous:false,
		parameters: Base64._utf8_encode(params),
		onSuccess: function(transport) {			
			//transport.responseText = handle_caller(gid,func,transport.responseText);
			handle_caller(gid,func,transport.responseXML);
		}
	});

	
	return !nA.success();
}


function ajax_update(sid,gid) {
	var goal = window.location.href;	
	var ext = false;
	var opt = '';
	if ( arguments[2] ) {
		goal = ajax_goal(arguments[2]);		
		if ( arguments[2].action ) {
			opt+= arguments[2].serialize();	
		}
	}

	if ( true == gid ) gid = sid;
	var params = 'getElementById='+sid+'.innerHTML'+'';
	params += '&'+opt;
	
	goal = sidder(goal);
	//$('content').update(goal+"<br>"+sid+"<br>"+gid+"<br>"+params+"<br>"+opt);
	
	var nA = new Ajax.Updater(gid, goal, {
		method: 'post',
		evalScripts: true,
		asynchronous:false,
		parameters: Base64._utf8_encode(params)
	});

	return !nA.success();
}

function ajax_change(sid) {
	var get = ajax_get(sid);
	$(sid).update(get);
}

function ajax_get(sid) {
	var goal = window.location.href;	
	
	var params = 'getElementById='+sid+'.innerHTML'+'';

	
	var response = '';
	
	var nA = new Ajax.Request(goal, {
		method: 'post',
		evalScripts: true,
		parameters: Base64._utf8_encode(params),
		asynchronous:false,
		onSuccess: function(request) {
			response = request.responseText;
		}
	});	

	return response;	
}

function ajax_send(obj) {
	var goal = ajax_goal(obj);
	var params = '';
	
	var nA = new Ajax.Request(goal, {
		method: 'post',
		evalScripts: true,
		parameters: Base64._utf8_encode(params)
	});

	return !nA.success();		
}




function asfajax_get(sourceid,goalid,url) {
	var surl = url;			
	
	var params = 'getElementById='+id+'.innerHTML'+'&getWatcher=true&';
	params += opt;


	var nA = new Ajax.Updater(id, surl, {
		method: 'post',
		evalScripts: true,
		parameters: Base64._utf8_encode(params)
	});

	return !nA.success();	
}

function ajax_goal(obj) {
	var goal = '';
	if ( obj.href ) 	goal = obj.href;
	if ( obj.action ) 	goal = obj.action;
	if ( obj.goal ) 	goal = obj.goal;
	return goal;
}


function updateContentGets(obj,opts,ids) {	
	//var succ = Actor('update',obj.href,Array('content','navitop','navimain','bottomtools'));
	
	var goal = '';
	if ( obj.href ) 	goal = obj.href;
	if ( obj.action ) 	goal = obj.action;
	if ( obj.goal ) 	goal = obj.goal;
	

	//alert(obj.href.indexOf("#"));
	
	var succ = Actor('update',goal,Array(ids),opts);
	//var succ = Actor('update',obj.href,Array('content','navimain','bottomtools'));
	
	return false;
}

function linker(id) {	
	$('progress').show();
	var obj = this;
	//window.location.replace(obj.href);
	//obj.href = str_replace("#","",obj.href);
	var ret = updateContent(obj,'');
	
	$('progress').hide();
	return false;
	document.save(this.href);
	return ret;
	
	
}


function hrefer(ori) {
	
	
	
	var h = "";
	
	var base = windowbase+"/"+windowlang+"/";
	var gett = str_replace(base,"",ori);
	
	h+= base;
	h+= "#";
	h+= gett;
	//h+= windowsrc;
	
	return h;
}


function watcher(obj) {
	if ( !ajax_links ) return false;
	
	var tester = '';
	var arr = obj.getElementsByTagName('a');
	//alert (obj+arr.length);
		if ( arr.length > 0 ) {
		for ( var item = 0; item < arr.length; item++ ) {
			value = arr[item];
			
			tester += "<br>";
			tester += item;
			tester += "<br>";
			tester += value;

			
			
			if ( !value ) continue;
			if ( !value.id ) 		continue;
			if ( !value.href ) 		continue;

			
			if ( value.onclick ) 	{
				if ( value.onclick.length > 1 ) continue;
			}

			if ( value.href.indexOf(window.location.host) <= 0 ) continue;
			if ( value.href.indexOf('REF=') > 0 ) continue;
			if ( value.href.indexOf('mailto') > 0 ) continue;
			//if ( value.href.indexOf('#') > 0 ) continue;
			
			//value.href = hrefer(value.href);
			
			$(value.id).onclick = linker.bindAsEventListener($(value.id));
			continue;


			continue;
			Event.observe($(value.id),'click',function() { alert(1); } );
			Event.observe(window,'click',linker.bindAsEventListener($(value.id)));
			
			
		}
	}
	
	tester = '';
	
	var arr = obj.getElementsByTagName('form');
	if ( arr.length > 0 ) {
		for ( var item = 0; item < arr.length; item++ ) {
			value = arr[item];						
			
			
			value.onsubmit = function() {
				//alert('click');
				updateContent(value,$(value.id).serialize());
				//alert(value.action);
				//alert($(value.id).serialize());
				return false;
			}
			tester += "<br>";
			tester += item;
			tester += "<br>";
			tester += value;
		}
	}
	
	
	//$('dump').update(tester);
}



function updateContent(obj,opts) {	
	contenting = true;
	//var succ = Actor('update',obj.href,Array('content','navitop','navimain','bottomtools'));
	
	var goal = '';
	if ( obj.href ) 	goal = obj.href;
	if ( obj.action ) 	goal = obj.action;
	if ( obj.goal ) 	goal = obj.goal;
	
	goal = sidder(goal);
	alert(goal);
	return false;
	
	//alert(obj.href.indexOf("#"));
	
	var succ = Actor('update',goal,Array('body'),opts);
	//var succ = Actor('update',obj.href,Array('content','navimain','bottomtools'));
	contenting = false;
	return false;
	return succ;
}



Act_brain = Class.create({
	initialize: function() {
		this.watches = new Object();
		this.messages = new Object();
	},
	
	watch_init: function(obj_id) {
		if ( !this.watches[$(obj_id).form.id]) this.watches[$(obj_id).form.id] = new Object();
		if ( !this.watches[$(obj_id).form.id][obj_id]) this.watches[$(obj_id).form.id][obj_id] = new Object();
	
	},
	
	watch: function(obj_id,state,message) {
		this.watch_init(obj_id);
		this.watches[$(obj_id).form.id][obj_id].state = state;	

		this.messages[obj_id] = new Object();
		this.messages[obj_id].state = state;
		this.messages[obj_id].message = message;				
	},
	
	watchstate: function(obj_id) {
		if ( !this.watches[$(obj_id).form.id] ) return true;
		var toreturn = true;
		for ( var item in this.watches[$(obj_id).form.id] ) {
			var value = this.watches[$(obj_id).form.id][item];
			if ( true == value.state ) continue;
			toreturn = false;
		}
		return toreturn;
		
	},
	
	messageout: function() {
		if ( !this.messages ) return false;
		var out = new Array();
		for ( var item in this.messages ) {
			var value = this.messages[item];
			if ( value.state ) continue;
			out += value.message +"<br>";
		}
		return out;
	}
});

Brain = new Act_brain();

Act_handler = Class.create({

	init: function(response) {
		
		this.data = false;
		this.rawText = false;
		
		this.response = response;
		
		this.handleRaw(response);
		

		if ( false === this.data ) return this.die(false);
		
		if ( typeof(this.data) != "object" ) return false;
		
		//$('dump2').update(dump(this.data));
		
		
		this.handle();
	},
	
	call: function() {		
		//$('dump2').update(dump(this.data.state));
		var goal = 'message';
		if ( this.data.goal ) goal = this.data.goal;
		if ( typeof(this.data.fieldvalue) != "undefined" ) $(this.data.fieldid).value = this.data.fieldvalue;
		Brain.watch(this.data.fieldid,this.data.state,this.data.message);
		var watchstate = Brain.watchstate(this.data.fieldid);
		if ( this.data.disableOnFailure ) {
			if ( false == watchstate ) {
				if ( this.data.disableOnFailure ) {						
					//$(this.data.disableOnFailure).disabled = true;
					//$(this.data.disableOnFailure).addClassName('disabled');
				}				
			} else {
				if ( this.data.disableOnFailure ) {						
					$(this.data.disableOnFailure).disabled = false;
					$(this.data.disableOnFailure).removeClassName('disabled');
				}						
			}
		}
		
		
		if ( false == watchstate ) {
			$(goal).update(Brain.messageout());
			if ( $(goal).style.display == "none" ) {
				$(goal).appear({duration:0.2});
			}			
		} else {
			$(goal).hide({duration:0.5});
		}
		
		switch ( this.data.callhandler ) {
			case "formfield":				
				if ( false == this.data.state) {	
					$(this.data.fieldid).addClassName('error');
				} else {
					$(this.data.fieldid).removeClassName('error');
				}
				
				
			break;
			
		}
	},
	
	handle: function() {		
		if ( this.data.callhandler ) return this.call();
		
	},
	
	die: function() {
		return false;
	},		
	

	handleRaw: function() {
		
		if ( this.response.responseText.length === 0 ) 	return this.die(false);
		
		var temp = this.response.responseText.split("\n");
		if ( typeof(temp[0]) == "undefined" ) 			return this.die(false);
		if ( typeof(temp[1]) == "undefined" ) 			return this.die(false);
		
		var rawSuccess = ( "true" == temp[0] ) ? true : false ;
		
		if ( false == rawSuccess ) 						return this.die(false);
		
		var rawText = temp[1];
		rawText = Base64.decode(rawText);
		//$('dump1').update(rawText);
		if ( false == rawText ) 						return this.die(false);
		rawText = Base64._utf8_decode(rawText);
		this.rawText = rawText;
		
		this.data = rawText;
		
		if ( this.data.isJSON() ) {
		
			this.data = this.data.evalJSON();	
			this.data = statecorrect(this.data);
		}		
				
		
	}
});

function handleRaws(text) {
	
	if ( text.length === 0 ) 	return text;
	
	var temp = text.split("\n");
	if ( typeof(temp[0]) == "undefined" ) 		return text	
	if ( typeof(temp[1]) == "undefined" ) 		return text	
	
	var rawSuccess = ( "true" == temp[0] ) ? true : false ;
	
	if ( false == rawSuccess ) return text;						
	
	var rawText = temp[1];
	rawText = Base64.decode(rawText);
	//$('dump1').update(rawText);
	if ( false == rawText ) return text

	rawText = Base64._utf8_decode(rawText);

	var data = rawText;
	
	if ( data.isJSON() ) {
	
		data = data.evalJSON();	
		data = statecorrect(data);
	}		
	
	return data;
}

function call(obj,req) {	
	obj.goal = windowbase+req;
	updateContent(obj,'');
	return true;
}

var nAct = new Act_handler();


Act = Class.create({
	initialize: function(obj,url,req,met,args) {
		
		this.obj = obj 		|| false;
		this.url = url 		|| false;
		this.req = req 		|| false;
		this.met = met 		|| false;
		this.args = args 	|| false;

		this.fullRequest 	= this.buildFullRequest();
		
		this.rawState 		= false;	
				
		if ( false === this.request() ) 		return true;
		
	},

	buildFullRequest: function() {
		var request = '';	
		//request = this.url;	
		//return request;
		if ( this.met == "post" ) {
			request = this.url;		
			return request;
		}
		
		request += this.req;
		request += "/field:"+urlencode(this.obj.name);
		request += "/value:"+urlencode(this.obj.value);
		//request = Base64._utf8_encode(request);
		//request = request.serialize;	
		request = this.url + "/" + request;
		return request;	
	},
	
	rawEval: function(response) {

		nAct.init(response);
	},
	
	request: function() {
		
		var ajax_request = false;
		var self = this;

		this.a = 10;
		
		if ( "get" == this.met ) {
			ajax_request = new Ajax.Request(this.fullRequest, {
					method: this.met,			
					evalScripts: true,
					onSuccess: this.rawEval
				}		
			);
		} else {					
			//alert(this.fullRequest);
			//alert(this.fullRequest);						
			
			ajax_request = new Ajax.Request(this.fullRequest, {
					method: 'post', 
					evalScripts: true, 
					parameters: Base64._utf8_encode(this.args),
					encoding: 'UTF-8',
					onSuccess: this.rawEval
				}		
			);
		}

		if ( ajax_request.success() ) {
		
			this.rawState = true;
			return true;
		}
		
		this.rawState = false;
		return false;	
	}, 
	
	update : function(id,url) {
		//alert(id);
		return false;
	}
});

var currenturl = false;

Actor_replace = function(url,id,opt) {
	var surl = url;			
	
	var params = '&getElementById='+id+'.innerHTML'+'&getWatcher=true&';
	params += opt;
	
	//params = '';

	//alert(url+"&"+params);

	var nA = new Ajax.Updater(id, surl, {
		method: 'post',
		evalScripts: true,
		parameters: Base64._utf8_encode(params)
	});

	return !nA.success();

}

Actor = function(sec,url,args,opt) {
	
	switch ( sec ) {

	
		case "update":
			//return true;
				
		
			var succ = true;
			
			for ( var cnt = 0; cnt < args.length; cnt++ ) {	
				var id = args[cnt];
				
				succ = Actor_replace(url,id,opt);
				if ( id == "content" ) {


					//$(id).hide();
					//Effect.BlindDown(id);		
				}				
				
				//watcher($(id));
				if ( true == succ ) return succ;	
				
				//watcher($(id));
				
			}
			
			return succ;
			return false;
			var succ = arguments[2].invoke('Actor_replace');
			return Actor_replace(arguments[1],args);
		break;		
	}
	return true;
}




