jQuery(document).ready(function($) {
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
	//fixed menu & bg
	$(function() {
		
		function sizeWin() {
			
			$('#wrap').css('height', 'auto');
			
			var bH = $('body').height(), bW = $('body').width();
			var wH = $('#wrap').height();
								
			if(bH < 700 || bW < 1100)
				$('#right').css({'position':'absolute'});
			else
				$('#right').css({'position':'fixed'});
		
			if(wH < bH)
				$('#wrap').height(bH);
		}
		
		//init
		sizeWin();
		
		$(window).resize(function() { sizeWin(); });
			
	});
	
	Cufon.replace('#left .post-title, #content h1');
	
	//lazy load
	$('#posts img').lazyload();
	
	//external links
	$('#nav a').each(function() {
		var a = $(this);
		
		if(a.attr('href').indexOf('http://stacy') == -1 && a.attr('href').indexOf('http://www.stacy') == -1 && a.attr('href') != '#')
			a.attr('target', '_blank');
		
	});
	
	$('#left .xscroll').jScrollPane({showArrows:false});
	
	//auto clear
	$('form').find('input[type=text], textarea').each(function() {
		var e = $(this), d = e.val();
		e.focus(function() { if(e.val() == d) { e.val(''); } });
		e.blur(function() { if(e.val() == '') { e.val(d); } });
	});
	
	//post actions
	$('#posts .post').each(function() {
		var post = $(this), trigs = post.find('.post-actions .triggers'), tabs = post.find('.post-actions .tabs');
		trigs.find('a.trigger[href=#]').click(function(e) {
			var i = trigs.find('a').index($(this)), a = tabs.find('.activetab'), n = tabs.find('.tab').eq(i);
			if(!n.hasClass('activetab')) {
				a.removeClass('activetab').slideUp();
				n.addClass('activetab').slideDown();
			}
		});
	});
	
	//form submit
	$('#posts .xform').each(function() {
		var form = $(this), errors = false, trigs = form.closest('.post-actions').find('.triggers'), tabs = form.closest('.tabs'), msg = tabs.find('.msg');
		form.submit(function(e) {
			
			var va = form.find('input[name=author]');
			var vc = form.find('input[name=comment]');
			var ve = form.find('input[name=email]');
			
			if(va.length > 0 && va.val() == 'name' || va.val() == "") { errors = true; va.fadeOut().fadeIn(); }
			if(ve.length > 0 && ve.val() == 'email' || ve.val() == "" || ve.val().indexOf('.') == -1 || ve.val().indexOf('@') == -1) { errors = true; ve.fadeOut().fadeIn(); }
			if(vc.length > 0 && vc.val() == 'message' || vc.val() == "") { errors = true; vc.fadeOut().fadeIn(); }
			
			if(!errors) {
				$.post(global_ajaxurl, form.serialize(), function(d) {
					d = cleanAjax(d);
					
					if(form.hasClass('xcomment')) {
						var a = form.find('input[name=author]').val(), c = form.find('textarea').val();
						addComment(form,a,c);
					}
					
					resetForm(form);
					
					msg.html(d);
					msg.slideDown().delay(4000).slideUp();
				});
			}
			
			return false;
			
		});
	});
	
	$('#content .xform').each(function() {
		var form = $(this), errors = false, msg = form.closest('.col').find('.msg');
		form.submit(function(e) {
			
			var n = form.find('input[name=name]');
			var e = form.find('input[name=email]');
			var m = form.find('textarea');
						
			if(n.val() == 'name' || n.val() == "") { errors = true; n.fadeOut().fadeIn(); }
			if(e.val() == 'email' || e.val() == "" || e.val().indexOf('.') == -1 || e.val().indexOf('@') == -1) { errors = true; e.fadeOut().fadeIn(); }
			if(m.val() == 'message' || m.val() == "") { errors = true; m.fadeOut().fadeIn(); }
			
			if(!errors) {
				$.post(global_ajaxurl, form.serialize(), function(d) {
					d = cleanAjax(d);
					msg.html(d);
					msg.slideDown();
				});
			}
			
			return false;
			
		});
	});
	
	function addComment(p,a,c) {
		var wrap = form.closest('.post-actions'), comments = wrap.find('.comments'), first = wrap.find('li.comment');
		if(first.length == 1 && first.html() == "There are no comments for this post.") { comments.find('ul').html(''); }
		$('<li/>').addClass('comment').html('<a href="#" class="author">'+a+'</a>: '+c).prependTo(comments.find('ul'));
		wrap.find('.triggers a.trigger:first').click();
		wrap.find('.tabs .comments .xscroll').jScrollPane();
	}
	
	function resetForm(o) {
		o.find('.inp').each(function() {
			$(this).focus().val('').blur();
		});
	}
	
	//clean stupid 0 off the end of result
	function cleanAjax(html) {
		var last = html.substr(html.length-1);
		if(last == '0') {
			return html.substr(0, html.length-1);
		}else {
			return html;
		}
	}
	
	
});
/*
function ResizeImage(image, maxwidth, maxheight) {
    if (image.className == "Thumbnail") {
        w = image.width;
        h = image.height;
                
        if( w == 0 || h == 0 ) {
            image.width = maxwidth;
            image.height = maxheight;
        }else if (w > h) {
            if (w > maxwidth) image.width = maxwidth;
        }else {
            if (h > maxheight) image.height = maxheight;
        }
                
        image.className = "ScaledThumbnail";
    }
}

jQuery('#posts').find('img').each(function() {
	var w = jQuery(this).width();
	//if(w > 702)
		//ResizeImage(jQuery(this), 702, 9999999);
}); */