/*******************************************************************************

	CSS on Sails Framework
	Title: Australian Nursing Federation
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: April 2012

*******************************************************************************/

var Site = {
	debug: {},

	/**
	 * Init Function
	 */
	init: function() {
		$("body").removeClass("no-js");
		Site.navigation();
		if ($('#slides').length > 0) {
			Site.slider();
		}
		Site.placeholder();
		Site.splashScreen();
		debug = Site.subscribeCountryRegion();
	},
	
	/**
	 * Navigation
	 */
	navigation: function() {
		$('#navigation > ul > li:has("ul")').each(function(){
			var elm = $(this),
				submenu = elm.find('> ul');

			elm.hover(function(){
				elm.addClass('hover');
			}, function(){
				elm.removeClass('hover');
			});
	
		});
		
		$(".node_news-media.level_1.has_children > a").attr("href","#");
	},
	
	/**
	 * Slider
	 */
	slider: function() {
		$("#slides").slides({ play: 7500, slideSpeed: 1500, preload: true });
	},
	
	/**
	 * Placeholder
	 */
	placeholder: function() {
		$('[placeholder]').focus(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('placeholder');
			}
		}).blur(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		}).blur().parents('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});
	},

	/**
	 * Splashscreen
	 */
	splashScreen: function() {
		// var showSplash = jQuery.cookie('show_splash'),
		// 	loc = window.location,
		// 	host = 'anmf.org.au',
		// 	splashUrl = '';

		// // If the page is not part of the main domain, do not redirect
		// if (!new RegExp(host).test(loc.host)) {
		// 	return;
		// } 

		// if ((typeof(SiteSettings) !== 'undefined') && (typeof(SiteSettings.splash) !== 'undefined')) {
		// 	splashUrl = SiteSettings.splash;
		// } else {
		// 	return;
		// }

		// if (!showSplash) {
		// 	Site.setCookie('show_splash', loc.href, 3456000, '/', '.' + host);

		// 	window.location = splashUrl;
		// }
	},

	/**
	 * Add helper for country region sellection
	 */
	subscribeCountryRegion: function() {
		var form = jQuery('.member-subscribe > form, .subscription-update'),
			country = {},
			region = {},
			regions = {},
			defaultOption = '',
			syncRegions = function() {
				var currentCountryId = country.find('option:selected').data('id');
				
				region.html(defaultOption);
				region.append(regions.find('optgroup[data-id=' + currentCountryId + ']').html());
			};

		if (form.size() == 0) { return; }

		country = form.find('#country');
		region = form.find('#region');
		regions = region.clone();
		defaultOption = region.find('option:first').clone();

		country.on('change', syncRegions);
		syncRegions();
	},

	/**
	 * Save cookie utility
	 */
	setCookie: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
		if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return; }
		var sExpires = "";
		if (vEnd) {
		  switch (vEnd.constructor) {
			case Number:
			  sExpires = vEnd === Infinity ? "; expires=Tue, 19 Jan 2038 03:14:07 GMT" : "; max-age=" + vEnd;
			  break;
			case String:
			  sExpires = "; expires=" + vEnd;
			  break;
			case Date:
			  sExpires = "; expires=" + vEnd.toGMTString();
			  break;
		  }
		}
		document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
	}
}

$(document).ready(function() {
	Site.init();
});



