/***
 * Creates new geobip object
 */
function geobip() {
	this.root				= '';
	this.session			= '';
	this.url				= '';
	
	this.townPk				= '';
	this.quarterPk			= '';
	this.distance			= 15;

	this.menu				= '';
	this.title				= '';

	this.projectsetting		= false;

	this.stopMapLoading		= false;

	this.ajax = new ajax();
	this.ajax.parent = this;
};

// Set root
geobip.prototype.setRoot = function (strRoot) {
	if(typeof strRoot == 'string') {
		this.root = strRoot;
	}
};

// Returns root
geobip.prototype.getRoot = function () {
	return this.root;
};

// Set url
geobip.prototype.setUrl = function (strUrl) {
	if(typeof strUrl == 'string') {
		this.url = strUrl;
	}
};

// Returns url
geobip.prototype.getUrl = function () {
	return this.url;
};

// Set session
geobip.prototype.setSession = function (strSession) {
	if(typeof strSession == 'string') {
		this.session = strSession;
	}
};

// Returns session
geobip.prototype.getSession = function () {
	return this.session;
};

// Set title
geobip.prototype.setTitle = function (title) {
	this.title = title;
	window.status = this.title;
};

// Returns session
geobip.prototype.getTitle = function () {
	return this.title;
};

// Set value
geobip.prototype.setHasProjectSetting = function (value) {
	this.projectsetting = Boolean(value);
};

// Returns value
geobip.prototype.hasProjectSetting = function () {
	return this.projectsetting;
};

// Set current town pk
geobip.prototype.setTownPk = function (townPk) {
	if(typeof townPk == 'string') {
		if(townPk != 'clear') {
			this.townPk = townPk;
		} else {
			this.townPk = '';
			this.quarterPk = '';
		}
	}
};

// Returns current town pk
geobip.prototype.getTownPk = function () {
	return this.townPk;
};

// Set current quarter pk
geobip.prototype.setQuarterPk = function (quarterPk) {
	if(typeof quarterPk == 'string') {
		this.quarterPk = quarterPk;
	}
};

// Returns current quarter pk
geobip.prototype.getQuarterPk = function () {
	return this.quarterPk;
};

// Stops map loading because auf double onclick events
geobip.prototype.stopLoadingMap = function () {
	this.stopMapLoading = true;
};

// Set focus on a field
geobip.prototype.setFieldFocus = function (fieldname) {
	var field = GetObject(fieldname);
	if(field && field.value == '') { field.focus(); };
};

// Switch loading box
geobip.prototype.switchLoading = function (show) {
	var lay = GetLayer('boxLoading');
	var body = document.getElementsByTagName("body")[0];
	if(lay && body && this.ajax.withLoading) {
		if(show == '1' || show == 1) {
			SwitchLayer(lay, 1);

			var bodyWidth = parseInt(GetWidth(body));
			var bodyHeight = parseInt(GetHeight(body));
			
			var loadWidth = parseInt(GetWidth(lay));
			var loadHeight = parseInt(GetHeight(lay));

			SetStyle(lay, 'left', (bodyWidth/2-loadWidth/2) + 'px');
			SetStyle(lay, 'top', (bodyHeight/2-loadHeight/2) + 'px');
		} else {
			SwitchLayer(lay, 0);
		}
	}
};

// Load box
geobip.prototype.load = function (target, type, query) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.load(target, type, query); }, 10);
	} else {
		this.ajax.load(target, this.getRoot() + 'load.php?' + this.getSession() + '&type=' + type + getQueryGlue(query));
	}
};

// Send box
geobip.prototype.send = function (target, type, query, formname) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.send(target, type, query, formname); }, 10);
	} else {
		this.ajax.send(target, this.getRoot() + 'load.php', this.getSession() + '&type=' + type + getQueryGlue(query), formname);
	}
};

// Finish upload
geobip.prototype.finishUpload = function (progressId) {
	var current = this;
	
	this.ajax._closeUploadStatus(progressId);
};

// Open detail box
geobip.prototype.loadContent = function (query) {
	if(BoxManager) BoxManager.closeBoxes();

	this.load('boxContent', 'content', query);
};

// Send detail box
geobip.prototype.sendContent = function (query, formname) {
	this.send('boxContent', 'content', query, formname);
};

// Refresh navigation
geobip.prototype._refreshNavigation = function () {
	var query = 'action=navigation';

	if(typeof this._refreshNavigation.arguments[0] != 'undefined' && this._refreshNavigation.arguments[0] != '') {
		var townPk = this._refreshNavigation.arguments[0];

		if(townPk != 'clear') {
			this.townPk = townPk;
		} else {
			this.townPk = '';
			this.quarterPk = '';
		}

		query += '&town=' + this.townPk;
	}
	if(typeof this._refreshNavigation.arguments[1] != 'undefined' && this._refreshNavigation.arguments[1] != '') {
		this.quarterPk = this._refreshNavigation.arguments[1];

		query += '&quarter=' + this.quarterPk;
	}
	
	this.load('boxNavigation', 'general', query);
};

// Refresh navigation
geobip.prototype._refreshTitle = function () {
	var query = 'action=title';

	if(typeof this._refreshTitle.arguments[0] != 'undefined' && this._refreshTitle.arguments[0] != '') {
		var townPk = this._refreshTitle.arguments[0];

		if(townPk != 'clear') {
			this.townPk = townPk;
		} else {
			this.townPk = '';
			this.quarterPk = '';
		}

		query += '&town=' + this.townPk;
	}
	if(typeof this._refreshTitle.arguments[1] != 'undefined' && this._refreshTitle.arguments[1] != '') {
		this.quarterPk = this._refreshTitle.arguments[1];

		query += '&quarter=' + this.quarterPk;
	}
	
	this.load('boxTitle', 'general', query);
};

// Set search value
geobip.prototype.setSearchListValue = function (val, formname) {
	if(document.forms[formname] && document.forms[formname].search) {
		document.forms[formname].search.value = val;
	}
};

// Do search
geobip.prototype.doSearchList = function (id, action, formname) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.doSearchList(id, action, formname); }, 10);
	} else if(document.forms[formname] && document.forms[formname].search && document.forms[formname].filtermethods) {
		var searchQuery = document.forms[formname].query.value;
		var searchValue = document.forms[formname].search.value;
		var searchFiltermethods = document.forms[formname].filtermethods.value;
		var query = searchQuery + '&list[0]=filtermethods-' + encodeURI(searchFiltermethods) + '&list[1]=filter-' + encodeURI(searchValue);

		this.loadList(id, action, query);
		
		if(document.forms['FORMSELECT']) {
			var elems = document.forms['FORMSELECT'].elements;
			for(var e=0;e<elems.length;e++) {
				elems[e].value = '';
			}
		}
	}
};

// Do extended search for objects
geobip.prototype.doSearchExtended = function (formname) {
	var boxSearchExtended = GetLayer('boxSearchExtended');
	if(boxSearchExtended) {
		//SwitchLayer(GetLayer('boxSearchExtended'), 0);
		//switchImage('imgSearchExtended', 0);

		//SwitchLayer(GetLayer('boxSearchResults'), 1);
		//switchImage('imgSearchResults', 1);
	
		this.switchLoading(1);
		this.send('boxSearchResults', 'box', 'action=view.search.results', formname);
	}
};
	
// Do search for towns by text
geobip.prototype.doSearchTown = function (fldTownId, fldDistanceId) {

	var field = GetObject('fld' + fldTownId);
	var search = GetObject('txt' + fldTownId);
	var distance = GetObject('fld' + fldDistanceId);

	if(field.value == '') {
		var townPk = 'clear';
	} else {
		var townPk = field.value;
	}

	if((townPk == 'clear' || field.value == '') && search.value == '') {
		return;
	}
	
	if(SelectObject) { SelectObject.closeFilterbox(); }
	
	SetContent(GetLayer('boxSearchTownResults'), '');
	
	this.load('boxSearchTownResults', 'content', 'action=view.search.town.results&searchTown=' + townPk + '&text=' + search.value + '&distance=' + distance.value);

	SwitchLayer(GetLayer('boxWelcome'), 0);
	SwitchLayer(GetLayer('boxSearchTownResults'), 1);
};

// Do search for objects by text
geobip.prototype.doSearchObject = function (fldObjectId) {
	
	var field = GetObject('fld' + fldObjectId);
	var search = GetObject('txt' + fldObjectId);

	this.ajax.withLoading = false;

	SetContent(GetLayer('boxSearchObjectResults'), '');

	this.load('boxSearchObjectResults', 'content', 'action=view.search.object.results&text=' + search.value);
	this.ajax.withLoading = true;

	SwitchLayer(GetLayer('boxWelcome'), 0);
	SwitchLayer(GetLayer('boxSearchObjectResults'), 1);
};

// Open box
geobip.prototype.loadBox = function (id, action, query) {
	if(BoxManager) {
		var Box = BoxManager.showBox(id);
		
		this.load(Box.getContentId(), 'box', 'action=' + action + getQueryGlue(query));
	} else {
		alert('Manager for Boxes not found!');
	}
};

// Open box
geobip.prototype.loadList = function (id, action, query) {
	this.load('box' + id, 'lists', 'action=' + action + getQueryGlue(query));
};

// Send box
geobip.prototype.sendBox = function (id, action, query, formname) {
	if(BoxManager) {
		var Box = BoxManager.showBox(id);
		
		this.send(Box.getContentId(), 'box', 'action=' + action + getQueryGlue(query), formname);
	} else {
		alert('Manager for Boxes not found!');
	}
};

// Open box
geobip.prototype.openBox = function (obj, id, title, width, height, action, query) {
	if(BoxManager) {
		if(typeof this.openBox.arguments[7] != 'undefined') {
			var resizeable = this.openBox.arguments[7];
		} else {
			var resizeable = '';
		}

		var Box = BoxManager.showBox(id, obj, title, width, height, resizeable);

		this.load(Box.getContentId(), 'box', 'action=' + action + getQueryGlue(query));

		return Box;
	} else {
		alert('Manager for Boxes not found!');
	}

	return null;
};

// Show box
geobip.prototype.showBox = function (id) {
	if(BoxManager) {
		return BoxManager.showBox(id);
	} else {
		alert('Manager for Boxes not found!');
	}

	return null;
};

// Close box
geobip.prototype.closeBox = function (id) {
	if(BoxManager) {
		BoxManager.closeBox(id);
	} else {
		alert('Manager for Boxes not found!');
	}
};

// Open box
geobip.prototype.loadLogin = function (id, action, query) {
	if(BoxManager) {
		var Box = BoxManager.showBox(id);
		
		this.load(Box.getContentId(), 'login', 'action=' + action + getQueryGlue(query));
	} else {
		alert('Manager for Boxes not found!');
	}
};

// Send box
geobip.prototype.sendLogin = function (id, action, query, formname) {
	if(BoxManager) {
		var Box = BoxManager.showBox(id);
		
		this.send(Box.getContentId(), 'login', 'action=' + action + getQueryGlue(query), formname);
	} else {
		alert('Manager for Boxes not found!');
	}
};

// Set current list height, only for FireFox or Browser with Style 'maxHeight'
geobip.prototype.setListHeight = function (listId) {
	var objTbl = GetObject('box' + listId);
	if(objTbl) {
		var height = parseInt(GetHeight(objTbl));

		if(height > 0 && !IE) {
			var objDiv = GetObject('List');
			var objContent = GetObject('ListContent');
			var objListHeader = GetObject('ListHeader');
			var objListActions = GetObject('ListActions');
			var objListFooter = GetObject('ListFooter');

			if(objListHeader) {
				var heightHeader = parseInt(GetHeight(objListHeader));
			} else {
				var heightHeader = 0;
			}
			if(objListActions) {
				var heightActions = parseInt(GetHeight(objListActions));
			} else {
				var heightActions = 0;
			}
			if(objListFooter) {
				var heightFooter = parseInt(GetHeight(objListFooter));
			} else {
				var heightFooter = 0;
			}

			var contentHeight = height - heightHeader - heightActions - heightFooter;

			SetStyle(objContent, 'height', parseInt(contentHeight) + 'px');
		}
	}
}

// Switch the extended search form for objects
geobip.prototype.switchSearchExtended = function () {
	var radLand = GetObject('radLand');
	var radRealty = GetObject('radRealty');
	var radPrivate = GetObject('radPrivate');
	var radBusiness = GetObject('radBusiness');
			
	var bLandPrivate = bLandBusiness = bRealtyPrivate = bRealtyBusiness = 0;
		
	if(radLand.checked && radPrivate.checked) {
		bLandPrivate = 1;
	} else if(radLand.checked && radBusiness.checked) {
		bLandBusiness = 1;
	} else if(radRealty.checked && radPrivate.checked) {
		bRealtyPrivate = 1;
	} else if(radRealty.checked && radBusiness.checked) {
		bRealtyBusiness = 1;
	}
			
	SwitchLayer(GetLayer('boxSearchExtendedLandPrivate'), bLandPrivate);
	SwitchLayer(GetLayer('boxSearchExtendedLandBusiness'), bLandBusiness);
	SwitchLayer(GetLayer('boxSearchExtendedRealtyPrivate'), bRealtyPrivate);
	SwitchLayer(GetLayer('boxSearchExtendedRealtyBusiness'), bRealtyBusiness);
}

// Load map
geobip.prototype.loadMap = function (townPk, quarterPk, posX, posY, zoom) {
	if(this.stopMapLoading) {
		this.stopMapLoading = false;
		return;
	}

	if(!BoxManager.isBoxClosed('Welcome')) {
		BoxManager.closeBox('Welcome');
	}
	if(!BoxManager.isBoxClosed('Quarters') && townPk != this.getTownPk()) {
		BoxManager.closeBox('Quarters');
	}
	if(!BoxManager.isBoxClosed('TownLegend') && townPk != this.getTownPk()) {
		BoxManager.closeBox('TownLegend');
	}

	if(townPk == this.getTownPk()) {
		MAP.doCenter(posX, posY, zoom);
		
		if(this.getTownPk() != '') {
			this.doSearchExtended('FORMSEARCHOBJECTS');
		}
	} else {
		if(MAP.isInitalized()) {
			MAP.destroy();
		}
		
		this._refreshNavigation(townPk, quarterPk);
		//this._refreshTitle(townPk, quarterPk);

		this.load('boxContent', 'content', 'action=view.map&town=' + townPk + '&quarter=' + quarterPk + '&posX=' + posX + '&posY=' + posY + '&zoom=' + zoom);

		this.openBox('', 'Search', 'Grundstücks- und Immobiliensuche', CONF_BOX_SEARCH_WIDTH, CONF_BOX_SEARCH_HEIGHT, 'view.search', '');

		this.switchLoading(0);
	}
};

// Open object image gallery
geobip.prototype.openObjectGallery = function (image) {
	if(image) {
		var objectPk = image.getAttribute('objectPk');
		var imagePk = image.getAttribute('imagePk');
		var title = image.getAttribute('alt');

		this.openBox('', 'Gallery' + objectPk, title, '550', '450', 'view.object.gallery', 'object=' + objectPk + '&image=' + imagePk);
	}
};

// Show image of object
geobip.prototype.showObjectImage = function (imageId, objectPk, imagePk, imagePath) {
	var image = GetImage(imageId);
	if(image) {
		image.setAttribute('objectPk', objectPk);
		image.setAttribute('imagePk', imagePk);
		image.src = this.getRoot() + imagePath;
	}
};

// Open download window
geobip.prototype.openDownload = function (name, path, file, pk) {
	window.location.href = this.getRoot() + 'download.php?' + this.getSession() + '&name=' + name + '&path=' + path + '&file=' + file + '&pk=' + pk;
};

// Open download window
geobip.prototype.openPrint = function (action, query) {
	var win = window.open(this.getRoot() + 'print.php?' + this.getSession() + '&action=' + action + getQueryGlue(query),'Print'+action,'dependent=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=600');
	if(win) {
		win.focus();
	}
};

// Open download window
geobip.prototype.openWindow = function (action) {
	var win = window.open(action,'','dependent=yes,toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=600');
	if(win) {
		win.focus();
	}
};
