/**
 * Im Cookie wird gespeichert, ob der Nag-Layer schon angezeigt wurde, oder nicht
 */
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 windowOpener(sURL, nWidth, nHeight, sName, winX, winY, bScrollbars, bLocation, bStatus, bPersonalbar, bResizable, bToolbar, bMenubar, bDependent, bDirectories) {
	if(sURL.href) sURL = sURL.href;

	// set defaults
	if (!nWidth)		nWidth = 300;
	if (!nHeight)		nHeight = 300
	if (!sName)			sName = 'popupWindow';
	if (!winX)			winX = 50;
	if (!winY)			winY = 50;
	if (!bScrollbars)	bScrollbars = "no"; else bScrollbars = "yes";
	if (!bLocation)		bLocation = "no";
	if (!bStatus)		bStatus = "no";
	if (!bPersonalbar)	bPersonalbar = "no";
	if (!bResizable)	bResizable = "no"; else bResizable = "yes";
	if (!bToolbar)		bToolbar = "no";
	if (!bMenubar)		bMenubar = "no";
	if (!bDependent)	bDependent = "no";
	if (!bDirectories)	bDirectories = "no";

	if ((typeof oPopup != 'undefined') && (oPopup != null)) {
		if(typeof oPopup.closed != undefined) {
			if (!oPopup.closed) oPopup.close();
		} else {
			try	{
				oPopup.close();
			} catch (e) {
				//do nothing popup reference is lost
			}
		}
	}

	oPopup = window.open(sURL, sName, "width=" +nWidth+ ",height=" +nHeight+ ",left=" +winX+ ",top=" +winY+ ",screenX=" +winX+ ",screenY=" +winY+ ",scrollbars=" +bScrollbars+ ",location=" +bLocation+ ",status=" +bStatus+ ",personalbar=" +bPersonalbar+ ",resizable=" +bResizable+ ",toolbar=" +bToolbar+ ",menubar=" +bMenubar+ ",dependent=" +bDependent+ ",directories=" +bDirectories);
	if ( (oPopup != undefined) && (oPopup != null) ) {
		if (typeof oPopup.focus == 'function') oPopup.focus();
		if (oPopup.opener != undefined) oPopup.opener = window;
	} else oPopup = null;
	return oPopup;
}



/**
 *
 */
function startImageAnimation() {
	try {
		$('#right #imageCycleAnimation').css('height', '207px');
		$('#right #imageCycleAnimation').css('z-index', '1');
		$('#right #imageCycleAnimation').cycle({
			fx:		'fade',
			random:	0,
			timeout:  8000
		});
	} catch(e) {}
}
function showIntroOverlay(sIframeURL) {
	tb_show('overlay', sIframeURL + '#TB_iframe=true?width=540&height=274&modal=true&nopadding=true')
}


function showPopup() {
	try {
		xPos = 50;
		yPos = 50;
		windowOpener(oAktionsPopup.url, oAktionsPopup.width, oAktionsPopup.height, null, xPos, yPos, null, null, null, null, null, null, null, null, null);
	} catch(e) {}
}


function extractCustomURLParams(sSource) {
	var aSize = sSource.split('&');

	var nWidth	= -1;
	var nHeight	= -1;

	if (typeof aSize[1] != 'undefined') {
		if (aSize[1].indexOf('w=') != -1) {
			nWidth = parseInt(aSize[1].replace('w=', ''));
		} else {
			nHeight = parseInt(aSize[1].replace('h=', ''));
		}
	}

	if (typeof aSize[2] != 'undefined') {
		if (aSize[2].indexOf('w=') != -1) {
			nWidth = parseInt(aSize[2].replace('w=', ''));
		} else {
			nHeight = parseInt(aSize[2].replace('h=', ''));
		}
	}

	return aValues = {
		'width'	: nWidth,
		'height': nHeight,
		'src'	: aSize[0]
	}
}


function initColorbox() {
	$("a[class='colorbox']").colorbox({
		'close' 	: 'Schliessen',
		'current'	: 'Bild {current} von {total}',
		'previous' 	: 'Zur&uuml;ck',
		'next' 		: 'Weiter'
	});

	$("a[class='colorboxIFrame']").each(function() {
		var aParams = extractCustomURLParams($(this).attr('href'));

		if ( (aParams['width'] != -1) && (aParams['height'] != -1) ) {
			$(this).colorbox({
				'close' 		: 'Schliessen',
				'current'		: 'Bild {current} von {total}',
				'previous' 		: 'Zur&uuml;ck',
				'next' 			: 'Weiter',
				'innerWidth'	: aParams['width'],
				'innerHeight'	: aParams['height'],
				'iframe'		: 'true'
			});
		}

	});

	/**
	 * wenn die URL das Format: img.jpg?&w=123&h=456 hat kann die Funktion die Infos für die Colorbox rausschälen und
	 * automatisch anpassen
	 */
	$("a[class='colorboxInlineImage']").each(function() {
		var aParams = extractCustomURLParams($(this).attr('href'));

		if ( (aParams['width'] != -1) && (aParams['height'] != -1) ) {
			$(this).colorbox({
				'close' 		: 'Schliessen',
				'current'		: 'Bild {current} von {total}',
				'previous' 		: 'Zur&uuml;ck',
				'next' 			: 'Weiter',
				'innerWidth'	: (aParams['width'] + 20),
				'innerHeight'	: 600,
				'html'			: '<img src="'+aParams['src']+'" width="'+aParams['width']+'" height="'+aParams['height']+'" />'
			});
		}

	});

	$(".ce_gallery a[rel*='lightbox']").colorbox({
		close:		'Schliessen',
		previous:	'Vorheriges Bild',
		next:		'Nächstes Bild',
		onComplete:	function(){
			$('#cboxCurrent').css('display', 'none');
			$('#cboxTitle').css({
				'position': 'absolute',
				'bottom': 0,
				'left': 0,
				'top': 'auto',
				'background': '#000',
				'opacity': '0.8',
				'color': '#FFF',
				'font-weight': 'bold',
				'padding': '5px 0 6px 0'
			});
		}
	});

}


function initPage() {
	startImageAnimation();
	initColorbox();

	try {
		if ((typeof(oAktionsPopup) != undefined) && (oAktionsPopup != null)) showPopup();
	} catch (e) {}
}
