function newWindow(link) {
	var win = window.open(link, '_blank');
	if (win) {
		win.focus();
		return false;
	}
	return true;
}

function openPopup(link, width, height) {
	width = width || 400;
	height = height || 500;
	var win = window.open(link, '_blank', "scrollbars=1,resizable=1,width=" + (width + 30) + ",height=" + (height + 30));
	if (win) {
		win.focus();
		return false;
	}
	return true;
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEq = name + "=";
	var ca = document.cookie.split(';');
	for (var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEq) == 0) return c.substring(nameEq.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function unrot(string) {
	return string.replace(/[a-zA-Z]/g, function(c) {
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
}

function initializeWatches() {
	var watching = (readCookie("merkliste") || "").split("|");
	$("ul.classifieds li span.watch").each(function() {
		var id = this.firstChild.className.substr(1);
		this.firstChild.lastChild.checked = $.inArray(id, watching) >= 0;
	});
}

function watch(span, event) {
	if (span.firstChild.lastChild.disabled) return false;
	var id = span.firstChild.className.substr(1);
	var watching = (readCookie("merkliste") || "").split("|");
	var idx = $.inArray(id, watching);
	if (idx >= 0) {
		watching.splice(idx, 1);
	} else {
		watching.push(id);
	}
	setTimeout(function() {
		span.firstChild.lastChild.checked = idx < 0;
	}, 10);
	createCookie("merkliste", watching.join("|"), 3);
	if (span.firstChild.getAttribute("href") != "#") {
		$.post(span.lastChild.href, {});
	}
	return false;
}

function showSubnav(item) {
	$(item).addClass("open").find("ul").show().each(function() {
		if ($.browser.msie && $.browser.version < 7) {
			$(this).show();
		} else {
			$(this).fadeTo(0, 0.85);
		}
	});
}
function hideSubnav(item) {
	$(item).removeClass("open").find("ul").hide();
}

$(document).ready(function() {
	$("span.watch").click(function(e) { return watch(this, e) });
	$("#flash").click(function() { $(this).fadeOut("slow") });

	$("#metanav li.dropdown")
		.hover(function() { showSubnav(this) }, function() { hideSubnav(this) })
		.find("a")
			.focus(function() {
				showSubnav(this.parentNode);
			})
			.blur(function() {
				try { hideSubnav(this.parentNode) } catch (e) {}
			});
	$("#metanav li.dropdown li a")
		.focus(function() {
			showSubnav(this.parentNode.parentNode.parentNode);
		})
		.blur(function() {
			try { hideSubnav(this.parentNode.parentNode.parentNode) } catch (e) {}
		});
	if ($.browser.msie && $.browser.version < 7) {
		$("#metanav li.dropdown ul").bgiframe();
	}
});
