/*------------------------------------------------------------------------
AMA Global JS

version: 1.1
author: andy windle
email: aw@ama.uk.com
www.ama.uk.com
--------------------------------------------------------------------------*/



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareLinks() { 
    if (!document.getElementById) return false;
    if (!document.getElementsByTagName) return false;
    //if (!document.getElementById("demoLogin")) return false;  
    if (!document.getElementById("modalBackground")) return false;
 
    //demoLogin = document.getElementById("demoLogin");  
    var modalBackground = document.getElementById("modalBackground"); 
  
    var links = document.getElementsByTagName("a");

    for (var i=0; i<links.length; i++){
           
        if(links[i].className.match("closeModalLink")){
            links[i].onclick = function() { 
                modalBackground.style.display = "none";   

                this.parentNode.parentNode.style.display = "none";  
                return false;
            }
        }
       
        if(links[i].className.match("modalTrigger")){
         
            links[i].onclick = function(event) {
            
            	self.scrollTo(0, 0);
            
                posx = getPosition('x', event);
                 
                modalID = this.getAttribute("id") + '_modal';
                modalBox = document.getElementById(modalID);
                //alert(modalID);
                
                if(modalID == 'demoLogin_modal') modalWidth = 400;
                else modalWidth = 700;
                
                // center the window
                //windowWidth = document.documentElement.clientWidth;
                posx = (960 - modalWidth) / 2;
                posx = posx + 200;
                 
                var posy = getPosition('y', event)-100;
                if(posy < 100) posy = 100;
                posy = 20;
                //alert(posx); 
                
                if(modalID != 'demoLogin_modal'){  
                    modalBox.style.top = (posy)+'px';
                    modalBox.style.left = posx+'px';
                }
                
                modalBackground.style.display = "block"; 
                modalBox.style.display = 'block';
                   
                return false;   
            }   
        }  
    } 
}

function getPosition(type, e) {
    var posx = 0;
    var posy = 0;
       
    if (!e) var e = this.event;
    if (e.pageX || e.pageY)     {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)     {
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }
    
    if (type = 'x') pos = posx;
    if (type = 'y') pos = posy;
    return(pos);
}
                                                    

addLoadEvent(prepareLinks);
//addLoadEvent(modalTrigger);   