function setBodyClass() {
	var url = new String(window.location.pathname),
		dirs = url.split('/'),
		classes = ' home';

	if (dirs.join('') != '') {
		classes = '';
		for (i = 0; i < dirs.length; i++) {
			var dir = dirs[i].split('.'); // removes file extentions

			if (dir[0]) {
				classes += ' ';
				classes += dir[0].toLowerCase();
			}
		}
	}
	// check if body has an existing class
	if (!document.body.className) {
		document.body.className = classes;
	} else {
		var newClassName = document.body.className;
		newClassName += classes;
		document.body.className = newClassName;
	}
}

function inputDefault(field) {
	if (!document.getElementById) return false;
	if (!document.getElementById(field)) return false;

	var fieldtochange = document.getElementById(field);

	var holder = fieldtochange.parentNode.firstChild;
	while (holder.nodeType == '3') {
		holder = holder.nextSibling;
	}

	if (holder == undefined) return false;
	holder = holder.innerHTML;

	if (fieldtochange.value == '') {
		fieldtochange.value = holder;
	}

	fieldtochange.onfocus = function() {
		if (this.value == holder)
			this.value = '';
	}
	fieldtochange.onblur = function() {
		if (this.value == '')
			this.value = holder;
	}
	return true;
}

function featClose() {
	$(this).parent('li').addClass('close').attr('title', 'Expand to view more details');
	window.location.hash = this.id;
}

function featOpen() {
	$(this).parent('li').removeAttr('class').attr('title', 'Collapse to hide details');
	window.location.hash = this.id;
}

function setJsClass() {
	if (document.body.className != '') {
		document.body.className += ' js';
	} else {
		document.body.className = 'js';
	}
}


function share()
{
	var url;
	var title;

	// twitter
	url = 'http://www.twitter.com/home?status=Currently+Reading+' + document.location + '+by+@WashingtonBrown';
	$('#sexy-twitter').attr('href', url);

	// facebook
	title = $('title').text();
	title = title.replace(/ /g, '+')
	url = 'http://www.facebook.com/share.php?u=' + document.location + '&t=' + title;
	$('#sexy-facebook').attr('href', url);
}

// Fires gAnalytics script for links attached to
$.fn.TrackLinks = function() {
	return this.each(function() {
		this.onclick = function() {
			if ( $(this).is('a[href]') ) {
				pageTracker._trackPageview(this.pathname);
			}
		};
	});
};

$(function() {
	function initialClose() {
		if (window.location.hash != '') {
			var hash = window.location.hash.replace('#', '');
			if (this.id == hash) {
				return;
			}
		}
		$(this).parent('li').addClass('close').attr('title', 'Expand to view more details');
	}
	// Add the onclick toggle event and then close them all to start with
	$('.faqs li h3').each(function() {
		$(this).toggle(featOpen, featClose);
	});
	$('.faqs li h3').addClass('fakelink').each(initialClose);
	$('p.toTop').remove();

	// setting class on the body on load
	setJsClass();

	// setting class on body on load
	setBodyClass();

	// input default values into home page newsletter form
	inputDefault('commsubscription1_firstName');
	inputDefault('commsubscription1_lastName');
	inputDefault('commsubscription1_email');

	// input default values into /property-news/property-updates form
	inputDefault('commsubscription4_firstName');
	inputDefault('commsubscription4_lastName');
	inputDefault('commsubscription4_email');

	// input default values into the search box
	inputDefault('search');

	// external links in new tab
	$("a[@rel='external']").click(function() { window.open($(this).attr('href')); return false; });

	// cool drop down menus
	if ($('#menu').length > 0) {
		$('#menu').droppy();
	}

	$('.email').defuscate();
	$('a.gatrack').TrackLinks();
});
