var isFlashPopup = false;

function popWin(url, wintitle, height) {
	if(!height) height = 450;
	if(!wintitle) wintitle='';
	
	if(wintitle.indexOf('~~~')!=-1) {
		parts=wintitle.split('~~~');
		wintitle = parts[0];
		type = parts[1];
		if(type=='advent') {
			isFlashPopup = true;
			popInfo = new DragWin(self.document, 'dialogShow', 'dragWin dragWinWide', 'popInfo', wintitle, 'close');
			popInfo.create();
			iframeHeight = 605;
			//popInfo.setContentWidth(800);
			popInfo.setPosTop(document.documentElement.scrollTop + 30 + 'px');
			popInfo.setContent('<iframe width="100%" height="'+iframeHeight+'" marginwidth="0" marginheight="0" frameborder="0" src="'+url+'" scrolling="no"></iframe>');
			toggleFlash(0);
		}
		if(type=='ecards') {
			isFlashPopup = false;
			popInfo = new DragWin(self.document, 'dialogShow', 'dragWin dragWinEcards', 'popInfo', wintitle, 'close');
			popInfo.create();
			iframeHeight = 510;
			//popInfo.setContentWidth(800);
			popInfo.setPosTop(document.documentElement.scrollTop + 30 + 'px');
			popInfo.setContent('<iframe width="100%" height="'+iframeHeight+'" marginwidth="0" marginheight="0" frameborder="0" src="'+url+'" scrolling="no"></iframe>');
		}
	}
	else {
		isFlashPopup = false;
		//document-Referenz, DOM-Id, css-ClassName, Instanz-Name, Window-Title, buttons, parentRef
		popInfo = new DragWin(self.document, 'dialogShow', 'dragWin', 'popInfo', wintitle, 'close');
		popInfo.create();
		iframeHeight = height;
		maxHeight = document.documentElement.clientHeight - 180;
		if(iframeHeight > maxHeight) iframeHeight = maxHeight;
		popInfo.setPosTop(document.documentElement.scrollTop + 130 + 'px');
		popInfo.setContent('<iframe width="100%" height="'+iframeHeight+'" marginwidth="0" marginheight="0" frameborder="0" src="'+url+'"></iframe>');
	}
}

function toggleFlash(state) {
	if(!isFlashPopup) return;
	flashEls=getElementsByClassName(document.getElementById('colCenter'),'flash');
	for(var i=0; i<flashEls.length; i++) {
		//flashEls[i].style.display = state ? 'block' : 'none';
		flashEls[i].style.visibility = state ? 'visible' : 'hidden';
	}
}

function popContent(content, wintitle) {
	if(!wintitle) wintitle='';
	//document-Referenz, DOM-Id, css-ClassName, Instanz-Name, Window-Title, buttons, parentRef
	popInfo = new DragWin(self.document, 'contentPop', 'dragWin', 'popInfo', wintitle, 'close');
	popInfo.create();
	popInfo.setPosTop('225px');
	popInfo.setContent(content);
}


function getPos(obj, direction) {  //abs. Position ermitteln; Element-ID, 'left'|'top'
	var xpos = 0;
	var ypos = 0;
	myElement = obj;
	do {
		xpos += myElement.offsetLeft;
		ypos += myElement.offsetTop;
		myElement = myElement.offsetParent;
	}
	while(myElement.tagName != 'BODY' && myElement.tagName != 'HTML');
	if(direction == 'x') return xpos;
	if(direction == 'y') return ypos;
}

function popImg(imgUrl) {
	//document-Referenz, DOM-Id, css-ClassName, Instanz-Name, Window-Title, buttons, parentRef
	popInfo = new DragWin(self.document, 'dialogShow', 'dragWin', 'popInfo', '', 'close');
	popInfo.create();
	runner = 0;
	popInfo.setPosTop(document.documentElement.scrollTop + 20 + 'px');
	popInfo.setContent('<img src="'+imgUrl+'" alt="">');
	checkImgPopWidth();
}

function popGallery() {
	//document-Referenz, DOM-Id, css-ClassName, Instanz-Name, Window-Title, buttons, parentRef
	popInfo = new DragWin(self.document, 'contentPop', 'dragWin galleryWin', 'popInfo', '', 'close');
	popInfo.create();
	popInfo.setPosTop(document.documentElement.scrollTop + 20 + 'px');
	popInfo.setContent('<div id="gallery"></div>');
	popInfo.show();
}


function checkImgPopWidth() {
	if(self.cipwTO) clearTimeout(cipwTO);
	if(popInfo.getContentImgWidth() < 40) {
		cipwTO=setTimeout("checkImgPopWidth()", 200);
	}
	popInfo.setContentWidth(popInfo.getContentImgWidth()+1);
	popInfo.centerH();
	setTimeout("popInfo.show()",200);
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
/*
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else 
*/	
	if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function getElementsByClassName(node, classname) {
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className)) a.push(els[i]);
	return a;
}

function getElementsByAttributeValue(node, attrib, value) {
	var elements = new Array();
	if(node) {
		if(node.tagName && node.getAttribute(attrib) && node.getAttribute(attrib).indexOf(value) != -1)
			elements[elements.length] = node;
	}
	if(node.hasChildNodes) {
		for(var x=0; x<node.childNodes.length; x++) {
			var childElements = getElementsByAttributeValue(node.childNodes[x], attrib, value);
			for(var i=0; i<childElements.length; i++)
				elements[elements.length] = childElements[i];
		}
	}
	return elements;
}

function addEvent(obj, evt, fn) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, fn, false);
	} else {
		obj.attachEvent('on'+evt, fn);
	}
}

function removeEvent(obj, evt, fn) {
	if(obj.removeEventListener) {
		obj.removeEventListener(evt, fn, false);
	} else {
		obj.detachEvent('on'+evt, fn);
	}
}

function loading(action) {
	if(action == 'show') {
		toggleSelects(0);
		disp = 'block';
	}
	else disp = 'none';
	if(document && document.getElementById('loading')) document.getElementById('loading').style.display = disp;
}


// ------------------------- Popup ---------------------------

var movingElement = false;
var scrollX = 0;
var scrollY = 0;
  
function DragWin(documentRef, elId, elClass, elInst, title, buttons, parentRef) {
	this.ovEl = false;
	this.documentRef = documentRef;
	if(!parentRef) this.parentRef = documentRef.body;
	else this.parentRef = parentRef;
	this.dEl = false;
	this.dElTitle = false;
	this.dElContent = false;
	this.elId = elId;
	this.elClass = elClass;
	this.offsetX = 0;
	this.offsetY = 0;
	this.content = '';
	this.buttons = buttons;
	this.title = title;
	this.elInst = elInst;
	this.startMove = function(e) {
		if(e && e.target) {
			var mouseX = e.pageX;
			var mouseY = e.pageY;
		}
		else {
			var mouseX = event.clientX;
			var mouseY = event.clientY;
		}
		this.offsetX = mouseX - this.dEl.offsetLeft;
		this.offsetY = mouseY - this.dEl.offsetTop;
		movingElement = this;
	}
	this.stopMove = function() {
		movingElement = false;
	}
	this.create = function() {
		setOverlay(1);
		// dragWin
		dEl = this.documentRef.createElement('table');
		dEl.id = this.elId;
		dEl.onmousemove = move;
		dEl.className = this.elClass;
		// border top
		dElBorderTop = dEl.insertRow(-1);
		borderTopLeft = this.documentRef.createElement('td');
		borderTopLeft.className = 'wBorderTopLeft';
		dElBorderTop.appendChild(borderTopLeft);
		borderTopCenter = this.documentRef.createElement('td');
		borderTopCenter.className = 'wBorderTopCenter';
		dElBorderTop.appendChild(borderTopCenter);
		borderTopRight = this.documentRef.createElement('td');
		borderTopRight.className = 'wBorderTopRight';
		dElBorderTop.onmousedown = function () {return false;}
		dElBorderTop.onselectstart = function () {return false;}
		dElBorderTop.ondragstart = function () {return false;}
		dElBorderTop.appendChild(borderTopRight);
		// title
		dElTitle = dEl.insertRow(-1);
		titleLeft = this.documentRef.createElement('td');
		titleLeft.className = 'wTitleLeft';
		dElTitle.appendChild(titleLeft);
		titleCenter = this.documentRef.createElement('td');
		titleCenter.className = 'wTitleCenter';
			titleContainer =  this.documentRef.createElement('div');
			titleContainer.style.width = '100%';
			titleContainer.style.height = '100%';
			titleContainer.style.position = 'relative';
			titleCenter.appendChild(titleContainer);
				titleContent = this.documentRef.createElement('div');
				titleContent.className = 'wTitle';
				titleContent.innerHTML = this.title;
				titleContainer.appendChild(titleContent);
		dElTitle.appendChild(titleCenter);
		titleRight = this.documentRef.createElement('td');
		titleRight.className = 'wTitleRight';
		//dElTitle.onmousedown = wrap(this, this.startMove);
		//dElTitle.onmouseup = wrap(this, this.stopMove);
		dElTitle.ondragstart = function () {return false;}
		dElTitle.appendChild(titleRight);
		// content
		dElContent = dEl.insertRow(-1);
		contentLeft = this.documentRef.createElement('td');
		contentLeft.className = 'wContentLeft';
		dElContent.appendChild(contentLeft);
		contentCenter = this.documentRef.createElement('td');
		contentCenter.className = 'wContentCenter';
		dElContent.appendChild(contentCenter);
		contentRight = this.documentRef.createElement('td');
		contentRight.className = 'wContentRight';
		dElContent.onmousedown = function () {return false;}
		dElContent.onselectstart = function () {return false;}
		dElContent.appendChild(contentRight);
		// border bottom
		dElBorderBottom = dEl.insertRow(-1);
		borderBottomLeft = this.documentRef.createElement('td');
		borderBottomLeft.className = 'wBorderBottomLeft';
		dElBorderBottom.appendChild(borderBottomLeft);
		borderBottomCenter = this.documentRef.createElement('td');
		borderBottomCenter.className = 'wBorderBottomCenter';
		dElBorderBottom.appendChild(borderBottomCenter);
		borderBottomRight = this.documentRef.createElement('td');
		borderBottomRight.className = 'wBorderBottomRight';
		dElBorderBottom.onmousedown = function () {return false;}
		dElBorderBottom.onselectstart = function () {return false;}
		dElBorderBottom.appendChild(borderBottomRight);
		
		addEvent(this.documentRef, 'mousedown', selectionDisable);
		addEvent(this.documentRef, 'selectstart', selectionDisable);	
		toggleSelects(0);
		this.parentRef.appendChild(dEl);
		this.dEl = dEl;
		this.dElTitle = titleContent;
		this.dElContent = contentCenter;
		this.setHandleButtons();
	}
	this.setContent = function(contentHTML) {
		this.dElContent.innerHTML = contentHTML;
	}
	this.setContentWidth = function(width) {
		this.dEl.style.width = width + contentLeft.offsetWidth + contentRight.offsetWidth + 'px';
	}
	this.setContentHeight = function(height) {
		this.dEl.style.height = height + dElTitle.offsetHeight + dElBorderBottom.offsetHeight + 'px';
	}
	this.setHandle = function(contentHTML) {
		this.dElTitle.innerHTML += contentHTML;
	}
	this.remove = function() {
		removeEvent(this.documentRef, 'mousedown', selectionDisable);
		removeEvent(this.documentRef, 'selectstart', selectionDisable);	
		toggleSelects(1);
		setOverlay(0);
		this.parentRef.removeChild(this.dEl);
		//this.documentRef.body.style.overflow = 'auto';
		this.dEl = false;
		this.dElDraghandle = false;
		this.dElContent = false;
	}
	this.setHandleButtons = function() {
		if(this.buttons.indexOf('close')!=-1) this.setHandle('<a href="javascript:toggleFlash(1);'+this.elInst+'.remove();" class="wButtonClose"></a>');
	}
	this.setPosLeft = function(lPos) {
		this.dEl.style.left = lPos;
	}
	this.setPosTop = function(tPos) {
		this.dEl.style.top = tPos;
	}
	this.show = function() {
		this.dEl.style.visibility = 'visible';
	}
	this.centerH = function() {
		this.dEl.style.marginLeft = -Math.floor(this.dEl.offsetWidth/2) + 'px';
	}
	this.getWidth = function() {
		return this.dEl.offsetWidth;
	}
	this.getContentWidth = function() {
		return this.dElContent.offsetWidth;
	}
	this.getContent = function() {
		return this.dElContent.innerHTML;
	}
	this.getContentImgWidth = function() {
		if(this.dElContent.childNodes[0].tagName == 'IMG') {
			return this.dElContent.childNodes[0].offsetWidth;
		}
	}
}

function selectionDisable() {
	return false;
}

function toggleSelects(flag) {
	ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf('msie')!=-1 && ua.indexOf('opera')==-1) {
		flag ? disp = 'visible' : disp = 'hidden';
		selects = document.getElementsByTagName('select');
		for(var i=0; i<selects.length; i++) {
			selects[i].style.visibility = disp;
		}
	}
}

function wrap(obj, method) {
	return function(e) {return method.call(obj, e, this)}
}

function move(e) {
	if(movingElement) {
		if(e && e.target) {
			var mouseX = e.pageX;
			var mouseY = e.pageY;
		}
		else {
			var mouseX = event.clientX;
			var mouseY = event.clientY;
		}
		xPos = mouseX - movingElement.offsetX;
		yPos = mouseY - movingElement.offsetY;
		
		if(xPos < document.body.scrollLeft) // ganz links
			movingElement.dEl.style.left = document.body.scrollLeft + 'px';
		else if(xPos + 2 + movingElement.dEl.offsetWidth > document.body.offsetWidth + document.body.scrollLeft)  // ganz rechts
			movingElement.dEl.style.left = document.body.offsetWidth + document.body.scrollLeft - movingElement.dEl.offsetWidth - 2 + 'px';
		else // ok 
			movingElement.dEl.style.left = xPos + 'px';
		
		if(yPos < document.body.scrollTop) // ganz oben
			movingElement.dEl.style.top = document.body.scrollTop + 'px';
		else if(yPos + movingElement.dEl.offsetHeight > document.body.offsetHeight + document.body.scrollTop)  // ganz rechts
			movingElement.dEl.style.top = document.body.offsetHeight + document.body.scrollTop - movingElement.dEl.offsetHeight + 'px';
		else // ok 
			movingElement.dEl.style.top = yPos + 'px';
	}
}


function setOverlay(on) {
	if(on) {
		if(!document.getElementById('overlay')) {
			objBody = document.getElementsByTagName("body").item(0);
			ov = document.createElement('div');
			ov.setAttribute('id','overlay');
			ov.style.position = 'absolute';
			ov.style.top = '0';
			ov.style.left = '0';
			ov.style.zIndex = '1000';
			objBody.appendChild(ov);
		}
		ovEl = document.getElementById('overlay');
		winDim = getPageSize();
		ovEl.style.width = winDim[0] + 'px';
		ovEl.style.height = winDim[1] + 'px';
		ovEl.style.display = 'block';
	}
	else {
		if(document.getElementById('overlay')) document.getElementById('overlay').style.display = 'none';
	}
}

function checkOverlay() {
	if(document.getElementById('overlay') && document.getElementById('overlay').style.display == 'block') {
		ovEl = document.getElementById('overlay');
		winDim = getPageSize();
		ovEl.style.width = winDim[0] + 'px';
		ovEl.style.height = winDim[1] + 'px';
	}
}


