   /**
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GPL.
    *
    * Copyright(c) 2005 by Harald Hanek. All rights reserved.
    *
    * To contact the author write to: harald.hanek@easycp.de
    * The latest version of Wikipedia Browser can be obtained from: http://www.gollum-browser.com
	*/

function refreshAd(){
	return true;
	var d=document.getElementById('googleads');
	if(d){	
		var s=d.getElementsByTagName('iframe');
		if(s && s.length){
			s[0].src=s[0].src+'&'+new Date().getTime();
		}
	}
	return true;
}


	function gollum()
	{
		this._History 		= new Array();	// Array der besuchten Seiten
		this._HistoryPos 	= 0;			// Position in der History
		this._Searchterms 	= '';			// Suchbegiffe
		this._resulturi 	= '';			// Url zur aktuellen Seite
		this._edituri 		= '';			// Url zum bearbeiten auf Wikipedia
		
		this._ajax = null;
		
		
		this.win = window;
		this._printwin;

		this._content;
		this._view;
		this._searchfield;
		
		this._babel;
		this._babeldiv;
		this._babelitem = new Array();

	 // Buttons		
		this._btn_back;
		this._btn_forward;
		this._btn_stop;
		this._btn_print;
		this._btn_home;
		this._btn_go;
		this._btn_save;
		
		this._btn_edit;
		this._btn_rec;
		
		
		var oThis = this;
		this.__init = function (){ oThis._init(); };
		this.__search = function (e){ oThis._search(e); };
		this.__selectsearch = function (){ oThis._selectsearch(); };
		this.__fetchResult = function (data){ oThis._fetchResult(data); };
		this.__resize = function (){ oThis._resize(); };
		this._init();
	};


	gollum.prototype._init = function()
	{
		this._content 		= Global.find('bc');
		this._view			= Global.find('vb');
		this._searchfield 	= Global.find('search');
		this._babel			= Global.find('babel');
		this._babeldiv		= Global.find('babeldiv');

		this._btn_back = new Button('btn_back');
		this._btn_back.setDisabled();
		this._btn_forward = new Button('btn_forward');
		this._btn_forward.setDisabled();
		
		this._btn_stop = new Button('btn_stop');
		this._btn_stop.setDisabled();
		
		this._btn_print = new Button('btn_print');
		this._btn_home = new Button('btn_home');
		this._btn_go = new Button('btn_go');
		this._btn_go.setDisabled();


		this._btn_edit = new Button('btn_edit');
		this._btn_edit.setDisabled();
		this._btn_rec = new Button('btn_rec');
		this._btn_rec.setDisabled();


		Global.addEvent(this.win, 'resize', this.__resize);

		Global.addEvent(this._searchfield, 'keyup', this.__search);
		Global.addEvent(this._searchfield, 'blur', this.__search);
		Global.addEvent(this._searchfield, 'mouseup', this.__selectsearch);
		this._resize();
	};


	gollum.prototype._selectsearch = function()
	{
		if(this._searchfield.value.length > 0)this._searchfield.select();
	};


	gollum.prototype._onload = function()
	{
		//if(typeof e == 'undefined') e = window.event;
		if(this._searchfield.value.length > 0) this._search(null, true);
			else this._searchfield.focus();
	};




	gollum.prototype._search = function(e, b)
	{
		if(typeof e == 'undefined') e = window.event;
		
		var v = this._searchfield.value;
		if(v.length > 0)
			this._btn_go.setEnabled();
		else
		{
			this._btn_go.setDisabled();
			return;
		}
		
		//v = this._capitalise(v);
		v = encodeURIComponent(v);
		//v = escape(v);
		
		//v = v.replace(new RegExp("q=[^&$]*"),"q="+v);
		//v = escape(this._Searchterms);
		
		if(this._xstatus === true) return;
		if((e && e.keyCode == 13) || b)
		{
			this._resize();		
			this._Searchterms = v;
			this._fetch();
			return;
		}
	};
	

	gollum.prototype._fetch = function()
	{
		//if(this._Searchterms) this._fetchdata("http://"+config.wikilang+config.uri+escape(this._Searchterms), this.__fetchResult);
		//if(this._Searchterms) this._fetchdata("http://"+config.wikilang+config.uri+this._Searchterms, this.__fetchResult);
		if(this._Searchterms) this._fetchdata(null, this._Searchterms, config.wikilang, this.__fetchResult);
	};
	
	
	gollum.prototype._home = function()
	{
		this._fetchdata("http://"+config.wikilang+config.uri, this.__fetchResult);
	};
	
	
	gollum.prototype._link = function(v)
	{
		this._btn_forward.setDisabled();
		if(!browser.isIE5up) v = encodeURI(v);
		this._fetchdata(v, this.__fetchResult);
	};
	
	
	gollum.prototype._stop = function()
	{
		this._destroy();	// vorhandenes Ajax Object killen
		
		if(this._HistoryPos > 0)
		{
			this._setHis(this._History[this._HistoryPos-1]);
			this._checkBtn();
			
		} else {

			document.title 				= msg[3];
			this._view.innerHTML 		= msg[3];
			this._resulturi 			= '';
	
			Global.setAttribute(this._content, 'lang', config.dlg[0]);
			Global.setAttribute(this._content, 'dir', config.dlg[1]);


			this._content.innerHTML = config.start;
			this._resize();
			this._checkBtn();
		}
	};


	gollum.prototype._fetchdata = function()
	{
		this._destroy();	// vorhandenes Ajax Object killen
		
		this._btn_forward.setDisabled();
		this._btn_back.setDisabled();
		this._btn_print.setDisabled();
		this._btn_home.setDisabled();
		this._btn_stop.setEnabled();
		
		wb.b._btn_favadd.setDisabled();


		this._content.innerHTML = config.co_loader;
		
		this._ajax = new xmlhttp();
		this._ajax._load("fetchData", this._fetchdata.arguments, fetch.data);
	};	
	

	gollum.prototype._destroy = function()
	{
		if(this._ajax != null)
		{
			delete this._ajax;
			this._ajax = null;
		}
	};
	
	
	gollum.prototype._fetchResult = function(result)
	{
		if(this._ajax == null)
			return;
		
		this._destroy();	// vorhandenes Ajax Object killen
		
		if(result == false)
		{
			alert(msg[21]);
			this._stop();
			return;
		}
		ras = result.substr(result.indexOf("{_")+2, result.indexOf("_}")-2);
		
		ra = ras.split("######");
		resulturi = ra[0];
		wikilang = ra[1];
		wikidir = ra[2];
		edituri = ra[3];
		if(edituri != '')
			edituri = edituri.replace(/&amp;/, "&");
		
		this._edituri = edituri;
		this._resulturi = resulturi;
		
		Global.setAttribute(this._content, 'lang', wikilang);
		Global.setAttribute(this._content, 'dir', wikidir);
		
		
		wb.b._listinit();
		
		data = result.substr(result.indexOf("_}")+2);
		
		this._content.innerHTML = '<a name="i"></a>'+data;
		
		this._cleaner();
		
			var title = "";
			try
			{
				title = this._content.getElementsByTagName("H1")[0].innerHTML; // Titel
			} catch(e){}

		this._save(title, resulturi, wikilang, wikidir, edituri, this._content.innerHTML); // in History speichern
			
		this._resize();
		this._content.getElementsByTagName("A")[0].focus();
		
		refreshAd();
	};


	gollum.prototype._cleaner = function()
	{
		var as = this._content.getElementsByTagName("FORM");
		for (i = 0; i <= as.length-1; i++)
		{
			if(as[i].target == "")
				as[i].target="_BLANK";
		}
	};
	
	
	gollum.prototype._print = function()
	{
		if(this._printwin && this._printwin.closed != true) this._printwin.close();
		this._printwin = null;
		this._printwin = window.open("", "print", "width=750, height=550, menubar=yes, scrollbars=yes");
		c = '<html dir="ltr" lang="de"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Wikipedia Browser</title><link rel="stylesheet" type="text/css" href="'+config.theme+'print.css"></head><body><div id="bc">'+this._content.innerHTML+'</div></body></html>';
		this._printwin.document.write(c);
		return;
	};
	
	
	gollum.prototype._edit = function()
	{
		if(this._edituri != '') window.open(this._edituri);
	};
	
	
	gollum.prototype._rec = function()
	{
		if(this._resulturi != '') dialog(fetch.reco, 'recommend', '', 'modal', 505, 380, false);
	};


	gollum.prototype._save = function(strSearch, resulturi, lang, dir, edituri, strCache)
	{
		document.title = strSearch;
		this._view.innerHTML = strSearch;
		var oData = document.createElement("DIV");
		oData.setAttribute("search", strSearch); 
		oData.setAttribute("uri", resulturi); 
		oData.setAttribute("edit", edituri); 
		oData.setAttribute("cache", strCache);
		oData.setAttribute("lang", lang); 
		oData.setAttribute("dir", dir);
		
		if(this._HistoryPos < this._History.length)
			this._History.splice(this._HistoryPos++,0,oData);
		else
		{
			this._History[this._History.length] = oData;
			this._HistoryPos = this._History.length;
		}	
		
		this._checkBtn();
	};

	
	gollum.prototype._checkBtn = function()
	{
		this._btn_stop.setDisabled();
		this._btn_home.setEnabled();
		
		if(this._HistoryPos > 1)
			this._btn_back.setEnabled();
		else
			this._btn_back.setDisabled();

		if(this._resulturi != '')
		{
			this._btn_print.setEnabled();
			this._btn_rec.setEnabled();
			
		} else {
			
			this._btn_print.setDisabled();
			this._btn_rec.setDisabled();
			
		}
		if(this._edituri != '') this._btn_edit.setEnabled();
			else this._btn_edit.setDisabled();
		
		wb.b._init();

		if(this._HistoryPos < this._History.length)
			this._btn_forward.setEnabled();
		else
			this._btn_forward.setDisabled();
	};
	
	
	gollum.prototype._goBack = function()
	{
		if(this._HistoryPos > 0) this._HistoryPos-=1;
		this._checkBtn();
		
		if(!this._HistoryPos) return;
		this._setHis(this._History[this._HistoryPos-1]);
	};
	
	
	gollum.prototype._goForward = function()
	{
		if(this._HistoryPos < this._History.length) this._HistoryPos++;
		//this._checkBtn();
		
		if(!this._HistoryPos) return;
		this._setHis(this._History[this._HistoryPos-1]);
	};


	gollum.prototype._setHis = function(elm)
	{
		this._content.innerHTML 	= elm.getAttribute("cache");
		document.title 				= elm.getAttribute("search");
		this._view.innerHTML 		= elm.getAttribute("search");
		this._resulturi 			= elm.getAttribute("uri");
		this._edituri 				= elm.getAttribute("edit");

		Global.setAttribute(this._content, 'lang', elm.getAttribute("lang"));
		Global.setAttribute(this._content, 'dir', elm.getAttribute("dir"));

		this._checkBtn();
		this._resize();
		wb.b._listinit();
	};

	
	gollum.prototype._resize = function()
	{
		var dif = Global.find('to').offsetHeight+Global.find('ad').offsetHeight+Global.find('vb').offsetHeight;
		var bookdif = Global.find('bookmarktoolbar').offsetHeight+dif;
		var w = Global.win_w(); 
		var h = Global.win_h();
	
		if (h >= dif && w > 0)
		{
			var rw = Global.find('bookmanager').offsetWidth;
			tw = (rw > 0)?rw+7:0;
			this._content.style.width = w-tw;
			this._content.style.height = h-dif;
			
			Global.find('bo').height = h-dif;
			Global.find('bookmarklist').style.height = h-bookdif;
			
			return;
		}
	};
	



	gollum.prototype._init_babel = function()
	{
		var a = this._babeldiv.getElementsByTagName("DIV");
		for(var i=0; i < a.length; i++)
		{
			this._babelitem[i] 	= new BabelButton(a[i], i);
		}
	};