// tmLib 1.0.0--theMechanism's general code library
// Copyright 2008 Jeffrey Barke (themechanism.com)
// Dual licensed under the MIT (MIT-LICENSE.txt)
//	and GPL (GPL-LICENSE.txt) licenses.

// Dependencies:
	// Prototype 1.6.0
	// Scriptaculous 1.8.0: Builder

tmLib={lnkExternalLaunch:function(strHref){objNewWin=window.open(strHref,'newwin');objNewWin.focus();},lnkExternalPrep:function(){if(!document.getElementsByTagName){return false;}
$$('a').each(function(obj){if(obj.href&&obj.getAttribute('rel')=='external'){obj.onclick=function(){tmLib.lnkExternalLaunch(obj.href);return false;}}});},lnkJumpNav:function(strHref){window.location.href=strHref;},frmAddHelpers:function(){$$('.input').each(function(obj){Event.observe(obj,'focus',tmLib._frmFocus,false);Event.observe(obj,'blur',tmLib._frmBlur,false);});},checkForm:function(objForm){var blnError=false;var objFirst=null;var arrLabels=objForm.getElementsByTagName('label');for(var i=0;i<arrLabels.length;i++){if(Element.hasClassName(arrLabels[i],'required')){if(arrLabels[i].htmlFor){var objInput=$(arrLabels[i].htmlFor);}else{var objInput=Element.down(arrLabels[i],'input');}
var strErrorText=null;if(objInput!=null){strErrorText=tmLib.frmValidate(objInput,arrLabels[i]);if(strErrorText!=null){if(objFirst==null){objFirst=objInput;}
blnError=true;tmLib._writeCorrection(arrLabels[i],strErrorText);tmLib._addLiveCorrect(objInput);}else{tmLib._writeCorrection(arrLabels[i]);}}}}
if(blnError){objFirst.focus();return false;}else{return true;}},checkValidity:function(e){var objInput=$(Event.element(e));var strErrorText=null;var arrLabels=document.getElementsByTagName('label');var intLen=arrLabels.length;for(var i=0;i<intLen;i++){if(arrLabels[i].htmlFor==objInput.id){var objLabel=$(arrLabels[i]);break;}else if(arrLabels[i]==objInput.parentNode){var objLabel=$(arrLabels[i]);}}
strErrorText=tmLib.frmValidate(this,objLabel);if(strErrorText!=null){tmLib._writeCorrection(objLabel,strErrorText);}else{tmLib._writeCorrection(objLabel);}},frmValidate:function(objInput,objLabel){if(objInput.value.strip()==''){return'Please enter your '+objLabel.innerHTML.toLowerCase().replace(/:/,'').replace(/\*/,'').stripTags()+'.';}else if(Element.hasClassName(objLabel,'requiredEmail')&&!tmLib.validEmail(objInput.value)){return'Please supply a valid email address';}},validEmail:function(strEmail){return strEmail.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/);},_frmBlur:function(e){Event.element(e).removeClassName('textFocus');},_frmFocus:function(e){obj=$(Event.element(e));if(obj){obj.addClassName('textFocus');if(obj.nodeName=='TEXTAREA'||obj.type=='text'){obj.select();}}},_addLiveCorrect:function(objInput){if(objInput.nodeName.toLowerCase()=='input'&&(objInput.getAttribute('type')=='text')){Event.observe(objInput,'keyup',tmLib.checkValidity,false);}else if(objInput.nodeName.toLowerCase()=='textarea'){Event.observe(objInput,'keyup',tmLib.checkValidity,false);}else if(objInput.nodeName.toLowerCase()=='select'){Event.observe(objInput,'change',tmLib.checkValidity,false);}else if(objInput.nodeName.toLowerCase()=='input'&&(objInput.getAttribute('type')=='file')){Event.observe(objInput,'change',tmLib.checkValidity,false);}},_removeError:function(objLabel){if(objLabel.htmlFor){var objInput=$(objLabel.htmlFor);}else{var objInput=Element.down(objLabel,'input');}
var objPar=$(objInput.parentNode);var arrErr=objPar.select('.msgError');if(arrErr.length>0){objPar.removeChild(arrErr[0]);};},_writeCorrection:function(objLabel,strText,blnCorrect){if(objLabel.htmlFor){var objInput=$(objLabel.htmlFor);}else{var objInput=Element.down(objLabel,'input');}
var objImage=objLabel.parentNode.getElementsByTagName('img')[0];if(!strText){if(objImage!=null){}
Element.removeClassName(objInput,'correctionText');tmLib._removeError(objLabel);}else{if(objImage==null){var objImg=Builder.node('img',{className:'correctionIcon'});}
tmLib._removeError(objLabel);var objP=Builder.node('em',{className:'msgError'},strText);objInput.parentNode.appendChild(objP);Element.addClassName(objInput,'correctionText');}
return true;}}
function tmLibCleanUp(){tmLib=null;}
Event.observe(window,'load',tmLib.lnkExternalPrep.bindAsEventListener(tmLib),false);Event.observe(window,'load',tmLib.frmAddHelpers.bindAsEventListener(tmLib),false);Event.observe(window,'unload',tmLibCleanUp,false);
/**
 * sl green client code
 *
 */

slgreen={init:function(){if($('btnLogout')){$('btnLogout').onclick=function(){new Ajax.Request('/logout.php',{method:'post',onSuccess:function(data){Effect.Fade('btnMyAccount',{duration:0.3,afterFinish:function(){Effect.Appear('btnCreateProfile',{duration:0.3});}});Effect.Fade('btnLogout',{duration:0.3,afterFinish:function(){Effect.Appear('btnLogin',{duration:0.3});}});},onFailure:function(){alert('Something went wrong...');}});return false;}}
if($('btnLogin')){$('btnLogin').onclick=function(){tmModalWindows.showOverlay('/templates/slgreen/includes/login.php');return false;}}
if($('mapFrame')){$('mapFrame').style.display='block';}
if($('btnFavorites')){var btn=$('btnFavorites');if(btn.hasClassName('favorite')){btn.onclick=function(){slgreen.addToFavorite(window.location.href,document.getElementsByTagName('title')[0].text);return false;}}
if(btn.hasClassName('delete')){btn.onclick=function(){slgreen.deleteFavorite(window.location.href);return false;}}}
if($('btnSocial')){$('btnSocial').onclick=function(){tmModalWindows.showOverlay('/templates/slgreen/includes/socialBookmark.php',{url:encodeURI(window.location.href),title:encodeURI(document.getElementsByTagName('title')[0].text)});return false;}}
if($('btnAlerts')){$('btnAlerts').onclick=function(){slgreen.addToPortfolio();return false;}}
this.initSlideshow();this.loadSlideshow2();},addToFavorite:function(strUrl,strTitle){var strParams='action=add&url='+strUrl+'&title='+encodeURI(strTitle);new Ajax.Request('/json/favorites.php',{method:'post',parameters:strParams,onSuccess:slgreen._addToFavorite,onFailure:slgreen.addToFavoriteFail});return false;},deleteFavorite:function(strUrl,strTitle){var strParams='action=delete&url='+strUrl;new Ajax.Request('/json/favorites.php',{method:'post',parameters:strParams,onSuccess:slgreen._deleteFavorite,onFailure:slgreen.addToFavoriteFail});return false;},addToPortfolio:function(){var strParams='action=add';new Ajax.Request('/json/portfolio.php',{method:'post',parameters:strParams,onSuccess:slgreen._addToPortfolio});return false;},handleLogin:function(objForm){this.removeErrors(objForm);var blnErr=false;var arrFields=new Array();arrFields.push($('txtUsername'));arrFields.push($('txtPassword'));if(arrFields[0].value==''){blnErr=true;this.writeError(arrFields[0],'Please enter your username.');}
if(arrFields[1].value==''){blnErr=true;this.writeError(arrFields[1],'Please enter your password.');}
if(blnErr==false){new Ajax.Request('/login.php',{method:'post',parameters:objForm.serialize(true),onSuccess:slgreen.handleResponse,onFailure:function(){alert('Something went wrong...');}});}
return false;},slideshowId:1,slideshowImage:function(obj){$$('.slideshow-photos li').each(function(obj){obj.removeClassName('selected');});obj.addClassName('selected');var src=obj.firstDescendant().href;$('slideshow-canvas').style.background='url('+src+') top center no-repeat';},initSlideshow:function(){if($('slideshow')){var li=$$('.slideshow-photos')[0].firstDescendant();slgreen.slideshowImage(li);$$('.slideshow-photos li a').each(function(obj){var img=new Image();img.src=obj.href;slgreen.slideshowPreload.push(img);});$$('.slideshow-photos li').each(function(obj){obj.onclick=function(){return false;}
obj.onmouseover=function(){slgreen.slideshowImage(this);$('caption').innerHTML=this.firstDescendant().firstDescendant().title;return false;}});var ssB=$('slideshow-back');var ssF=$('slideshow-forward');Event.observe(ssB,'mouseover',function(){this.style.background='url(/templates/slgreen/images/icons/ss-arrow-left.gif) center left no-repeat';},false);Event.observe(ssF,'mouseover',function(){this.style.background='url(/templates/slgreen/images/icons/ss-arrow-right.gif) center right no-repeat';},false);Event.observe(ssB,'mouseout',function(){this.style.background='none';},false);Event.observe(ssF,'mouseout',function(){this.style.background='none';},false);ssB.onclick=function(){var li=$$('.slideshow-photos li.selected')[0];if(li.previous()){var nli=li.previous();}else{var nli=li.parentNode.select('li');nli=nli[nli.length-1]}
slgreen.slideshowImage(nli);return false;}
ssF.onclick=function(){var li=$$('.slideshow-photos li.selected')[0];if(li.next()){var nli=li.next();}else{var nli=li.parentNode.firstDescendant();}
slgreen.slideshowImage(nli);return false;}}},slideshowTimer:false,slideshowPreload:new Array(),slideshowNum:0,slideshowLoad:0,slideshowDelay:3000,slideshowFade:0.3,slideshowImage2:function(obj){$$('#canvas div').each(function(obj){new Effect.Fade(obj,{duration:0.5,afterFinish:function(obj){obj.element.parentNode.removeChild(obj.element);obj.element=null;}});});$$('#ss-carousel .carousel ul li a').each(function(obj){obj.removeClassName('selected');});obj.addClassName('selected');var src=obj.href;var img=Builder.node('div',{style:'display:none;background-image: url('+src+')'});$('canvas').appendChild(img);new Effect.Appear(img,{duration:0.5});$('caption').innerHTML=obj.firstDescendant().alt;},slideshowPlay2:function(){var a=$$('#ss-carousel .carousel ul li a.selected')[0];if($(a.parentNode).next()){var obj=$(a.parentNode).next().firstDescendant();}else{var obj=$(a.parentNode.parentNode);obj=obj.firstDescendant();obj=obj.firstDescendant();}
slgreen.slideshowImage2(obj);slgreen.slideshowTimer=setTimeout(slgreen.slideshowPlay2,slgreen.slideshowDelay);},pauseSlideshow2:function(){clearTimeout(slgreen.slideshowTimer);slgreen.slideshowTimer=false;},initSlideshow2:function(){if($('slideshow2')){var a=$$('#ss-carousel .carousel ul li a')[0];slgreen.slideshowImage2(a);$$('#ss-carousel .carousel ul li a').each(function(obj){obj.onclick=function(){slgreen.slideshowImage2(this);slgreen.pauseSlideshow2();return false;}});$('ss-pause').onclick=function(){if(slgreen.slideshowTimer!=false){slgreen.pauseSlideshow2();}
return false;}
$('ss-play').onclick=function(){if(slgreen.slideshowTimer==false){slgreen.slideshowPlay2();}
return false;}
$('ss-back').onclick=function(){var li=$$('#ss-carousel .carousel ul li a.selected')[0];li=li.parentNode;if(li.previous()){var nli=li.previous();}else{var nli=li.parentNode.select('li');nli=nli[nli.length-1]}
slgreen.slideshowImage2(nli.firstDescendant());return false;}
$('ss-next').onclick=function(){var li=$$('#ss-carousel .carousel ul li a.selected')[0];li=li.parentNode;if(li.next()){var nli=li.next();}else{var nli=$(li.parentNode).firstDescendant();}
slgreen.slideshowImage2(nli.firstDescendant());return false;}
setTimeout(slgreen.slideshowPlay2,slgreen.slideshowDelay);$$('#ss-carousel a').each(function(obj){if(obj.hasClassName('carousel-back')){obj.setOpacity(0.3);obj.onclick=function(){var c=$$('#ss-carousel .carousel ul')[0];var total=($$('#ss-carousel .carousel ul li').length)*55;var move=330;var offset=parseInt(c.style.left);if(!offset){offset=0;}
if((Math.abs(offset)-move)<0){offset=Math.abs(offset);}else{offset=offset+move;}
if(!c.style.left){var curr=0;}else{var curr=parseInt(c.style.left);}
new Effect.Move(c,{x:offset,y:0,duration:0.5,afterFinish:function(obj){if(parseInt(obj.element.style.left)==0){$$('#ss-carousel a.carousel-back')[0].setOpacity(0.3);$$('#ss-carousel a.carousel-forward')[0].setOpacity(1.0);}}});return false;}}
if(obj.hasClassName('carousel-forward')){obj.onclick=function(){var c=$$('#ss-carousel .carousel ul')[0];var total=($$('#ss-carousel .carousel ul li').length)*55;var move=330;var offset=parseInt(c.style.left);if(!offset){offset=0;}
if(Math.abs((offset-(move+move)))>total){offset=-1*(total-move-5);}else{offset=offset-move;}
if(!c.style.left){var curr=0;}else{var curr=parseInt(c.style.left);}
if(Math.abs(move-curr+5)<total){new Effect.Move(c,{x:offset,y:0,duration:0.5,afterFinish:function(obj){if(Math.abs(move-parseInt(obj.element.style.left)+5)>=total){$$('#ss-carousel a.carousel-back')[0].setOpacity(1.0);$$('#ss-carousel a.carousel-forward')[0].setOpacity(0.3);}}});}
return false;}}});}},launchSlideshow2:function(){slgreen.slideshowLoad++;if(slgreen.slideshowLoad==slgreen.slideshowNum){slgreen.initSlideshow2();}},loadSlideshow2:function(){$$('#ss-carousel .carousel ul li a').each(function(obj){var img=new Image();img.src=obj.href;img.onload=slgreen.launchSlideshow2;slgreen.slideshowPreload.push(img);slgreen.slideshowNum++;});},_addToFavorite:function(data){var objData=data.responseText.evalJSON(true);if(objData.result){var objBtn=$('btnFavorites');objBtn.style.backgroundImage='url(/templates/slgreen/images/buttons/checkBox.png)';objBtn.title='This page has been marked as a favorite. Click again to delete from favorites.';objBtn.onclick=function(){slgreen.deleteFavorite(window.location.href);return false;}}else{if(objData.loggedin==false){tmModalWindows.showOverlay('/templates/slgreen/includes/notallowed.php');}}},addToFavoriteFail:function(){$('btnFavorites').style.backgroundImage='url(/templates/slgreen/images/buttons/favorite.png)';},_deleteFavorite:function(data){var objData=data.responseText.evalJSON(true);if(objData.result){var objBtn=$('btnFavorites');objBtn.style.backgroundImage='url(/templates/slgreen/images/buttons/favorite.png)';objBtn.title='Add to favorites';objBtn.onclick=function(){slgreen.addToFavorite(window.location.href,document.getElementsByTagName('title')[0].text);return false;}}},_addToPortfolio:function(data){var objData=data.responseText.evalJSON(true);if(objData.result){tmModalWindows.showOverlay('/templates/slgreen/includes/portfolio.php?message='+objData.message);}else{if(objData.loggedin==false){tmModalWindows.showOverlay('/templates/slgreen/includes/notallowed.php');}}},handleResponse:function(data){var objData=data.responseText.evalJSON(true);if(objData.result){if(objData.login){Effect.Fade('btnCreateProfile',{duration:0.3,afterFinish:function(){Effect.Appear('btnMyAccount',{duration:0.3});}});Effect.Fade('btnLogin',{duration:0.3,afterFinish:function(){Effect.Appear('btnLogout',{duration:0.3});}});tmModalWindows.hideModal()}}else{if(objData.errMsg){var objForm=$(objData.form);var err=Builder.node('em',{className:'msgError'},objData.errMsg);objForm.insertBefore(err,objForm.firstChild)}
if(objData.errorFields&&objData.errorMsgs){var len=objData.errorFields.length;for(var i=0;i<len;i++){slgreen.writeError($(objData.errorFields[i]),objData.errorMsgs[i]);}}}},removeErrors:function(objForm){objForm.select('.msgError').each(function(obj){obj.parentNode.removeChild(obj);});},writeError:function(obj,msg){var err=Builder.node('em',{className:'msgError'},msg);obj.parentNode.appendChild(err);}}
function slgreenCleanUp(){slgreen=null;}
Event.observe(window,'load',slgreen.init.bindAsEventListener(slgreen),false);Event.observe(window,'unload',slgreenCleanUp,false);