/**** Add application wide javascripts below this point  ******/
var testimonial_items;
var current_testimonial=0;
var testimonial_pause=0;
var init_height;
var init_fontsize;

$(document).ready(function() {
  var initend = ((  parseInt($("#range_end").val(),0) - 1965)/5) *11;
	var initstart = ((  parseInt($("#range_start").val(),0) - 1965)/5) *11;
  $('#example3').slider({ steps: 9, range: true});
	$('#example3').css('display', 'block');
	$('#range_start, #range_end, label[for=range_start],label[for=range_end]').css('display', 'none');
	$("#example3").slider("moveTo", initstart, 0);
	$("#example3").slider("moveTo", initend, 1);

	if($('#birmingham_google_map').length){
		show_address('55 Temple Row, birmingham, UK');
	}
	if($('#full_google_map').length){
		show_map('55 Temple Row, birmingham, UK');
	}
	$("form.validate").validate();
	$("#content_navigation ul li").addClass("unselected");
	$("#content_navigation ul li.selected").removeClass("unselected");
  $('#example3').bind("slidechange", function(e,ui) { 
    var range_start = (Math.floor( ($("#example3").slider("value", 0)/11*5)+1965) );
    var range_end = (Math.floor( ($("#example3").slider("value", 1)/11*5)+1965) );
    $("#range_start").val(range_start);
    $("#range_end").val(range_end);
  });
  
  init_height = $("#testimonials p").eq(0).height();
  $("#testimonials").css("height", init_height+10);
  testimonial_items = $("#testimonials p").hide().filter(":eq(0)").show().add("#testimonials p").size();
	setInterval(auto_testimonial,8000);
	$("#testimonials p").hover(function(){testimonial_pase=1;}, function(){testimonial_pause=0;});
	
	$("#acc_link1").click(function(){
	  $("body").css("font-size", "13px");
    $.cookie('accessibility', 'acc_link1',{path: '/'});
	  return false;
	});
	$("#acc_link2").click(function(){
	  $("body").css("font-size", "15px");
    $.cookie('accessibility', 'acc_link2',{path: '/'});
	  return false;
	});
	$("#acc_link3").click(function(){
	  $("body").css("font-size", "19px");
    $.cookie('accessibility', 'acc_link3',{path: '/'});
	  return false;
	});
	
	$.getScript("http://stphilips.oneblackbear.com/javascripts/jquery.cookie.js",function(){
	  var access = $.cookie("accessibility");
	  $("#"+access).click();
	});
	
	$(".practice_area input, .sub_practice_area input").click(function(){
	  $(".barrister_select input").removeAttr("checked");
	  $(this).parents("form").submit();
	});
  $(".barrister_select input").click(function(){$(this).parents("form").submit();});
	$("#sel_all").click(function(){if($(this).attr("checked")) $(".barrister_selector").attr("checked", true); else $(".barrister_selector").removeAttr("checked"); $(this).parents("form").submit(); });
	$("#sel_none").click(function(){if($(this).attr("checked")) $(".barrister_selector").removeAttr("checked"); $(this).parents("form").submit(); });
  //$(".practice_area input:not(:checked)").parent().find("img").fadeTo("fast",0.2);
  
  subarea(".commercial_image", ".commercial_sub");
  subarea(".crime_image", ".crime_sub");
  subarea(".pi_image", ".pi_sub");
  subarea(".employment_image", ".employment_sub");
  subarea(".regulatory_image", ".regulatory_sub");
  subarea(".family_image", ".family_sub");
  subarea(".ip_image", ".ip_sub");
  subarea(".property_image", ".property_sub");
  subarea(".construction_image", ".construction_sub");
  
  subarea(".adr_image", ".adr_sub");
  subarea(".public_image", ".public_sub");
  subarea(".insolvency_image", ".insolvency_sub");

  $(".sub_practice_area input:checked").parents(".sub_practice_area").show();
});

/******************** Inject Analytics Code ****************************/

$(document).ready(function() {

  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");  
  $.getScript(gaJsHost + "google-analytics.com/ga.js", function(){  
    try {  
      var pageTracker = _gat._getTracker("UA-12666047-1");  
      pageTracker._trackPageview();  
      } catch(err) {}  

      var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;


      $('a').each(function(){  
        var href = $(this).attr('href');  

        if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){  
          $(this).click(function() {  
            var extLink = href.replace(/^https?\:\/\//i, '');  
            pageTracker._trackEvent('External', 'Click', extLink);  
          });  
        } else if(href.match(/^mailto\:/i)){  
          $(this).click(function() {  
            var mailLink = href.replace(/^mailto\:/i, '');  
            pageTracker._trackEvent('Email', 'Click', mailLink);  
          });  
        } else if (href.match(filetypes)){  
          $(this).click(function() {  
            var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;  
            var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');  
            pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);  
          });  
        }  
      });

  });


});



/************************************************************************/


function subarea(selectr, areasel) {
  $(selectr).click(function(){
    $(".sub_practice_area").not(areasel).hide();
    $(areasel).toggle();
    return false;
  });
}

function auto_testimonial() {
  
  var nowheight;
  if (testimonial_pause==0){ 
    $("#testimonials p:eq("+current_testimonial+")").fadeOut("slow",function(){
      $(this).hide();
      
      current_testimonial = ++current_testimonial%testimonial_items;
      $("#testimonials p:eq("+current_testimonial+")").css({"font-size":"11px", "opacity":0}).show();
      nowheight = $("#testimonials p:eq("+current_testimonial+")").height();
      if(nowheight > 60 ) {
        $("#testimonials p:eq("+current_testimonial+")").css("fontSize", "11px").fadeTo("slow",1);
      } else if(nowheight > 35) {
        $("#testimonials p:eq("+current_testimonial+")").css("fontSize", "15px").fadeTo("slow",1);
      } else if(nowheight <= 35) {
        $("#testimonials p:eq("+current_testimonial+")").css("fontSize", "20px").fadeTo("slow",1);
      }
      
    });
    
    
    
    
  }
}


function show_address(address) {
	if(GBrowserIsCompatible()) {
		googlemap = new GMap2(document.getElementById('birmingham_google_map'));
		geocoder = new GClientGeocoder();
	}
	if(geocoder){
		geocoder.getLatLng(address,
											function(point) {
												if (!point) alert(address + " not found");
												else {
													googlemap.setCenter(point, 15);
													var marker = new GMarker(point);
													googlemap.addOverlay(marker);
													googlemap.addControl(new GSmallZoomControl());
												}
											});
	}
}

var googlemap;
function show_map(address) {
	if(GBrowserIsCompatible()) {
		googlemap = new GMap2(document.getElementById('full_google_map'));
		geocoder = new GClientGeocoder();
	}
	if(geocoder){
		geocoder.getLatLng(address,
		function(point) {
			if (!point) alert(address + " not found");
			else {
				googlemap.setCenter(point, 15);
				var marker = new GMarker(point);
				googlemap.addOverlay(marker);
        var mapControl = new GMapTypeControl();
        googlemap.addControl(mapControl);
        googlemap.addControl(new GLargeMapControl());
        gdir = new GDirections(googlemap, document.getElementById("directions"));
        
        /***** Parking Spots *******/
        var parkingIcon = new GIcon(G_DEFAULT_ICON);
        parkingIcon.image = "http://stphilips.oneblackbear.com/images/parking.png";
        parkingIcon.shadow = "http://stphilips.oneblackbear.com/images/parkingshadow.png";
        parkingIcon.iconSize = new GSize(25, 33);
        var parkmarker1 = new GMarker(new GLatLng("52.483283","-1.899251"), parkingIcon);
        var parkmarker2 = new GMarker(new GLatLng("52.48206","-1.893103"), parkingIcon);
        googlemap.addOverlay(parkmarker1);
        googlemap.addOverlay(parkmarker2);
			}
		});
	}
}

function setDirections(fromAddress, toAddress) {
  gdir.load("from: " + fromAddress + ", UK to: " + toAddress);
  return false;
}