/* Modernizr 2.0.6 (Custom Build) | MIT & BSD
 * Contains: video
 */
;window.Modernizr=function(a,b,c){function w(a,b){return!!~(""+a).indexOf(b)}function v(a,b){return typeof a===b}function u(a,b){return t(prefixes.join(a+";")+(b||""))}function t(a){j.cssText=a}var d="2.0.6",e={},f=b.documentElement,g=b.head||b.getElementsByTagName("head")[0],h="modernizr",i=b.createElement(h),j=i.style,k,l=Object.prototype.toString,m={},n={},o={},p=[],q,r={}.hasOwnProperty,s;!v(r,c)&&!v(r.call,c)?s=function(a,b){return r.call(a,b)}:s=function(a,b){return b in a&&v(a.constructor.prototype[b],c)},m.video=function(){var a=b.createElement("video"),c=!1;try{if(c=!!a.canPlayType){c=new Boolean(c),c.ogg=a.canPlayType('video/ogg; codecs="theora"');var d='video/mp4; codecs="avc1.42E01E';c.h264=a.canPlayType(d+'"')||a.canPlayType(d+', mp4a.40.2"'),c.webm=a.canPlayType('video/webm; codecs="vp8, vorbis"')}}catch(e){}return c};for(var x in m)s(m,x)&&(q=x.toLowerCase(),e[q]=m[x](),p.push((e[q]?"":"no-")+q));t(""),i=k=null,e._version=d;return e}(this,this.document);

/*!
 * HTML5 Placeholder jQuery Plugin v1.8.3
 * @link http://mths.be/placeholder
 * @author Mathias Bynens <http://mathiasbynens.be/>
 */
(function(f){var e='placeholder' in document.createElement('input'),a='placeholder' in document.createElement('textarea');if(e&&a){f.fn.placeholder=function(){return this};f.fn.placeholder.input=f.fn.placeholder.textarea=true}else{f.fn.placeholder=function(){return this.filter((e?'textarea':':input')+'[placeholder]').bind('focus.placeholder',b).bind('blur.placeholder',d).trigger('blur.placeholder').end()};f.fn.placeholder.input=e;f.fn.placeholder.textarea=a;f(function(){f('form').bind('submit.placeholder',function(){var g=f('.placeholder',this).each(b);setTimeout(function(){g.each(d)},10)})});f(window).bind('unload.placeholder',function(){f('.placeholder').val('')})}function c(h){var g={},i=/^jQuery\d+$/;f.each(h.attributes,function(k,j){if(j.specified&&!i.test(j.name)){g[j.name]=j.value}});return g}function b(){var g=f(this);if(g.val()===g.attr('placeholder')&&g.hasClass('placeholder')){if(g.data('placeholder-password')){g.hide().next().attr('id',g.removeAttr('id').data('placeholder-id')).show().focus()}else{g.val('').removeClass('placeholder')}}}function d(h){var l,k=f(this),g=k,j=this.id;if(k.val()===''){if(k.is(':password')){if(!k.data('placeholder-textinput')){try{l=k.clone().attr({type:'text'})}catch(i){l=f('<input>').attr(f.extend(c(this),{type:'text'}))}l.removeAttr('name').data('placeholder-password',true).data('placeholder-id',j).bind('focus.placeholder',b);k.data('placeholder-textinput',l).data('placeholder-id',j).before(l)}k=k.removeAttr('id').hide().prev().attr('id',j).show()}k.addClass('placeholder').val(k.attr('placeholder'))}else{k.removeClass('placeholder')}}}(jQuery));

var validator;
var addthis_config = {"data_track_clickback":true};
var focuscounted = false;

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

jQuery.validator.addMethod("phonembmi", function(phone_number, element) {
    phone_number = phone_number.replace(/[".","\-"," ","\/","\\",")","(","ext"]/g, ""); 
	return this.optional(element) || !isNaN(phone_number);
}, "Please specify a phone number");

function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
	return result;
}

function calcroofpeak(){
	
	var width = parseFloat($('#width').val());
	var height = parseFloat($('#height').val());
	var pitch = parseFloat($('#roofpitch').val());
	
	if (!isNaN(width) && width!=0 && !isNaN(height) && height!=0 && !isNaN(pitch) && pitch!=0){
		var peakheight = height+((width/2/12)*pitch);
		$('#roofpeak').html(roundNumber(peakheight,3) + "");
	} else {
		$('#roofpeak').html("-");
	}
	
}

$(document).ready(function() {

	var fancyoptions = {
		overlayColor : '#000',
		transitionIn : 'elastic',
		transitionOut : 'elastic'
	}
	
	var opensubmenu = false;
	
	if ($('#home').length){
		if (screen.width>=768) {
			$("#slides").slides({
				generatePagination: false,
				play: 12000,
				pause: 4000,
				hoverPause: true
			});
		}
		
		$("#homequote").validate({
			errorLabelContainer: $("#homequote div.quoteerror"),
			focusInvalid: false
		});
	}
	
	if ($('#sidequote').length){
		$("#sidequote").validate({
			errorLabelContainer: $("#sidequote div.quoteerror"),
			focusInvalid: false
		});

		$('#sidezipcode').blur(function(){
			if ($('#sidezipcode').val()!=""){
				var country = "US";
				var zipcode = $(this).val();
				$.ajax({
					url : dir+"/lib/request.php",
					type : "POST",
					data : "r=z2&zipcode="+zipcode+"&country=US",
					success : function(resp){

						if (resp.length>0){
						
							resp = resp[0];
							var resparray = new Array();
							resparray.push(resp.city);
							if (resp.county != ""){
								resparray.push(resp.county+' county');
								$('#sitecounty').val(resp.county);
							}
							if (resp.city != resp.state){
								resparray.push(resp.state);	
							}
							if (country == "US" || country == "CA"){
								$('#sitestate').val(resp.abbrev);
							}
							$('#customstate').val(resp.state);
							
						} else {
							$('#customstate').val("");
							$('#sitestate').val("");
							$('#sitecounty').val("");							
						}
					}
				});
			} else {

			}
		});
	}
	
	if ($('.livechatlink').length){
	
		$('.livechatlink').click(function(e){
			e.preventDefault();
			window.open(dir+'/livesupport/livezilla.php','', 'width=500,height=540,left=0,top=0,resizable=yes,menubar=no,location=no,status=yes,scrollbars=yes');
		});
		
	}
	
	if ($('#openmobilenav').length){
	
		$('#openmobilenav').click(function(e) {
			e.preventDefault();
			if ($('#mobilenavbar').is(':hidden')){
				if (opensubmenu){
					$('#mobilenavbar ul > li').show();
				}
				$('#mobilenavbar').show();
			} else {
				if (opensubmenu){
					$('#mobilenavbar ul > li').show();
					$('#mobilenavbar .gallerymenu .gallerysubmenu').hide();
				} else {
					$('#mobilenavbar').hide();
				}
			}
			opensubmenu = false;
			
		});
		
		$('#openmobilecompanynav').click(function(e) {
			e.preventDefault();
			if ($('#mobilenavbar').is(':hidden')){
				$('#mobilenavbar ul > li').hide();
				$('#mobilenavbar .thecompanymenu').show();
				$('#mobilenavbar .thecompanymenu ul > li').show();
				$('#mobilenavbar').show();
				opensubmenu = true;
			} else {
				if (!opensubmenu){
					$('#mobilenavbar ul > li').hide();
					$('#mobilenavbar .thecompanymenu').show();
					$('#mobilenavbar .thecompanymenu ul > li').show();
					opensubmenu = true;
				} else{
					$('#mobilenavbar').hide();
					$('#mobilenavbar ul > li').show();
					opensubmenu = false;
				}
			}
		});
		
		$('#openmobilebuildingsnav').click(function(e) {
			e.preventDefault();
			if ($('#mobilenavbar').is(':hidden')){
				$('#mobilenavbar ul > li').hide();
				$('#mobilenavbar .steelbuildingsmenu').show();
				$('#mobilenavbar .steelbuildingsmenu ul > li').show();
				$('#mobilenavbar').show();
				opensubmenu = true;
			} else {
				if (!opensubmenu){
					$('#mobilenavbar ul > li').hide();
					$('#mobilenavbar .steelbuildingsmenu').show();
					$('#mobilenavbar .steelbuildingsmenu ul > li').show();
					opensubmenu = true;
				} else{
					$('#mobilenavbar').hide();
					$('#mobilenavbar ul > li').show();
					opensubmenu = false;
				}
			}
		});
		
		$('#openmobilegallerynav').click(function(e) {
			e.preventDefault();
			if ($('#mobilenavbar').is(':hidden')){
				$('#mobilenavbar ul > li').hide();
				$('#mobilenavbar .gallerymenu').show();
				$('#mobilenavbar .gallerymenu ul > li').show();
				$('#mobilenavbar .gallerymenu .gallerysubmenu').show();
				$('#mobilenavbar').show();
				opensubmenu = true;
			} else {
				if (!opensubmenu){
					$('#mobilenavbar ul > li').hide();
					$('#mobilenavbar .gallerymenu').show();
					$('#mobilenavbar .gallerymenu ul > li').show();
					$('#mobilenavbar .gallerymenu .gallerysubmenu').show();
					opensubmenu = true;
				} else{
					$('#mobilenavbar').hide();
					$('#mobilenavbar ul > li').show();
					$('#mobilenavbar .gallerymenu .gallerysubmenu').hide();
					opensubmenu = false;
				}
			}
		});
		
	}
	
	$('input, textarea').placeholder();
	
	if ($('#buildingtypepage').length){
	
		var videofrm = $('#videoframe');
		var defw = videofrm.width();
		var defh = videofrm.height();	
		
		if (!Modernizr.video){
			$("video").removeClass("whiteborder greyoutline");
			
			if (!$.browser.flash){
				
				$("object").removeClass("whiteborder greyoutline");
				$("video object img").addClass("whiteborder greyoutline");
			}
		} else {
			$('#videoframe .bigcontrol').show();
		}
		
		$('#typequote input[title]').qtip({
			show : 'focus',
			hide : 'blur',
			style: {
			  classes: 'ui-tooltip-dark ui-tooltip-shadow'
		   },
		   position: {
			  my: 'bottom center', 
			  at: 'top center'
		   }
		});
		
		$(".typegal li.img a").fancybox(fancyoptions);
		
		$('#videoframe').click( function(){
			$('#typevideo').get(0).play();
			$('#videoframe').unbind('click');
			$('#videoframe .bigcontrol').hide();
		});
		
		$('#typequote').validate({
			focusInvalid : false,
			errorElement: "div"
		});
		
	}
	
	if ($('#quote').length){
	
		$("#quote").validate();
	
		if ($('#special').val()!="0"){
			$('#buildinguse').focus();
			calcroofpeak();
		} else {
			$('#width').focus();
		}
		
		/*$('#quote .quotesubmit').html("<a href=\"#\" id=\"sendform\"><img src=\""+dir+"/img/page/btn-submit-darkorange.jpg\" alt=\"Submit form\" /></a>");*/
		
		if ($('#sendform').length){
			$('#sendform').click(function(e){
				e.preventDefault();
				$('#quote').submit();
			})
		}
		
		$('#roofpitch,#width,#height').bind('change blur',function(e){
			calcroofpeak();
		});
		
		$('#zipcode,#sidezipcode').blur(function(){
			if (($('#sitecountry').val() == "US" || $('#sitecountry').val() == "CA") && $('#zipcode').val()!=""){
				var country = $('#sitecountry').val();
				var zipcode = $(this).val();
				$.ajax({
					url : dir+"/lib/request.php",
					type : "POST",
					data : "r=z2&zipcode="+zipcode+"&country="+country,
					success : function(resp){

						if (resp.length>0){
						
							resp = resp[0];
							var resparray = new Array();
							resparray.push(resp.city);
							if (resp.county != ""){
								resparray.push(resp.county+' county');
								$('#sitecounty').val(resp.county);
							}
							if (resp.city != resp.state){
								resparray.push(resp.state);	
							}
							if (country == "US" || country == "CA"){
								$('#sitestate').val(resp.abbrev);
							}
							$('#place').html("<em>Location:</em> " + resparray.join(", "));
							$('#customstate').val(resp.state);
							
						} else {
							$('#customstate').val("");
							$('#sitestate').val("");
							$('#sitecounty').val("");
							$('#place').html("");
							
						}
					}
				});
			} else {
				$('#place').html("");
			}
		});
		
	}
	
	if ($('#sidegallery').length){
		$('#sidegallery ul li:nth-child(2n+1)').addClass('odd');
	}
	
	if ($('#testimonial').length){
		$("#smallrefpics a").fancybox(fancyoptions);
	}
	
	if ($('#special2').length){
		$(".smallspecpics a").fancybox(fancyoptions);
	}
	
	if ($('#typegal').length){
		$("#typegal li a").fancybox(fancyoptions);
	}
	
	if ($('#smallshed').length){
		$(".shed .left a").fancybox(fancyoptions);
		
		$('#ssq1').click(function(e){
			$('#shedtype').html('on a '+$('#ss1title').text());
			$('#shedquotetype').val($('#ss1title').text());
		});
		
		$('#ssq2').click(function(e){
			$('#shedtype').html('on a '+$('#ss2title').text());
			$('#shedquotetype').val($('#ss2title').text());
		});
		
		$('.ssq').fancybox({
			overlayColor : '#000',
			transitionIn : 'elastic',
			transitionOut : 'elastic',
			padding : 20,
			onComplete : function(){
				$('#name').focus();
			}
		});
		
		$("#shedquote").validate();
		
		$('#closeformpop').click(function(e){
			e.preventDefault();
			parent.$.fancybox.close();
			$('#shedquote')[0].reset();
			$('#shedquote').data('validator').resetForm();
		});
		
		$('#submitshedquote').click(function(e){
			e.preventDefault();
			validator = $("#shedquote").validate();
			if (validator.form() == true){
				$.ajax({
					url: dir+"/lib/request.php",
					type : "POST",
					data : "r=ssq&"+$('#shedquote').serialize(),
					beforeSend: function(){
						//$('#contactloader').show();
						$('#submitshedquote').attr("disabled", "true");
					},
					success: function(){
						//$('#contactloader').hide();
						$('#submitshedquote').removeAttr("disabled");
						_gaq.push(['_trackPageview', "/thankyou?t=7"]);
						alert("Your request was sent successfully. Thank you!");
						$('#shedquote').data('validator').resetForm();
						$('#shedquote')[0].reset();
						parent.$.fancybox.close();
					}
				});
				
			}
		});
		
		$('#zipcode').blur(function(){
			if ($('#zipcode').val()!=""){
				var country = "US";
				var zipcode = $(this).val();
				$.ajax({
					url : dir+"/lib/request.php",
					type : "POST",
					data : "r=z2&zipcode="+zipcode+"&country="+country,
					success : function(resp){

						if (resp.length>0){
						
							resp = resp[0];
							var resparray = new Array();
							resparray.push(resp.city);
							if (resp.county != ""){
								resparray.push(resp.county+' county');
								$('#sitecounty').val(resp.county);
							}
							if (resp.city != resp.state){
								resparray.push(resp.state);	
							}
							if (country == "US" || country == "CA"){
								$('#sitestate').val(resp.abbrev);
							}
							$('#customstate').val(resp.state);
							
						} else {
							$('#customstate').val("");
							$('#sitestate').val("");
							$('#sitecounty').val("");							
						}
					}
				});
			} else {

			}
		});
	}

	if ($('#typequote').length || $('#homequote').length){
		$('#zipcode').blur(function(){
			if ($('#zipcode').val()!=""){
				var country = "US";
				var zipcode = $(this).val();
				$.ajax({
					url : dir+"/lib/request.php",
					type : "POST",
					data : "r=z2&zipcode="+zipcode+"&country=US",
					success : function(resp){

						if (resp.length>0){
						
							resp = resp[0];
							var resparray = new Array();
							resparray.push(resp.city);
							if (resp.county != ""){
								resparray.push(resp.county+' county');
								$('#sitecounty').val(resp.county);
							}
							if (resp.city != resp.state){
								resparray.push(resp.state);	
							}
							if (country == "US" || country == "CA"){
								$('#sitestate').val(resp.abbrev);
							}
							$('#customstate').val(resp.state);
							
						} else {
							$('#customstate').val("");
							$('#sitestate').val("");
							$('#sitecounty').val("");							
						}
					}
				});
			} else {

			}
		});
	}
	
	if ($('#gallery').length){
		$("#gallery .photolist a").fancybox(fancyoptions);
	}
	
	if ($('#contactus').length){
	
		$("#contactmsg").validate();
	
		$('#contactsend').click(function(e){
			e.preventDefault();
			validator = $("#contactmsg").validate();
			if (validator.form() == true){
				$.ajax({
					url: dir+"/lib/request.php",
					type : "POST",
					data : "r=c&"+$('#contactmsg').serialize(),
					beforeSend: function(){
						$('#contactloader').show();
						$('#contactsend').attr("disabled", "true");
					},
					success: function(){
						$('#contactloader').hide();
						$('#contactsend').removeAttr("disabled");
						$('#contactmsg')[0].reset();
						_gaq.push(['_trackPageview', "/thankyou?t=4"]);
						alert("Your message was sent successfully. Thank you!");
						$('#contactmsg').data('validator').resetForm();
					}
				});
				
			}
		});
		
	}
	
	if ($('#export').length){
	
		$("#exportform").validate();
	
		$('#exportsend').click(function(e){
			e.preventDefault();
			validator = $("#exportform").validate();
			if (validator.form() == true){
				$.ajax({
					url: dir+"/lib/request.php",
					type : "POST",
					data : "r=e&"+$('#exportform').serialize(),
					beforeSend: function(){
						$('#exportloader').show();
						$('#exportsend').attr("disabled", "true");
					},
					success: function(){
						$('#exportloader').hide();
						$('#exportsend').removeAttr("disabled");
						$('#exportform')[0].reset();
						_gaq.push(['_trackPageview', "/thankyou?t=4"]);
						alert("Your message was sent successfully. Thank you!");
						$('#exportform').data('validator').resetForm();
					}
				});
				
			}
		});
		
	}
	
	if ($('#green-buildings').length){
		$("#greenquote").validate({
			errorLabelContainer: $("#greenquote div.quoteerror"),
			focusInvalid: false
		});
	}
	
	$('.privacypolicylink').click(function(e){
		e.preventDefault();
		window.open(dir+'/privacy-policy?sa','','scrollbars=yes,menubar=no,height=400,width=600,resizable=yes,toolbar=no,location=no,status=no');
	});
	
	//event tracking
	
	/*$('#nav a,#menuquote').hover(function(e){
		_gaq.push(['_trackEvent', 'Navigation menu', e.target]);
	});*/
	
	$('#menuquote').click(function(e){
		_gaq.push(['_trackEvent', 'Quote menu click', 'Main green on top']);
	});
	
	$('#steelbuildingsfreequote').click(function(e){
		_gaq.push(['_trackEvent', 'Quote menu click', 'In steel buildings tab']);
	});
	
	$('form input').focus(function(e){
		if (!focuscounted){
			var pri = e.target.form.id;
			if (e.target.form.className != ""){
				var sec = e.target.form.className;
			} else {
				var sec = 'page - ' + $('body')[0].id;
			}
			_gaq.push(['_trackEvent', 'Form focus', pri, sec]);
			focuscounted = true;
		}
		
		_gaq.push(['_trackEvent', 'Input focus', pri, e.target.id]);
		
	});

	if ($('#buildingspecialquote').length){
		
		$("#specialquote").validate({
			submitHandler : function(form){
				$.ajax({
					url : dir+'/lib/request.php',
					data : 'r=specq&'+$('#specialquote').serialize(),
					type : 'POST',
					beforeSend : function(){
						$('#buildingspecialquote .overlay').animate({
							'opacity' : 'show'
						},'fast');
					},
					success : function(resp){
						$('.answer').show();
						$('.answer').html(resp);
						$('#buildingspecialquote .overlay').animate({
							'opacity' : 'hide'
						},'fast');
						_gaq.push(['_trackPageview', "/thankyou?t=2"]);
						gapi.plusone.go("gplusbtn");
					}
				});
			}
		});

		$('#requestspecialquote').fancybox({
			overlayColor : '#000',
			transitionIn : 'elastic',
			transitionOut : 'elastic',
			padding : 20,
			onComplete : function(){
				$('#name').focus();
			},
			onClosed : function(){
				$('#specialquote')[0].reset();
				$('#specialquote').data('validator').resetForm();
				$('#buildingspecialquote .overlay').hide();
				$('.answer').html("").hide();
			}
		});

		$('#zipcode').blur(function(){
			if ($('#zipcode').val()!=""){
				var country = "US";
				var zipcode = $(this).val();
				$.ajax({
					url : dir+"/lib/request.php",
					type : "POST",
					data : "r=z2&zipcode="+zipcode+"&country=US",
					success : function(resp){

						if (resp.length>0){
						
							resp = resp[0];
							var resparray = new Array();
							resparray.push(resp.city);
							if (resp.county != ""){
								resparray.push(resp.county+' county');
								$('#sitecounty').val(resp.county);
							}
							if (resp.city != resp.state){
								resparray.push(resp.state);	
							}
							if (country == "US" || country == "CA"){
								$('#sitestate').val(resp.abbrev);
							}
							$('#customstate').val(resp.state);
							
						} else {
							$('#customstate').val("");
							$('#sitestate').val("");
							$('#sitecounty').val("");							
						}
					}
				});
			} else {

			}
		});

		$('.closespecialformpop').live('click',function(e){
			e.preventDefault();
			parent.$.fancybox.close();
		});

		$('#specialstateselect').change(function(e){
			$.ajax({
				url: dir+"/lib/request.php",
				type : "POST",
				data : "r=s&bid="+$('#buildingid').val()+"&sid="+$('#specialstateselect').val(),
				success: function(resp){

					if (resp.length>0){
						
						$('#newprice,#oldprice').animate({
							marginLeft: "-500px"
						},300,function(){
							$('#selectone').hide();
							$('#priceoverlay').hide();

							$('#newprice').html(resp.specialprice);
							$('#oldprice').html("Was "+resp.regularprice);
							
							$('#newprice,#oldprice').animate({
								marginLeft:"0px"
							},300,function(){
								
							});
						});

						$('#buildingidtext').html(resp.stateid+resp.buildingid);
						$('#stateid').val(resp.stateid);
						$('#buildingcodetext').html(resp.buildingcode);
						$('#windloadtext').html(resp.windload);
						$('#liveloadtext').html(resp.liveload);
						$('#snowloadtext').html(resp.snowload);
						$('#exposuretext').html(resp.exposure);
						$('#formbuildingcodes').html(' - ' + resp.statename + ' building codes');
						$('#formspecialprice').html('for '+resp.specialprice);
						$('#specialprice').val(resp.sprice);

						
					} else {
						$('#customstate').val("");
						$('#sitestate').val("");
						$('#sitecounty').val("");
						$('#place').html("");

						$('#buildingidtext').html("");
						$('#buildingcodetext').html("");
						$('#windloadtext').html("");
						$('#snowloadtext').html("");
						$('#exposuretext').html("");
						$('#formbuildingcodes').html("");
						$('#formspecialprice').html("");
						$('#specialprice').val("");
						
					}
				}
			});
		});

	}
	
});
