/* ---------------------------------------------------------------- */
/**
 * fine writing instruments stuff
 */
/* ---------------------------------------------------------------- */
function toggleImageText() {
	$("#imageText").toggle();
	$("#subHeadlineShowButton").toggle();
	$("#subHeadlineHideButton").toggle();
}

$(document).ready(function() {
			$("#imageText").toggle();
			$("#subHeadlineShowButton").toggle();
			if (typeof $.suggest == 'function') {
				$("#searchForm").suggest(PULSE_PELIKAN_searchSuggestURL);
			}
		});

function showPromiText(promiName) {
	$('.promiText').hide();
	$("#" + promiName).show();
}
/**
 * popup2thickbox
 */
/* ---------------------------------------------------------------- */
function popup2(file, width, height) {
	popup("/pulse/coloring-pages.jsp?path=" + file, 'popup2', width, height);
}

function popup(file, name, width, height, i1, i2, i3) {
	if (file.indexOf('/pulse/vfs-public/flash/hqwi/') === 0) {
		uri = "/pulse/flash-popup.jsp?file=" + file;
	} else {
		uri = file;
	}
	if (uri.indexOf('?') > -1) {
		uri += "&";
	} else {
		uri += "?";
	}
	// uri += "width=" + (width-29) + "&height=" + (height-12) +
	// "&TB_iframe=true&TB_transparent=true";
	uri += "width=" + (width) + "&height=" + (height + 25)
			+ "&TB_iframe=true&TB_transparent=true";
	tb_show('', uri);
}

function historyPopUp(locale, file) {
	uri = "/pulse/history.jsp?locale=" + locale + "&file=" + file
			+ "&TB_iframe=true" + "&TB_transparent=true&width=" + (550)
			+ "&height=" + (450);
	tb_show('', uri);
}

/* ---------------------------------------------------------------- */
/**
 * Product finder
 */
/* ---------------------------------------------------------------- */
$(document).ready(function() {
			// console.log('setting up pf_listeners...');
			$("input.productfinder").click(function() {
						// console.log('click: ', this);
						pf_update();
					});
		});

function pf_onLoadCheck() {
	// console.log('pf_onLoadCheck: ', this);
	hOff = $("#FilterContentOffset").val();
	if (($("#productFinder").length > 0) && (hOff !== '')
			&& (document.location.href.indexOf("offset=" + hOff) == -1)) {
		$("#FilterContentFilteredContents").html("");
		pf_doAjax(pf_buildQueryString(Number(hOff)));
	} else if ($("#productFinder") !== null) {
		inputs = $("input.productfinder");
		for (i = 0; i < inputs.length; i++) {
			if ((inputs[i].checked)
					&& (document.location.href.indexOf(inputs[i].name + "="
							+ inputs[i].val()) == -1)) {
				$("#FilterContentFilteredContents").html("");
				pf_doAjax(pf_buildQueryString());
				break;
			}
		}
	}
}

function pf_update(offset) {
	// console.log('pf_update: ', this, arguments);
	$("#FilterContentFilteredContents").fadeOut("fast", function() {
				pf_doAjax(pf_buildQueryString(offset));
			});
}

function pf_buildQueryString(offset) {
	// console.log('pf_buildQueryString: ', this, arguments);
	if (typeof(offset) != 'number') {
		offset = 1;
	}
	$("#FilterContentOffset").val(offset);
	queryString = "offset=" + offset;
	$("input.productfinder").each(function() {
				if (this.checked === true) {
					queryString += "&" + this.name + "=" + this.value;
				}
			});
	return queryString;
}

function pf_doAjax(queryString) {
	// console.log('pf_doAjax: ', this, arguments, PULSE_PELIKAN_productFinderURL);
	$.ajax({
				url : PULSE_PELIKAN_productFinderURL,
				type : 'GET',
				dataType : 'html',
				data : queryString,
				timeout : 10000,
				error : function(req, msg) {
					try {
						status = "";
						statusText = "";
						try {
							status = req.status;
							statusText = req.statusText;
						} catch (e) {
						}
						$("#FilterContentFilteredContents").fadeIn();
						alert("Communication Error:\n" + status + " "
								+ statusText + " (" + msg + ")");
					} catch (e) {
						// console.log('error: ', e, arguments);
					}
				},
				success : function(html) {
					try {
						$("#content").html(html);
						$("#FilterContentFilteredContents").fadeIn();
					} catch (e) {
						// console.log('success: ', e, arguments);
					}
				}
			});
}

/* ---------------------------------------------------------------- */
/**
 * Newsletter scripts
 */
/* ---------------------------------------------------------------- */
var compulsoryFieldsIn = [];
compulsoryFieldsIn[0] = "stdIn1";
compulsoryFieldsIn[1] = "stdIn2";
compulsoryFieldsIn[2] = "stdIn3";

// check for fields (subscribtion)
function checkFieldsIn() {
	var checkOK = true;
	for (i = 0; i < compulsoryFieldsIn.length; i++) {
		cfObj = document.getElementById(compulsoryFieldsIn[i]);

		if (cfObj !== null) {
			if (cfObj.type.toLowerCase() == "text") {
				if (cfObj.value.match(/^\s*$/)) {
					checkOK = false;
				}
			} else if (cfObj.type.toLowerCase() == "radio"
					|| cfObj.type.toLowerCase() == "checkbox") {
				tmpObj = document.getElementsByName(cfObj.name);
				tmpCheck = false;
				for (j = 0; j < tmpObj.length; j++) {
					if (tmpObj[j].checked === true) {
						tmpCheck = true;
						break;
					}
				}
				checkOK = tmpCheck;
			} else if (cfObj.type.toLowerCase().indexOf("select") >= 0) {
				if (compulsoryFieldsIn[i] == "stdIn12"
						&& cfObj.selectedIndex === 0) {
					checkOK = false;
				}
			}
		}
		if (!checkOK) {
			break;
		}
	}
	if (checkOK) {
		document.getElementById('subscribe').submit();
	} else {
		window.alert(subError);
	}
}

// check for fields (un-subscribtion)
function checkFieldsOut() {
	var checkOK = true;
	if (document.getElementById('stdOut3').value.match(/^\s*$/)) {
		checkOK = false;
	}
	if (checkOK) {
		document.getElementById('unsubscribe').submit();
	} else {
		window.alert(unSubError);
	}
}

// checks fields (Lehrer-Info Newsletter subscribtion)
function checkFieldsInLiNewsletter() {
	var checkOK = true;
	if (document.getElementById('emailIn').value.match(/^\s*$/)) {
		checkOK = false;
	}
	if (checkOK) {
		document.getElementById('subscribe').submit();
	} else {
		window.alert(subError);
	}
}

/* ----------------------------------------------------------- */
/* form selector */
/* ----------------------------------------------------------- */

$(document).ready(function() {
			if ($('#formSelectorSelector').size() != 1) {
				return;
			}
			$('#formSelectorIntro').show();
			$('#formSelectorSelector').change(function() {
						$('#formSelectorSelector > option').each(function() {
									if (this.value !== '') {
										$('#' + this.value).hide();
									}
								});
						if (this.value !== '') {
							$('#' + this.value).fadeIn("slow");
						}
					});
			$('#formSelectorSelector > option').each(function() {
						if (this.value !== '') {
							$('#' + this.value).hide();
						}
					});
			/* look for an erroneous form */
			activeForm = $('form.activeForm');
			if (activeForm.size() == 1) {
				activeForm.parent('div').show();
				selectedId = activeForm.parent('div').attr('id');
				$('#formSelectorSelector > option').each(function() {
							if (this.value == selectedId) {
								this.selected = 'selected';
							}
						});
			}
		});

/* ----------------------------------------------------------- */
/* trafficmaxx */
/* ----------------------------------------------------------- */
function tm_tracker(tm_pageCategory) {
	var tm_sid = "7d857c1cfdb7bc7a2ea0ac54537438b7";
	var tm_conversionName = "";
	var tm_campaignName = "";

	var pageTitle = document.title;
	var pageUrl = document.URL;

	var swidth = "na";
	var sheight = "na";
	var scolor = "na";
	var swidth = screen.width;
	var sheight = screen.height;
	var scolor = navigator.appName != 'Netscape'
			? screen.colorDepth
			: screen.pixelDepth;

	var page = "http" + (document.URL.indexOf('https:') == 0 ? 's' : '')
			+ '://s1.trafficmaxx.de/thx.php?sid=' + tm_sid + "\x26java=y";
	var tm_ref = escape(parent == self
			? document.referrer
			: top.document.referrer);
	tm_ref = (typeof(top.document) == "object")
			? top.document.referrer
			: tm_ref;
	var tm_lang = navigator.userLanguage
			? navigator.userLanguage
			: navigator.language;

	if (typeof(tm_pageCategory) == "undefined")
		tm_pageCategory = "";
	if (typeof(tm_conversionName) == "undefined")
		tm_conversionName = "";
	if (typeof(tm_campaignName) == "undefined")
		tm_campaignName = "";

	tm = "\x26swidth=" + swidth;
	tm += "\x26sheight=" + sheight;
	tm += "\x26scolor=" + scolor;
	tm += "\x26la=" + escape(tm_lang.toLowerCase());
	tm += "\x26pageTitle=" + escape(pageTitle);
	tm += "\x26pageUrl=" + escape(pageUrl.toLowerCase());
	tm += "\x26pageCategory=" + escape(tm_pageCategory);
	tm += "\x26campaignName=" + escape(tm_campaignName);
	tm += "\x26ref=" + escape(tm_ref.toLowerCase());
	tm += "\x26conversionName=" + escape(tm_conversionName);
	pq = new Image();
	pq.src = page + tm;
}

/* -------------------------------------------------------------- */
/* German FWI mashup */
/* -------------------------------------------------------------- */
$(document).ready(function() {
	$(".fwiMashup").click(function(e) {
		var name = $(this).parent($(".button")).prev().prev().prev().prev()
				.prev().prev().prev().prev().html();
		var street = $(this).parent($(".button")).prev().prev().prev().prev()
				.prev().prev().prev().html();
		var postalCode = $(this).parent($(".button")).prev().prev().prev()
				.prev().prev().prev().html();
		var city = $(this).parent($(".button")).prev().prev().prev().prev()
				.prev().html();
		uri = "../../mashup.jsp?&name=" + prepareURLParam(name) + "&street="
				+ prepareURLParam(street) + "&postalCode="
				+ prepareURLParam(postalCode) + "&city="
				+ prepareURLParam(city);
		popup(uri, 'googlemaps', 820, 520);

		return false;
	});
});

function prepareURLParam(p) {
	processed = $.trim(p);
	processed = processed.replace(/\s+/, ' ');
	processed = processed.replace(/<br.+?>/, '<br>');
	return encodeURIComponent(processed);
}

/*----------------------------------------------------------------------------*/
/**
 * function for to open the country selector upwards if not enough space to the
 * window bottom is availiable
 */
/*----------------------------------------------------------------------------*/
$(document).ready(function() {
	$("#box").mouseenter(function() {
		var navHeight = $(".navbox").outerHeight();
		var windowHeight = $(window).height();
		var navTotalOffset = $("#box").offset();
		var navScrollOffset = $(document).scrollTop();
		if ((windowHeight - (navTotalOffset.top - navScrollOffset)) < navHeight) {
			var marginTop = navHeight + 17;
			$(".navbox").css("margin-top", "-" + marginTop + "px");
		}
	});
	$("#box").mouseleave(function() {
				$(".navbox").css("margin-top", "0px");
			});
});
/*----------------------------------------------------------------------------*/
/**
 * opens the shop selection modal from the German shop teaser.
 */
/*----------------------------------------------------------------------------*/
function deDEShopSelector() {
	tb_show(
			'',
			'/pulse/shop-selector-de_DE.jsp?TB_iframe=true&TB_transparent=true&width=800&height=350')
}
/*----------------------------------------------------------------------------*/
/**
 * load contents of new printer supplies papers and foils section into the popup
 */
/*----------------------------------------------------------------------------*/
$(document).ready(function() {
	$("a#fancybox-close").click(function() {
				$("h1#paperRangePopupHeadline").removeAttr("style");
			});
	try {
		$("a.printerSuppliesLink").fancybox({
					'type' : 'iframe',
					'width' : 616,
					'height' : 290,
					'padding' : 0,
					'margin' : 0,
					'overlayShow' : false,
					'hideOnContentClick' : false,
					'autoDimensions' : false
				});
	} catch (e) {
		// ignore
	}
	/**
	 * close fancybox if commerce connector link is clicked and initialize the
	 * commerce connector in a new thickbox overlay
	 */
	$("#commerceConnectorLinkPaperRange").click(function(e) {
				e.preventDefault();
				var url = $("#commerceConnectorLinkPaperRange a").attr('href');
				parent.$.fancybox.close();
				parent.tb_show('', url, null);
			});
	$("#videoLinkPaperRange").click(function(e) {
		e.preventDefault();
		var url = $("#videoLinkPaperRange a").attr('href');
		parent.$.fancybox.close();
		PPRVideoPopup = window.open(url, 'PPRVideoPopupWindow', 'width=752,'
						+ 'height=422,' + 'directories=no,' + 'location=no,'
						+ 'menubar=no,' + 'resizable=no,' + 'scrollbars=0,'
						+ 'status=no,' + 'toolbar=no');
		PPRVideoPopup.focus();
	});

	var panViewFront = false;
	var panViewBack = false;
	/**
	 * hide small product image and display the larger product image
	 */
	$("#productImageFront, #productImageBack").mouseenter(function() {
		$(this).find("img.paperRangePopupProductImage").css("display", "none");
		/**
		 * check if panview plugin has already been started, if so return
		 */
		if ($(this).attr("id") == "productImageFront") {
			if (panViewFront === false) {
				$("#productImageFront").find("img.paperRangePopupDetailImage")
						.panView(166, 249);
				panViewFront = true;
			} else {
				$("#productImageFront").find("div").css('display', 'block');
			}
		} else if ($(this).attr("id") == "productImageBack") {
			if (panViewBack === false) {
				$("#productImageBack").find("img.paperRangePopupDetailImage")
						.panView(166, 249);
				panViewBack = true;
			} else {
				$("#productImageBack").find("div").css('display', 'block');
			}
		};
	});
	$("#productImageFront, #productImageBack").mouseleave(function() {
		$(this).find("div").css("display", "none");
		$(this).find("img.paperRangePopupProductImage").css("display",
				"inline-block");
	});
});
/**
 * switch styles on paper range tablerow hover
 */
$(document).ready(function() {
	$("tr.paperRangeTableRow").mouseenter(function() {
		$(this).css("background-color", "#afddf4");
		$(this).find("td.paperRangeProductTableHeadRow")
				.addClass("paperRangeProductTableHeadRowHover");
		$(this).find("td.paperRangeTableBorderLeft")
				.addClass("paperRangeTableBorderLeftHover");
		$(this).find("td.paperRangeTableBorderRight")
				.addClass("paperRangeTableBorderRightHover");
		var $img = $(this).find("img.starsImage");
		$img
				.attr('src', $img.attr('src').replace(/_normal.png$/,
								"_hover.png"));
	});
	$("tr.paperRangeTableRow").mouseleave(function() {
		$(this).css("background-color", "#ffffff");
		$(this).find("td.paperRangeProductTableHeadRow")
				.removeClass("paperRangeProductTableHeadRowHover");
		$(this).find("td.paperRangeTableBorderLeft")
				.removeClass("paperRangeTableBorderLeftHover");
		$(this).find("td.paperRangeTableBorderRight")
				.removeClass("paperRangeTableBorderRightHover");
		var $img = $(this).find("img.starsImage");
		$img
				.attr('src', $img.attr('src').replace(/_hover.png$/,
								"_normal.png"));
	});
	$("tr.paperRangeTableRow").click(function() {
				$(this).find("a.printerSuppliesLink").trigger("click");
			});
});

