var ghsweb={mode:null,browsers_mouseover:false,websites_list:[],websites_img:{'length':0},websites_preloaded:[],websites_page:0,websites_num_pages:0,websites_timer:null,websites_fading:false,email_regex:/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/,phone_regex:/^\([0-9]{2}\) [0-9]{4}\-[0-9]{4}$/,sending_form:false,init:function(a,b){this.mode=a;this.websites_list=b;this.websites_num_pages=b.length/3;$('#cancel_contact').click(function(){setTimeout(function(){$('#contato input, #contato textarea').val('').blur()},500);$('#l_switch').trigger('click');return false});$('#portfolio').hover(function(){ghsweb.browsers_mouseover=true;clearTimeout(ghsweb.websites_timer);$('.arrow').animate({'opacity':'1.0'},{'duration':250,'queue':false})},function(){ghsweb.browsers_mouseover=false;ghsweb.websites_timer=setTimeout(function(){ghsweb.rotate_websites()},4000);$('.arrow').animate({'opacity':'0'},{'duration':250,'queue':false})});$('#arrow_prev').click(function(){if(ghsweb.websites_fading){return false}if(ghsweb.websites_page==0){ghsweb.rotate_websites(ghsweb.websites_num_pages-1)}else{ghsweb.rotate_websites(ghsweb.websites_page-1)}return false});$('#arrow_next').click(function(){if(ghsweb.websites_fading){return false}if(ghsweb.websites_page+1>=ghsweb.websites_num_pages){ghsweb.rotate_websites(0)}else{ghsweb.rotate_websites(ghsweb.websites_page+1)}return false});$('#f_body').keyup(function(){if($(this).val().length>$(this).attr('maxlength')){$(this).val($(this).val().substr(0,$(this).attr('maxlength')))}});$('#contato form').submit(ghsweb.contact_submit);$('#l_switch').click(function(){ghsweb.switch_page(false);return false});$('#f_phone').mask('(99) 9999-9999');this.start_preload()},rotate_websites:function(a){clearTimeout(this.websites_timer);if(typeof(a)!='undefined'){this.websites_page=a}else{if(this.mode!='portfolio'||this.browsers_mouseover){return false}if(this.websites_page+1>=this.websites_num_pages){this.websites_page=0}else{this.websites_page++}}this.websites_fading=true;var b=this.websites_list[this.websites_page*3];var c=this.websites_list[1+(this.websites_page*3)];var d=this.websites_list[2+(this.websites_page*3)];$('#browser_left').attr('href',b.url).find('img').fadeOut(250,function(){$(this).remove();$(ghsweb.websites_img[b.filename]).appendTo('#browser_left').css('display','none').attr('alt',b.title).attr('title',b.title).attr('width','144').attr('height','100').fadeIn(250)});$('#main_browser').attr('href',c.url).find('img').fadeOut(250,function(){$(this).remove();$(ghsweb.websites_img[c.filename]).appendTo('#main_browser').css('display','none').attr('alt',c.title).attr('title',c.title).fadeIn(250)});$('#browser_right').attr('href',d.url).find('img').fadeOut(250,function(){$(this).remove();$(ghsweb.websites_img[d.filename]).appendTo('#browser_right').css('display','none').attr('alt',d.title).attr('title',d.title).attr('width','144').attr('height','100').fadeIn(250);ghsweb.websites_fading=false});this.websites_timer=setTimeout(function(){ghsweb.rotate_websites()},4000)},switch_page:function(a){if(ghsweb.sending_form){return false}if(ghsweb.mode=='portfolio'){$('#browser_right').animate({'margin-top':'144px'},500);setTimeout(function(){$('#browser_left').animate({'margin-top':'144px'},500)},50);setTimeout(function(){$('#main_browser').animate({'margin-top':'144px'},400,function(){$('#portfolio').css('display','none');$('#contato').css('display','block').find('form').animate({'margin-top':'4px'},400,function(){$('#f_name').focus()})})},100);$('#footer').fadeOut(500,function(){$('#l_switch').attr('href','/').html('Voltar ao portfólio');$('#contact_info').css('display','inline');$(this).fadeIn(500)});$('.error').removeClass('error');ghsweb.mode='contact'}else{$('#contato form').animate({'margin-top':'145px'},500,function(){$('#contato').css('display','none');$('#portfolio').css('display','block');$('#browser_right').animate({'margin-top':'0px'},500);setTimeout(function(){$('#browser_left').animate({'margin-top':'0px'},500)},50);setTimeout(function(){$('#main_browser').animate({'margin-top':'0'},400)},100)});if(a){$('#footer').fadeOut(500,function(){$('#l_switch').remove();$('#contact_info').css('display','inline').find('span').remove();$(this).fadeIn(500)})}else{$('#footer').fadeOut(500,function(){$('#l_switch').attr('href','/?contato').html('Entre em contato');$('#contact_info').css('display','none');$(this).fadeIn(500)})}ghsweb.mode='portfolio';ghsweb.websites_timer=setTimeout(function(){ghsweb.rotate_websites()},4000)}},contact_submit:function(){if(ghsweb.sending_form){return false}var b=$('#f_name').val();var c=$('#f_email').val();var d=$('#f_phone').val();var e=$('#f_subject').val();var f=$('#f_body').val();var g=false;if(!b.length){$('#f_name').addClass('error').bind('keyup blur',function(){if($(this).val().length)$(this).removeClass('error').unbind('keyup blur')});g=true}if(!c.match(ghsweb.email_regex)){$('#f_email').addClass('error').bind('keyup blur',function(){if($(this).val().match(ghsweb.email_regex))$(this).removeClass('error').unbind('keyup blur')});g=true}if(!d.match(ghsweb.phone_regex)){$('#f_phone').addClass('error').bind('keyup blur',function(){if($(this).val().match(ghsweb.phone_regex))$(this).removeClass('error').unbind('keyup blur')});g=true}if(!e.length){$('#f_subject').addClass('error').bind('keyup blur',function(){if($(this).val().length)$(this).removeClass('error').unbind('keyup blur')});g=true}if(f.length<20){$('#f_body').addClass('error').bind('keyup blur',function(){if($(this).val().length>=20)$(this).removeClass('error').unbind('keyup blur')});g=true}if(g){$('.error:eq(0)').focus()}else{ghsweb.sending_form=true;$('.error').removeClass('error');$('input, textarea, button').attr('disabled','disabled');$('<div id="form_overlay"></div>').prependTo('#contato form').animate({'opacity':'0.7'},250);$('<img src="/img/loading.gif" alt="Carregando" id="form_loading" />').prependTo('#contato form');var h=function(){ghsweb.sending_form=false;$('#form_overlay').fadeOut(250,function(){$(this).remove()});$('#form_loading').remove();$('input, textarea, button').removeAttr('disabled')};$('#error_msg').animate({'top':'-100px'},250,function(){$(this).remove()});setTimeout(function(){$.ajax({type:'POST',url:'/?contato',data:{'name':b,'email':c,'phone':d,'subject':e,'body':f,'submit':'Enviar'},success:function(a){$('<div id="sucesso" style="top:-100px;"><p>Sua mensagem foi enviada com sucesso!</p></div>').prependTo('body').animate({'top':'0px'},500);h();ghsweb.switch_page(true)},error:function(a){if(a.responseText=='antiflood'){$('<div id="error_msg" style="top:-100px;"><p>Bloqueio antiflood ativado. Aguarde alguns minutos para enviar outra mensagem.</p></div>').prependTo('body').animate({'top':'0px'},500)}else{alert('Houve um erro ao enviar a sua mensagem. Tente novamente mais tarde.')}h()}})},250)}return false},start_preload:function(){for(var i=0,j=this.websites_list.length;i<j;i++){this.preload_image(this.websites_list[i].filename)}setTimeout(function(){ghsweb.check_preload()},500)},check_preload:function(){if(this.websites_preloaded.length==this.websites_img.length){this.websites_timer=setTimeout(function(){ghsweb.rotate_websites()},4000)}else{setTimeout(function(){ghsweb.check_preload()},500)}},preload_image:function(a){if(typeof(this.websites_img[a])=='undefined'){this.websites_img[a]=new Image();this.websites_img[a].src='/img/portfolio/'+a;this.websites_img.length++}if(this.websites_img[a].complete){this.websites_preloaded.push(a)}else{setTimeout(function(){ghsweb.preload_image(a)},100)}}};(function(a){var b=(a.browser.msie?"paste":"input")+".mask",c=window.orientation!=undefined;a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn"},a.fn.extend({caret:function(a,b){if(this.length!=0){if(typeof a=="number"){b=typeof b=="number"?b:a;return this.each(function(){if(this.setSelectionRange)this.setSelectionRange(a,b);else if(this.createTextRange){var c=this.createTextRange();c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select()}})}if(this[0].setSelectionRange)a=this[0].selectionStart,b=this[0].selectionEnd;else if(document.selection&&document.selection.createRange){var c=document.selection.createRange();a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length}return{begin:a,end:b}}},unmask:function(){return this.trigger("unmask")},mask:function(d,e){if(!d&&this.length>0){var f=a(this[0]);return f.data(a.mask.dataName)()}e=a.extend({placeholder:"_",completed:null},e);var g=a.mask.definitions,h=[],i=d.length,j=null,k=d.length;a.each(d.split(""),function(a,b){b=="?"?(k--,i=a):g[b]?(h.push(new RegExp(g[b])),j==null&&(j=h.length-1)):h.push(null)});return this.trigger("unmask").each(function(){function v(a){var b=f.val(),c=-1;for(var d=0,g=0;d<k;d++)if(h[d]){l[d]=e.placeholder;while(g++<b.length){var m=b.charAt(g-1);if(h[d].test(m)){l[d]=m,c=d;break}}if(g>b.length)break}else l[d]==b.charAt(g)&&d!=i&&(g++,c=d);if(!a&&c+1<i)f.val(""),t(0,k);else if(a||c+1>=i)u(),a||f.val(f.val().substring(0,c+1));return i?d:j}function u(){return f.val(l.join("")).val()}function t(a,b){for(var c=a;c<b&&c<k;c++)h[c]&&(l[c]=e.placeholder)}function s(a){var b=a.which,c=f.caret();if(a.ctrlKey||a.altKey||a.metaKey||b<32)return!0;if(b){c.end-c.begin!=0&&(t(c.begin,c.end),p(c.begin,c.end-1));var d=n(c.begin-1);if(d<k){var g=String.fromCharCode(b);if(h[d].test(g)){q(d),l[d]=g,u();var i=n(d);f.caret(i),e.completed&&i>=k&&e.completed.call(f)}}return!1}}function r(a){var b=a.which;if(b==8||b==46||c&&b==127){var d=f.caret(),e=d.begin,g=d.end;g-e==0&&(e=b!=46?o(e):g=n(e-1),g=b==46?n(g):g),t(e,g),p(e,g-1);return!1}if(b==27){f.val(m),f.caret(0,v());return!1}}function q(a){for(var b=a,c=e.placeholder;b<k;b++)if(h[b]){var d=n(b),f=l[b];l[b]=c;if(d<k&&h[d].test(f))c=f;else break}}function p(a,b){if(!(a<0)){for(var c=a,d=n(b);c<k;c++)if(h[c]){if(d<k&&h[c].test(l[d]))l[c]=l[d],l[d]=e.placeholder;else break;d=n(d)}u(),f.caret(Math.max(j,a))}}function o(a){while(--a>=0&&!h[a]);return a}function n(a){while(++a<=k&&!h[a]);return a}var f=a(this),l=a.map(d.split(""),function(a,b){if(a!="?")return g[a]?e.placeholder:a}),m=f.val();f.data(a.mask.dataName,function(){return a.map(l,function(a,b){return h[b]&&a!=e.placeholder?a:null}).join("")}),f.attr("readonly")||f.one("unmask",function(){f.unbind(".mask").removeData(a.mask.dataName)}).bind("focus.mask",function(){m=f.val();var b=v();u();var c=function(){b==d.length?f.caret(0,b):f.caret(b)};(a.browser.msie?c:function(){setTimeout(c,0)})()}).bind("blur.mask",function(){v(),f.val()!=m&&f.change()}).bind("keydown.mask",r).bind("keypress.mask",s).bind(b,function(){setTimeout(function(){f.caret(v(!0))},0)}),v()})}})})(jQuery)
