/* set variables */

modalOverlayColor = '#12454a';
modalOverlayOpacity = 75; // percent
modalContainerColor = '#000000';
modalBorderWidth = 5;
modalNavHeight = 20;
modalClose = '/template/shared/images/modalwindow_close.gif';
modalCloseWidth = 10;
modalCloseHeight = 10;
modalImageDefaultWidth = 640;
modalImageDefaultHeight = 480;
modalImagePlaceholder = '/template/shared/images/modalwindow_loader.gif';
modalImagePlaceholderWidth = 45;
modalImagePlaceholderHeight = 5;
modalAnimationTime = 0.5 // seconds
modalZIndex = 900;

/* call modal window */

function modalWindow(modalSrc,modalWidth,modalHeight,modalTitle,modalHTMLID) {
modalElementSrc = modalSrc;
modalElementWidth = modalWidth || modalImageDefaultWidth;
modalElementHeight = modalHeight || modalImageDefaultHeight;
modalElementTitle = modalTitle;
modalElementHTMLID = modalHTMLID;
modalElementType = getModalType(modalElementSrc);
modalElementID = 'modal';
modalOverlayID = modalElementID + '_modalOverlay';
modalContainerID = modalElementID + '_container';
modalNavID = modalElementID + '_nav';
modalImageLoaderID = modalElementID + '_imageLoader';
modalImagePlaceholderID = modalElementID + '_imagePlaceholder';
modalImageContainerID = modalElementID + '_imageContainer';
modalImageID = modalElementID + '_image';
modalOverlay = true;
modalContainer = false;
modalNav = false;
modalContent = false;
modalInitialized = false;
addElement(modalOverlayID);
var overlay = document.getElementById(modalOverlayID);
modalZIndex++;
overlay.style.zIndex = modalZIndex;
overlay.style.position = 'absolute';
overlay.style.backgroundColor = modalOverlayColor;
positionModalOverlay();
window.onresize = positionModalOverlay;
// window.onscroll = positionModalOverlay;
if (BrowserDetect.browser == 'Firefox' && BrowserDetect.OS == 'Mac') { // fix mac firefox opacity bug
modalOverlayOpacity = 100;
} // end fix mac firefox opacity bug
modalOpacity = modalOverlayOpacity;
fadeIn(modalOverlayID);
// document.body.parentNode.style.overflow = 'hidden';
}

/* create elements */

function addElement(elementID) {
newElement = document.createElement('div');
newElement.id = elementID;
newElement.style.display = 'none';
document.body.insertBefore(newElement,document.body.firstChild);
}

function createModalContainer() {
modalContainer = true;
addElement(modalContainerID);
var container = document.getElementById(modalContainerID);
modalZIndex++;
container.style.zIndex = modalZIndex;
container.style.position = 'absolute';
container.style.backgroundColor = modalContainerColor;
container.style.borderColor = '#ffffff';
container.style.borderWidth = modalBorderWidth + 'px';
container.style.borderStyle = 'solid';
positionModalContainer();
window.onresize = positionModalContainer;
createModalNav();
modalOpacity = 100;
fadeIn(modalContainerID,modalNavID);
}

function createModalNav() {
modalNav = true;
addElement(modalNavID);
var nav = document.getElementById(modalNavID);
// modalZIndex++;
nav.style.zIndex = modalZIndex;
nav.style.position = 'absolute';
nav.style.backgroundColor = '#ffffff';
nav.style.borderColor = '#ffffff';
nav.style.borderWidth = modalBorderWidth + 'px';
nav.style.borderBottomWidth = '0px';
nav.style.borderStyle = 'solid';
positionModalNav();
window.onresize = positionModalNav;
getModalNavContent();
}

/* position elements */

function positionModalOverlay() {
if (document.getElementById(modalOverlayID)) {
pageSize = modalPageSize();
var overlay = document.getElementById(modalOverlayID);
var overlayWidth,overlayHeight,overlayLeft,overlayTop;
if (pageSize.windowWidth < modalElementWidth) {
overlayWidth = modalElementWidth + 'px';
} else {
overlayWidth = '100%';
}
if (pageSize.pageHeight < modalElementHeight) {
overlayHeight = modalElementHeight;
} else {
overlayHeight = pageSize.pageHeight;
}
overlay.style.left = '0px';
overlay.style.top = '0px';
overlay.style.width = overlayWidth;
overlay.style.height = overlayHeight + 'px';
}
}

function positionModalElement(id) {
if (document.getElementById(id)) {
var element = document.getElementById(id);
element.style.width = modalElementWidth + 'px';
element.style.height = modalElementHeight + 'px';
var elementLeft,elementTop;
elementLeft = Math.round((pageSize.windowWidth - modalElementWidth) / 2);
if (pageSize.verticalScroll) {
elementLeft -= 15;
}
pageScroll = modalPageScroll();
elementTop = (pageScroll + ((pageSize.windowHeight - modalElementHeight) / 2));
if (elementTop > pageSize.pageHeight - modalElementHeight) {
elementTop = pageSize.pageHeight - modalElementHeight;
}
if (elementLeft < 0) {
elementLeft = 0;
}
if (elementTop < modalNavHeight + modalBorderWidth) {
elementTop = modalNavHeight + modalBorderWidth;
}
element.style.left = elementLeft + 'px';
element.style.top = elementTop + 'px';
}
}

function positionModalContainer() {
positionModalOverlay();
positionModalElement(modalContainerID);
}

function positionModalNav() {
positionModalContainer();
if (document.getElementById(modalNavID)) {
var nav = document.getElementById(modalNavID);
nav.style.width = modalElementWidth + 'px';
nav.style.height = modalNavHeight + 'px';
var navLeft,navTop;
var navLeft = Math.round((pageSize.windowWidth - modalElementWidth) / 2);
if (pageSize.verticalScroll) {
navLeft -= 15;
}
pageScroll = modalPageScroll();
navTop = (pageScroll + ((pageSize.windowHeight - modalElementHeight) / 2));
if (navTop > pageSize.pageHeight - modalElementHeight) {
navTop = pageSize.pageHeight - modalElementHeight;
}
navTop = navTop - modalNavHeight - modalBorderWidth;
if (navLeft < 0) {
navLeft = 0;
}
if (navTop < 0) {
navTop = 0;
}
nav.style.left = navLeft + 'px';
nav.style.top = navTop + 'px';
}
}

function positionModalImageLoader() {
positionModalNav();
positionModalElement(modalImageLoaderID);
positionModalImagePlaceholder();
}

function positionModalImagePlaceholder() {
if (document.getElementById(modalImagePlaceholderID)) {
var placeholder = document.getElementById(modalImagePlaceholderID);
var placeholderLeft,placeholderTop;
placeholderLeft = Math.round((modalElementWidth - modalImagePlaceholderWidth) / 2);
if (placeholderLeft < modalBorderWidth) {
// placeholderLeft = modalBorderWidth;
}
placeholderTop = Math.round((modalElementHeight - modalImagePlaceholderHeight) / 2);
if (placeholderTop < modalBorderWidth) {
// placeholderTop = modalBorderWidth;
}
placeholder.style.left = placeholderLeft + 'px';
placeholder.style.top = placeholderTop + 'px';
}
}

function positionModalImageContainer() {
positionModalImageLoader();
positionModalElement(modalImageContainerID);
}

/* get element content */

function getModalContent() {
modalContent = true;
var container = document.getElementById(modalContainerID);
switch (modalElementType) {
case 'flash':
if (hasReqestedVersion) {
AC_FL_RunContent(
"div_id", modalContainerID,
"src", modalElementSrc,
"width", modalElementWidth,
"height", modalElementHeight,
"align", "middle",
"id", modalContainerID + "_flash",
"quality", "high",
"bgcolor", modalContainerColor,
"name", modalContainerID + "_flash",
"wmode","opaque",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
}
modalInitialized = true;
break;
case 'image':
getModalImage(modalElementSrc);
break;
case 'iframe':
container.innerHTML = '<iframe src ="'+ modalElementSrc +'" width="'+ modalElementWidth +'" height="'+ modalElementHeight +'" frameborder="0" scrolling="auto"></iframe>';
modalInitialized = true;
break;
case 'html':
if (modalElementHTMLID) {
if (document.getElementById(modalElementHTMLID)) {
modalElementHTML = document.getElementById(modalElementHTMLID).innerHTML;
var bottomPadding = modalNavHeight / 2;
container.innerHTML = '<div style="width:'+ modalElementWidth +'px;height:'+ modalElementHeight +'px;overflow:auto"><div class="modalcontent" style="margin:'+ modalNavHeight +'px;padding-bottom:'+ bottomPadding +'px">'+ modalElementHTML +'</div></div>';
}
}
modalInitialized = true;
break;
}
container.style.filter = 'alpha(opacity=100)';
container.style.opacity = 1;
}

function getModalNavContent() {
var navHTML = '';
var navMargin = Math.round((modalNavHeight - modalCloseHeight) / 2);
// close
navHTML += '<div style="float:right;margin:'+ navMargin +'px">';
navHTML += '<a href="javascript:;" onclick="closeModalWindow()">';
navHTML += '<img src="'+ modalClose +'" width="'+ modalCloseWidth +'" height="'+ modalCloseHeight +'" border="0" />';
navHTML += '</a>';
navHTML += '</div>';
// title
if (modalElementTitle) {
var titleWidth = modalElementWidth - modalCloseHeight - (navMargin * 4);
navHTML += '<div class="modaltitle" style="width:'+ titleWidth +'px;height:'+ modalNavHeight +'px;line-height:'+ modalNavHeight +'px;margin:0px '+ navMargin +'px;overflow:hidden">';
navHTML += modalElementTitle;
navHTML += '</div>';
}
document.getElementById(modalNavID).innerHTML = navHTML;
}

/* type function */

function getModalType(thisSrc) {
var ext;
thisSrc = thisSrc.toLowerCase();
// Html
if (!thisSrc || thisSrc.length == 0 || thisSrc.charAt(thisSrc.length - 1) == '#') {
return 'html';
}
// YouTube
var youTube = thisSrc.indexOf("youtube.com");
if (youTube != -1) {
return 'flash';
}
// File
var splitSrc = thisSrc.split('?');
var thisFile = splitSrc[0];
var dot = thisFile.lastIndexOf('.');
if (dot != -1) { // valid extension
ext = thisFile.substr(dot,thisFile.length);
ext = ext.replace('.','');
} else {
ext = false;
}
switch (ext) {
case 'jpg':
case 'jpeg':
case 'gif':
case 'png':
case 'bmp':
return 'image';
break;
case 'swf':
return 'flash';
break;
default:
return 'iframe';
break;
}
}

/* image functions */

function getModalImage(modalElementSrc) {
addElement(modalImageLoaderID);
var imageLoader = document.getElementById(modalImageLoaderID);
modalZIndex++;
imageLoader.style.zIndex = modalZIndex;
imageLoader.style.position = 'absolute';
imageLoader.style.margin = modalBorderWidth + 'px';
imageLoader.innerHTML = '<img src="'+ modalImagePlaceholder +'" width="'+ modalImagePlaceholderWidth +'" height="'+ modalImagePlaceholderHeight +'" border="0" id="'+ modalImagePlaceholderID +'" style="position:absolute" />';
positionModalImageLoader();
addElement(modalImageContainerID);
var imageContainer = document.getElementById(modalImageContainerID);
modalZIndex++;
imageContainer.style.zIndex = modalZIndex;
imageContainer.style.position = 'absolute';
imageContainer.style.margin = modalBorderWidth + 'px';
var image = new Image();
image.onload = function() { modalImageLoaded(image) };
image.src = modalElementSrc;
positionModalImageContainer();
window.onresize = positionModalImageContainer;
imageContainer.style.display = '';
imageLoader.style.display = '';
}

function modalImageLoaded(imageObj) {
var imageLoader = document.getElementById(modalImageLoaderID);
document.body.removeChild(imageLoader);
modalInitialized = true;
var imageHTML = '<a href="javascript:;" onclick="closeModalWindow()">';
// imageHTML += '<img src="'+ imageObj.src +'" width="'+ imageObj.width +'" height="'+ imageObj.height +'" id="'+ modalImageID +'" border="0" style="display:none" />';
imageHTML += '<img src="'+ imageObj.src +'" width="'+ modalElementWidth +'" height="'+ modalElementHeight +'" id="'+ modalImageID +'" border="0" style="display:none" />';
imageHTML += '</a>';
var imageContainer = document.getElementById(modalImageContainerID);
imageContainer.innerHTML = imageHTML;
fadeIn(modalImageID);
}

/* fade functions */

function fadeIn(id) {
stepTotal = (modalAnimationTime * 1000) / 50;
if (stepTotal < 1) {
stepTotal = 1;
}
step = 0;
var fadeFunction = 'fadeInStep(';
for (var i = 0; i < arguments.length; i++) {
if (i > 0) {
fadeFunction += ',';
}
fadeFunction += '"'+ arguments[i] +'"';
}
fadeFunction += ')';
fadeInterval = setInterval(fadeFunction,50);
}

function fadeInStep(id) {
step++;
if (step <= stepTotal) {
for (var i = 0; i < arguments.length; i++) { // for each
var element = document.getElementById(arguments[i]);
var filterValue = ((100 / stepTotal) * step) - (100 - modalOpacity);
var opacityValue = filterValue / 100;
element.style.filter = 'alpha(opacity='+ filterValue +')';
element.style.opacity = opacityValue;
element.style.display = '';
} // end for each
} else {
window.clearInterval(fadeInterval);
if (!modalContainer) {
createModalContainer();
} else if (!modalContent) {
getModalContent();
document.onkeypress = modalKeyPress;
document.onkeydown = modalKeyPress;
}
}
}

function fadeOut(id) {
stepTotal = (modalAnimationTime * 1000) / 50;
if (stepTotal < 1) {
stepTotal = 1;
}
step = stepTotal;
var fadeFunction = 'fadeOutStep(';
for (var i = 0; i < arguments.length; i++) {
if (i > 0) {
fadeFunction += ',';
}
fadeFunction += '"'+ arguments[i] +'"';
}
fadeFunction += ')';
fadeInterval = setInterval(fadeFunction,50);
}

function fadeOutStep(id) {
step--;
if (step >= 0) {
for (var i = 0; i < arguments.length; i++) { // for each
var element = document.getElementById(arguments[i]);
var filterValue = (100 / stepTotal) * step;
var opacityValue = filterValue / 100;
element.style.filter = 'alpha(opacity='+ filterValue +')';
element.style.opacity = opacityValue;
} // end for each
} else {
window.clearInterval(fadeInterval);
fadeIn(id);
}
}

/* page dimensions */

function modalPageScroll() {
var yScroll,pageScroll;
if (window.pageYOffset) {
yScroll = window.pageYOffset;
} else if (document.documentElement && document.documentElement.scrollTop) {
yScroll = document.documentElement.scrollTop;
} else if (document.body) {
yScroll = document.body.scrollTop;
}
pageScroll = yScroll;
return pageScroll;
}

function modalPageSize() {
var yScroll,pageSize;
if (modalInitialized) {
document.getElementById(modalOverlayID).style.display = 'none';
}
if (window.innerHeight && window.scrollMaxY) {	
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){
yScroll = document.body.parentNode.scrollHeight;
} else {
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (window.innerHeight) {
windowWidth = window.innerWidth;
windowHeight = window.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) {
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
if (yScroll <= windowHeight) {
pageHeight = windowHeight;
verticalScroll = false;
} else {
pageHeight = yScroll;
verticalScroll = true;
}
pageSize = {'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'verticalScroll':verticalScroll};
if (modalInitialized) {
document.getElementById(modalOverlayID).style.display = '';
}
return pageSize;
}

/* keyboard functions */

function modalKeyPress(e) {
if (window.event) { // IE
var thisKeyCode = window.event.keyCode;
} else if (e.which) { // Netscape/Firefox/Opera
var thisKeyCode = e.which;
}
if (thisKeyCode == 27) { // esc
closeModalWindow();
}
}

/* close modal window */

function closeModalWindow() {
if (document.getElementById(modalOverlayID)) {
var overlay = document.getElementById(modalOverlayID);
document.body.removeChild(overlay);
}
if (document.getElementById(modalContainerID)) {
var container = document.getElementById(modalContainerID);
document.body.removeChild(container);
}
if (document.getElementById(modalNavID)) {
var nav = document.getElementById(modalNavID);
document.body.removeChild(nav);
}
if (document.getElementById(modalImageLoaderID)) {
var imageLoader = document.getElementById(modalImageLoaderID);
document.body.removeChild(imageLoader);
}
if (document.getElementById(modalImageContainerID)) {
var imageContainer = document.getElementById(modalImageContainerID);
document.body.removeChild(imageContainer);
}
window.onresize = null;
// window.onscroll = null;
document.onkeypress = null;
document.onkeydown = null;
}

/* string functions */

function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/* initialize */

function initialize() {
var aElements = document.getElementsByTagName('a');
for (var i = 0; i < aElements.length; i++) { // for each a tag
var aElement = aElements[i];
if (aElement.rel) { // rel exists
var aRel = aElement.rel;
var parameters = aRel.split(';');
if (trim(parameters[0]) == 'modalwindow') { // modalwindow
var modalParameters = new Array();
modalParameters[0] = aElement.href;
modalParameters[3] = aElement.title;
for (var j = 1; j < parameters.length; j++) { // for each parameter
var pair = parameters[j].split('=');
var parameter = trim(pair[0]);
var value = trim(pair[1]);
switch (parameter) { // parameter assignment
case 'width':
modalParameters[1] = value;
break;
case 'height':
modalParameters[2] = value;
break;
case 'id':
modalParameters[4] = value;
break;
} // end parameter assignment
} // end for each parameter
setModalWindowFunction(aElement,modalParameters);
aElement.href = 'javascript:;';
} // end modalwindow
} // end rel exists
} // end for each a tag
}

function setModalWindowFunction(element,modalParameters) {
element.onclick = function() { modalWindow(modalParameters[0],modalParameters[1],modalParameters[2],modalParameters[3],modalParameters[4]) };
}

addLoadEvent(initialize);