var closeButton = new Image();
closeButton.src = 'images/slip/close_on.png';

function scrollSlips(event) {
	// media slip
	var mediaSlipContainer = document.getElementById('mediaSlipContainer');
	if(mediaSlipContainer && mediaSlipContainer.style.display == 'block') {
		var modalShade = document.getElementById('mediaModalShade');
		var fixedNotSupported = modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
		
		if(fixedNotSupported) {
			modalShade.style.left = window.scrollX ? window.scrollX : document.documentElement.scrollLeft;
			modalShade.style.top = window.scrollY ? window.scrollY : document.documentElement.scrollTop;
		
			var slipTable = document.getElementById('mediaSlipTable');
			var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
			
			if(slipTable.offsetHeight < currentHeight) {
				slipResize('media');
			}
		}
	}

	// detail slip
	var detailSlip = document.getElementById('detailSlipContainer');
	if (detailSlip && detailSlip.style.display == 'block') {
		var modalShade = document.getElementById('detailModalShade');
		var fixedNotSupported = modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
		
		if (fixedNotSupported) {
			modalShade.style.left = window.scrollX ? window.scrollX : document.documentElement.scrollLeft;
			modalShade.style.top = window.scrollY ? window.scrollY : document.documentElement.scrollTop;
		
			var slipTable = document.getElementById('detailSlipTable');
			var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
			
			if(slipTable.offsetHeight < currentHeight) {
				slipResize('detail');
			}
		}
	}
}

function showSlip(handle) {
	var theSlip = $(handle+'SlipContainer');
	if(theSlip && !theSlip.animation) {
		var printStyle = document.getElementById('printStyle');
		var modalShade = document.getElementById(handle+'ModalShade');
		var fixedNotSupported = modalShade && modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';

		// display it as a block, but keep it hidden, so that it'll calculate it's own height correctly
		theSlip.style.visibility = 'hidden';
		theSlip.style.display = 'block';
		if(modalShade) {
			modalShade.style.visibility = 'hidden';
			modalShade.style.display = 'block';
			
			if(Browser.Engine.trident && !modalShade.hasIframe) {
				modalShade.hasIframe = true;
				modalShade.innerHTML = '<iframe src="blank.html"></iframe>';
			}
		}
		
		// update the scroll container, reset defaults
		resetSlipScroll(handle);
		
		// Get the position correct
		slipResize(handle);

		// Setup the printing
		if(printStyle) {
			if(printStyle.hrefSave) {
				printStyle.hrefSave.push(printStyle.href);
			} else {
				printStyle.hrefSave = new Array(printStyle.href);
			}
			printStyle.href = 'css/print_'+handle+'slip.css';
		}

		// finally, show the slip and the modal shade backdrop
		if(modalShade) {
			modalShade.style.visibility = 'visible';
		}
		theSlip.setStyle('opacity', 0);
		theSlip.animation = new Fx.Morph(theSlip.id, {duration: 500, transition: Fx.Transitions.Quart.easeOut, onStart: function() {
				theSlip.style.visibility = 'visible';
			}, onComplete: function() {
				setSlipScrollers(theSlip, 'auto');
				theSlip.animation = null;
			}
		});
		theSlip.animation.start({
			'opacity': [0, 1]
		});
	}
}

function setSlipScrollers(container, overflow) {
	var divs = container.getElementsByTagName('div');
	for(var i = 0; i < divs.length; i++) {
		if(divs[i].className == 'slipScroller') {
			divs[i].style.overflow = overflow;
		}
	}
}

function resetSlipScroll(handle) {
	var slipScrollContainer = document.getElementById(handle+'SlipScrollContainer');
	if(slipScrollContainer) {
		var slipScroll = document.getElementById(handle+'SlipScroll');
		var slipContent = document.getElementById(handle+'SlipContent');
	
		slipScrollContainer.style.width = slipContent.offsetWidth+'px';
		slipScrollContainer.style.height = slipContent.offsetHeight+'px';
		slipScroll.style.left = '0px';
		slipScroll.style.top = '0px';
		slipContent.style.left = '0px';
		slipContent.style.top = '0px';
	}
}

function slideSlip(handle, slideTo, data) {
	var slipScrollContainer = document.getElementById(handle+'SlipScrollContainer');
	if(slipScrollContainer) {
		var slipScroll = document.getElementById(handle+'SlipScroll');
		var slipContent = document.getElementById(handle+'SlipContent');
		var newContent = document.createElement('div');

		if(!slipScroll.animation) {
			newContent.className = 'slipContent';
			newContent.style.left = (slipContent.offsetLeft + slipContent.offsetWidth)+'px';
			newContent.style.top = slipContent.offsetTop+'px';

			newContent.innerHTML = data.replace(/\<script.+?\<\/script\>/g, '');
			if(Browser.Engine.trident4 && document.getElementById(handle+'ModalShade')) {
				slipResize(handle);
			}

			slipScroll.appendChild(newContent);

			if(slideTo == 'left') {
				newContent.style.left = (slipContent.offsetLeft - newContent.offsetWidth)+'px';
			} else if(slideTo == 'top') {
				newContent.style.left = (slipContent.offsetLeft - ((slipContent.offsetWidth - newContent.offsetWidth) / 2))+'px';
				newContent.style.top = (slipContent.offsetTop - newContent.offsetHeight)+'px';
			} else if(slideTo == 'bottom') {
				newContent.style.left = (slipContent.offsetLeft - ((slipContent.offsetWidth - newContent.offsetWidth) / 2))+'px';
				newContent.style.top = (slipContent.offsetTop + slipContent.offsetHeight)+'px';
			}

			setSlipScrollers(slipContent, 'hidden');

			if(slipScrollContainer.offsetHeight != newContent.offsetHeight || slipScrollContainer.offsetWidth != newContent.offsetWidth) {
				if(slipScrollContainer.offsetHeight != newContent.offsetHeight) {
					var slipTable = document.getElementById(handle+'SlipTable');
					if(slipTable.animation) {
						slipTable.animation.cancel();
					}
					
					slipTable.animation = new Fx.Morph(slipTable.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
						slipTable.animation = null;
						}
					});
					
					var newTop = slipTable.offsetTop + ((slipScrollContainer.offsetHeight - newContent.offsetHeight) / 2);
					
					if(newTop < 0) {
						newTop = 0;
					}
					
					slipTable.animation.start({
						'top': [slipTable.offsetTop, newTop]
					});
				}
			
				if(slipScrollContainer.animation) {
					slipScrollContainer.animation.cancel();
				}
				
				slipScrollContainer.animation = new Fx.Morph(slipScrollContainer.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
						slipResize(handle);
						slipScrollContainer.animation = null;
					}
				});
				slipScrollContainer.animation.start({
					'height': [slipScrollContainer.offsetHeight, newContent.offsetHeight],
					'width': [slipScrollContainer.offsetWidth, newContent.offsetWidth]
				});
			}

			slipScroll.animation = new Fx.Morph(slipScroll.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
					slipScroll.removeChild(slipContent);
					newContent.id = handle+'SlipContent';
					setSlipScrollers(newContent, 'auto');
					if(data.indexOf('--evalScriptsOnSlide-->') != -1) {
						evalScripts(data);
					}
					slipScroll.animation = null;
				}
			});

			slipScroll.animation.start({
				'left': [slipScroll.offsetLeft, -newContent.offsetLeft],
				'top': [slipScroll.offsetTop, -newContent.offsetTop]
			});
		}
	}
}

function hideSlip(handle) {
	var fixedNotSupported;
	var theSlip = $(handle+'SlipContainer');
	
	if(theSlip && !theSlip.animation) {
		// hide the modal shade
		var modalShade = document.getElementById(handle+'ModalShade');
		if(modalShade) {
			modalShade.style.display = "none";
			fixedNotSupported = modalShade && modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
		}
		// hide the slip
		var slipContent = document.getElementById(handle+'SlipContent');
		var slipTable = document.getElementById(handle+'SlipTable');
	
		theSlip.setStyle('opacity', 1);
		theSlip.animation = new Fx.Morph(theSlip.id, {duration: 500, transition: Fx.Transitions.Quart.easeOut, onStart: function() {
				setSlipScrollers(slipContent, 'hidden');
			}, onComplete: function() {
				theSlip.style.display = 'none';
				if(slipContent) {
					slipContent.innerHTML = '';
				}
				if(slipTable) {
					slipTable.style.top = "0px";
					
					if(slipTable.slipHasMoved) {
						slipTable.slipHasMoved = false;
						slipTable.setAttribute('align', 'center');
						slipTable.style.left = '';
					}
				}
				theSlip.animation = null;
			}
		});
		theSlip.animation.start({
			'opacity': [1, 0]
		});
		//theSlip.style.display = "none";
		
		if(modalShade && !fixedNotSupported) {
			theSlip.className = 'slipContainer fixed';
		}
		
		if(theSlip.getAttribute('keepShown') == 'yes') {
			theSlip.removeAttribute('keepShown');
		}
		
		// fix back the printing
		var printStyle = document.getElementById('printStyle');
		if(printStyle) {
			printStyle.href = printStyle.hrefSave && printStyle.hrefSave.length ? printStyle.hrefSave.pop() : 'css/print.css';
		}
	}
}

function slipResize(handle) {
	// Always resize the modal shade
	var modalShade = document.getElementById(handle+'ModalShade');
	var fixedNotSupported = modalShade && modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
	if(fixedNotSupported && modalShade && modalShade.style.display == 'block') {
		// offset the modal shade if we have to
		modalShade.style.left = window.scrollX ? window.scrollX : document.documentElement.scrollLeft;
		modalShade.style.top = window.scrollY ? window.scrollY : document.documentElement.scrollTop;
	}
	
	var slipTable = document.getElementById(handle+'SlipTable');
	var slipContainer = document.getElementById(handle+'SlipContainer');

	if(slipTable && slipContainer && slipContainer.style.display == 'block') {
		if(slipTable.slipHasMoved)
			return;
		
		var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
		var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);

		if(handle == 'media') {
			var mediaSlipImg = document.getElementById('mediaSlipImg');
			var mediaSlipScale = document.getElementById('mediaSlipScale');
			
			if(!mediaSlipImg.origWidth) {
				mediaSlipImg.origWidth = mediaSlipImg.offsetWidth;
			}
			if(!mediaSlipImg.origHeight) {
				mediaSlipImg.origHeight = mediaSlipImg.offsetHeight;
			}
			
			var width = mediaSlipImg.origWidth;
			var height = mediaSlipImg.origHeight;
			
			// check the dimensions - if this would be wider or taller than the window, scale it down appropriately
			if(width > (currentWidth-80)) {
				// restrain the width, and scale the height accordingly
				var targetWidth = (currentWidth-80);
				var targetHeight = Math.floor( (targetWidth*height) / width );
		
				width = targetWidth;
				height = targetHeight;
			}
			if(height > (currentHeight-60)) {
				// restrain the height, and scale the width accordingly
				var targetHeight = (currentHeight-60);
				var targetWidth = Math.floor( (targetHeight*width) / height );
				
				width = targetWidth; 
				height = targetHeight;
			}
			
			// adjust the dimensions to accomodate the media
			mediaSlipImg.style.width = width+'px';
			mediaSlipImg.style.height = height+'px';
			
			// update the scale
			var scale = Math.floor(width/mediaSlipImg.origWidth*100);
			mediaSlipScale.innerHTML = (scale == 100 ? '' : 'Scale: '+scale+'%');
		} else {
			// check if the slip is taller than the current window height, and cancel the fixed positioning if so
			if (slipTable.offsetHeight > currentHeight && slipContainer.className != 'slipContainer absolute') {
				slipContainer.className = 'slipContainer absolute';
			} else if(slipTable.offsetHeight <= currentHeight && !fixedNotSupported && slipContainer.className != 'slipContainer fixed') {
				slipContainer.className = 'slipContainer fixed';
			}
		}
		
		// center the popup within the window, counting on the "currentWidth" and "currentHeight" we read in before
		var verticalPosition = fixedNotSupported ? (window.scrollY ? window.scrollY : document.documentElement.scrollTop) : 0;
		
		// ...vertically center it the slipTable
		verticalPosition = (currentHeight/2)-((slipTable.offsetHeight)/2)+verticalPosition;
		if(verticalPosition < 0) {
			verticalPosition = 0;
		}
		
		slipTable.style.top = verticalPosition+'px';
	}
}

function startSlipMove(handle, e) {
	if(document.slipMoveInAction)
		return;
	
	document.slipMoveInAction = true;
	
	if(!e)
		e = window.event;
	
	document.slipHandle = handle;
	document.slipMouseX = e.clientX;
	document.slipMouseY = e.clientY;
	
	var slipContainer = document.getElementById(handle+'SlipContainer');
	var slipTable = document.getElementById(handle+'SlipTable');
	var slipContent = document.getElementById(handle+'SlipTable');
	
	if(slipTable.offsetLeft) {
		document.slipTableOffsetLeft = slipTable.offsetLeft;
	} else {
		var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
		
		document.slipTableOffsetLeft = Math.floor((currentWidth - slipTable.rows[0].offsetWidth)/2);
		if(document.slipTableOffsetLeft < 0)
			document.slipTableOffsetLeft = 0;
	}
	document.slipTableOffsetTop = slipTable.offsetTop;
	
	if(slipContainer.className != 'slipContainer absolute') {
		var scrollTop = window.scrollY ? window.scrollY : document.documentElement.scrollTop;
		document.slipTableOffsetTop += scrollTop;
		slipContainer.className = 'slipContainer absolute';
	}
	
	slipTable.slipHasMoved = true;
	slipTable.setAttribute('align', 'left');
	slipTable.style.left = document.slipTableOffsetLeft+'px';
	slipTable.style.top = document.slipTableOffsetTop+'px';
	
	if(window.addEventListener) {
		window.addEventListener("mousemove", slipMove, false);
		window.addEventListener("mouseup", slipMoveStop, false);
	} else if(document.attachEvent) {
		document.attachEvent("onmousemove", slipMove);
		document.attachEvent("onmouseup", slipMoveStop);
	}
}

function slipMoveStop(e) {
	if(window.addEventListener) {
		window.removeEventListener('mousemove', slipMove, false);
		window.removeEventListener('mouseup', slipMoveStop, false);
	} else if(document.attachEvent) {
		//var slipTable = document.getElementById(document.slipHandle+'SlipTable');
		
		document.detachEvent("onmousemove", slipMove);
		document.detachEvent("onmouseup", slipMoveStop);
	}
	
	document.slipMoveInAction = false;
}

function slipMove(e) {
	if(!e)
		e = window.event;
	var slipTable = document.getElementById(document.slipHandle+'SlipTable');
	
	var diffX = document.slipMouseX - e.clientX;
	var diffY = document.slipMouseY - e.clientY;
	
	var newLeft = document.slipTableOffsetLeft - diffX;
	var newTop = document.slipTableOffsetTop - diffY;
	
	slipTable.style.left = newLeft+'px';
	slipTable.style.top = newTop+'px';
}

/************** General ***************/
function resizeSlips() {
	slipResize('media');
	slipResize('detail');
}

/************** Events ****************/
if(window.addEventListener) {
	window.addEventListener("keypress", keyTrack, false);
	window.addEventListener('scroll', scrollSlips, false);
	window.addEventListener('resize', resizeSlips, false);
} else if(document.attachEvent) {
	document.attachEvent("onkeypress", keyTrack);
	window.attachEvent('onscroll', scrollSlips);
	window.attachEvent('onresize', resizeSlips);
}

function keyTrack(e) {
	var keyCode = getKeyCode(e);
	if(keyCode == 27) {
		var el = document.getElementById('mediaSlipContainer');
		var el2 = document.getElementById('detailSlipContainer');
		var el3 = document.getElementById('criticalNoticeSlipContainer');

		if(el && el.style.display == 'block') {
			hideSlip('media');
		} else if(el2 && el2.style.display == 'block') {
			hideSlip('detail');
		} else if(el3 && el3.style.display == 'block') {
			hideSlip('criticalNotice');
		}
		
		if(e.preventDefault) { // stop firefox from stoping our status animations
			e.preventDefault();
		}
	}
}

function getKeyCode(e) {
	if(window.event) {
		return window.event.keyCode;
	} else if(e) {
		return e.keyCode ? e.keyCode : e.which;
	} else {
		return null;
	}
}

function newWindowKey(e) {
	if(e) {
		return e.ctrlKey || e.metaKey;
	} else if(window.event) {
		return window.event.ctrlKey;
	} else {
		return null;
	}
}