// JavaScript Document

function pointerOn(name)
{
	//console.log(name);
	$('pointer').className = "pos_" + name;
}

function pointerOff()
{
	$('pointer').className = "no_pos";
}

function emptyme(e) {
	if(e.value == "Enter Email Address") {
		e.value = "";
	}
}

function goHome()
{	
	document.location = '/';
}


function subnavswap(e)
{
	var server = "http://mcdc.visioncreative.net"
	var path = server  + "/images/"; 
	var overstate = path + e.id + "_over.png";
	//alert(e.src);
	if(e.src == overstate){
		e.src = path + e.id + ".png";
	} else {
		e.src = overstate;
	}
	
}

mcdc = {
	aFeatures: null,
	curFeature: 0,
	maxFeature: 0,
	timer: null,
	
	init: function() {
		if($("flashMapContainer")) {
			return;	
		} else {
			if($("feature_container")) {
				mcdc.aFeatures = $("feature_container").getElements("div[class=feature_group]");
			} else {
				mcdc.aFeatures = $("inside_feature_container").getElements("div[class=inside_feature_group]");
			}
			mcdc.maxFeature = mcdc.aFeatures.length - 1;
	
			mcdc.aFeatures.each(function(feature,i) {
				feature.set("tween",{duration: 2000});
				if(i > 0) {
					feature.set("opacity",0);
					feature.setStyle("display","block");
				}
			});
			
			mcdc.timer = mcdc.fadeth.periodical(6000);


		}
	},

	fadeth: function() {
		if(mcdc.curFeature == mcdc.maxFeature) {
			mcdc.aFeatures.each(function(feature,i) {
				if(i == mcdc.maxFeature) {
					feature.fade("out");
				} else if(i > 0) {
					feature.set("opacity",0);
				}
			});	

			mcdc.curFeature = 0;
		} else {
			mcdc.curFeature++;
			mcdc.aFeatures[mcdc.curFeature].fade("in");
		}
	},
	
		resetRequired: function(sField) {
		$("title_" + sField).className = "fieldTitle";
	},
	
	checkFields: function() {
		missingFields = false;
		reqFields = new Array("firstname","lastname","address","city","state","zip","email","email_confirm");
		reqFields.each(function(fld) {
			if($(fld).value == "") {
				$("title_" + fld).className = "fieldTitleRequired";		
				missingFields = true;
			}
		});
		
		if(missingFields) {
			alert("You forgot some necessary information.  Please fill out the required fields to continue.")
			return;
		}
		
		if($("email").value != $("email_confirm").value) {
			$("title_email").className = "fieldTitleRequired";
			$("title_email_confirm").className = "fieldTitleRequired";
			
			alert("The email addresses you entered do not match.");
			return;
		}

		
		return true;
	},

	submitContact: function() {
		if(this.checkFields()) {
			$("contactForm").submit();
		}

	},
	
	regEvent: function() {
		if(this.checkFields()) {
			$("registrationForm").submit();
		}
	},
	
	cL: null,
	fC: null,
	pD: null,

	goCounty: function() {
		new Request({
			method: "post",
			url: "/countyFetcher.php",
			onSuccess: function(responseText) {
				$("countyListings").innerHTML = responseText;
				
				mcdc.fC.set("tween",{duration: 'short'});
				mcdc.fC.tween("height",mcdc.getFeatureHeight());		
			}
		}).send("county=" + $("countySelector").value);
	},

	goCountyMap: function(sCounty) {
		new Request({
			method: "post",
			url: "/countyFetcher.php",
			onSuccess: function(responseText) {
				$("countyListings").innerHTML = responseText;
				
				mcdc.fC.set("tween",{duration: 'short'});
				mcdc.fC.tween("height",mcdc.getFeatureHeight());		
			}
		}).send("county=" + sCounty);
	},
	
	getFeatureHeight: function() {
		listingSize = mcdc.cL.getScrollSize();
		
		var overflow = listingSize.y - 228;
		
		if(overflow > 0) {
			newFeatureSize = 340 + overflow;
			return newFeatureSize;
		} else {
//			if(mcdc.fC.getStyle("height").toInt() > 348) {
//				mcdc.fC.set("tween",{duration: 'short'});
				return 340;
//			}
		}
	},
	
	goProject: function(projID) {
		new Request({
			method: "post",
			url: "/projectFetcher.php",
			onSuccess: function(responseText) {
				mcdc.pD.innerHTML = responseText;
				var pdSize = mcdc.pD.getScrollSize();
				
				if(Browser.Engine.trident4) {
					$("countySelector").setStyle("display","none");
				}
				
				new Fx.Elements([mcdc.pD,mcdc.fC]).start({
					'0': {'opacity': 1},
					'1': {'height': pdSize.y}
				});	
			}
		}).send("projID=" + projID);
	},
	
	fadeProject: function() {
		new Fx.Elements([mcdc.pD,mcdc.fC],{
			onComplete: function() {
				 mcdc.pD.innerHTML = "";
			}
		}).start({
			'0': {'opacity': 0},
			'1': {'height': mcdc.getFeatureHeight()}
		});	
		
		if(Browser.Engine.trident4) {
			$("countySelector").setStyle("display","inline");
		}

	},
	
	searchProjects: function() {
		$("searchForm").set("send",{
			url: "/countyFetcher.php",
			methos: "post",
			onSuccess: function(responseText) {
				$("countyListings").innerHTML = responseText;

				if($('projectDetails').get('opacity') == 1) {
					mcdc.fadeProject();
				} else {
					mcdc.fC.tween("height",mcdc.getFeatureHeight());	
				}
			}			
		});
		$("searchForm").send();
	},
	
	swapPic: function(pic,w,h,x) {
		var elDiv = $("picContainer");
		var shadow = $("leShadow");
		var image = $("leImage");
		
		elDiv.set("tween",{
			duration: "short",
			onComplete: function() {
				elDiv.set("tween",{onComplete: function() {}});	

				//resize shadows for the image
				shadow.setStyles({
					"height": h,
					"width": w,
					"left": x
				});
				
				$("shadow2").setStyle("width",w - 54);
				$("shadow4").setStyle("height",h - 54);
				$("shadow6").setStyle("height",h - 54);
				$("shadow8").setStyle("width",w - 54);
				
				image.set("src","");
				image.setStyle("left",x + 15);
				
				image.onload = function() {
					elDiv.morph({"height": h + 5,"opacity":1});							 
				};

				image.set("src","/projectImages/" + pic);
			}
		});

//		elDiv.setStyle("height",h + 5)
		elDiv.fade("out");
	},
	
	printProject: function(projID) {
		projWin = window.open("printProject.php?projID=" + projID);
	},
	
	fetchArchive: function(url,el) {
		var elDiv = $("archives");
		
		elDiv.set("tween",{
			duration: "short",
			onComplete: function() {
				new Request({
					method: "post",
					url: url,
					data: {archiveYear: el.value },
					onSuccess: function(responseText) {
						elDiv.set("tween",{onComplete: function() {}});
						elDiv.setStyle("height",elDiv.getScrollSize().y);
						
						nuDiv = new Element("div");
						nuDiv.innerHTML = responseText;
						
						elDiv.innerHTML = "";
						nuDiv.inject(elDiv);
						
						var iHeight = nuDiv.getScrollSize().y;

						elDiv.morph({"opacity": 1, "height": iHeight});
					}
				}).send();
			}
		});

		elDiv.fade("out");
	}

}


locMap = {
	init: function () {
		if($("LocationsMap")) locMap.addEvents();
	},
	addEvents: function () {
		var areas = $("LocationsMap").getElements("area");
		areas.each(function(item,index) {
			item.addEvents({
				mouseenter: function(){
					locMap.highlight(item.id);
				},
				mouseleave: function(){
					locMap.lowlight(item.id);
				},
				click: function (){
					
				} 
			});
		});
	},
	highlight: function (e) {
		switch(e){
			
			case 'map_lan':
				var myFx = new Fx.Morph('off_lan').start('.office_highlight');
			break;
			case 'map_cad':
				var myFx = new Fx.Morph('off_cad').start('.office_highlight');
			break;
			case 'map_aa':
				var myFx = new Fx.Morph('off_aa').start('.office_highlight');
			break
			case 'map_gr':
				var myFx = new Fx.Morph('off_gr').start('.office_highlight');
			break;
			case 'map_clem':
				var myFx = new Fx.Morph('off_clem').start('.office_highlight');
			break;
		}
	},
	lowlight: function (e) {
		switch(e){
			case 'map_lan':
				var myFx = new Fx.Morph('off_lan').start('.office_location');
			break;
			case 'map_cad':
				var myFx = new Fx.Morph('off_cad').start('.office_location');
			break;
			case 'map_aa':
				var myFx = new Fx.Morph('off_aa').start('.office_location');
			break;
			case 'map_gr':
				var myFx = new Fx.Morph('off_gr').start('.office_location');
			break;
			case 'map_clem':
				var myFx = new Fx.Morph('off_clem').start('.office_location');
			break;
		}
	}
}


bios = {
	init: function () {
		if($('bioLinks')) bios.addEvents();
	},
	addEvents: function () {
		var links = $('bioLinks').getElements('li');
		links.each(function(item, index) {
				item.addEvents({
					click: function () {
						var a = $('left').getElement('a[name='+item.id+']');
						var scroll = new Fx.Scroll(window).toElement(a);
					}
					});
				});
	}
}


test = {
	init: function () {
		if($('borrow')) test.addEvents();
	},
	addEvents: function () {
		$('borrow').addEvent('click', function(){
			$('borrowContent').setStyles({display:'block'});
			$('lendingContent').setStyles({display:'none'});
			$('borrow').addClass('borrow-on');
			$('lending').removeClass('lending-on');
		});
		$('lending').addEvent('click', function(){
			$('lendingContent').setStyles({display:'block'});
			$('borrowContent').setStyles({display:'none'});
			$('lending').addClass('lending-on');
			$('borrow').removeClass('borrow-on');
		});
	}
}




window.addEvent('domready', bios.init);
window.addEvent('domready', test.init);


window.addEvent('domready', mcdc.init);
window.addEvent('domready', locMap.init);