/* !!! --- jQuery noconflict: Use "$j" instead of "$" --- !!! */
var setBoxHeights;

$j(document).ready(function () {

	/* ---------------------- */	
	/* --- Toggle Content --- */
	/* ---------------------- */
	
	var slideTime = 600; //ms
	
	/* on load, hide all toggled elements */
	$j(".togglecontent").each(function(){
		$j(this).css({display:"none"});
	});
	
	$j(".toggler").each(function(){
		$j(this).css({display:"inline"});
	});		
	
	$j(".less").each(function(){
		$j(this).css({display:"none"});
	});
	
	$j(".block").each(function (i) {
		var thisArrow = $j(this).find(".toggler");
		var thisContent = $j(this).find(".togglecontent");
		thisArrow.click(function(e) {
			thisArrow.toggleClass("opened");
			thisArrow.addClass("helpopened");
			var isopen = thisArrow.hasClass("opened");
			if(isopen==1) {
				thisArrow.find(".less").show();
				thisArrow.find(".more").hide();
			}else{
				$j(this).find(".less").hide();
				$j(this).find(".more").show();			
			};
			thisContent.addClass("contentopened");
			thisContent.slideToggle(slideTime);
			$j(".togglecontent").not(".contentopened").slideUp(slideTime);
			$j(".toggler").not(".helpopened").removeClass("opened");
			$j(".toggler").not(".helpopened").find(".less").hide();
			$j(".toggler").not(".helpopened").find(".more").show();
			thisContent.removeClass("contentopened");
			thisArrow.removeClass("helpopened");
		return false;
		});
	});
	
	/* ---------------------------------------------------------------------------------- */	
	/* --- passt den äußeren Schatten der Höhe und der Scrollposition des Fensters an --- */
	/* ---------------------------------------------------------------------------------- */	
	
	if (!($j.browser.msie && $j.browser.version.substr(0, 1) < 7)) {
		var win = $j(window);
		var winheight = win.height();
		win.scroll(resizeshadow);
		win.resize(resizeshadow);
	}
	function resizeshadow() {
		var winheight = win.height();
		var scrolltop = win.scrollTop();	
		var newheight = winheight + scrolltop;
		$j("#shadowcenter").height(newheight);
	}
	
	/* ----------------------------------------------------------- */
	/* --- Adapt height of boxes in one row to the highest one --- */
	/* ----------------------------------------------------------- */
	
	
	setBoxHeights = function($row) {
		var heights = [];
		var boxes = [];
		var cols = 0;
		var maxBoxes = 0;
		var max = 0;
		var sums = [];
		var add = 17;
		var maxCols = 20;
		
		$row.find('.linkbottom,.bottomright').css('position', 'static');
		
		for (var i = 0; i < maxCols-1; ++i) {
			heights[i] = [];
			$row.find(".col"+(i+1)+" .boxcontent").each(function (idx) {
				heights[i][idx] = $j(this).height();
				boxes[i] = idx + 1;
				cols = i + 1;
			});
			if (boxes[i] > maxBoxes) maxBoxes = boxes[i];
		}
		
		if (cols <= 1) return;
		
		var isCompound = function(i, idx) { return (idx + 1) >= boxes[i]; };
		var getHeight = function(i, idx) { return heights[i][Math.min(boxes[i], idx)]; };
		
		for (var idx = 0; idx < maxBoxes; ++idx) {
			var max = 0;
			for (var i = 0; i < cols; ++i) {
				max = Math.max(max, !isCompound(i, idx) ? getHeight(i, idx) : 0);
			}
			for (var i = 0; i < cols; ++i) if (!isCompound(i, idx)) heights[i][idx] = max;
		}
		
		max = 0;
		for (var i = 0; i < cols; ++i) {
			var sum = 0;
			for (var idx = 0; idx < boxes[i]; idx++) sum += getHeight(i, idx) + add;
			sums[i] = sum;
			if (sum > max) max = sum;
		}
		
		for (var i = 0; i < cols; ++i) {
			if (sums[i] <= max) heights[i][boxes[i] - 1] += max - sums[i];
		}
		
		for (var i = 0; i < cols; ++i) {
			$row.find(".col"+(i+1)+" .boxcontent").each(function (idx) { $j(this).height(heights[i][idx]) });
		}
		
		$row.find('.linkbottom,.bottomright').css('position', 'absolute');
	}
	
	initialize();
	
	
	/* ------------------------------------------------------------------------ */	
	/* --- Hyphenator: Hyphenates Content automatically. Used with each box --- */
	/* ------------------------------------------------------------------------ */

//	$j(".boxcontent").each(function(){
//		$j(this).addClass("hyphenate");
//	});

	/* -------------------------------------------------------- */	
	/* --- Left Navigation always in visible area of window --- */
	/* -------------------------------------------------------- */
	
	
	var $scrollingDiv = $j("#left");

	$j(window).scroll(function() {
		$scrollingDiv
		.stop()
		.animate({ "marginTop": adaptleftcolumn() + "px" }, "slow");
	});

	function adaptleftcolumn() {
		var scrolltop = win.scrollTop();
        var winheight = win.height();
        var columnheight = $j("#left").height() + 70;
        var newmargin = 0;
        if (columnheight < winheight) {
            if (scrolltop > 280) {
                newmargin = scrolltop - 280;
            } else {
                newmargin = 0;
            }
        } else {
            newmargin = 0;
        }
        return newmargin;
	}
	
	/* -------------------------------------- */
	/* Init Checkboxes (js checkboxes plugin) */
	/* -------------------------------------- */

	$j('input[type=checkbox]').checkbox();

	/* Mission statements */
	$opaque = $j('#opaque');
	$opaque.add('.banner-info-box').fadeTo(0, 0, function() { $j(this).show(); });

	$j('#banner').hover(function() { $opaque.fadeTo(150, 1); }, function() { $opaque.stop(true, true).fadeTo(150, 0); }); $j('#nav-product-head > li > a').hover(function() { $j(this).siblings('.banner-info-box:first').stop(true, true).fadeTo(150, 1); }, function() { $j(this).siblings('.banner-info-box:first').stop(true, true).fadeTo(150, 0); });



});

/* ------------------------------------------------------ */
/* Newsletter: Clear input fields on click (TODO: remove) */
/* ------------------------------------------------------ */

function inputclear(field, defaulttext) {
	if (field.value == defaulttext) {
	field.value = "";
	}
}

function inputrecall(field, defaulttext) {
	if (field.value == "") {
	field.value = defaulttext;
	}
}

String.prototype.multi_char_cut=function() {
	var s = this.split("");
	for(x=0;x<arguments.length;x++) delete s[arguments[x]-1];
	return s.join("");
};

function initialize() {
	$j(".contentrow").each(function() {
		$row = $j(this);
		setBoxHeights($row);
		
		$row.find(".boxcontent").each(function() {
			var $this = $j(this);
			$this.find("img").load(function() {
				var $parentrow = $this.closest('.contentrow');
				$parentrow.css({"height": "auto"});
				$this.css({"height": "auto"});
				setBoxHeights($parentrow);
			});
		});
		
	});
	
	$j('#MainDiv').css({visibility: 'visible'});
}



