﻿
$(document).ready(function () {
	initPage();
	loadMap();
});

//$('#ViewLocation').live('pagecreate', function (e) {
//	alert("Got here");
//	loadMap();
//});

function initPage() {
	if (!usingMobileUrl) {
		$('.twocolumnlist').easyListSplitter({
			colNumber: 2 // Insert here the number of columns you want. Consider that the plugin will create the number of cols requested only if there's enough items in the list.
		});

		$('.subnav').easyListSplitter({
			colNumber: 2
		});

		$('.twocolumnlist-always').easyListSplitter({
			colNumber: 2, 
			minElements: 0
		});

		// Detecting the existence of slides... if none, change min-height of page:
		jQuery.fn.exists = function () { return this.length > 0; }
		if (!$("#slides").exists()) {
			$("#article").addClass("maketaller");
		}
	}

	// Throw anything with the css class of .belowsubnav to the div called #belowsubnav
	// $('.belowsubnav').appendTo("#belowsubnav");

	// Google Analytics Event Tracking:
	//if (enableGoogleAnalytics)	// enableGoogleAnalytics variable is defined in GoogleAnalyticsInclude.cshtml
	//	EnableGoogleAnalyticsEventTracking();
}


// Calling this function will track the clicking of external links, file downloads, and mailto clicks...
// from: http://www.blastam.com/blog/index.php/2011/04/how-to-track-downloads-in-google-analytics/
function EnableGoogleAnalyticsEventTracking() {

	var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
	var baseHref = '';
	if (jQuery('base').attr('href') != undefined)
		baseHref = jQuery('base').attr('href');

	jQuery('a').each(function () {
		var href = jQuery(this).attr('href');
		if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
			jQuery(this).click(function () {
				var extLink = href.replace(/^https?\:\/\//i, '');
				_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
				if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
					setTimeout(function () { location.href = href; }, 200);
					return false;
				}
			});
		}
		else if (href && href.match(/^mailto\:/i)) {
			jQuery(this).click(function () {
				var mailLink = href.replace(/^mailto\:/i, '');
				_gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
			});
		}
		else if (href && href.match(filetypes)) {
			jQuery(this).click(function () {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				var filePath = href;
				_gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
				if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
					setTimeout(function () { location.href = baseHref + href; }, 200);
					return false;
				}
			});
		}
	});
}


function MakeAllLinksExternal() {
	for (var i = 0; i < $('a').length; i++) {
		$('a')[i].rel = 'external';
	}
}

function loadMap() {
	var mapId = "map";

	if (document.getElementById(mapId) == null) {
		return;
	}

	var mapsElement = document.getElementById(mapId);
	var coords = mapsElement.innerHTML.split(",");
	if (coords.length != 3) {
		mapsElement.display = "none";
		return;
	}

	var latlng = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
	var myOptions = {
		zoom: parseFloat(coords[2]),
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		disableDefaultUI: true,
		mapTypeControl: true,
		zoomControl: true,
		zoomControlOptions: {
			style: google.maps.ZoomControlStyle.SMALL
		}
	};

	var map = new google.maps.Map(document.getElementById(mapId), myOptions);

	var marker = new google.maps.Marker({
		position: latlng,
		map: map
	});
}
