﻿var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=625,height=475,scrollbars=0,resizable=0';
var _DIALOG_FEATURES = 'dialogWidth=625px,dialogHeight=475px,center=yes,location=0,statusbar=0,menubar=0,scrollbars=0,resizable=0';
function isUndefined(a) { return typeof a == 'undefined' }

function raw_popup(url, target, features) {
  if (isUndefined(features)) {
      if (window.showModelessDialog){features=_DIALOG_FEATURES;} else {features=_POPUP_FEATURES;}
  }
  if (isUndefined(target)) {target = '_blank';}
  if (window.showModelessDialog) {
      var theWindow=window.showModelessDialog(url, 'gvpopup', _DIALOG_FEATURES);
  } else {
      var theWindow = window.open(url, target, features);
      // edit for popup blockers
      if (theWindow != null) {theWindow.focus();}
  }
  return theWindow;
}

function link_popup(src, features) {return raw_popup(src.getAttribute('href'),src.getAttribute('target') || '_blank',features);}

// To be passed as an event listener
// pops up a window grabbing the url from the event source's href
function event_popup(e) {link_popup(e.currentTarget);e.preventDefault();}

// DOM
function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className){return getElem(elem).className.split(' ').has(className);}
function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),function(elem) { return hasClass(elem, className) });
}

// DOM EVENTS
function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}

function mlisten(event,elem_list,func){map(elem_list, function(elem) { listen(event, elem, func) } );}
function W3CDOM_Event(currentTarget){
    this.currentTarget=currentTarget;
    this.preventDefault=function() { window.event.returnValue = false }
    return this;
}

function setPopups(){
    var popupAnchors = document.getElementsByTagName("a");
    for (i=0;i<popupAnchors.length;i++){
        if (popupAnchors[i].className=='popup'){
            if (popupAnchors[i].id!=null){if (popupAnchors[i].id !='') {listen('click', popupAnchors[i].id, event_popup);}}
        }
    }
}

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-575;//300 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-402;//300 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}