/**
 * Main script for 180byDesign site.
 *
 * @author		Michael Gilley
 * @version		$Id v2.1$
 * @copyright	180byDesign, October, 2009
 * @link		http://www.180bydesign.com
 * @package		180site
 **/

$(function(){var progress,detail,page,cp,set,scroll=stopPage=stopLoad=loading=haltload=internal=false,detail=setProj=null,queue=[];function hide(context){context.find('.hide').hide().removeClass('hide')}function setError(er){$('<div id="error">').text(er).prepend('<h1>Oops!</h1>').prependTo('#contents')}function loadDetails(pid,callback){callback=callback||function(){};if(($('#details-'+pid).length>0)||($.inArray(pid,queue)>=0)){if(!stopLoad)callback()}else{queue.push(pid);$.ajax({url:window.path+'details/id:'+pid,beforeSend:function(){loading=true;if(pid==set[cp]){detail=setTimeout(function(){$('<div class="details-loading"/>').hide().prependTo('#details').fadeIn()},1000)}},error:function(x,s,e){var er='Sorry, there seems to be a problem with your internet connection. Please make sure that your computer is connected to the internet and then try refreshing the page in your browser. Thank you.';setError(er);loading=false},success:function(data,status){clearTimeout(detail);$('.details-loading').fadeOut(50,function(){$(this).remove()});var el=$(data).hide().appendTo('#details .container');hide(el);Cufon.refresh();if(!stopLoad)callback();loading=false;queue.splice($.inArray(pid,queue),1)}})}}function checkLoad(callback){setTimeout(function(){if($('#details-'+set[cp]).length>0){callback()}else{checkLoad(callback)}},20)}function getCp(dir){if((cp+dir)>set.length-1){return 0}else if((cp+dir)<0){return set.length-1}else{return cp+dir}}function nextOut(){var p=cp;cp=getCp(1);stopLoad=true;loadDetails(set[cp],nextIn);$('#details-'+set[p]).animate({left:-25,opacity:0},450,'easeOutExpo',function(){stopLoad=false;$(this).hide();if($.inArray(set[cp],queue)<0){nextIn()}else if($.inArray(set[cp],queue)>=0&&$('.details-loading').length==0){$('<div class="details-loading"/>').hide().prependTo('#details').fadeIn();checkLoad(nextIn)}else{stopLoad=false}})}function nextIn(){$('#details-'+set[cp]).css({left:25,opacity:0}).show().animate({left:0,opacity:1},450,'easeOutExpo');loadDetails(set[getCp(1)])}function prevOut(){var p=cp;cp=getCp(-1);stopLoad=true;loadDetails(set[cp],prevIn);$('#details-'+set[p]).animate({left:25,opacity:0},450,'easeOutExpo',function(){stopLoad=false;$(this).hide();if($.inArray(set[cp],queue)<0){prevIn()}else if($.inArray(set[cp],queue)>=0&&$('.details-loading').length==0){$('<div class="details-loading"/>').hide().prependTo('#details').fadeIn();checkLoad(prevIn)}else{stopLoad=false}})}function prevIn(){$('#details-'+set[cp]).css({left:-25,opacity:0}).show().animate({left:0,opacity:1},450,'easeOutExpo');loadDetails(set[getCp(-1)])}function dynLoadDetail(hash){var reg=/[0-9]+/g;if(!reg.test(hash))return false;var detail=hash.match(reg)[0],index=$.inArray(detail,set),mov=-25,callback;if(index>=0){if(index>cp){callback=nextIn}else if(index<cp){callback=prevIn;mov=25}else{callback=nextIn}}else{callback=nextIn;index=set.push(detail)-1}cp=index;stopLoad=true;loadDetails(set[cp],callback);$('.details:visible').animate({left:mov,opacity:0},450,'easeOutExpo',function(){stopLoad=false;$(this).hide();if($.inArray(set[cp],queue)<0){callback()}else{stopLoad=false}})}function goDetails(){var $projects=$('#projects'),$heading=$('.heading');$('.project').hide();$('#bg').fadeOut('fast');$('.blinder').css({opacity:0,display:'block'}).animate({opacity:1},350,'easeOutExpo',function(){$heading.data('origH',$heading.height());$projects.animate({height:0},'slow','easeOutExpo');$heading.animate({height:504},'slow','easeOutExpo',function(){$('#details').fadeIn(150,function(){if($.inArray(set[cp],queue)<0){nextIn()}else{stopLoad=false}loadDetails(set[getCp(-1)])})})});$('#details').click(function(event){var t=$(event.target);if(t.is('.thumbs a')){if(!t.hasClass('active')){var pos=(t.attr('class').split('-')[1]-1)*-709;t.parents('ul:first').find('a.active').removeClass('active');t.addClass('active');t.parents('.thumbs').find('.window').animate({left:pos},450,'easeOutExpo')}return false}else if(t.is('.thumbs img')){var pos=parseInt(t.parents('.thumbs').find('a.active').attr('class').split(' active')[0].split('tab-')[1]);var n=pos+1;if(pos==t.parents('.thumbs').find('img').length){pos=0;n=1}pos=pos*-709;t.parents('.thumbs').find('a.active').removeClass('active');t.parents('.thumbs').find('a.tab-'+n).addClass('active');t.parents('.thumbs').find('.window').animate({left:pos},450,'easeOutExpo');return false}else if(t.is('.panels a')&&t.next().is(':hidden')){t.next().slideToggle(350,'easeOutExpo');t.parents('.panels').find('.panel div:not(:animated)').slideToggle(350,'easeOutExpo');return false}})}function fadeProjects(el){cp=$('.project:visible').index(el);set=$.map($('.project:visible'),function(n,i){return $(n).attr('id').split('-')[1]});stopLoad=true;loadDetails(set[cp],nextIn);if($('.filter').hasClass('active')){$('.filter').removeClass('active');$('#filters').slideUp(350,'easeOutExpo')}var orig=$(el).position(),a=[],d=0;$('.project').each(function(){var pos=$(this).position(),dis=Math.floor(Math.sqrt(Math.pow(orig.left-pos.left,2)+Math.pow(orig.top-pos.top,2))/2),self=this;a.push(dis);setTimeout(function(){$(self).animate({opacity:0},350);if($.browser.msie)$(self).find('span').hide()},dis)});for(i=0;i<=a.length-1;i++){d=Math.max(d,a[i])}setTimeout(function(){if(internal){$.history.load('project/'+set[cp])}else{goDetails()}},d+400)}function hideDetails(){$('#viewoptions').find('a').removeClass('selected').end().find('.grid').addClass('selected');$('#details').fadeOut(150,function(){$('#details-'+set[cp]).hide();$('a.filter').removeClass('hidden');if($('a.filter').hasClass('clicked')){$('a.filter').removeClass('clicked').click()}var hH=$('.heading').data('origH');var pH=$('#projects').data('origH');$('#projects').animate({height:pH},'slow','easeOutExpo');$('.heading').animate({height:hH},'slow','easeOutExpo',function(){$('.blinder').animate({opacity:0},550,'easeOutExpo');$('#bg').fadeIn();setTimeout(function(){var f=$('#filters a.selected').text().toLowerCase().replace(/\s/,'_');if(f=='show_all'){$('#projects .project').hide().css('opacity',1).fadeIn(250,'easeOutExpo')}else{$('#projects').find('.project').hide().css('opacity',1).end().find('.'+f).css('opacity',0).show().animate({opacity:1},250,'easeOutExpo')}if($.browser.msie)$('.project span:not(".hover")').show();projectMargins()},250)})}).unbind()}function projectMargins(){var c=0;$('.project').removeClass('last').each(function(i){if($(this).is(':visible')){c++;if((c)%4==0)$(this).addClass('last')}})}function goProjects(){hide($('body'));$('.fade').hide();$('.project').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')}).click(function(){$('#viewoptions').find('a').removeClass('selected').end().find('.detailed').addClass('selected');internal=true;fadeProjects(this);$('a.filter').addClass('hidden');return false});$('a.filter').click(function(){if(!$(this).hasClass('active')){if(!$(this).hasClass('hidden')){$(this).addClass('active');$('#filters').slideDown(350,'easeOutExpo')}else{$(this).addClass('clicked');$('a.grid').click()}}else{$(this).removeClass('active');$('#filters').slideUp(350,'easeOutExpo')}return false});$('#filters a').click(function(){if(!$(this).hasClass('selected')){$('#filters a.selected').removeClass('selected');$(this).addClass('selected');var f=$(this).text().toLowerCase().replace(/\s/,'_');if(f=='show_all'){$('#projects .project').show()}else{$('#projects').find('.project').hide().end().find('.'+f).show()}projectMargins()}return false});$('a.detailed').click(function(){if(!$(this).hasClass('selected')){$('#viewoptions').find('a').removeClass('selected').end().find('.detailed').addClass('selected');internal=true;fadeProjects($('.project:visible:first'));$('a.filter').addClass('hidden')}return false});$('a.grid').click(function(){if(!$(this).hasClass('selected')){$.history.load('work')}return false});$('#details-next').click(function(){internal=true;$.history.load('project/'+set[getCp(1)]);nextOut();return false});$('#details-prev').click(function(){internal=true;$.history.load('project/'+set[getCp(-1)]);prevOut();return false});$('#projects').data('origH',$('#projects').height());$('#projects').height($('#projects').data('origH'));projectMargins();if(detail&&$('#project-'+detail).length>0){fadeProjects($('#project-'+detail))}}function goBlog(){$('.title a, a.more-link, h2 a, .widget a').click(function(){var href=$(this).attr('href').slice(window.path.length).replace(/\/$/g,'');$.history.load(href);return false});$('.comments a:not(.single)').click(function(){var href=$(this).attr('href').slice(window.path.length).replace(/\/#(.+)$/,'');scroll=true;$.history.load(href);return false});$('.comments a.single').click(function(){$.scrollTo('#respond',450,{easing:'easeOutExpo'});$('#commentform :input:first').focus().select();return false});$('#commentform').submit(function(){var self=this;$.ajax({url:$(self).attr('action'),type:'POST',data:$(self).serialize(),beforeSend:function(){$('#check').hide();if(!(/^[a-z]([0-9a-z_])+$/i.test($('#author').val()))||$('#author').val()==''){$('#check').text('Field is required and may only use a-z, 0-9, underscores, and it must begin with a letter.').slideDown();$('#author').focus().select();return false}else if(!(mailreg.test($('#email').val()))){$('#check').text('Field is required and must be in the form me@example.com').slideDown();$('#email').focus().select();return false}},complete:function(xhr,status){var list=$('.commentlist');var c=$('<div/>').append(xhr.responseText.replace(/<script(.|\s)*?\/script>/g,"")).find('.commentlist li:last').hide();list.append(c);c.slideDown(450,'easeOutExpo');$('#comment').val('')}});return false});if(scroll){setTimeout(function(){$.scrollTo('#respond',450,{easing:'easeOutExpo'});$('#commentform :input:first').focus().select()},500);scroll=false}}function goContact(){hide($(document.body));function checkLength(o,n,min,max){if(o.val().length>max||o.val().length<min){o.addClass('error');$('#formcheck').text(n+" must be between "+min+" and "+max+" characters long.").slideDown(350,'easeOutExpo');return false}else{return true}}function checkRegexp(o,regexp,n){if(!(regexp.test(o.val()))){o.addClass('error');$('#formcheck').text(n).slideDown(350,'easeOutExpo');return false}else{return true}}$('#PagesContactForm').submit(function(){var valid=true,name=$('#PagesFirstName'),email=$('#PagesEmail'),message=$('#PagesMessage'),allFields=$([]).add(name).add(email).add(message);allFields.removeClass('error');valid=valid&&checkLength(name,'First Name',3,16);valid=valid&&checkLength(email,'Email',6,80);valid=valid&&checkLength(message,'Message',1,1000);valid=valid&&checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"First Name may only use a-z, 0-9, underscores, and it must begin with a letter.");valid=valid&&checkRegexp(email,mailreg,"eg. me@example.com");$('.error:first').focus().select();if(valid){var self=$(this);$('#contents .form').height($('#contents .form').height()).wrapInner('<div class="fade"></div>').find('.fade').fadeOut(450,'easeOutExpo');$.ajax({url:self.attr('action'),type:'POST',data:self.serialize(),error:function(x,s,e){var er='Sorry, there seems to be a problem with your internet connection. Please make sure that your computer is connected to the internet and then try again. Thank you.';setError(er);$('#contents .form .fade').fadeIn()},success:function(data,status){$('.thanks').prependTo('#contents .form').fadeIn()}})}return false})}function dispatch(){switch(page){case"work":goProjects();break;case"project":if(setProj!==null&&$('#project-'+setProj).length>0){goProjects();$('#project-'+setProj).click()}break;case"blog":goBlog();break;case"contact":goContact();break}}function loadPage(req){var pageLoaded=false,html;stopPage=true;$('#contents').animate({left:-25,opacity:0},450,'easeOutExpo',function(){stopPage=false;if(pageLoaded){$('#contents').hide().html(html);Cufon.refresh();$('#contents').css({left:25,opacity:0}).show().animate({opacity:1,left:0},450,'easeOutExpo');dispatch()}});$.ajax({url:window.path+req+'/',beforeSend:function(){progress=setTimeout(function(){$('#loader').fadeIn()},1000)},error:function(xhr,status,er){if(xhr.status!='404'){var er='Sorry, there seems to be a problem with your internet connection. Please make sure that your computer is connected to the internet and then try refreshing the page in your browser. Thank you.';setError(er)}},complete:function(xhr,status){clearTimeout(progress);$('#loader').fadeOut(50);if(req.slice(0,4)=='blog'){html=$('<div/>').append(xhr.responseText.replace(/<script(.|\s)*?\/script>/g,"")).find('#contents > .container')}else{html=$(xhr.responseText)}pageLoaded=true;if(!stopPage){$('#contents').hide().html(html);Cufon.refresh();$('#contents').css({left:25,opacity:0}).show().animate({left:0,opacity:1},450,'easeOutExpo');dispatch()}}})}function setMenu(page){if(!$('#menu a[rel='+page+']').hasClass('active')){var $a=$('#menu').find('a').removeClass('active').end().find('a[rel='+page+']');$a.css('bottom',3).animate({bottom:0},250,'easeOutBounce',function(){$(this).addClass('active')})}}function pageLoad(hash){if(hash){page=hash.replace(/[\/|\-](.+)$/g,'');if(typeof pageTracker!='undefined'){pageTracker._trackPageview('/'+hash)}if(page=='project'){var reg=/[0-9]+/g;if($('#details').is(':visible')&&!internal){dynLoadDetail(hash)}else if($('#projects').length<=0&&reg.test(hash)){setProj=hash.match(reg)[0];setMenu('work');loadPage('work')}else if($('#details .details').length==0){var n=hash.match(reg)[0];$('#project-'+n).click()}else if($('#details').is(':hidden')){goDetails()}internal=false;return true}if(page=='work'&&$('#details').is(':visible')){hideDetails();return true}setMenu(page);if(!haltload){loadPage(hash);document.title=page.slice(0,1).toUpperCase()+page.slice(1)+' :: 180byDesign'}else{haltload=false}}else{page=window.location.href.split('/');page=page[window.path.split('/').length-1];if(page=='work'&&$('#details').is(':visible')){hideDetails();return true}if(typeof progress!='undefined'){var a=$('#menu').find('a').removeClass('active').end().find('a[rel='+hash+']');a.css('bottom',3).animate({bottom:0},250,'easeOutBounce',function(){$(this).addClass('active')});loadPage(page)}else{dispatch()}}}$('#menu a').click(function(){$.history.load($(this).attr('rel'));return false});$('#seemore').live('click',function(){$.history.load('work');return false});$.history.init(pageLoad);var mailreg=/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i});