$(document).ready(function(e){
/****************************************************************
sign up for newsletter
*****************************************************************/
	$('#newsletter-signup-form').submit(function(e){
		e.preventDefault();
		e.stopPropagation();
		var val = $('#emailSignup').val();
		if(!val.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
			$('#newsletter-signup-msg').addClass('error').html('The email you entered is invalid.');
		 	$('#emailSignup').focus();
			return;
		}
		$.ajax({ url: '/_includes/newsletter.signup.php',
			   	 type: 'POST',
			   	 data: 'em='+escape(val),
				 success: function(data){
					 if(data == 'invalid'){
						 $('#newsletter-signup-msg').addClass('error').html('The email you entered is invalid.');
						 $('#emailSignup').focus();
					 } else {
						 $('#newsletter-signup-msg').removeClass('error').html('Thank you!');
						 $('#newsletter-signup-form').fadeOut();
					 }
				 }
		});
		
	}).children('#emailSignup').
		focus(function(e){ if(this.value=='EMAIL')this.value=''; }).
		blur(function(e){ if(this.value=='')this.value='EMAIL'; });
});

/****************************************************************
pop-up window functionality
*****************************************************************/
window.onerror=null;
var win = null;

function newWindow(mypage, myname, w, h, scroll) {
closePopWin();
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize';
win = window.open(mypage, myname, winprops);
}

function closePopWin() {
     if(win != null) {
          if(!win.closed) {
              win.close();
              win = null;
          } else {
              win = null;
          }
     }
}
/****************************************************************
pop-up window functionality
*****************************************************************/
function popwin(loc,winname,w,h,scroll,resize) {
	var newwin = window.open( loc, winname, "width="+w+",height="+h+",top="+((screen.height - h) / 2)+",left="+((screen.width - w) / 2)+",location=no,scrollbars="+scroll+",menubar=no,toolbar=no,resizable="+resize);
} // function..popwin


/****************************************************************
mouseover functionality
*****************************************************************/	

function imghighlight(whichimg) {
	var imgsrc = new String(whichimg.src);
	imgsrc = imgsrc.replace(new RegExp('_off\.','g'),'_on\.');
	whichimg.src = imgsrc;
}

function imgnohighlight(whichimg) {
	var imgsrc = new String(whichimg.src);
	imgsrc = imgsrc.replace(new RegExp('_on\.','g'),'_off\.');
	whichimg.src = imgsrc;
}
/****************************************************************
mouseover functionality
*****************************************************************/	



/****************************************************************
Print Page
*****************************************************************/	
function printPage()
{
  if(window.print){window.print();}
  else {alert("Printing Instructions:\n\n - If you are using a Macintosh, please select \"Print\" from the \"File\" menu.\n - If you are using Windows, right click the page and select \"Print\" from the pop-up menu.");}
}
/****************************************************************
Print Page
*****************************************************************/



/*
	Image Cross Fade Redux
	Version 1.0
	Last revision: 02.15.2006
	steve@slayeroffice.com

	Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html
*/

//window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgs = new Array(), zInterval = null, current=new Array(), pause=false, divID=1;
function startSlideshow()
{
	divID = ActiveSlideshows[0];
	setTimeout(so_xfade,3000);
}

function so_init(divID)
{
	//console.log("so_init(" + divID + ")");
	if(!d.getElementById || !d.createElement)return;
	imgs[divID] = new Array();
	current[divID] = 0;
	css = d.createElement('link');
	//css.setAttribute('href','slideshow2.css');
	//css.setAttribute('rel','stylesheet');
	//css.setAttribute('type','text/css');
	//d.getElementsByTagName('head')[0].appendChild(css);

	imgs[divID] = d.getElementById('rotator_'+divID).getElementsByTagName('img');
	//console.log("Found " + imgs[divID].length + " images");
	currentCount = imgs[divID].length ;
	//console.log("Should Show: " + currentCount);
	i = 0;
	while(i < currentCount)
	{
		//console.log("Image Count: " + i);
	   //	console.log("Image " + i + " " + d.getElementById('rotator_'+divID).getElementsByTagName('img')[i].src);
		imgs[divID][i].xOpacity = 0;
		i++;
	}
	imgs[divID][0].style.display = 'block';
	imgs[divID][0].xOpacity = .99;


}

function so_xfade()
{

//	console.log("Fading "+divID);
	cOpacity = imgs[divID][current[divID]].xOpacity;
	nIndex = imgs[divID][current[divID]+1]?current[divID]+1:0;
	nOpacity = imgs[divID][nIndex].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	imgs[divID][nIndex].style.display = 'block';
	imgs[divID][current[divID]].xOpacity = cOpacity;
	imgs[divID][nIndex].xOpacity = nOpacity;

	if(imgs[divID].length>1){
		setOpacity(imgs[divID][current[divID]]);
		setOpacity(imgs[divID][nIndex]);
	}else{
		divID = CurrentSlideshow < NumSlideshows-1 ? ActiveSlideshows[CurrentSlideshow+1] : ActiveSlideshows[0];
		CurrentSlideshow = CurrentSlideshow  < NumSlideshows-1 ? CurrentSlideshow+1 : 0;
	}

//console.log("Opac: " + cOpacity);
	if(cOpacity<=0)
	{
		imgs[divID][current[divID]].style.display = 'none';

		current[divID] = nIndex;
//		console.log("Current Slide: " + CurrentSlideshow);

		divID = CurrentSlideshow < NumSlideshows-1 ? ActiveSlideshows[CurrentSlideshow+1] : ActiveSlideshows[0];
		//console.log("showing: "+divID);
		CurrentSlideshow = CurrentSlideshow  < NumSlideshows-1 ? CurrentSlideshow+1 : 0;
		setTimeout(so_xfade,3000);

	}
	else
	{
		setTimeout(so_xfade,50);
	}

	function setOpacity(obj)
	{
		if(obj.xOpacity>.99)
		{
			obj.xOpacity = .99;
			return;
		}

		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
	}
}
