var h = window.location.href, qs = parse_qs(), b = 'http://' + window.location.host + '/';

function parse_qs() {
	var pairs = window.location.search.substr(1).split('&'), args = {};

	for (var p in pairs) {
		var a = pairs[p].split('=');
		args[a[0]] = a[1];
	}

	return args;
}

function current(lang) {
	return qs.tl ? (lang == qs.tl) : !lang;
}

function load_path(path) {
	window.location.href = (window.location.pathname == '/translate_c') ? h.replace('u=' + qs.u, 'u=' + qs.u + path) : (b + path);
}

function ts(current) {
	return 'color: #434963;' + (current ? ' text-decoration: none;' : '');
}

function get_trans_link(lang, title, loc) {
	return '<a style="' + ts(current(lang)) + '" " href="http://translate.google.com/translate_p?hl=en&amp;sl=en&amp;tl=' + 
		lang + '&amp;u=' + loc + '">' + title + '</a>';
}

function write_translation_links() {
	var loc = qs.u || h;
	
	document.write(['<a style="' + ts(current('')) + '" " href="' + loc + '">English</a>', 
		get_trans_link('es', 'Spanish', loc), 
		get_trans_link('it', 'Italian', loc), 
		get_trans_link('pt', 'Portuguese', loc)].join(' | '));
}

var validate = function (form) {
	for (var i = 0, f; f = form.elements[i]; i++) {
		var valid = true;
		
		if (f.getAttribute('required') !== null) {
			if (f.nodeName == 'SELECT') {
				if (f.selectedIndex <= 0) valid = false;
			} else if (f.nodeName == 'TEXTAREA') {
				if (!f.value.replace(/^\s+|\s+$/g, '')) valid = false;
			} else {
				var type = f.getAttribute('type');
				
				if (type == 'radio' || type == 'checkbox') {
					if (!f.checked) valid = false;
				} else {
					if (f.getAttribute('require_valid_email') !== null) {
						if (!f.value.match(/^[\w\d.%+-]+@[\d\w.-]{2,}\.[\w]{2,4}$/)) valid = false;
					} else {
						if (!f.value) valid = false;
					}
				}
			}
		}
		
		if (!valid) {
			alert(f.name.replace(/.*?\[/, '').replace(/\]$/, '') + ' is required.');
			return false;
		}
	}
	
	return true;
};

$(function() {
	jQuery.fn.hoverClass = function(c) {
		return this.each(function() {
			jQuery(this).hover(function() {
				jQuery(this).addClass(c);
			}, function() {
				jQuery(this).removeClass(c);
			});
		});
	};
	
	$('#gallery_switcher').change(function() {
		window.location.href = $(this).val();
		
		return false;
	});

	$('table.calendar td').hoverClass('hover').click(function() {
		var loc = $(this).find('a.day').attr('href');
		
		if (loc) {
			window.location.href = $(this).find('a.day').attr('href');
		}
	});
	
	$('.search_box').focus(function() {
		var default_value = 'Enter Keyword or Phrase';
		
		if ($(this).val() == default_value) {
			$(this).val('');
		} else if ($(this).val() == '') {
			$(this).val(default_value);
		}
	});
	
	$('.search_box').blur(function() {
		var default_value = 'Enter Keyword or Phrase';
		
		if ($(this).val() == '') {
			$(this).val(default_value);
		}
	});
	
	$('h3.toggle a').click(function() {
		$(this).parent().toggleClass('exp').next().toggle();
		return false;
	});
	
	$('.collapse #nav a.index:only-child').addClass('hide');
	
	if (window.location.hash) {
		$("h3.toggle a[name='" + window.location.hash.substr(1) + "']").click();
	}
	
	$('.sitemap ul:first').css({'marginLeft': '0px', 'paddingLeft': '0px'})
		.find('a.level1').css({'textDecoration': 'none', 'fontWeight': 'bold'});
	
	//two column sitemap...
	$('.sitemap ul li.level1:lt(5)').css('width', '50%');
	$('.sitemap ul:first').prepend('<div id="rcol" style="float: right; width: 45%;"></div>');
	$('.sitemap ul li.level1:gt(5)').appendTo('#rcol').find(':first').css('margin-top', '0px');
	
	//only show current state for last left nav item
	$('#nav a.current:not(.level2, .hide):not(:last)').removeClass('current');
	
	$('#nav li.level2').prepend('<img class="caret" src="/static/img/ln_caret.gif" />');
	
	$('#nav li.level4').prepend('<img class="bullet" src="/static/img/ln_bullet.gif" />');
	
	$(':not(div.item.border) + div.item.border').addClass('first');
	
	$('ul a.article, ul a.event, ul a.hide').parent().hide();
	
	//$('.noh *').show();
	$('.focus').focus();
});
