window.addEvent('domready', function() {
    if ($('colormenu')) colorslide();
	if ($('collapse')) collapse();
	if ($('portfoliomain')) rolloverThumbs();
	if ($('mbBottomContainer')) lightbox();
	if ($('projectslideshow')) slideshow();
	if ($('biothumbs')) bioRolloverThumbs();
	if ($('emailsignup')) emailsignup();
	if ($('dropdown')) dropdown();
	if ($('foodmap')) foodmaptips();
	if ($('headlines_content')) fade_headlines_caller();
});

function fade_headlines_caller() {
	$$('.fader').each(function(el,index) {
		el.set('opacity',0);
	});
	$$('.fader')[0].set('opacity',1);
	if ($$('.fader').length > 1) setInterval('fade_headlines()',5000);
}

function fade_headlines() {
	var i = 0;
	$$('.fader').each(function(el,index) {
		if (el.hasClass('show')) {
			el.toggleClass('hide');
			el.toggleClass('show');
			el.fade('out');
			i = index.toInt();
		} else {
			//
		}
	});

	if (i < ($$('.fader').length-1)) {
		i = i+1;
	} else {
		i = 0;
	}
	$$('.fader')[i].toggleClass('hide');
	$$('.fader')[i].toggleClass('show');
	$$('.fader')[i].fade('in');
}

function foodmaptips() {
	var tooltip = new Tips('.tooltip',{
		className: 'tooltips',
		fixed: false
	});
	
	var tooltip = new Tips('.bottomtooltip',{
		className: 'bottomtooltips',
		fixed: false
	});
	
	var tooltip = new Tips('.toptooltip',{
		className: 'toptooltips',
		fixed: false
	});
}

function dropdown() {
	var d = $('dropdown');
	var li = d.getFirst('li');
	var ul = $('options');

	li.onmouseover = function() { ul.setStyle('display','block'); }
	li.onmouseout = function() { ul.setStyle('display','none'); }
}


function emailsignup() {
	var submit = $('emailsubmit');
	var emailinput = $('email');
	emailinput.onclick = function() {
		this.set('value','');
	}
	
	submit.onclick = function() {		
		var email = $('email').value;
		var emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (email == '' || email == ' ' || !emailExp.test(email)) {
			emailinput.set('value','Invalid Email Address');
		} else {
			var req = new Request({
				method: 'post',
				url: '/library/ajax/emailsignup.php',
				data: { 'email' : email },
				onRequest: function() {
					//alert("getting");
				},
				onComplete: function(response) {
					var form = $('emailsignup');
					form.set('html','<img src="/library/images/text-thankyou.gif" alt="Thank you." class="thankyou" />');
				}
			}).send();
		}
		
		return false;
	}
}


function changepage(page) {
	var myindex  = page.selectedIndex;
    var ddvalue = page.options[myindex].value;
    var baseURL  = "/portfolio/"+ddvalue;
    top.location.href = baseURL;
    return true;
}

function slideshow() {
	var tooltip = new Tips('.tooltip',{
		className: 'tooltips',
		fixed: false
	});
	
	var next = $('buttonnext');
	var prev = $('buttonprev');
	var pslider = $('projectslideshow');
	var dnext = $('nextdisable');
	var dprev = $('prevdisable');
	var thumbs = $('thumbnails');
	
	prev.set('opacity',.5);
	dprev.setStyle('display','block');
	dnext.setStyle('display','none');
	
	var liw = 462;
	var total = pslider.getChildren('li').length;
	var counter = 1;
	
	pslider.setStyle('width',liw*total);
	nextprev(counter,total,prev,next,dprev,dnext);
	
	// reset thumbnails
	var thumbdiv = $('thumbnails');
	var links = thumbdiv.getElements('a');
	var currentclass = pslider.get('class');
	links.each(function(el,index) {
		var myclass = el.getParent('li').get('class');
		if (myclass != currentclass) {
			el.set('opacity',0);
		} else {
			el.setStyle('display','block');
			nextprev(counter,total,prev,next,dprev,dnext);
		}
	});
	
}

function nextprev(counter,total,prev,next,dprev,dnext) {
	
	var pslider = $('projectslideshow');
	var currentclass = pslider.get('class');
	var thumbdiv = $('thumbnails');
	var lis = thumbdiv.getChildren('li');
	var links = thumbdiv.getElements('a');
	var fx = new Fx.Tween(pslider);
	var newpos = -(462*counter)+462;
	fx.start('margin-left',newpos);
		
	if (counter >= total) {
		prev.set('opacity',1);
		next.set('opacity',.5);
		dprev.setStyle('display','none');
		dnext.setStyle('display','block');
	} else if (counter < total && counter > 1) {
		prev.set('opacity',1);
		next.set('opacity',1);
		dprev.setStyle('display','none');
		dnext.setStyle('display','none');
	} else if (counter <= 1) {
		prev.set('opacity',.5);
		next.set('opacity',1);
		dprev.setStyle('display','block');
		dnext.setStyle('display','none');
	}
	
	next.onclick = function() {
		counter++;
		nextprev(counter,total,prev,next,dprev,dnext);
		return false;
	}
	prev.onclick = function() {
		counter--;
		nextprev(counter,total,prev,next,dprev,dnext);
		return false;
	}
	
	links.each(function(el,index) {
		var pclass = el.getParent('li').get('class');
		if (pclass == currentclass) {
		var myid = el.get('id');
		var green = el.getPrevious('div.green');
		if (myid == counter) {				
			green.setStyles({
				'border':'1px solid #00A94F',
				'height':28,
				'width':28,
				'background':'transparent none',
				'display':'block'
			});
			green.set('opacity',1);
		} else {
			el.onclick = function() {
				nextprev(myid,total,prev,next,dprev,dnext);
				green.setStyles({
					'border':'1px solid #00A94F',
					'height':28,
					'width':28,
					'background':'transparent none',
					'display':'block'
				});
				green.set('opacity',1);
				return false;
			}
			green.setStyle('display','none');
			green.set('opacity',0);
		}
		}
	});
	
}



function lightbox() {
	var tooltip = new Tips('.ptooltip',{
		className: 'ptooltips',
		fixed: false
	});
}


function bioRolloverThumbs() {
	var tooltip = new Tips('.tooltip',{
		className: 'tooltips',
		fixed: false
	});
	
	
	var thumbdiv = $('biothumbs');
	var links = thumbdiv.getElements('a');
	links.each(function(el,index) {
		el.onmouseover = function() {			
			links.each(function(el,index) {
				var purple = el.getPrevious('div.purple');
				purple.set('opacity',.8);
				purple.setStyle('display','block');
			});
			var mypurple = this.getPrevious('div.purple');
			mypurple.setStyle('display','none');
		}
		el.onmouseout = function() {
			links.each(function(el,index) {
				var purple = el.getPrevious('div.purple');
				purple.setStyle('display','none');
			});
		}
		el.onclick = function() {
			var myid = el.get('id');
			
			if (myid == null) {
				return false;
			}
		}
	});
}


function rolloverThumbs() {
	var tooltip = new Tips('.tooltip',{
		className: 'tooltips',
		fixed: false
	});
	
	
	var thumbdiv = $('thumbnails');
	var links = thumbdiv.getElements('a');
	links.each(function(el,index) {
		el.setStyle('display','block');
		el.onmouseover = function() {
			var myclass = el.getParent('li').get('class');
			
			// for all the li's, display green div, unless the class matches mine
			links.each(function(el,index) {
				var liclass = el.getParent('li').get('class');
				var green = el.getPrevious('div.green');
				if (liclass != myclass) {
					green.set('opacity',.8);
					green.setStyle('display','block');
				}
			});
		}
		el.onmouseout = function() {
			links.each(function(el,index) {
				var green = el.getPrevious('div.green');
				green.setStyle('display','none');
			});
		}
	});
}

function collapse() {
	
	/*
	var ul = $('collapse');
	var lis = ul.getChildren('li');
	lis.each(function(el,index) {
		var button = el.getFirst('a.button');
		var div = el.getFirst('div');
		var divh = el.getSize().y;
		var open = false;
		div.setStyles({
			overflow: 'hidden',
			height: 0
		});
		
		button.onclick = function() {
			var fx = new Fx.Tween(div,{duration:250});
			if (open == false) {
				this.setStyle('background-position','0px -30px');
				fx.start('height',divh);
				open = true;
			} else {
				this.setStyle('background-position','0px 0px');
				fx.start('height',0);
				open = false;
			}
			return false;
		}	
	});
		*/
	var ul = $('collapse');
	var lis = ul.getChildren('li');
	lis.each(function(el,index) {
		var button = el.getFirst('a.button');
		var div = el.getFirst('div');
		
		if (button.hasClass('open')) {
			var open = true;
			div.setStyle('display','block');
		} else {
			var open = false;
			div.setStyle('display','none');
		}
		
		button.onclick = function() {
			var fx = new Fx.Tween(div,{duration:250});
			if (open == false) {
				this.setStyle('background-position','0px -30px');
				div.setStyle('display','block');
				open = true;
			} else {
				this.setStyle('background-position','0px 0px');
				div.setStyle('display','none');
				open = false;
			}
			return false;
		}	
	});


}

function colorslide() {
	var colormenu = $('colormenu');
	var links = colormenu.getChildren('a');
	links.each(function(el,index) {
		var fx = new Fx.Tween(el,{duration:150});		
		el.onmouseover = function() {
			fx.start('margin-top',0);
		}
		
		el.onmouseout = function() {
			fx.start('margin-top',-10);
		}
	});
}



/*
* * * * * * * * * * * * * * * * * * * * *
IE :hover Fix --- taken from A List Apart
* * * * * * * * * * * * * * * * * * * * *
*/
sfHover = function() {
	if ($('menu')) {
		var sfEls = document.getElementById("menu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
