var defderf = {
	totalSites: 7,
	currentSite: 1,
	refreshRate: 4000,
	
	init: function (){
		defderf.insertControls();
		if($('portfolioItem')){
			defderf.startRefresher();
		}
		if($('pauseplayLink') && $('previousLink') && $('nextLink')){
			Event.observe($('pauseplayLink'), 'click', function(e){defderf.pausePlay($('pauseplayLink'));Event.stop(e);}, false);
			Event.observe($('previousLink'), 'click', function(e){
				defderf.portfolioRefresher(defderf.currentSite-1);
				if(intervalID){defderf.restartRefresher();}// restart refresher if it's not paused
				Event.stop(e);}, false);
			Event.observe($('nextLink'), 'click', function(e){
				defderf.portfolioRefresher(defderf.currentSite+1);
				if(intervalID){defderf.restartRefresher();}//restart refresher if it's not paused
				Event.stop(e);}, false);
		}
		defderf.attachExternalLinks();
		if($('showWebsites') && $('showLogos')){
			Event.observe($('showWebsites'), 'click', function(e){defderf.swapPortfolioView('sites');Event.stop(e);}, false);
			Event.observe($('showLogos'), 'click', function(e){defderf.swapPortfolioView('logos');Event.stop(e);}, false);
		}
	},
	insertControls: function(){
		if($('portfolioItem')){
			controls = '<ul id="scrollercontrols">\n';
			controls += '<li><a href="#" id="previousLink">&lt; previous</a></li>\n';
			controls += '<li><a href="#" id="pauseplayLink">pause</a></li>\n';
			controls += '<li><a href="#" id="nextLink">next &gt;</a></li>\n';
			controls += '</ul>\n';
			
			new Insertion.Before($('portfolioItem'),controls);
		}
	},
	startRefresher: function(){
		intervalID = setInterval(function(){defderf.portfolioRefresher(defderf.currentSite+1)},defderf.refreshRate);
	},
	clearRefresher: function(){
		if(intervalID){
			clearInterval(intervalID);
			intervalID = null;
		}
	},
	restartRefresher: function(){
		defderf.clearRefresher();
		defderf.startRefresher();
	},
	portfolioRefresher: function(cSite){
		if(cSite > 0 && cSite <= defderf.totalSites){
			defderf.currentSite=cSite;
		}
		else if (cSite < 1){
			defderf.currentSite = defderf.totalSites;
		}
		else {
			defderf.currentSite = 1;
		}
		new Ajax.Request(
			'includes/portfolioUpdater.php',
			{
				method: 'post', 
				parameters: "site="+defderf.currentSite, 
				onComplete: defderf.portfolioResponse
			});

	},
	portfolioResponse: function(resp){
		if($('portfolioItem')){
			$('portfolioItem').innerHTML = resp.responseText;
			defderf.attachExternalLinks();
		}
	},
	pausePlay: function(el){
		//alert(intervalID);
		if(intervalID){
			el.innerHTML = "resume";
			defderf.clearRefresher();
		}
		else {
			el.innerHTML = "pause";
			defderf.startRefresher();	
		}
	},
	// attach new window events to all "externalSite" links
	attachExternalLinks: function(){
		exsites = document.getElementsByClassName('externalSite',$('portfolioItem'));
		if(exsites.length>0){
			exsites.each(function(alink){
				Event.observe(alink, 'click', function(e){defderf.popWin(alink);Event.stop(e);}, false);
			});
		}
	},
	swapPortfolioView: function(view){
		if($('showWebsites') && $('showLogos') && $('portfoliosites') && $('portfoliologos')){
			if(view == "sites"){
				$('showWebsites').style.fontWeight = "bold";
				$('showLogos').style.fontWeight = "normal";
				$('portfoliosites').style.display = "block";
				$('portfoliologos').style.display = "none";
			}
			else if (view == "logos"){
				$('showLogos').style.fontWeight = "bold";
				$('showWebsites').style.fontWeight = "normal";
				$('portfoliologos').style.display = "block";
				$('portfoliosites').style.display = "none";
			}
		}
	},
	popWin: function(el){
		window.open(el.href);
	}
}

var contactForm = {
	emailRegex:/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/,
	phoneRegex:/^\(?([2-9])(\d{2})\)?[- .]?(\d{3})[- .]?(\d{4})$/,
	
	init: function(){
		if($('submitbtn')){
			Event.observe($('submitbtn'), 'click', function(e){contactForm.validateForm(e,$('contactform'));Event.stop(e);}, false);
		}
	},
	validateForm: function(ev,formid){
		isValidForm = "";
		reqFieldLIs = document.getElementsByClassName('required',formid);
		reqFieldLIs.each(function(li){
			// need to find the form element in each required list item
			formel = contactForm.getFormElement(li);
			
			isValidField = contactForm.validateField(formel);
			//alert(isValidField);
			if(!isValidField){
				Element.addClassName(li,'invalid');
				isValidForm += "invalid";
			}
			else {
				Element.removeClassName(li,'invalid');
			}
		});
		if(isValidForm.length == ""){
			contactForm.submitForm(formid);
		}
	},
	validateField: function(fieldid){
		isValid = true;
		tag = fieldid.tagName.toLowerCase();
		name = fieldid.name.toLowerCase();
		
		if((name == "name" || name == "question") && $F(fieldid)==""){
			isValid = false;
		}
		else if(name == "email" && !contactForm.emailRegex.test($F(fieldid))){
			isValid = false;
		}
		
		return isValid;
	},
	getFormElement: function(el){
		children = el.childNodes;
		$A(children).each(function(child){
			tn = child.tagName.toLowerCase();
			if(tn == "input" || tn == "textarea"){
				formChild = child;
			}
		});
		return formChild;
	},
	submitForm: function(formid){
		if($('confirmation')){
			confEl = $('confirmation');
			confEl.parentNode.removeChild(confEl);
		}
		params = Form.serialize(formid);
		Form.disable(formid);
		new Ajax.Request(
			'includes/mailer.php',
			{
				method: 'post', 
				parameters: params, 
				onComplete: function(resp){contactForm.contactResponse(resp,formid);},
				onFailure: function(){contactform.contactFailure(formid);},
				onLoading: function(){contactForm.contactLoading(formid);}
			});
	},
	contactResponse: function(resp,formid){
		Form.enable(formid);
		new Insertion.Top(formid,resp.responseText);
		if($('loading')){
			loadingEl = $('loading');
			loadingEl.parentNode.removeChild(loadingEl);
		}
	},
	contactFailure: function(formid){
		Form.enable(formid);
		failureHTML = "<p id=\"confirmation\" class=\"alert\">";
		failureHTML += "There was error processing your request. Please try again at a later time. You can also contact me by phone at 215-740-3163.";
		failureHTML += "</p>";
		new Insertion.Top(formid,failureHTML);
		if($('loading')){
			loadingEl = $('loading');
			loadingEl.parentNode.removeChild(loadingEl);
		}
		
	},
	contactLoading: function(formid){
		loadinghtml = '<div id="loading"><img src="images/loading-icon.gif" width="16" height="16" alt="loading" /> LOADING</div>';
		new Insertion.Top(formid,loadinghtml);
	}
}

function pageLoader() {
	defderf.init();
	contactForm.init();
}
Event.observe(window, 'load', pageLoader, false);
