// JavaScript Document
function hideAllPops(){
	if(document.getElementById("hingham")!=null){
		document.getElementById("hingham").style.display = "none";
	}
	if(document.getElementById("norwell")!=null){
		document.getElementById("norwell").style.display = "none";
	}
	if(document.getElementById("holidayHours")!=null){
		document.getElementById("holidayHours").style.display = "none";
	}
	if(document.getElementById("educationVid")!=null){
		document.getElementById("educationVid").style.display = "none";
	}
	if(document.getElementById("communityVid")!=null){
		document.getElementById("communityVid").style.display = "none";
	}
	
}

function showHolidayHours(){
	
	hideAllPops();
	document.getElementById("holidayHours").style.display = "inline";
	show_info(null, 726, 568);
}

function showTrainingVideo(){
	
	hideAllPops();
	document.getElementById("educationVid").style.display = "inline";
	show_info(null, 680, 384);
	document.getElementById("edu_video").style.visibility = "visible";
}

function showHaitiVideo(){
	
	hideAllPops();
	document.getElementById("communityVid").style.display = "inline";
	show_info(null, 680, 384);
	document.getElementById("haiti_video").style.visibility = "visible";
}


function showNorwell(){
	if(document.getElementById('norwell_map') == null){
		var map = document.createElement("iframe");
		map.setAttribute("id", "norwell_map");
		map.setAttribute("width", "490");
		map.setAttribute("height", "370");
		map.setAttribute("frameborder", "0");
		map.setAttribute("scrolling", "no");
		map.setAttribute("marginheight", "0");
		map.setAttribute("marginwidth", "0");
		map.setAttribute("src", "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=10+Washington+St.+Norwell,+MA+02061&sll=37.0625,-95.677068&sspn=47.080837,79.365234&ie=UTF8&view=map&ll=42.188974,-70.876408&spn=0.04706,0.084114&z=13&iwloc=A&output=embed"	);
		
		document.getElementById('norwell').appendChild(map);
	}


	hideAllPops();
	document.getElementById("norwell").style.display = "inline";
	show_info(null, 726, 411);
}

function showHingham(){
	if(document.getElementById('hingham_map') == null){
		var map = document.createElement("iframe");
		map.setAttribute("id", "hingham_map");
		map.setAttribute("width", "490");
		map.setAttribute("height", "370");
		map.setAttribute("frameborder", "0");
		map.setAttribute("scrolling", "no");
		map.setAttribute("marginheight", "0");
		map.setAttribute("marginwidth", "0");
		map.setAttribute("src", "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=65+South+St.+Hingham,+MA+02043&sll=42.143264,-70.835724&sspn=0.02167,0.038753&ie=UTF8&ll=42.245008,-70.889153&spn=0.011755,0.021029&z=15&iwloc=A&output=embed"	);
		
		document.getElementById('hingham').appendChild(map);
	}
	
	hideAllPops();
	document.getElementById("hingham").style.display = "inline";
	show_info(null, 726, 411);
}

function popupWindow(page, title, vars){
	window.open(page,title,vars);
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400');");
}

function close_info(){
	var content = document.getElementById("frameContent");
	//remove content from popup before hiding it...
	 if(content.getElementsByTagName('embed').length>0){
		  content.removeChild(content.getElementsByTagName('embed')[0]);
	 }							
						
	document.getElementById('frame').style.display = "none";
	document.getElementById('cover').style.visibility = "hidden";
	document.getElementById('cover').style.width = "0px";
	document.getElementById('cover').style.height = "0px";
	//var objBody = document.getElementsByTagName("body").item(0);
	showFlash();
	
}

function show_info(elemID, frameWidth, frameHeight){
	//define height and width of popup frame
	hideFlash();
			
	if(frameWidth==null){
		var frameWidth = 726;
	}
	if(frameHeight==null){
		var frameHeight = 468;
	}

	if(frameWidth != 726){
		var closeBtn = 	document.getElementById('closeButton');
		closeBtn.style.left = frameWidth - 20 + "px";
	}

	// stretch overlay to fill page and fade in
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var cover = document.getElementById('cover');
	cover.style.width = arrayPageSize[0]+arrayPageScroll[0]+'px';
	cover.style.height = arrayPageSize[1]+arrayPageScroll[1]+'px';
	cover.style.visibility = "visible";
	
	var frame = document.getElementById("frame");
	var boxTop = 80; /*arrayPageScroll[1] + 80;*/
	//top and left corners are based size of viewable area
	var boxLeft = arrayPageScroll[0] + Math.round(arrayPageSize[0] / 2);

	if(boxLeft > frameWidth/2){ boxLeft -= Math.round(frameWidth/2);}
	if(boxTop > frameHeight/2){ boxTop -= Math.round(frameHeight/2);}
	frame.style.top = boxTop + 'px';
	frame.style.left = boxLeft + 'px';
	frame.style.height = frameHeight + 'px';
	frame.style.width = frameWidth + 'px';

	frame.style.visibility = 'visible';
	frame.style.display = 'inline';
}


// -----------------------------------------------------------------------------------
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

