jQuery(document).ready(function($) {
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
	$(function() {
		var bh = $('body').height(), wh = $('#wrap').height()+25;
		if(bh > wh) {
			$('#foot-runner').css({'position':'absolute', 'bottom':'0', 'left':'0', 'width':'100%'});
		}
	});
	
	$('#search-form').find('input[type=text]').val('Search');
	
	//home cycle
	$('#content .home .cycle ul').cycle({
		speed:1500,
		timeout:5000,
		fx:'fade'
	});
	
	//auto clear form
	$('form').find('input[type=text], textarea').each(function() {
		var i = $(this), d = i.val();
		i.focus(function() { if(i.val() == d) { i.val(''); } }); 
		i.blur(function() { if(i.val() == "") { i.val(d); } });	
	});
	
	//blog scrolling
	$('#posts .comments .xscroll').jScrollPane({showArrows:false, scrollbarWidth:4, animateTo:true});
	
	//blog actions/forms
	$('#posts .post .post-actions').each(function() {
		var wrap = $(this), panel = wrap.find('.post-panel');
		var trig = wrap.find('.tabs a.s1'), strig = wrap.find('.tabs .s5');
		
		trig.click(function(e) {
			if(!panel.swapForm(1,0) && !panel.swapForm(2,0)) {
				panel.slideToggle(400);
				$(this).toggleClass('active');
			}
		});
		
		strig.click(function(e) {
			if(panel.css('display') == "none") {
				trig.click();
			}
			panel.swapForm(0,1);
		});
		
		//submit
		wrap.find('a.xsend').click(function(e) {
			var form = $(this).closest('form'), errors = false;
			var n = form.find('input').eq(0), e = form.find('input').eq(1), m = form.find('textarea');
			if(!checkLength(n)) { errors = true; }
			if(!checkEmail(e)) { errors = true; }
			if(!checkLength(m)) { error = true; }
			if(!errors) {
				var data = form.serialize();
				$.post(ajaxurl, data, function(r) {
					r = cleanAjax(r);
					
					if(form.hasClass('form_comment')) {
						$('<li class="comment" />')
							.html('<a href="#">'+n.val()+': </a> '+m.val()+' <strong>Written just seconds ago.</strong>')
							.prependTo(panel.find('.comments ul'));
						panel.showMsg(0, 'Thanks for the Comment!', 'We just added it over to the right!');
					}
					
					if(form.hasClass('form_share')) {
						panel.showMsg(1, 'Thanks for Sharing!', 'We sent an email to your friend letting them know about this post!');
					}
					
					clearForm(form);
					
				});
			}
		});
		
		//init
		panel.hide();
		
	});
	
	$.fn.swapForm = function(a,b) {
		a = $(this).find('.panel').eq(a), b = $(this).find('.panel').eq(b);
		if(a.is(':visible')) {
			a.slideUp(500); b.slideDown(500)
			return true;
		}else {
			return false;
		}
	}
	
	$.fn.showMsg = function(e,h,m) {
		e = $(this).find('.panel').eq(e), msg = $(this).find('.msg');
		msg.html('<h3>'+h+'</h3><p>'+m+'</p>');
		e.slideUp(500); msg.slideDown(500);
	}
	
	function clearForm(form) {
		form.find('input[type=text], textarea, input[type=password]').val('');
	}
	
	function checkEmail(v) {
		var reg = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
		if(!reg.test(v.val())) {
			console.log('Email failed');
			v.addClass('error');
			return false;
		}
		v.removeClass('error');
		return true;
	}
	
	function checkLength(v) {
		if(v.val().length < 1) {
			console.log('Length failed');
			v.addClass('error');
			return false;
		}
		v.removeClass('error');
		return true;
	}
	
	//clean stupid 0 off the end of ajax result
	function cleanAjax(html) {
		var last = html.substr(html.length-1);
		if(last == '0') {
			return html.substr(0, html.length-1);
		}else {
			return html;
		}
	}
	
	//faq
	$('#content .faqs .faq').each(function() {
		var faq = $(this), i = faq.find('a.icon');
		faq.find('h3').click(function(e) {
			e.preventDefault();
			faq.parent('.faqs').find('.faq').not(faq).find('.txt:visible').parent().find('h3 a').click();
			faq.find('.txt').slideToggle('fast');
			if(i.hasClass('iclosed')) {
				i.removeClass('iclosed').addClass('iopen');
			}else {
				i.removeClass('iopen').addClass('iclosed');
			}
		});
	});
	
	//contact page
	$('#content .contact a.xsend').click(function(e) {
		var form = $(this).closest('form');
		var n = form.find('input[name=name]'), e = form.find('input[name=email]'), m = form.find('textarea');
		var errors = false;
		if(!checkLength(n)) { errors = true; }
		if(!checkEmail(e)) { errors = true; }
		if(!checkLength(m)) { error = true; }
		if(!errors) {
			var data = form.serialize();
			$.post(ajaxurl, data, function(r) {
				r = cleanAjax(r);
				var p = form.parent();
				var h = p.height(), w = p.width();
				p.find('.msg').height(h).width(w).slideDown(700);
				form.slideUp(700);
				clearForm(form);
				
			});
		}
	});
	
});

// shop related
jQuery(document).ready(function($) {
	
	//cat nav
	$('#cat-list').each(function() {
		var cats = $(this);
		
		//add classes to create border on 2nd level cats
		cats.find('li.toplevel').each(function() {
			$(this).find('ul.children:first').addClass('firstchild');
		});
				
		cats.find('li').each(function() {
			
			//indent
			var indent = 10;
			var total = $(this).find('ul.children');
			total.each(function() {
				$(this).css('padding-left', indent+'px');
				indent = indent+10;
			});
			
			//icons and click event
			if($(this).find('ul.children').length != 0) {
				$(this).find('a:first').addClass('open');
				
				$(this).find('a:first').click(function(e) {
					e.preventDefault();
					$(this).parent().find('ul.children:first').slideToggle(400);
					if($(this).hasClass('open')) {
						$(this).addClass('close').removeClass('open');
					}else {
						$(this).addClass('open').removeClass('close');
					}
				});
				
			}
		});
		
		//init
		cats.find('li.toplevel:first a:first').click();
		
	});
	
	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	equalHeight($('#checkout .xlogin'));
	
	//product gallery
	$(function() {
		var gal =  $('#prodgal'), ul = 0, s = gal.find('ul.thumbs'), w = gal.find('.track').width();
		
		gal.find('ul.images').cycle({
			speed:1500,
			timeout:0,
			fx:'fade',
			pager:s,
			pagerAnchorBuilder:function(i, ele) {
				var img = $(ele).find('img'), src = img.attr('src');				
				return '<li><a href="#"><img src="'+src+'" width="92" height="72" alt="" /></a></li>';
			}
		});
		
		s.find('li').each(function() {
			ul = ul + $(this).outerWidth(true);
		});
		
		if(ul>w) {
			gal.find('.track').mousemove(function(e) {
				var l = -1*(e.pageX - $(this).offset().left)*((ul-w)/w);
				s.stop().animate({'left':l}, {queue:false, duration:500});
				//s.css('left', l);
			});
		}
		
	});
		
});
