var oldOnScroll;
var oldOnResize;
var siteRoot = '';

function toggleEl(elId, iconElId, showImage, hideImage) {
	el = $(elId);
	iconEl = $(iconElId);
	if (el.style.display == 'none') {
		Effect.Grow(el);
		iconEl.src = iconEl.src.replace(showImage, hideImage);
	} else {
		Effect.Shrink(el);
		iconEl.src = iconEl.src.replace(hideImage, showImage);
	}
	return false;
}

function toggleAbstract(id) {
	return(toggleEl('abstract' +id, 'growicona' +id, '/buttonabstract_plus.gif',
		'/buttonabstract_minus.gif'));
}

function toggleBio(id) {
	return(toggleEl('bio' +id, 'growiconb' +id, '/button_plus.gif',
		'/button_minus.gif'));
}

function toggleDesc(id) {
	return(toggleEl('desc' +id, 'growicon' +id, '/buttonabstract_plus.gif',
		'/buttonabstract_minus.gif'));
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {};
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth;
        var pageHeight = document.body.scrollHeight;
        
        if (pageWidth < document.documentElement.scrollWidth)
        	pageWidth = document.documentElement.scrollWidth;
        if (pageHeight < document.documentElement.scrollHeight)
        	pageHeight = document.documentElement.scrollHeight;
        if (pageWidth < document.documentElement.clientWidth)
        	pageWidth = document.documentElement.clientWidth;
        if (pageHeight < document.documentElement.clientHeight)
        	pageHeight = document.documentElement.clientHeight;
		if (pageWidth < window.innerWidth) pageWidth = window.innerWidth;
		if (pageWidth < window.innerHeight) pageWidth = window.innerHeight;
        pageWidth += 'px';
        pageHeight += 'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth +'px';
      var pageHeight = document.body.offsetHeight +'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';				 
  } else {
     dark.style.display='none';
  }
}

function centerPop(el) {
	var de = document.documentElement;
	
	el.style.left = de.scrollLeft +(de.clientWidth - el.getWidth() + 16) / 2 +'px';
	if (el.getHeight() - 16 < de.clientHeight) {
		el.style.top = de.scrollTop +(de.clientHeight - el.getHeight() - 16) / 2
			+'px';
	}
}

function iframePop(el, height, width) {
	var iframe = $('popup');
	var d = new Date();
	
	iframe.src = el.href +'&d=' +d.getTime();
	grayOut(true);
	if (height) iframe.style.height = height +'px';
	if (width) iframe.style.width = width +'px';
	oldOnScroll = window.onscroll;
	oldOnResize = window.onresize;
	window.onscroll = function() { centerPop(iframe); }
	window.onresize = function() { centerPop(iframe); }
	iframe.style.display = 'block';
	centerPop(iframe);
	return false;
}

function closePop() {
	var iframe = $('popup');

	iframe.style.display = 'none';	
	iframe.src = '';
	grayOut(false);
	window.onscroll = oldOnScroll;
	window.onresize = oldOnResize;
	if (oldOnScroll) oldOnScroll();
	
	return false;
}

function validRegMiniForm(f) {
	if (!validText(f.name, 'Name'))
		return false;
	
	if (trim(f.email.value) == '' && trim(f.phone.value) == '') {
		alert('Please enter email or phone.');
		f.email.focus();
		return false;
	}
	
	if (trim(f.email.value) != '' && !validateEmail(f.email.value)) {
		f.email.focus();
		return false;
	}
	
	return true;
}

function loadSetPhotos(photosetId, photoNum) {
	$('photowait_' +photoNum).src = siteRoot +'/_elems/wait2.gif';
	$('photowait_' +photoNum).style.display = 'block';
	new Ajax.Updater('photoset_photos', siteRoot +'/_ajax/getsetphotos.php', {
		parameters:{ photoset_id:photosetId, photo_num:photoNum },
		onComplete:function(transport) {
			Lightbox.updateImageList();
			$('photowait_' +photoNum).style.display = 'none';			
			$('photowait_' +photoNum).src = '';
			Lightbox.start($('curphoto'));
		}
	});
	return false;
}

function showBannerEffect() {
	return;
	var img2 = $('bannerimg').cloneNode(false);
	img2.style.display = 'none';
	img2.style.top = '24px';
	img2.style.position = 'absolute';
	$('bannerimg').parentNode.appendChild(img2);
	img2.style.position = 'absolute';
	$(img2).show();
	Effect.Puff(img2, { delay:2.0} );
}

function b64Decode(input) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
 
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	while (i < input.length) {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		output += String.fromCharCode(chr1);
		if (enc3 != 64) output += String.fromCharCode(chr2);
		if (enc4 != 64) output += String.fromCharCode(chr3);
	}
	return output;
}

function emWrite(lbl, cssClass, em1, em2, em3) {
	var em = String.fromCharCode(em2 -10) +String.fromCharCode(em3 -2) +em1;
	var decoded = b64Decode(em);
	var s = '<a href="mailto:' +decoded +'"';
	if (cssClass != '') s += ' class="' +cssClass +'"';
	s += '>email ' +lbl +'</a>';
	document.write(s);
}
