// 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) {
	$.get(
		'/writejpos.cfm',
		{
			'hId': hId,
			'd': d
		}
	);
}

function writeBPos(bId) {
	$.get(
		'/writebpos.cfm',
		{
			'bId': bId,
			'tsc': tagInfo[bId].tsc,
			'pos': tagInfo[bId].pos,
			'checkSum': tagInfo[bId].checkSum
		}
	);
}

function smokeAlert(msg) {
	$.jGrowl(msg);
}
function smokeGood(msg) {
	$.jGrowl(msg);
}
function smokeNok(msg) {
	$.jGrowl(msg);
}



function addToFav(obj, id) {
	var switchImage = (arguments.length == 3 && arguments[2] == true);
	var tmp = ($.cookie('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('BIDS', tmp + ',' + id, {expires: 30, domain: '.suche.ch', path: '/'});
		smokeGood(unescape('Dieser Banner wurde zu \"Ihren Favoriten\" auf der \"suche.ch-Startseite\" hinzugef%FCgt'));
	}
	if(switchImage) $('img',obj).attr('src','style/image/more-add-d.png');
}

function showMoreBox(listItem) {
	$('div.more-info', listItem).css('display', ''); 
	$('div.more-opt', listItem).css('display', ''); 
}

function showMoreBoxes() {
	$('#banners div.more-info').css('display','');
	$('#banners div.more-opt').css('display','');
}

function hideMoreBoxes() {
	$('#banners div.more-info').css('display','none');
	$('#banners div.more-opt').css('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 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(tag,ort) {
	if(tag === undefined || tag === null || tag === false) {
		if($.trim($('#tag').val()) == '') {
			alert(unescape('Bitte geben Sie zuerst einen Suchbegriff ein!'));
			$('#tag').focus();
			return;
		} else {
			tag = $.trim($('#tag').val());
		}
	}
	if(ort === undefined || ort === null || ort === false) {
		ort = '';
		if($.trim($('#ort').val()) != '') {
			ort = $.trim($('#ort').val());
		}
	}

	var out = '/info/' + escape(UTF8.encode(tag)) + '/';

	if(ort != '') {
		out += ort + '/';
		var els = $('#umkDiv input[name="r"]');
		var	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;
	}
	// AutoCompleter
	/*
	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;	
	}
	*/
	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;
	}
}


