var cleanMarkup=function(){$('#sub-inner :first-child img').css('margin-top','-16px');}
var navActive=function(){var segments=document.location.pathname.split('/');$("nav.main a").each(function(){if($(this).attr('href').match((segments[1]))&&segments[1]!=''){$(this).parent('li').addClass('active');}});}
var calloutAnimation=function(){$('.callout-animation').hover(function(){$(this).find('.excerpt').animate({top:'0'},'1000');},function(){$(this).find('.excerpt').animate({top:'144px'},'1000');});}
var clearEmptyHeaders=function(){$('.heading').show();$('.heading').each(function(){var all_hidden=true;$('[data-location-state="'+$(this).attr('data-heading-state')+'"]').each(function(){if($(this).is(":visible")){all_hidden=false;}});if(all_hidden){$(this).hide();}});}
filters=function(){var filter=function(){$('.filtered').hide();$('.filters :checked').each(function(checked_box_index,checked_box){$('.filtered.'+$(checked_box).val()).show();});if($('.filters input[value!="all"]').not(':checked').size()==0){$('input[value="all"]').attr('checked',true);}
else{$('input[value="all"]').attr('checked',false);}
$('#total-results').html($('.laboratories .secondary tr').not(':hidden').not('.heading').size()-1);clearEmptyHeaders();}
filter();$('.filters input[value="all"]').change(function(){if($('.filters input[value="all"]').attr('checked')){$('.filters input[type="checkbox"][name!=]').attr('checked',true);}
else{$('.filters input[type="checkbox"]').attr('checked',false);}});$('.filters input[type="checkbox"]').change(function(){filter();});}
var labTabs=function(){var $tabs=$("#tabs").tabs();$.address.change(function(event){$("#tabs").tabs("select",window.location.hash);});$("#tabs").bind("tabsselect",function(event,ui){window.location.hash=ui.tab.hash;});}
var articlesSearch=function(){$('.articles-search').submit(function(){var url=$('form.sub.search').attr('action');var data=new Object();$('#main').html('<img src="/images/layout/ajax-loader.gif" style="display:block; margin: 100px auto" />');$('form.sub.search input[type!="submit"]').each(function(index,element){name=$(element).attr('name');value=$(element).val();data[name]=value;});data['keywords']=$.trim(data['keywords']);if(data['keywords']==''){$.ajax({type:"POST",url:'/technical-articles',success:function(data){$('#main').html($(data).find("#main").html());$('#main').show();filters();}});}
else{$.ajax({type:"POST",url:url,data:data,success:function(data){$('#main').html($(data).find("#content"));$('#main').show();filters();}});}
return false;});}
map=function(){var infowindow;var mapOptions={mapTypeId:google.maps.MapTypeId.TERRAIN,scrollwheel:false};var map=new google.maps.Map(document.getElementById("map-canvas"),mapOptions);var markersArray=[];var locations=[];var boxes=".laboratories .filters input[type=checkbox]";var bounds=map.getBounds();var initializeGoogleMap=function(){setInitialZoom();google.maps.event.addListener(map,'click',function(){if(infowindow)infowindow.close();});$.getJSON('/laboratories/get-json-locations',function(data){locations=data;for(i=0;i<locations.length;i++){var point=new google.maps.LatLng(locations[i].location_latitude,locations[i].location_longitude);console.log(locations[i]);var marker=new google.maps.Marker({position:point,title:locations[i].title,map:map});marker.categories=locations[i].location_industries;marker.url_title=locations[i].url_title;markersArray.push(marker);marker.infowindow_content="<div class='infowindow'><h5><a href='"+locations[i].page_url+"'>"+locations[i].title+"</a></h5><p>"+locations[i].location_address_1+"<br />"+locations[i].location_city+", "+locations[i].location_state+" "+locations[i].location_zip_code+"</p><p>"+locations[i].location_phone+"</p><p><a href='"+locations[i].page_url+"' class='button'>Look Closer</a></p></div>";}
infowindow=new google.maps.InfoWindow();for(var i=0;i<markersArray.length;i++){var marker=markersArray[i];google.maps.event.addListener(marker,'click',function(){infowindow.setContent(this.infowindow_content);infowindow.open(map,this);});}
hideShowMarkers();});}
var getViewportMarkers=function(){var bounds=map.getBounds();var results=[];$.each(markersArray,function(index,marker){marker_latlng=marker.getPosition();if(bounds.contains(marker_latlng)){results.push(marker);}});return results;}
var hideShowMarkers=function(){var filter_categories=new Array();var marker_categories=new Array();var show_location=false;$(boxes+':checked ').each(function(){filter_categories.push($(this).attr('value'));});for(var i=0;i<markersArray.length;i++){var show_location=false;var marker_categories=markersArray[i].categories.split(' ');$.each(marker_categories,function(index,value){if($.inArray(value,filter_categories)!=-1){show_location=true;}});if(show_location){markersArray[i].setVisible(true);}
else{markersArray[i].setVisible(false);}}}
var getDistance=function(lat1,lon1,lat2,lon2){var R=6371;var dLat=(lat2-lat1)*Math.PI/180;var dLon=(lon2-lon1)*Math.PI/180;var a=Math.sin(dLat/2)*Math.sin(dLat/2)+Math.cos(lat1*Math.PI/180)*Math.cos(lat2*Math.PI/180)*Math.sin(dLon/2)*Math.sin(dLon/2);var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));var d=R*c;return d;}
var sortByDistance=function(a,b){var x=a.distance;var y=b.distance;return((x<y)?-1:((x>y)?1:0));}
var searchMap=function(address){var geocoder=new google.maps.Geocoder();if(geocoder){geocoder.geocode({'address':address},function(results,status){if(status==google.maps.GeocoderStatus.OK){for(var i=0;i<locations.length;i++){locations[i].distance=getDistance(results[0].geometry.location.lat(),results[0].geometry.location.lng(),locations[i].location_latitude,locations[i].location_longitude);}
locations.sort(sortByDistance);var closestLocation=new google.maps.LatLng(locations[0].location_latitude,locations[0].location_longitude);map.setCenter(closestLocation);map.setZoom(9);$('.filtered').hide();$.each(getViewportMarkers(),function(index,marker){$('.'+marker.url_title).show();});}
else{setInitialZoom();}
clearEmptyHeaders();$('#total-results').html($('.laboratories .secondary tr').not(':hidden').not('.heading').size()-1);});}}
var setInitialZoom=function(){var centerCoords=new google.maps.LatLng(38,-94);map.setCenter(centerCoords);map.setZoom(4);}
initializeGoogleMap();$(boxes).click(function(){hideShowMarkers();});$('.laboratories .search').submit(function(){var address=$('.sub.search input[type="text"]').val();searchMap(address);return false;});}
$(document).ready(function(){$('figure.left, figure.right').imageWidth();$('.carousel').scrollable({"circular":true,"speed":1000});$('.callout-animation .video a, #main-video').colorbox({iframe:true,innerWidth:800,innerHeight:400});$(".autocomplete input[name='keywords']").autocomplete("/index.php/testing-services/search-ajax/",{cacheLength:0,minChars:3}).result(function(event,data,formatted){$("#result").html(!data?"No match!":"Selected: "+formatted);});;if($('.laboratories').size()>0)map();articlesSearch();navActive();labTabs();filters();calloutAnimation();cleanMarkup();});
