jQuery.noConflict();

function log(msg) {
	try {
		console.log(msg);
	}

	catch (e) {
		// console is not supported.
	}
}

var FP = {
	currentVideo : null,
	player : null
}


jQuery(function() {

	/**** VALIDATORS ****/

	/**
	 * Validate: If enabled, then input mus be required.
	 */
	jQuery.validator.addMethod('requiredIfEnabled', function(value, element) {
		return jQuery(element).attr('disabled') || value.length > 0;
	});

	/**
	 * Validates:
	 * 1234567890,
	 * 123-456-7890,
	 * (123)456-7890, (123) 456-7890,
	 * 123.456.7890
	 * 123 456 7890
	 */
	jQuery.validator.addMethod('phoneUS', function(phoneNumber, element) {
		phoneNumber = phoneNumber.replace(/\s+/g, "");
		var isValid = this.optional(element)
			|| (phoneNumber.length == 10 && phoneNumber.match(/^[0-9]+$/))
			|| phoneNumber.match(/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/)
			|| phoneNumber.match(/^[0-9]{3}\.[0-9]{3}\.[0-9]{4}$/)
			|| phoneNumber.match(/^\([0-9]{3}\)( |)[0-9]{3}\-[0-9]{4}$/)
			|| phoneNumber.match(/^[0-9]{3} [0-9]{3} [0-9]{4}$/);

		return isValid;
	});

	/**
	 * Validates a more flexible regular expression for a phone number. As long it
	 * contains at most 20 digits it is valid. It is valid if the data contains
	 * " ", "-", "(", ")", ".".
	 */
	jQuery.validator.addMethod('phone2', function(phoneNumber, element) {

		// Filter out all the valid symbols.
		var filteredNumbers = phoneNumber.replace(/( |\-|\(|\)|\.)/g, '');

		// Determine if the filteredNumbers contain more than 10 digits, less than 20 digits, and only contain digits.
		var isAllNumbersAfterFilter = filteredNumbers.length >= 10 && filteredNumbers.length <= 20 && filteredNumbers.match(/^[0-9]+$/) != null;

		return isAllNumbersAfterFilter;
	});

	/**
	 * Validate: U.S. zipcode.
	 */
	jQuery.validator.addMethod('zipCode', function(zipcode, element) {
		zipcode = zipcode.replace(/\s+/g, "");
		return this.optional(element) || zipcode.length == 5 && zipcode.match(/^[0-9]+$/);

	});

	// setup button action. it will fire our overlay
	jQuery('a.alliedPlayer').overlay({

		// Add mask for modal effect.
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.6
		},

		// Disallow mask to close when mask is clicked.
		closeOnClick: false,

		// when overlay is opened, load our player
		onLoad: function() {
			if (FP.currentVideo) {
				FP.player = flowplayer('fpPlayer', flowplayerSwf, {
					clip: {
						url: FP.currentVideo,
						provider: 'allied'
					},

					// streaming plugins are configured under the plugins node
					plugins: {

						// here is our rtmp plugin configuration
						allied: {
							url: flowplayerRtmp,

							// netConnectionUrl defines where the streams are found
							netConnectionUrl: videoRtmpServer
						}
					}
				});

				FP.player.load();
			}
		},

		// when overlay is closed, unload our player
		onClose: function() {
			if (FP.player) {
				FP.player.unload();
			}

			FP.currentVideo = null;
		}
	});

	jQuery('a.alliedPlayer').click(function() {
		FP.currentVideo = jQuery(this).attr('href');
		return false;
	});

	//coverage map light box
	jQuery("a[rel=#coverageMap]").overlay({
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.6
		},
		target: "#coverageMap",
		closeOnClick: true
	});
	jQuery("a[rel=#coverageMap]").click(function() {
		if (jQuery(this).attr('href') != null){
			jQuery("#coverageMapIframe").attr("src", jQuery(this).attr("href"));
			jQuery("#coverageMapIframe").load(this.getTrigger().attr("href"));
		}
	});
});


function changeUrl(url) {
	document.location = url;
}


function openNewWindow(url, width, height, moreParams) {
	var options = "";
	if (width){
		options += "width=" + width;
	}
	if (height){
		if (width){
			options += ", ";
		}
		options += "height=" + height;
	}
	if (moreParams) {
		if (options != "") {
			options += ", ";
		}
		options += moreParams;
	}
	window.open(url, "externalWindow", options);
}




