var selected_header;
var selected_tab;
var header_switching = false;

function nextRandomPhoto(){
	$('#recentphotoscontainer > a:first-child > img').animate(
		{marginBottom: '-402px'},
		500,
		function(){
			$('#recentphotoscontainer > a:first-child > img').css('margin-bottom','0px');
			$('#recentphotoscontainer').append($('#recentphotoscontainer > a:first-child'));
		}
	);
}

$(document).ready(function(){
	//////// HEADER STUFF ////////////////////////////////////////////
	$('#nav_catlinks > a').click(function(){
		$(this).blur();
		if ($(this).hasClass('selected') || header_switching) return false;		//If already shown, dont to anything
		header_switching = true;
		
		$(this).siblings().removeClass('selected');
		$(this).addClass('selected');
		
		var header_to_show = $('#header_'+this.id.substr(8));
		header_to_show.css('z-index',27).animate(
			{marginTop: '0px'},
			500,
			function(){
				header_to_show.css('z-index',26);
				header_to_show.siblings().css('margin-top','160px').css('z-index','25');
				$('#nav_pagelinks > a').removeClass('selected');
				header_switching = false;
			}		
		);
		return false;
	});
	$('#nav_pagelinks > a').click(function(){
		$(this).blur();
		if ($(this).hasClass('selected') || header_switching) return false;		//If already shown, dont to anything
		header_switching = true;
		
		$(this).siblings().removeClass('selected');
		$(this).addClass('selected');
		
		var header_to_show = $('#header_'+this.id.substr(9));
		header_to_show.css('margin-top','-160px').css('z-index',27).animate(
			{marginTop: '0px'},
			500,
			function(){
				header_to_show.css('z-index',26);
				header_to_show.siblings().css('margin-top','160px').css('z-index','25');
				$('#nav_catlinks > a').removeClass('selected');
				header_switching = false;
			}		
		);
		return false;
	});
	
	//////// INITIALIZE TWITTER PLUGIN ////////////////////////////////////////////
	if ($("#twitter_nextlink").size() > 0){
		$("#twitter_post_container").tweet({
			username: "theozero",
			count: 10,
			loading_text: "Loading tweets..."
		});
		$("#twitter_followlink").hover(function(){$("#twitter_tweet_bubble").animate( {width:108} ); }, function(){$("#twitter_tweet_bubble").animate( {width:0} ); });
		$("#twitter_nextlink").click(function(){
			$('ul.tweet_list li:first-child').animate({height:0},'slow',function(){
				$('ul.tweet_list').append($('ul.tweet_list li:first-child').height(130));
			});
			return false;
		});
		window.setInterval(function(){
			$("#twitter_nextlink").addClass('hover').delay(1000).click().removeClass('hover');
		},5000);
	}
	
	//////// CONTACT FORM ////////////////////////////////////////////
	var orig_name = autoclear($('#contact_name'));
	var orig_email = autoclear($('#contact_email'));
	var orig_subject = autoclear($('#contact_subject'));
	var orig_message = autoclear($('#contact_message'));
	$('#contact_sendbutton').click(function(){
		var name = $('#contact_name').val();
		var email = $('#contact_email').val();
		var subject = $('#contact_subject').val();
		var message = $('#contact_message').val();
		
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (name == orig_name || email == orig_email || subject == orig_subject || message == orig_message ){
			alert('Please fill in all fields!');
			if (name == orig_name) $('#contact_name').focus();
			else if (email == orig_email) $('#contact_email').focus();
			else if (subject == orig_subject) $('#contact_subject').focus();
			else if (message == orig_message) $('#contact_message').focus();
		} else if ( !filter.test(email) ){
			alert('Please enter a valid e-mail address!')
			$('#contact_email').val('').focus();
		} else {
			$('#contact_name').attr('disabled','disabled');
			$('#contact_email').attr('disabled','disabled');
			$('#contact_subject').attr('disabled','disabled');
			$('#contact_message').attr('disabled','disabled');
			$('#contact_sendbutton').attr('disabled','disabled');

			$('#contact_send_status').text('Sending...').show();
			$.ajax({
				type: "POST",
				url: "/wp-content/themes/990grid/scripts/contact.php",
				data: 'email='+email+'&subject='+escape(subject)+'&name='+escape(name)+'&message='+escape(message),
				cache: false,
				success: function(msg){
					if (msg == 'ok'){
						$('#contact_send_status').text('Sent!').fadeOut(3000, function(){
							$('#contact_name').val(orig_name).attr('disabled','');
							$('#contact_email').val(orig_email).attr('disabled','');
							$('#contact_subject').val(orig_subject).attr('disabled','');
							$('#contact_message').val(orig_message).attr('disabled','');
							$('#contact_sendbutton').removeClass('sending').attr('disabled','');
						});

					} else {
						$('#contact_send_status').text('ERROR! Try again.').addClass('error');
						$('#contact_name').attr('disabled','');
						$('#contact_email').attr('disabled','');
						$('#contact_subject').attr('disabled','');
						$('#contact_message').attr('disabled','');
						$('#contact_sendbutton').removeClass('sending').attr('disabled','');
						
					}
				},
				error: function(){
					$(inputBox).addClass('error');
				}
			});
		}
	})
	
	function autoclear(el){
		var original_value = el.val();
		el.focus(function(){
			if (el.val() == original_value){
				el.val('');
			}
		});
		el.blur(function(){
			if (el.val() == ''){
				el.val(original_value);
			}
		});
		return original_value;
	}
	
	//////// IMAGE GALLERY ////////////////////////////////////////////
	if ($('#recentphotoscontainer').size() > 0){
		$('#recentphotoscontainer img').click(function(){
			nextRandomPhoto();
			return false;
		});
		var randomGalInterval = setInterval("nextRandomPhoto()", 8000);
	}
	$('.imagethumbsholder > a').lightBox({containerBorderSize: 2, overlayOpacity:.9, 
											imageLoading:'http://theoephraim.com/wp-content/themes/990grid/scripts/lightbox/images/lightbox-ico-loading.gif',
											imageBtnPrev:'http://theoephraim.com/wp-content/themes/990grid/scripts/lightbox/images/lightbox-btn-prev.gif',
											imageBtnNext:'http://theoephraim.com/wp-content/themes/990grid/scripts/lightbox/images/lightbox-btn-next.gif'});
	//////// MAIN POST CONTENT HEIGHT FIX ////////////////////////////////////////////
	// sets teh height of the post so that the bottom of the content lines up either in the middle or at the top of the grid
	if ($('.postcontent').size() > 0){
		$('.postcontent').height( 112 * Math.ceil( $('.postcontent').innerHeight() / 112 ) - ($('.postcontent').innerHeight() - $('.postcontent').height()) - 2);
	}
});
