//##############################################################################
//##  DOM																	  ##
//##############################################################################
$(document).ready( externalLinks );

/*###########################################
##  Legacy Transparency					   ##
###########################################*/
$(document).ready(function() { $('img[src$=png]').addClass('transparent'); });

/*###########################################
##  Cufón Init							   ##
###########################################*/
Cufon.replace('h1', { fontFamily: 'Myriad Pro', fontWeight: '400', hover: true }); //Regular
Cufon.replace('h2, #bodyContent b > a, #bodyColumns .copy b > a, #footer b > a', { fontFamily: 'Myriad Pro', fontWeight: '900', hover: true }); //Black SemiCondensed
Cufon.replace('h3, h4, .content .meeting dt, .cpage li a', { fontFamily: 'Myriad Pro', fontWeight: '700', hover: true }); //Bold Condensed

/*###########################################
##  Content Sliders						   ##
###########################################*/
/* Options */
var __global_sliderObject = '#slides';
var __global_pagerObject = '#sliderNav';

/* Controls */
$(function() {
  if ($(__global_sliderObject).exists()) { //main promotion slider
    $(__global_sliderObject)
      .cycle({ 
        fx:		'fade', 
        timeout:	6000, //how long to wait between slides
	    speed:	600, //how long transition takes
	    pause:	1, //stop on mouseover
        pager:	__global_pagerObject
    });
  }
});

$(document).ready(function() {
  var __global_toggleObject = '.toggleLink';
  var __global_contentObject = '#byTheNumbers';
  if ($(__global_contentObject).exists()) {
    /* Options */
    var showText='Click Here to read more...'; //Link text for when it is hidden
    var hideText='Click Here to hide...'; //Link text for when it is visible
    var is_visible = false;
  
    //create dynamic toggle anchor
    $('<p><a href="#" title="'+showText+'" class="toggleLink">'+showText+'</a></p>').insertBefore(__global_contentObject); //content prepended "show"
  
    /* Controls */
    $(__global_contentObject).hide(); //hide it initially on page load
    $(__global_toggleObject).click(function() { //attach click event to show/hide link
      is_visible = !is_visible; //flip-flop visibility setting
	  $(this).html( (!is_visible) ? showText : hideText); //replace anchor text accordingly
	  $(__global_contentObject).slideToggle(); //toggle content
	  return false; //kill any link destination
    });
  }
});

/*###########################################
##  Breadcrumbs Styling					   ##
###########################################*/
$(document).ready(function() {
  var __global_pathObject = '#breadcrumbs';
  if ($(__global_pathObject).exists()) {
    $(__global_pathObject+' a:first-child').addClass('home');
    $(__global_pathObject+' a:last-child').addClass('active');
  }
});

/*###########################################
##  Link Styling						   ##
###########################################*/
$(document).ready(function() {
  $('a[href$=pdf]').addClass('pdf'); //PDF
});

/*###########################################
##  Charts & Graphs						   ##
###########################################*/
/* Load JS include */
jQuery(function($) {
  if(!$('.pieChart').length||$('script[src*=gchart]').length)
    return loadCharts();
  var s = document.createElement('script');
  $.ajaxSetup({cache:true});
  $.getScript('/public/js/jquery.gchart.pack.js', loadCharts);
  $.ajaxSetup({cache:false});
});

/* Create Charts */
function loadCharts() {
  var gChartLoaded = (jQuery().gchart)? true : false;
  if (gChartLoaded) {
  	var __global_chartObjects = $('.pieChart');
  	var __global_chartOptions = [
  	{
      type: 'pie',
  		name: 'milesOfPublicRoadways',
  		data: [
			$.gchart.series('State Highways (16%)', [16,69,15], 0, 100),
			$.gchart.series('County Roads (69%)', [16,69,15], 0, 100),
			$.gchart.series('City Streets (15%)', [16,69,15], 0, 100)
		],
		legend: 'bottomVertical',
		css: {
			width		: '350px',
			height		: '300px',
			float		: 'left'
		}
  	},
  	{
    type: 'pie',
		name: 'trafficLoad',
  		data: [
			$.gchart.series('State Highways (76%)', [76,9,15], 0, 100),
			$.gchart.series('County Roads (9%)', [76,9,15], 0, 100),
			$.gchart.series('City Streets (15%)', [76,9,15], 0, 100)
		],
		legend: 'bottomVertical',
		css: {
			width		: '350px',
			height		: '300px',
			float		: 'left'
		}
  	},
  	{
    type: 'pie',
		name: 'totalRevenuesByCategory',
  		data: [
			$.gchart.series('Motor Fuel Taxes', [426.6,112.1,42.3], 0, 590.6),
			$.gchart.series('User Fees', [426.6,112.1,42.3], 0, 590.6),
			//$.gchart.series('Natural Gas Severance Taxes', [426.6,112.1,9.6,42.3], 0, 590.6),
			$.gchart.series('Miscellaneous Revenues', [426.6,112.1,42.3], 0, 590.6)
		],
		legend: 'right',
		css: {
			width		: '600px',
			height		: '350px'
		}
  	},
  	{
    type: 'pie',
		name: 'totalRevenueToGovernments',
  		data: [
			$.gchart.series('State', [407.1,82], 0, 490),
			$.gchart.series('Counties/Cities', [407.1,82], 0, 490)
		],
		legend: 'bottomVertical',
		css: {
			width		: '600px',
			height		: '350px'
		}
  	}];
  	$.each(__global_chartObjects, function() { //grab all items marked as a pieChart
      var target = $(this).attr('id');
      $.each(__global_chartOptions, function(elm, obj) { //match those on the page to data
      	if (obj.name == target) { //found one, process
          if (obj.css) $('#'+target).css(obj.css); //set custom chart CSS if it exists
          $('#'+target).gchart({ type: obj.type, axes: obj.axes, series: obj.data, legend: obj.legend }); //build chart
      	}
      });
  	});
  }

};

//##############################################################################
//##############################################################################
//##  Google Maps Controls													  ##
//##############################################################################
(function($) {

	function meetingMap(lat,lng,detailUrl) {
		this.pt = new google.maps.LatLng(lat, lng);
		this.detailUrl = detailUrl;
		this.map = null;
	}
	meetingMap.getDirections = function(meeting, request, panel) {
		if (!meetingMap.dirService) meetingMap.dirService =  new google.maps.DirectionsService();
		var dir =  new google.maps.DirectionsRenderer();
		dir.setMap(meeting.map);
		dir.setPanel(panel);
		meetingMap.dirService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				dir.setDirections(response);
			}
		});
	}
	meetingMap.prototype = {
		init: function(elId,options) {
			var el = $(elId);
			el.data('map', this);
			var p = el.parent();
			this.map = new google.maps.Map(el.get(0),$.extend({
				zoom: 16,
				center: this.pt,
				mapTypeId: google.maps.MapTypeId.HYBRID
			  },options)
			);
			var inpId = el.attr('id')+'fromLoc';
			p.append($('<div class="getdir"><form><label for="'+inpId+'">Get directions from</label> <input type="text" id="'+inpId+'" maxlength="100" /> <button>Go</button></form></div>'));
			p.parent().append($('<div class="directions" />'));
			p.find('button').click(function(e) { e.preventDefault(); $(elId).data('map').getDirections($('#'+inpId).val(),$(elId).parent().parent().find('.directions').get(0)); return false; })
			p.find('form').submit(function() { p.find('button').click(); return false; });
			return this.addMarker();
		},
		addMarker: function() {
			this.marker = new google.maps.Marker({
				position: this.pt,
				title: 'Meeting location',
				map: this.map,
				draggable: false
			});
			return this;
		},
		getDirections: function(to, contId) {
			var req = { destination: this.pt, origin: to +',USA', travelMode: google.maps.DirectionsTravelMode.DRIVING };
			meetingMap.getDirections(this, req, contId);
			return this;
		}
	};
	window['meetingMap'] = meetingMap;
})(jQuery);

//##############################################################################
//##############################################################################
//##  Photo/Video Gallery            									      ##
//##############################################################################
if($('#gallery_loading').length > 0)
jQuery(function($) {
  $('#gallery_loading').hide();
  $('#gallery_container').slideDown('slow');
  $('.colorbox').click(function(e) {
  	var $this = $(this);
  	var sel = $this.attr('href')+'>a:not(.loaded)';
  	$(sel).each(function() { $('<img src="'+$(this).attr('thumb')+'" width="75" height="75" />').appendTo(this); })
  		.addClass('loaded')
  		.colorbox({slideshow: true,slideshowSpeed:6000, onCleanup: function() { $(this).parent().hide(); }});
  	$this.colorbox({
  		inline: true, initialWidth: 300, initialHeight: 300, href: $(this).attr('href'), title: $this.attr('title'),
	  	maxHeight:'50%', open: true,
	  	onLoad: function() { $($(this).attr('href')).show(); },
	  	onCleanup: function() {
	  		$($(this).attr('href')).hide();
	  	}
	});
	return false;
  });
});
jQuery(function($) {
  $('.cYouTube').colorbox({iframe:true, innerWidth:425, innerHeight:344}).each(function() { this.href = $(this).attr('vsrc'); });
});


//##############################################################################
//##############################################################################
//##  User Handler Functions												  ##
//##############################################################################
function externalLinks() {
  var exs = $('a[rel=external]');
  $.each(exs, function() {
    if ($(this).attr('href')) { $(this).attr('target','_blank'); }
  });
}
//##############################################################################
//##############################################################################
//##  Globals																  ##
//##############################################################################
jQuery.fn.exists = function(){return jQuery(this).length>0;}
//##############################################################################
//##############################################################################

