// Only document-ready functions that should be called once each page-load

// $(document).ready(function(){
// Use jQuery(window).load instead of $(document).ready because of jQuery bug with Safari and calculating heights
jQuery(window).load(function(){
	
	// Fix for all content wells to be the same height
	var heights = new Array();	
	$('div.height-fix').each(function(index){ heights[index] = $(this).height(); });
	$('div.height-fix').each(function(){ $(this).height(Math.max.apply(0, heights)); })
	
	$('input.empty').each(function(){ this.value = ''; });
	
	var navigation = 0;
	
	$('.link a.main').hover(
		function(){
			var dropdown = $(this).next();
			$('.dropdown:visible').each(function(){ if ($(this) != dropdown) $(this).fadeOut(); });
			dropdown.clearQueue();
			dropdown.fadeIn();
			clearTimeout(navigation);
		}, function(){
			var dropdown = $(this).next();
			navigation = setTimeout(function(){ dropdown.fadeOut(); }, 250);
		}
	);
	
	$('.link .dropdown').hover(
		function(){
			clearTimeout(navigation);
		}, function(){
			var dropdown = $(this);
			navigation = setTimeout(function(){ dropdown.fadeOut(); }, 250);
		}
	);
	
	$('form.contact').submit(function(){
		if ($('input#name').val() == ''){
			alert('Please enter your name.');
			$(this).children('input#name').focus();
			
			return false;
		} else if ($('input#email').val() == ''){
			alert('Please enter a valid e-mail address.');
			$(this).children('input#email').focus();
			
			return false;
		} else if ($('input#accept').attr('checked') == false){
			alert('Have you read the disclaimer? If so, please check the disclaimer box.');
			$(this).children('input#accept').focus();
			
			return false;
		}
	});
	
});

var overlay_height = 0;

$('.click-hide').live('focus', function(){ if (this.value == this.defaultValue) this.value = ''; });

$('.click-hide').live('blur', function(){ if (this.value == '') this.value = this.defaultValue; });

$('.overlay').live('click', function(){
	var scroll = document.documentElement.scrollTop || document.body.scrollTop;
	$('#overlay-content').css('top', scroll+'px');
	$('#overlay').fadeTo('normal', 0.75);
	
	$('#overlay-inner').html('Loading...');
	$('#overlay-inner').css('margin', '5px auto 10px');
	$.ajax({url:this.href, success:function(response){
		$('#overlay-content').fadeIn();
		$('#overlay-inner').html(response);
	}});
			
	return false;		
});

$('.hide-overlay').live('click', hideOverlay);

$('.secondary-overlay').live('click', function(){
	overlay_position = $('#overlay-inner').offset().left;
	
	$('#secondary-overlay').html('Loading...');
	$.ajax({url:this.href, success:function(response){
		if ($('#overlay-inner').css('margin-left') != '50px') $('#overlay-inner').css('margin-left', overlay_position + 'px');
		$('#overlay-inner').animate({marginLeft: '50px'}, 1000, null, function(){
			$('#secondary-overlay').fadeIn();
			$('#secondary-overlay').html(response);
			$('#secondary-overlay #scroll').animate({height: ($('#overlay-inner form').height()-80) + 'px'}, 1000);
		});
	}});

	return false;
});

$('.hide-secondary-overlay').live('click', function(){
	$('#secondary-overlay').fadeOut();
	$('#overlay-inner').animate({marginLeft: overlay_position + 'px'}, 1000);

	return false;	
});

$('.insert').live('click', function(){
	if (this.tagName == 'IMG') tinyMCE.execCommand('mceInsertContent', false, '<img src="'+this.src+'" alt="'+this.alt+'">');
	else tinyMCE.execCommand('mceInsertContent', false, '<a href="'+this.href+'">'+$(this).text()+'</a>');
	
	return false;
});

$('.assign').live('click', function(){
	$('#upload_id').html(this.alt);
	$('#article_upload_id').val(this.id);
});

$('input.accessible').live('click', function(){
	if (this.value == 'page' && !$('#accessible_at').is(':visible')) {
		$('#accessible_at').show();
		$('#accessible_at-container').animate({height: $('#accessible_at').height(), opacity: 1}, 'slow');
		$('#article_navigation_true').checked = true;
		$('#article_navigation_false').checked = false;
	}
	if (this.value == 'blog' && $('#accessible_at').is(':visible')) {
		$('#accessible_at-container').animate({height: '0', opacity: 0}, 'slow', '', function(){ $('#accessible_at').hide()})
		$('#article_accessible_at').value = '';
		$('#article_navigation_true').checked = false;
		$('#article_navigation_false').checked = true;
	}		
});

function hideOverlay() {
	$('#overlay-content').fadeOut();
	$('#overlay').fadeOut();
	
	return false;
}

function unique(a) {
	var r = new Array();
	o:for (var i = 0, n = a.length; i < n; i++) {
		for (var x = 0, y = r.length; x < y; x++) { if (r[x] == a[i]) continue o; }
		r[r.length] = a[i];
	}
	return r;
}

function include(arr, obj) {
	for (var i = 0; i < arr.length; i++) { if (arr[i] == obj) return true; }
}