// JavaScript Document
// modal windows
// ----------------------------------------------

// credits and copyright
// ----------------------------------------------
// theMechanism. Jeffrey Barke. 20080514
// This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License <http://creativecommons.org/licenses/by-sa/3.0/us/>

var tmModalWindows={id:'overlay',counter:1,current:null,allOpen:new Array(),init:function(){var css=Builder.node('link',{rel:'stylesheet',type:'text/css',media:'screen, projection, tv',href:'/_dev/client/tmModalWindows/modalWindows.css'});$$('head')[0].appendChild(css);this.createOverlay();},hideModal:function(objDiv){if(!objDiv){objDiv=$('tmModWin-'+tmModalWindows.current);}
tmModalWindows.current=null;var intIndex=tmModalWindows.allOpen.indexOf(objDiv.id.substr(9)*1);if(intIndex>-1){tmModalWindows.allOpen.splice(intIndex,1);}
new Effect.Parallel([new Effect.Fade(objDiv,{sync:true}),new Effect.Fade(objDiv,{sync:true})],{duration:0.5,afterFinish:function(){if(tmModalWindows.allOpen.length==0){tmModalWindows.hideOverlay();}}});},hideOverlay:function(){new Effect.Fade($(this.id),{duration:0.2,fps:80,from:0.8,to:0.0});},showModal:function(strUrl,objParameters){if(tmModalWindows.current==null){var objDiv=Builder.node('div',{id:'tmModWin-'+tmModalWindows.counter,className:'modalWindow',style:'display:none;'});var div=Builder.node('div',{className:'modalWindow-inner'});if(strUrl.substr(0,7)=='http://'||strUrl.substr(0,1)=='/'){div.innerHTML='<div class="modalLoading">Please wait. Loading&#8230;</div>';if(!objParameters){objParameters={}}
new Ajax.Request(strUrl,{method:'get',parameters:objParameters,onSuccess:function(data){div.innerHTML=data.responseText;}});}else{div.innerHTML=strUrl;}
objDiv.appendChild(div);var objDim=Element.getPageSize();objDiv.style.top=(objDim.scroll.top+100)+'px';document.body.appendChild(objDiv);tmModalWindows.current=tmModalWindows.counter;tmModalWindows.allOpen.push(tmModalWindows.counter);tmModalWindows.counter++;new Effect.Parallel([new Effect.Appear(objDiv,{sync:true}),new Effect.Appear(objDiv,{sync:true})],{duration:0.5});}},showOverlay:function(strUrl,objParam){new Effect.Appear($(this.id),{duration:0.2,fps:80,from:0.00,to:0.6,afterFinish:function(){tmModalWindows.showModal(strUrl,objParam);}});},createOverlay:function(){var objOverlay=Builder.node('div',{id:tmModalWindows.id,style:'display:none;'});var objDim=Element.getPageSize();if(objDim.page.height>=objDim.window.height){var intHeight=objDim.page.height;}else{var intHeight=objDim.window.height;}
objOverlay.style.height=intHeight+'px';objOverlay.style.width=objDim.page.width+'px';document.body.appendChild(objOverlay);objOverlay.onclick=function(){for(var i=0;i<tmModalWindows.allOpen.length;i++){tmModalWindows.hideModal($('tmModWin-'+tmModalWindows.allOpen[i]));}
return false;}}}
Element.getPageSize=function(){var xScroll,yScroll,scrollOffsetY;if(window.innerHeight&&window.scrollMaxY){xScroll=document.body.scrollWidth;yScroll=window.innerHeight+window.scrollMaxY;}else if(document.body.scrollHeight>document.body.offsetHeight){xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;}else{xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;}
if(self.pageYOffset){scrollOffsetY=self.pageYOffset;}else if(document.documentElement&&document.documentElement.scrollTop){scrollOffsetY=document.documentElement.scrollTop;}else if(document.body){scrollOffsetY=document.body.scrollTop;}
var windowWidth,windowHeight;if(self.innerHeight){windowWidth=self.innerWidth;windowHeight=self.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(windowHeight&&yScroll){pageHeight=Math.max(windowHeight,yScroll);}
if(windowWidth,xScroll){pageWidth=Math.max(windowWidth,xScroll);}
return{page:{width:pageWidth,height:pageHeight},window:{width:windowWidth,height:windowHeight},scroll:{top:scrollOffsetY}};}
Event.observe(window,'load',tmModalWindows.init.bindAsEventListener(tmModalWindows),false);