if (document.documentElement) {
	document.documentElement.className = "javascript";
}

$(document).ready(function () {
	
	$("body").addClass("javascript");
	
	$("#header .mainnavi > ul > li").submenu();
	
	$("#fotokategorien a, #fotokategorien strong").accordion({ collapsedHeight : 30, expandedHeight : 130 });
	
	$("#loginbox label, #newsletter label").embeddedLabels();
	
	new Slideshow($("#slideshow li, #slideshow td"), $("#slideshow-previous"), $("#slideshow-next"));
	
	/* IE-Fixes */
	
	if (/*@cc_on!@*/false) {
		
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch (e) {}
		
		$(".submenu a, #loginbox, #fotokategorien, #newsletter").iePngFix();
		
		var stop = function (e) { return false; }
		$("#slideshow").bind("dragstart", stop).bind("selectstart", stop);
		
	}
	
});

function Slideshow (items, previousButton, nextButton) {
	this.items = items;
	this.previousButton = previousButton;
	this.nextButton = nextButton;
	
	this.buttons = previousButton.add(nextButton);
	this.currentItemNumber = 0;
	
	this.setupNavigation();
	this.goTo(this.currentItemNumber);
}
Slideshow.prototype = {
	next : function () {
		this.goTo(this.currentItemNumber + 1);
	},
	previous : function () {
		this.goTo(this.currentItemNumber - 1);
	},
	setupNavigation : function () {
		var slideshowInstance = this;
		this.previousButton.click(function (e) {
			slideshowInstance.previous();
			e.preventDefault();
		});
		this.items.add(this.nextButton).click(function (e) {
			slideshowInstance.next();
			e.preventDefault();
		});
	},
	updateButtons : function () {
		this.buttons.removeClass("inactive");
		if (this.currentItemNumber == 0) {
			this.previousButton.addClass("inactive");
		}
		if (this.currentItemNumber == this.items.length - 1) {
			this.nextButton.addClass("inactive");
		}
	},
	goTo : function (itemNumber) {
		var newItem = this.items.eq(itemNumber);
		if (!newItem.length) return;
		if (this.currentItem) {
			this.currentItem.removeClass("shown");
		}
		this.currentItem = newItem;
		this.currentItemNumber = itemNumber;
		newItem.addClass("shown");
		this.updateButtons();
	}
};

$.fn.iePngFix = (!$.browser.msie || $.browser.version >= 7) ?
	(function () {
		return this;
	}) : (function () {
		return this.each(function () {
			var imageUri = this.currentStyle.backgroundImage.replace(/^url\(["']?([^"']*)["']?\)$/, "$1");
			if (imageUri == "none") return;
			//alert(this.nodeName + " " + this.id + "\n" + imageUri);
			this.style.backgroundImage = "none";
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='" + imageUri + "')";
			$(this).children().children().each(function () {
				if (this.currentStyle.position != "absolute") {
					this.style.position = "relative";
				}
			});
		});
	});

$.fn.embeddedLabels = function () {
	return this.each(function () {
		var label = $(this).hide();
		$("#" + label.attr("for"))
			.val(label.text())
			.attr("defaultValue", label.text())
			.focus(function () {
				var input = $(this);
				if (input.val() == input.attr("defaultValue")) {
					input.val("");
				}
			})
			.blur(function () {
				var input = $(this);
				if (input.val() == "") {
					input.val(input.attr("defaultValue"));
				}
			});
	});
};

$.fn.selectText = function () {
	var elem = this.get(0);
	if (!elem) return this;
	if (document.selection && document.selection.createRange) {
		var textRange = document.selection.createRange();
		textRange.moveToElementText(elem);
		textRange.select();
	} else if (document.createRange && window.getSelection) {
		var range = document.createRange();
		range.selectNode(elem);
		var selection = window.getSelection();
		selection.removeAllRanges();
		selection.addRange(range);
	}
	return this;
}

$.fn.submenu = function () {
	var f = arguments.callee;
	return this.each(function () {
		var elem = $(this);
		elem.mouseover(function (e) {
			if (f.submenu) {
				f.submenu.hide();
				window.clearTimeout(f.timeout);
			}
			f.submenu = elem.find(".submenu").show();
			if (elem.hasClass("last")) {
				f.submenu.css("left", -(f.submenu.outerWidth() - elem.outerWidth()) + "px");
			}
		}).mouseout(function (e) {
			if (!f.submenu) return;
			f.timeout = window.setTimeout(function () {
				f.submenu.hide();
			}, 800);
		});
	});
};

$.fn.accordion = function (options) {
	this.mousemove(handleMouseMove);
	
	var animating = false,
		activeElement = null,
		increment = options.increment || 5,
		intervalTime = options.intervalTime || 15,
		maxGrowth = options.expandedHeight - options.collapsedHeight;
	
	function handleMouseMove () {
		if (activeElement == this || animating) return;
		animating = true;
		newElement = this;
		newElement.parentNode.className = "expanded";
		var growth = 0;
		interval = window.setInterval(function () {
			growth += increment;
			newElement.style.height = (options.collapsedHeight + growth) + "px";
			if (activeElement) {
				activeElement.style.height = (options.expandedHeight - growth) + "px";
			}
			if (growth >= maxGrowth) {
				window.clearTimeout(interval);
				if (activeElement) {
					activeElement.parentNode.className = "";
				}
				activeElement = newElement;
				animating = false;
			}
		}, intervalTime);
	}
	
};