﻿var sUserAgent = navigator.userAgent;
var bIsIE      = sUserAgent.indexOf('MSIE') > -1;
var fIE        = bIsIE ? parseFloat(sUserAgent.substring(sUserAgent.indexOf('MSIE') + 5, sUserAgent.indexOf(';', sUserAgent.indexOf('MSIE'))), 10) : -1;

function addLinkTypeIcons() {
	$("a[type='application/pdf']").prepend('<img src="images/pdf14x14.png" alt="" style="vertical-align: middle">&nbsp;');
	$("a[type='application/zip']").prepend('<img src="images/zip14x14.png" alt="" style="vertical-align: middle">&nbsp;');
}

function fireEvent(oElement, sEvent) {
	// IE
	if (oElement.fireEvent) {
		oElement.fireEvent('on' + sEvent);
	}
	// Gecko based
	else if (document.createEvent) {
		var oEvent = document.createEvent('HTMLEvents');
		if (oEvent.initEvent) {
			oEvent.initEvent(sEvent, true, true);
		}
		if (oElement.dispatchEvent) {
			oElement.dispatchEvent(oEvent);
		}
	}
}

function getRadioValue(aoRadioElements) {
	return aoRadioElements.filter(':checked').val();
}

function getSelectorFromNames(asNames) {
	var asSelectors = array();
	for (var i = 0; i < asNames.length; i++) {
		asSelectors[asSelectors.length] = "[name='" + asNames[i] + "']";
	}
	return asSelectors.join(',');
}

function isEmpty(oValue) {
	return oValue == null || oValue.length === 0;
}

var iKeepAliveSessionTimeout = 720000;

function keepAliveSession() {
	var oElement  = $('#KeepAliveSessionImage');
	var sImageUrl = 'images/keepAliveSessionImage.php?id=' + (new Date()).getTime();
	if (oElement.length === 0) {
		$('body').append('<img id="KeepAliveSessionImage" src="' + sImageUrl + '" width="1" height="1" alt="0" style="position: absolute; top: -1px; left: -1px">'); 
	}
	else {
		oElement.attr('src', sImageUrl);
	}
	window.setTimeout("keepAliveSession()", iKeepAliveSessionTimeout);
}

function setCheckboxChecked(aoCheckboxElements, bChecked) {
	aoCheckboxElements.each(function() {
		if (bChecked != $(this).attr('checked')) {
			$(this).attr('checked', bChecked);
			$(this).trigger('updateState');
		}
	});
}

function setRadioValue(aoRadioElements, sCheckedValue) {
	aoRadioElements.each(function() {
		var sElementValue = $(this).val();
		var bChecked      = $(this).attr('checked');
		
		var bChange = 
			( bChecked && sElementValue != sCheckedValue) || 
			(!bChecked && sElementValue == sCheckedValue);
		if (bChange) {
			$(this).attr('checked', !bChecked);
			$(this).trigger('updateState');
		}
	});
}

function showPopup(sId, sUrl, sTitle, iWidth, iHeight) {
	if (iWidth   == null) iWidth   = 9999;
	if (iHeight  == null) iHeight  = 9999;

	iWidth  = Math.min(iWidth,  ($(window).width()  - 40 - 2));
	iHeight = Math.min(iHeight, ($(window).height() - 40 - 2));
	
	var oContainerCss = { };
	if (iWidth  != null) oContainerCss['width']  = iWidth;
	if (iHeight != null) oContainerCss['height'] = iHeight;

	$('#' + sId + ' iframe')
		.attr('src', sUrl)
		.css({
			height: iHeight - 36,
			width:  iWidth  -  2
	});
	$('#' + sId)
		.modal({
			containerCss : oContainerCss,
			onShow : function(dialog) {
				$('.DialogTitle').html('<div class="DialogCloseButton" title="schließen"><\/div>');
				$('.DialogCloseButton', dialog.data[0])
					.click(function() { $.modal.close();});
					
				$('.DialogTitle', dialog.data[0]).append(sTitle !== null && sTitle.length > 0 ? sTitle : '&nbsp;');
			},
			onClose : function(dialog) {
				$('#' + sId + ' iframe').attr('src', 'javascript:void(0)');
				$.modal.close();
			},
			persist : true
		});
}

String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^s*|s*$/g, '');
}

$(document).ready(function() {
	if (bIsIE && fIE < 7) { // IE-Version selbst überprüfen, da in pngFix fehlerhaft
		$(document).pngFix();
	}

	$("input[type='checkbox'],input[type='radio']").customInput();
	$("input[type='checkbox'],input[type='radio']").css('outline', 'none');
	
	var asPreloadUrls = [];
	
	// CssButtons
	var asButtonHeights = [ '15', '20', '36' ];
	var iButtonHeight   = null;
	var sButtonHeight   = null;
	for (iButtonHeight in asButtonHeights) {
		sButtonHeight = asButtonHeights[iButtonHeight];
		$('.CssButton.WeiterButton' + sButtonHeight).each(function() {
			$(this).append('<span></span>');
			
			if (!bIsIE || fIE >= 7) {
				$(this).hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
				$(this).children('a')
					.focus(function() { $(this).parent().addClass('focus'); })
					.blur(function() { $(this).parent().removeClass('focus'); });
			}
			
			// Breite des umgebenden DIVs so lange erhöhen bis Button ohne Umbruch dargestellt wird
			while($(this).height() >= sButtonHeight * 2) {
				$(this).width($(this).width() + 1);
			}
		});
		$('.CssButton.ZurueckButton' + sButtonHeight).each(function() {
			$(this).prepend('<span></span>');
			
			if (!bIsIE || fIE >= 7) {
				$(this).hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
				$(this).children('a')
					.focus(function() { $(this).parent().addClass('focus'); })
					.blur(function() { $(this).parent().removeClass('focus'); });
			}
			
			// Breite des umgebenden DIVs so lange erhöhen bis Button ohne Umbruch dargestellt wird
			while($(this).height() >= sButtonHeight * 2) {
				$(this).width($(this).width() + 1);
			}
		});
	}

	// zoombare Bilder
	$('a.zoooooom').zoomimage({
		controls : false,
		caption  : false,
		onLoad   : function(boxID) {
			$('#' + boxID).mouseout(function() { $('div.zooimage').zoomimageClear(); });
		}
	});
	
	// CloseButton
	var oCloseButtonImages = $('.CloseButton img');
	if (oCloseButtonImages.length > 0) {
		asPreloadUrls[asPreloadUrls.length] = '/images/schliessen_hover.gif';
		oCloseButtonImages.hover(
			function() { this.src = this.src.replace(/^(.*schliessen).gif$/, '$1_hover.gif'); }, 
			function() { this.src = this.src.replace('_hover', ''); 
		});
	}
	
	// Info Buttons
	var oInfoButtonImages = $('img[alt="Info"]');
	if (oInfoButtonImages.length > 0) {
		asPreloadUrls[asPreloadUrls.length] = '/images/infoKlein_hover.gif';
		asPreloadUrls[asPreloadUrls.length] = '/images/infoGross_hover.gif';
		oInfoButtonImages.hover(
			function() { this.src = this.src.replace(/^(.*info(Klein|Gross)).gif$/, '$1_hover.gif'); }, 
			function() { this.src = this.src.replace('_hover', ''); 
		});
	}
	
	// Bilder vorladen
	for (var i = 0; i < asPreloadUrls.length; i++) {
		var oImage = $('<img>').attr('src', asPreloadUrls[i] );
	}

	window.setTimeout('keepAliveSession()', iKeepAliveSessionTimeout);
});