/*
 * CONTROLLER - Javascript controller for the site.
 */

var Condo = function( $ ) {
    /* Private members */
    
    /* Priviledged members */
    
    /* Private methods */
    
    // iOS orientation bug fix
    function _fix() {
        var addEvent = 'addEventListener',
    	    type = 'gesturestart',
    	    qsa = 'querySelectorAll',
    	    scales = [1, 1],
    	    meta = qsa in document ? document[qsa]('meta[name=viewport]') : [];

    	function fix() {
    		meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
    		document.removeEventListener(type, fix, true);
    	}

    	if ((meta = meta[meta.length - 1]) && addEvent in document) {
    		fix();
    		scales = [1, 2];
    		document[addEvent](type, fix, true);
    	}
    }
    
    // What runs on document.ready
    function _init() {
        $(".carousel a").click(function(e) {
            e.preventDefault();
            
            $(".photo img").attr("src", $(this).attr("href") );
            $(".carousel a.active").removeClass("active");
            $(this).addClass("active");
        });
        
        // Preload the images
        $(".carousel a").each(function(){
           $('<img/>')[0].src = $(this).attr("href"); 
        });
        
        // Replace all of the mailto hrefs (avoid spam)
        var at = / at /;
        var dot = / dot /g;
        $('span.mailme').each(function() {
          var addr = $(this).text().replace(at,"@").replace(dot,".");
          $(this).after('<a href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>')
                .hover(function(){window.status="Send a letter!";}, function(){window.status="";})
                .remove();
        });
        
        // iOS orientation bug fix
        _fix();
    };
    $(document).ready(_init);
    
    return {}
    
}( window.jQuery );

/*
*   PLUGINS
*/

/* 
    Scrollability
    https://github.com/joehewitt/scrollability
    Slightly modified to help prevent errant click events on Mixel thumbnails (iOS4)
    
    Added the following statement to the onTouchStart function:
    
        if( $touchCandidate.is(".mixel.thumb") || $touchCandidate.is(".mixel.thumb img") ) {
            event.preventDefault();
        }    
*/

(function(){function W(){var a=document.createElement("style");document.head.appendChild(a);x=document.styleSheets[document.styleSheets.length-1]}function X(a){function b(a){a.preventDefault();L=true;j&&(clearTimeout(j),j=0);m&&(y(m),m=null);var b=s?a.touches[0]:a;v=b.clientX;i=b.clientY;if(h.length>1)for(b=0;b<h.length;++b){var c=h[b];c.disable&&c.disable(v,i,z,E)&&(c.terminate(),h.splice(b--,1),h.length==1&&(c=h[0],t("scrollability-lock",c.node,{direction:c.direction})))}h.forEach(function(b){var c=
b.filter(v,i);b.track(c,a.timeStamp)})}function g(){if(m){var a=document.createEvent("MouseEvents");a.initMouseEvent("click",true,true,window,1);m[0].dispatchEvent(a);y(m)}document.removeEventListener(s?"touchmove":"mousemove",b,false);document.removeEventListener(s?"touchend":"mouseup",g,false);h.forEach(function(a){a.takeoff()})}var e=s?a.touches[0]:a,m=null;v=z=e.clientX;i=E=e.clientY;L=false;h=Y(a.target,v,i,a.timeStamp);if(!h.length)return true;var c=a.target,e=$(a.target),j=setTimeout(function(){j=
0;for(var a=[],b=c;b;b=b.parentNode)if(b.nodeType==1)b.className=(b.className?b.className+" ":"")+"touched",a.push(b);m=a},50);(e.is(".mixel.thumb")||e.is(".mixel.thumb img"))&&a.preventDefault();document.addEventListener(s?"touchmove":"mousemove",b,false);document.addEventListener(s?"touchend":"mouseup",g,false)}function Z(a,b,g,e){function m(a,b){u.push({node:a,callback:b,keyframes:[]})}function c(a){q=a}function j(a){F=a;O(M,N)}function h(a,b){k=a;l=b}function O(a,b){D=b-N;N=b;n=a-M;M=a;Math.abs(n)>=
aa?(A&&--A,A=0):++A;if(B)if(d>l&&z==l){var c=d-l;n*=(1-c/G)*H}else d<k&&C==k&&(c=k-d,n*=(1-c/G)*H);d+=n;i(d);f.style.webkitAnimationName="";u.forEach(function(a){a.node.style.webkitAnimationName=""});return true}function s(a){var b=l-k;if(p&&k<0){var c=o-P*2,d=c/(b+o)*c;a>l?(d=Math.max(d-(a-l),Q),a=0):a<k?(d=Math.max(d-(k-a),Q),a=c-d):a=Math.abs(l-a)/b*(c-d);a+=P;return"translate3d(0, "+Math.round(a)+"px,  0) scaleY("+Math.round(d)+")"}}function v(){function b(a){var e=d-m;if(a||c-j>=ba||g<0!=e<0)p.push({position:d,
time:c}),u.forEach(function(a){a.keyframes.push({time:c,css:a.callback(d)})}),g=e,m=d,j=c}var c=0,m=d,j=0,g=0,e,f,h=0,p=[];if(I&&(Math.abs(d-l)>E||Math.abs(n)>ca))if(d>l){if(l!=z){l+=o+q;k+=o+q;var r=k%o,r=-Math.round((d+o-r)/(o+q));t("scrollability-page",a.node,{page:r})}}else k!=C&&(l-=o+q,k-=o+q,r=k%o,r=-Math.round((d-o-r)/(o+q)),t("scrollability-page",a.node,{page:r}));for(r=true;r;){if(d>l&&B)if(n>0){var i=d-l,i=1-i/G;n=Math.max(n-R,0)*i;d+=n}else h||(e=d,f=l-d),d=h==w?e+f:f*(-Math.pow(2,-10*
h/w)+1)+e,r=++h<=w&&Math.floor(Math.abs(d))>l;else if(d<k&&B)n<0?(i=k-d,i=1-i/G,n=Math.min(n+R,0)*i,d+=n):(h||(e=d,f=k-d),d=h==w?e+f:f*(-Math.pow(2,-10*h/w)+1)+e,r=++h<=w&&Math.ceil(d)<k);else{r=Math.floor(Math.abs(n)*10)>0;if(!r)break;n*=da;d+=n}b(!r);c+=S}I?(d=Math.round(d/(o+q))*(o+q),b(true)):d>l&&B?(d=l,b(true)):d<k&&B&&(d=k,b(true));e=p.length?p[p.length-1].time:0;f="scrollability"+T++;h=U(a,p,f,e,F);return{time:e,position:d,keyframes:p,name:f,css:h}}function i(b){if(t("scrollability-scroll",
f,{direction:a.direction,position:b})&&(y(b),p&&L))b=p,b.style.webkitTransition="",b.style.opacity="1"}function y(b){f.style.webkitTransform=a.update(b+F);f.scrollableOffset=F;u.forEach(function(a){a.node.style.webkitTransform=a.callback(b)})}function J(b){if(p)b?(b=p,b.style.webkitTransition="",b.style.opacity="1"):(p.style.opacity="0",p.style.webkitTransition="opacity 0.33s linear");f.removeEventListener("webkitAnimationEnd",f.normalEnd,false);delete f.earlyEnd;delete f.normalEnd;a.mute||t("scrollability-end",
f)}var f=a.node,B=a.constrained,I=a.paginated,o=a.viewport||0,p=a.scrollbar,d=a.position,k=a.min,l=a.max,C=k,z=Math.round(l/o)*o,q=0,n=0,w=I?ea:fa,G=a.bounce,E=o*ga,M=startTouch=a.filter(b,g),N=e,D=0,A=0,u=[],F=f.scrollableOffset||0;if(!a.mute&&!t("scrollability-start",f,{position:d,track:m,setSpacing:c,setOffset:j,setBounds:h}))return null;I&&(q===void 0&&(b=Math.round(Math.abs(C)%o),g=(Math.abs(C)-b)/o+1,q=b/g),k=l=Math.round(d/(o+q))*(o+q),C+=q);p&&(m(p,s),p.parentNode||f.parentNode.appendChild(p));
f.earlyEnd&&(K(f),u.forEach(function(a){K(a.node)}),f.earlyEnd(),i(d));a.reposition=i;a.track=O;a.takeoff=function(){t("scrollability-takeoff",f,{position:d,min:k,max:l,setBounds:h});A&&(n=0);d+=n;i(d);n=n/D*S;var b=v();b.time?(t("scrollability-animate",f,{direction:a.direction,time:b.time,keyframes:b.keyframes}),f.cleanup&&f.cleanup(),x.insertRule(b.css,0),u.forEach(function(c){c.name="scrollability-track"+T++;c=U(a,c.keyframes,c.name,b.time);x.insertRule(c,0)}),f.earlyEnd=function(){J(true)},f.normalEnd=
function(){y(b.keyframes[b.keyframes.length-1].position);J()},f.cleanup=function(){delete f.cleanup;x.deleteRule(0);u.forEach(function(){x.deleteRule(0)})},f.addEventListener("webkitAnimationEnd",f.normalEnd,false),K(f,b.name,b.time),u.forEach(function(a){K(a.node,a.name,b.time)})):J()};a.terminate=function(){J()};return a}function Y(a,b,g,e){for(var m=[],c=document.querySelectorAll(".scrollable.universal"),j=0;j<c.length;++j)D(c[j],m,b,g,e);c.length||D(a,m,b,g,e);return m}function D(a,b,g,e,m){for(;a;){if(a.nodeType==
1){var c;a:{var j=a;c=j.className.split(" ");if(c.indexOf("scrollable")!=-1)for(var h=0;h<c.length;++h){var i=c[h];if(V[i]){j=V[i](j);j.direction=i;j.paginated=c.indexOf("paginated")!=-1;c=j;break a}}c=void 0}if(c){i=false;for(j=0;j<b.length;++j)if(b[j].node==a){i=true;break}i||(c=Z(c,g,e,m))&&b.push(c)}}a=a.parentNode}}function U(a,b,g,e,m){var c=["@-webkit-keyframes "+g+" {"];b.forEach(function(b){b=Math.floor(b.time/e*100)+"% {-webkit-transform: "+(b.css||a.update(b.position+m))+";}";c.push(b)});
c.push("}");return c.join("\n")}function y(a){for(var b=0;b<a.length;++b){var g=a[b];g.className=g.className.replace("touched","")}}function ha(a){if(!a.scrollableScrollbar)(a.scrollableScrollbar=document.createElement("div")).className="scrollability-scrollbar";return a.scrollableScrollbar}function K(a,b,g){if(b)a.style.webkitAnimation=b+" "+g+"ms linear both";a.style.webkitAnimationPlayState=b?"running":"paused"}function t(a,b,g){var e=document.createEvent("Events");e.initEvent(a,false,true);if(g)for(a in g)e[a]=
g[a];return b.dispatchEvent(e)}"webkitTransform"in document.documentElement.style&&/OS 5_/.exec(navigator.userAgent);var s="ontouchstart"in window,da=0.9925,aa=4,H=0.75,R=0.01,fa=240,ea=160,ga=0.5,ca=2,P=2,Q=7,S=4,ba=24,z,E,v,i,L,h=[],T=0,x,V={horizontal:function(a){var b=a.parentNode,g=a.querySelector(".scrollable > .clipper")||a;if(a.style.webkitAnimation)a.style.webkitAnimationPlayState="paused";var e=getComputedStyle(a).webkitTransform,e=(new WebKitCSSMatrix(e)).m41-(a.scrollableOffset||0);return{node:a,
min:-g.offsetWidth+b.offsetWidth,max:0,position:e,viewport:b.offsetWidth,bounce:b.offsetWidth*H,constrained:true,filter:function(a){return a},disable:function(a,b,e,g){a=Math.abs(a-e);b=Math.abs(b-g);if(b>a&&b>10)return true},update:function(a){return"translate3d("+Math.round(a)+"px, 0, 0)"}}},vertical:function(a){var b=a.parentNode,g=a.querySelector(".scrollable > .clipper")||a;if(a.style.webkitAnimation)a.style.webkitAnimationPlayState="paused";var e=getComputedStyle(a).webkitTransform,e=(new WebKitCSSMatrix(e)).m42;
return{node:a,scrollbar:ha(a),position:e,min:-g.offsetHeight+b.offsetHeight,max:0,viewport:b.offsetHeight,bounce:b.offsetHeight*H,constrained:true,filter:function(a,b){return b},disable:function(a,b,e,g){a=Math.abs(a-e);b=Math.abs(b-g);if(a>b&&a>10)return true},update:function(a){return"translate3d(0, "+Math.round(a)+"px, 0)"}}}};$(document).ready(function(){document.addEventListener(s?"touchstart":"mousedown",X,false);window.addEventListener("load",W,false)})})();
