﻿// Define a global namespace object
var DMD = window.DMD || {};

/********************************************************************
 Customization
*********************************************************************/
$.fn.isVisible = function() {return this.css('display') != 'none';}

jQuery.fn.extend({
	//---------------------------------------------------------------
	pulsate : function(speed, callback) {
		// Use FadeTo to avoid the 'display:none' of FadeOut
		return $(this).fadeTo(speed, 0.01).fadeTo(speed, 1.0, callback);
	},

	//---------------------------------------------------------------
	scrollTo : function(speed, easing) {
		return this.each(function() {
			var targetOffset = $(this).offset().top;
			$('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
	}
});

/********************************************************************
 Class: Page
*********************************************************************/
DMD.Page = function()
{
	//---------------------------------------------------------------
	function decryptEmail(email)
	{
		// See DMD2.Web.TemplateBase.EmailEncode for associated encryption method
		var link = $(email).html().replace('®', '@').replace(/·/g, '.');
 		$(email).html(link).attr('href', 'mailto:' + link);
	}
	
	return {
		//---------------------------------------------------------------
		init : function()
		{
		},
		
		//---------------------------------------------------------------
		initDecryptEmail : function()
		{
			// Add 'mailto' links to associated <a> elements
 			$('a.mailto').each(function(){decryptEmail(this);});
		},
		
		//---------------------------------------------------------------
		initCheckboxToggle : function()
		{
			$('#checkboxToggle').click(function(){
				var checkedValue = $('#checkboxToggle').attr('checked') ? 'checked' : '';
				$(':checkbox').attr('checked', checkedValue);
			});
		}
	}
}();

/********************************************************************
 Class: Utility
*********************************************************************/
DMD.Util = function()
{
	return {
		//---------------------------------------------------------------
		trim : function(value) {
			return value.replace(/^\s+|\s+$/g, '');
		},
		lTrim : function(value) {
			return value.replace(/^\s+/, '');
		},
		rTrim : function(value) {
			return value.replace(/\s+$/, '');
		}
	}
}();

/********************************************************************
 Class: Async
*********************************************************************/
DMD.Async = function()
{
	return {
		rootUrl : '/BuyNZ/Utility/Async',

		//---------------------------------------------------------------
		getGlobalVar : function(xmlDoc, attribute)
		{
			var global = xmlDoc.getElementsByTagName('global');

			return global.length > 0
				? global[0].getAttribute(attribute)
				: '';
		},

		//---------------------------------------------------------------
		getObjectVar : function(xmlDoc, object, attribute)
		{
			var object = xmlDoc.getElementsByTagName(object);

			return object.length > 0
				? object[0].getElementsByTagName(attribute)[0].firstChild.nodeValue
				: '';
		},

		//---------------------------------------------------------------
		getContentEncoded : function(xmlDoc, tagName)
		{
			var value = '';

			var content = xmlDoc.getElementsByTagName(tagName);
			if(content.length > 0)
			{
				value = FA.Util.isDefined(content[0].textContent)
					? content[0].textContent
					: content[0].text;
			}

			return value;
		}
	}
}();
