// Main functions
UTF8 = {
	encode: function(s){
		for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
			s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
		);
		return s.join("");
	},
	decode: function(s){
		for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
			((a = s[i][c](0)) & 0x80) &&
			(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
			o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
		);
		return s.join("");
	}
};

function writeHPos(hId, d) {
	new Ajax('/writehpos.cfm', {
		method: 'get',
		data: {
			'hId': hId,
			'd': d
		}
	}).request();
}

function writeBPos(bId) {
	new Ajax('/writebpos.cfm', {
		method: 'get',
		data: {
			'bId': bId,
			'tsc': tagInfo[bId].tsc,
			'pos': tagInfo[bId].pos,
			'checkSum': tagInfo[bId].checkSum
		}
	}).request();
}
function setTagMode(mode) {
	switch(mode) {
		case 'auto':
			Cookie.set('TAGCLOUDMODE', 'auto', {duration: 30, domain: '.suche.ch', path: '/'});
			break;
		case 'hide':
			Cookie.set('TAGCLOUDMODE', 'hide', {duration: 30, domain: '.suche.ch', path: '/'});
			break;
		case 'show':
			Cookie.set('TAGCLOUDMODE', 'show', {duration: 30, domain: '.suche.ch', path: '/'});
			break;
	}
}

function smokeAlert(msg) {
	Growl.Smoke({text: msg});
}
function smokeGood(msg) {
	Growl.Smoke({image:'/style/image/growl-good.gif', text: msg});
}
function smokeNok(msg) {
	Growl.Smoke({image:'/style/image/growl-nok.gif', text: msg});
}



function addToFav(obj, id) {
	var switchImage = (arguments.length == 3 && arguments[2] == true);
	var tmp = (Cookie.get('BIDS') || ''),
		bids = tmp.split(','),
		exec = true;

	for(var i = 0; i < bids.length; i++) {
		if(bids[i] == id) {
			exec = false;
			smokeNok('Dieser Eintrag ist bereits unter Ihren Favoriten');
			break;
		}
	}
	if(exec) {
		Cookie.set('BIDS', tmp + ',' + id, {duration: 30, domain: '.suche.ch', path: '/'});
		smokeGood(unescape('Dieser Banner wurde zu \"Ihren Favoriten\" auf der \"suche.ch-Startseite\" hinzugef%FCgt'));
	}
	if(switchImage) obj.getElement('img').src = 'style/image/more-add-d.png';
}

function showMoreBox(listItem) {
	if(listItem.getElement('div.more-info')) listItem.getElement('div.more-info').setStyle('display', ''); //FxStyle = new Fx.Style(listItem.getElement('div.more-info'), 'opacity', {duration: 100}).start(0, 1);
	if(listItem.getElement('div.more-opt')) listItem.getElement('div.more-opt').setStyle('display', ''); //FxStyleOpt = new Fx.Style(listItem.getElement('div.more-opt'), 'opacity', {duration: 100}).start(0, 1);
}


function hideMoreBoxes() {
	if($('banners')) {
		/*
		if(FxStyle) FxStyle.stop();
		if(FxStyleOpt) FxStyleOpt.stop();
		$('banners').getElements('div.more-info').forEach(function(moreBox) {
			moreBox.setStyle('opacity', 0);
		});
		$('banners').getElements('div.more-opt').forEach(function(optBox) {
			optBox.setStyle('opacity', 0);
		});
		*/
		$('banners').getElements('div.more-info').forEach(function(moreBox) {
			moreBox.setStyle('display', 'none');
		});
		$('banners').getElements('div.more-opt').forEach(function(optBox) {
			optBox.setStyle('display', 'none');
		});
	 }
}

function mouseOverLock() {
	mouseOverLockForHS = true;
	window.setTimeout("mouseOverLockForHS = false", hs.expandDuration);
}

function showBox(obj, href) {
	mouseOverLock();
	
	try	 {
		if(href.indexOf('?') != -1) {
			obj.href = href + "&rnd=_" + Math.random();
		}
		else {
			obj.href = href + "?rnd=_" + Math.random();
		}
	}
	catch(err) {
	}
	
	hs.width = 820;
	hs.height = 520;	
	hs.preserveContent  = false;
	hs.Expander.prototype.onAfterExpand = function() {
		this.a.href = this.a.getProperty('rel');
	}
	return hs.htmlExpand(obj, {
		objectType: 'iframe',
		objectLoadTime: 'after',
		objectWidth: 820,
		objectHeight: 520
		}
	);
}

function showBoxFavorites(obj, href) {
	mouseOverLock();
	obj.href = href;
	hs.width = 920;
	hs.height = 580;
	hs.preserveContent  = false;
	hs.Expander.prototype.onAfterExpand = function() {
		this.a.href = this.a.getProperty('rel');
	}
	return hs.htmlExpand(obj, {
		objectType: 'iframe',
		objectLoadTime: 'after'
		}
	);
}

function showBoxFavoritesEmail(obj, href) {
	mouseOverLock();
	obj.href = href;
	hs.width = 460;
	hs.height = 250;
	hs.preserveContent  = false;
	hs.Expander.prototype.onAfterExpand = function() {
		this.a.href = this.a.getProperty('rel');
	}
	return hs.htmlExpand(obj, {
		objectType: 'iframe',
		objectLoadTime: 'after'
		}
	);
}

function showBoxVideo(obj, href, w, h) {
	mouseOverLock();
	obj.href = href;
	if (window.ie) {
		hs.width = w + 2;
		hs.height = h + 20;
	}
	else {
		hs.width = w;
		hs.height = h;
	}
	hs.preserveContent  = false;
	hs.Expander.prototype.onAfterExpand = function() {
		this.a.href = this.a.getProperty('rel');
	}
	return hs.htmlExpand(obj, {
		objectType: 'iframe',
		objectLoadTime: 'after'
		}
	);
}

function showTagOptions(obj) {
	var single = document.createElement('a'),
		combined = document.createElement('a')
		els = $('umkDiv').getElements('input[name=r]'),
		umkreis = 0;
	hideTagOptions();
	for(var i = 0; i < els.length; i++) {
		if(els[i].checked) {
			umkreis = els[i].value;
			break;
		}
	}
	$('inline-suggestion').style.display = '';
	single.innerHTML = obj.innerHTML + ((htmlLocation != '' && htmlLocation == $('ort').value) ? (' in ' + htmlLocation) : '');
	single.href = '/info/' + obj.innerHTML + '/' + ((srLocation != '' && htmlLocation == $('ort').value) ? (srLocation + '/') : '');
	combined.innerHTML = htmlOldTags + ' ' + obj.innerHTML + ((htmlLocation != '' && htmlLocation == $('ort').value) ? (' in ' + htmlLocation) : '');
	combined.href = '/info/' + oldTags + ' ' + obj.innerHTML + '/' + ((srLocation != '' && htmlLocation == $('ort').value) ? (srLocation + '/') : '');
	$('inline-suggestion-single').appendChild(single);
	$('inline-suggestion-combined').appendChild(combined);
	obj.blur();
	//optionTimer = window.setTimeout("hideTagOptions();", 7000);
}

function hideTagOptions() {
	window.clearTimeout(optionTimer);
	$('inline-suggestion-single').innerHTML = '';
	$('inline-suggestion-combined').innerHTML = '';
	$('inline-suggestion').style.display = 'none';
}

function encode_utf8(rohtext) {
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++) {
		var c=rohtext.charCodeAt(n);
		if(c<128) {
			utftext += String.fromCharCode(c);
		} else if ((c>127) && (c<2048)) {
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		} else {
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		}
	}
	return utftext.replace(/\//g, "%2F");
}

function searchEncode(input) {
	var output = '',
		letter = '';
	for(var i = 0; i < input.length; i++) {
		letter = input.substr(i, 1);
		
	}
}

function sendForm() {
	if($('tag').value.trim() == '') {
		alert(unescape('Bitte geben Sie zuerst einen Suchbegriff ein!'));
		$('tag').focus();
		return;
	}
	var out = '/info/' + escape(UTF8.encode($('tag').value.trim())) + '/';

	if($('ort').value.trim() != '') {
		out += $('ort').value.trim() + '/';
		var els = $('umkDiv').getElements('input[name=r]'),
			umkreis = 0;
		for(var i = 0; i < els.length; i++) {
			if(els[i].checked && els[i].value > 0) {
				umkreis = els[i].value;
				break;
			}
		}
		if(umkreis > 0) out += '?r=' + umkreis;
	}
	if(!window.webkit) {
		if(!multipleLocations) ortIndicator.setStyle('display', '');
		new Ajax('/search-verify.cfm', {
			method: 'get',
			data: {
				method: 'verify',
				location: $('ort').value
			},
			onError: function() {
				alert('Es ist ein Fehler aufgetreten, bitte laden Sie die Seite neu');
			},
			onComplete: function(resp) {
				if(!multipleLocations) ortIndicator.setStyle('display', 'none');
				var status = resp.trim();
				if(status == 'ok') {
					$('tag').blur();
					$('ort').blur();
					document.location.href = out;
				} else if(status == 'locationFail') {
					alert('Die von Ihnen eingegebene Ortschaft existiert nicht!');
					$('ort').focus();
				} else {
					alert('Es ist ein Fehler aufgetreten, bitte laden Sie die Seite neu');
				}
			}
		}).request();
	}
	else {
		document.location.href = out;	
	}
}

function bookMark() {
	var title = "suche.ch - Das Schweizer Internet-Portal";
	var url = "http://www.suche.ch/";
	if(window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if(window.external) {
		window.external.AddFavorite(url, title);
	} else if(window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href', url);
		elem.setAttribute('title', title);
		elem.setAttribute('rel', 'sidebar');
		elem.click();
		return true;
	}
}


