﻿
//search control
function SubmitOnEnterKey(event, url, targetcontrol) {
	if (event.keyCode == '13') {
		RedirectToSearch($('#' + targetcontrol), url);
	}
}

function RedirectToSearch(searchinputcontrol, url) {
	var keyword = encodeURIComponent(searchinputcontrol.val());
	window.location.replace(url + "?query=" + keyword);
}

// $Id: winlivesearch.js,v 1.1.6.2 2009/06/15 20:09:54 mfb Exp $

$(function () {
	$('.winlivesearch-form-keys').addClass('winlivesearch-blur');
	$('.winlivesearch-form-keys').blur(function () {
		if (!$(this).val()) {
			$(this).addClass('winlivesearch-blur');
		}
	});
	$('.winlivesearch-form-keys').focus(function () {
		$(this).removeClass('winlivesearch-blur');
	});
});


//text resizer

//$(document).ready(function () {
//	// Which div or page element are we resizing?
//	if (text_resize_scope) { // Admin-specified scope takes precedence.
//		if ($('#' + text_resize_scope).length > 0) {
//			var element_to_resize = $('#' + text_resize_scope); // ID specified by admin
//		}
//		else if ($('.' + text_resize_scope).length > 0) {
//			var element_to_resize = $('.' + text_resize_scope); // CLASS specified by admin
//		}
//		else {
//			var element_to_resize = $(text_resize_scope); // It's just a tag specified by admin
//		}
//	}
//	else { // Look for some default scopes that might exist.
//		if ($('DIV.left-corner').length > 0) {
//			var element_to_resize = $('DIV.left-corner'); // Main body div for Garland
//		}
//		else if ($('#content-inner').length > 0) {
//			var element_to_resize = $('#content-inner'); // Main body div for Zen-based themes
//		}
//		else if ($('#squeeze > #content').length > 0) {
//			var element_to_resize = $('#squeeze > #content'); // Main body div for Zen Classic
//		}
//	}
//	// Set the initial font size if necessary
//	if ($.cookie('text_resize') != null) {
//		element_to_resize.css('font-size', parseFloat($.cookie('text_resize')) + 'px');
//		//alert( "Should be size: " + $.cookie('text_resize'));
//	}
//	else {
//		//alert('Couldn\'t find text_resize cookie.');
//	}
//	if (text_resize_line_height_allow) {
//		//alert('line height adjustment allowed! The current line-height is '+parseFloat(element_to_resize.css('line-height'), 10));
//		// Set the initial line height if necessary
//		if ($.cookie('text_resize_line_height') != null) {
//			element_to_resize.css('line-height', parseFloat($.cookie('text_resize_line_height')) + 'px');
//		}
//	}
//	// Changer links will change the text size when clicked
//	$('a.changer').click(function () {
//		// Set the current font size of the specified section as a variable
//		var currentFontSize = parseFloat(element_to_resize.css('font-size'), 10);
//		//alert('currentFontSize = '+currentFontSize);
//		// Set the current line-height
//		var current_line_height = parseFloat(element_to_resize.css('line-height'), 10);
//		//alert('current_line_height = '+current_line_height);
//		// javascript lets us choose which link was clicked, by ID
//		if (this.id == 'text_resize_increase') {
//			var new_font_size = currentFontSize * 1.2;
//			if (text_resize_line_height_allow) { var new_line_height = current_line_height * 1.2; }
//			// Allow resizing as long as font size doesn't go above text_resize_maximum.
//			if (new_font_size <= text_resize_maximum) {
//				$.cookie('text_resize', new_font_size, { path: '/' });
//				if (text_resize_line_height_allow) { $.cookie('text_resize_line_height', new_line_height, { path: '/' }); }
//				var allow_change = true;
//			}
//			else {
//				$.cookie('text_resize', text_resize_maximum, { path: '/' });
//				if (text_resize_line_height_allow) { $.cookie('text_resize_line_height', text_resize_line_height_max, { path: '/' }); }
//				var reset_size_max = true;
//			}
//		}
//		else if (this.id == 'text_resize_decrease') {
//			var new_font_size = currentFontSize * 0.8;
//			if (text_resize_line_height_allow) { var new_line_height = current_line_height * 0.8; }
//			if (new_font_size >= text_resize_minimum) {
//				// Allow resizing as long as font size doesn't go below text_resize_minimum.
//				$.cookie('text_resize', new_font_size, { path: '/' });
//				if (text_resize_line_height_allow) { $.cookie('text_resize_line_height', new_line_height, { path: '/' }); }
//				var allow_change = true;
//			}
//			else {
//				// If it goes below text_resize_minimum, just leave it at text_resize_minimum.
//				$.cookie('text_resize', text_resize_minimum, { path: '/' });
//				if (text_resize_line_height_allow) { $.cookie('text_resize_line_height', text_resize_line_height_min, { path: '/' }); }
//				var reset_size_min = true;
//			}
//		}
//		else if (this.id == 'text_resize_reset') {
//			$.cookie('text_resize', text_resize_minimum, { path: '/' });
//			if (text_resize_line_height_allow) { $.cookie('text_resize_line_height', text_resize_line_height_min, { path: '/' }); }
//			var reset_size_min = true;
//		}
//		// jQuery lets us set the font size value of the main text div
//		if (allow_change == true) {
//			element_to_resize.css('font-size', new_font_size + 'px'); // Add 'px' onto the end, otherwise ems are used as units by default
//			if (text_resize_line_height_allow) { element_to_resize.css('line-height', new_line_height + 'px'); }
//			return false;
//		}
//		else if (reset_size_min == true) {
//			element_to_resize.css('font-size', text_resize_minimum + 'px');
//			if (text_resize_line_height_allow) { element_to_resize.css('line-height', text_resize_line_height_min + 'px'); }
//			return false;
//		}
//		else if (reset_size_max == true) {
//			element_to_resize.css('font-size', text_resize_maximum + 'px');
//			if (text_resize_line_height_allow) { element_to_resize.css('line-height', text_resize_line_height_max + 'px'); }
//			return false;
//		}
//	});
//});

//image swap
function MM_swapImgRestore() { //v3.0
	var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}
function MM_preloadImages() { //v3.0
	var d = document; if (d.images) {
		if (!d.MM_p) d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
	}
}

function MM_findObj(n, d) { //v4.01
	var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

//GeoCodeCalc
var GeoCodeCalc = {};
GeoCodeCalc.EarthRadiusInMiles = 3956.0;
GeoCodeCalc.EarthRadiusInKilometers = 6367.0;
GeoCodeCalc.ToRadian = function (v) { return v * (Math.PI / 180); };
GeoCodeCalc.DiffRadian = function (v1, v2) {
	return GeoCodeCalc.ToRadian(v2) - GeoCodeCalc.ToRadian(v1);
};
GeoCodeCalc.CalcDistance = function (lat1, lng1, lat2, lng2, radius) {
	return radius * 2 * Math.asin(Math.min(1, Math.sqrt((Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(GeoCodeCalc.ToRadian(lat1)) * Math.cos(GeoCodeCalc.ToRadian(lat2)) * Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0)))));
};

//Mapping functions


function SetMapBoundaries(boundingbox) {
	if (boundingbox[0] < top) {
		top = boundingbox[0];
	}

	if (boundingbox[1] < left) {
		left = boundingbox[1];
	}

	if (boundingbox[2] > bottom) {
		bottom = boundingbox[2];
	}

	if (boundingbox[3] > right) {
		right = boundingbox[3];
	}

	var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(top, left), new Microsoft.Maps.Location(bottom, right));
	map.setView({ bounds: viewBoundaries });
}

function GetCurrentUserLocation() {
	var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);
	return geoLocationProvider.getCurrentPosition();
}

function RecalculateMapBoundaries() {
	//global boundaries -- used by SetMapBoundaries
	left = 180;
	top = 90;
	right = -180;
	bottom = -90;

	$.each(locations, function (index, location) {
		if (location.hidden == false) {
			SetMapBoundaries(location.bbox);
		}
	});
}


//FEED AGGREGATOR

/*
* Date Format 1.2.3
* (c) 2007-2009 Steven Levithan <stevenlevithan.com>
* MIT license
*
* Includes enhancements by Scott Trenda <scott.trenda.net>
* and Kris Kowal <cixar.com/~kris.kowal/>
*
* Accepts a date, a mask, or a date and a mask.
* Returns a formatted version of the given date.
* The date defaults to the current date/time.
* The mask defaults to dateFormat.masks.default.
*/

var dateFormat = function () {
	var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date;
		if (isNaN(date)) throw SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var _ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d: d,
				dd: pad(d),
				ddd: dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m: m + 1,
				mm: pad(m + 1),
				mmm: dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy: String(y).slice(2),
				yyyy: y,
				h: H % 12 || 12,
				hh: pad(H % 12 || 12),
				H: H,
				HH: pad(H),
				M: M,
				MM: pad(M),
				s: s,
				ss: pad(s),
				l: pad(L, 3),
				L: pad(L > 99 ? Math.round(L / 10) : L),
				t: H < 12 ? "a" : "p",
				tt: H < 12 ? "am" : "pm",
				T: H < 12 ? "A" : "P",
				TT: H < 12 ? "AM" : "PM",
				Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
} ();

// Some common format strings
dateFormat.masks = {
	"default": "ddd mmm dd yyyy HH:MM:ss",
	shortDate: "m/d/yy",
	mediumDate: "mmm d, yyyy",
	longDate: "mmmm d, yyyy",
	fullDate: "dddd, mmmm d, yyyy",
	shortTime: "h:MM TT",
	mediumTime: "h:MM:ss TT",
	longTime: "h:MM:ss TT Z",
	isoDate: "yyyy-mm-dd",
	isoTime: "HH:MM:ss",
	isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};

(function ($) {
	$.extend({
		jGFeed: function (url, fnk, num, key) {
			// Make sure url to get is defined
			if (url == null) return false;
			// Build Google Feed API URL
			var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(url);
			if (num != null) gurl += "&num=" + num;
			if (key != null) gurl += "&key=" + key;
			// AJAX request the API
			$.getJSON(gurl, function (data) {
				if (typeof fnk == 'function')
					fnk.call(this, data.responseData.feed);
				else
					return false;
			});
		}
	});
})(jQuery);

function CreateFeed(container, options) {
	try{
		jQuery.jGFeed(options.url,
						function (feeds) {
							// Check for errors
							if (!feeds) {
								// there was an error - set full width on all divs;
								//$(containerLocation).AddClass("rss-single-feed").RemoveClass(".rss-double-feed");
								return false;
							}

							//if single feed - add double feed
							container.addClass(options.style);
							container.removeClass("rss-container");

							// do whatever you want with feeds here
							//create feed container
							$("#feed-container-template").tmpl(feeds).appendTo(container);
							var feedContent = container.find(".feed-content");
							for (var i = 0; i < feeds.entries.length; i++) {
								var feeditem = feeds.entries[i];
								feeditem.parsedDate = dateFormat(new Date(feeditem.publishedDate), "mmm dd, yyyy hh:MM tt Z");
								$("#feed-template").tmpl(feeditem).appendTo(feedContent);
							}
							if (options.rss_link != undefined) {
								$("#feed-link").tmpl(options).appendTo(container.find(".content"));
							}
						},
						options.count);
	}
	catch(err){
		console.log("%s", err.description);
	}
}

function ReadFeed(container, options) {
	try {
		$.ajax({
			url: options.url,
			success: function (rssdata) {
				// Check for errors
				if (!rssdata) {
					// there was an error - set full width on all divs;
					//$(containerLocation).AddClass("rss-single-feed").RemoveClass(".rss-double-feed");
					return false;
				}

				//if single feed - add double feed
				container.addClass(options.style);
				container.removeClass("rss-container");

				var items = new Object();

				items.title = $(rssdata).find('channel').children('title').text();
				$("#feed-container-template").tmpl(items).appendTo(container);

				var feedContent = container.find(".feed-content");
				$(rssdata).find('item').each(function (index) {
					if (index < options.count) {
						//name the current found item this for this particular loop run
						var $item = $(this);

						var feeditem = new Object();
						// grab the post title
						feeditem.title = $item.find('title').text();
						// grab the post's URL
						feeditem.link = $item.find('link').text();
						// next, the description
						feeditem.description = $item.find('description').text();
						//don't forget the pubdate
						var pubDate = $item.find('pubDate').text().replace("Z", "GMT");
						feeditem.parsedDate = dateFormat(new Date(pubDate), "mmm dd, yyyy hh:MM tt Z");
						$("#feed-template").tmpl(feeditem).appendTo(feedContent);
					}
				});

				if (options.rss_link != undefined) {
					$("#feed-link").tmpl(options).appendTo(container.find(".content"));
				}
			},
			datatype: "xml"
		});
	}
	catch (err) {
		console.log("%s", err.description);
	}
}


// END FEED AGGREGATOR


