/* sIFR Config */
var gothambold = { src: '/wp-content/themes/huffstapes/_f/gothambold.swf' };
var gothambook = { src: '/wp-content/themes/huffstapes/_f/gothambook.swf' };
sIFR.activate(gothambold, gothambook);
sIFR.replace(gothambold, {
	selector: 'h2.episode-title',
	css: [
		'.sIFR-root { font-size: 27px; letter-spacing:-1.2; text-transform: uppercase; text-align: right; }',
		'a {color: #555555; text-decoration: none; }',
		'a:hover {color: #333333; }'
	],
	thickness: '84', preventWrap: false, forceSingleLine: true, offsetTop: 1, offsetLeft: -2, tuneHeight: -8, wmode: 'transparent'
});

sIFR.replace(gothambook, {
	selector: 'h3.episode-subtitle',
	css: [
		'.sIFR-root { font-size: 16px; letter-spacing:-.2; text-align: right; }',
		'em { font-style: normal; color: #e87f1e; }'
	],
	wmode: 'transparent'
});

/* Poll Global Variables */
var polls_ajax_url = 'http://huffandstapes.com/wp-content/plugins/wp-polls/wp-polls.php';
var polls_text_wait = 'Your last request is still being processed. Please wait a while ...';
var polls_text_valid = 'Please choose a valid poll answer.';
var polls_text_multiple = 'Maximum number of choices allowed:';
var poll_show_loading = 1;		// loading animation == true
var poll_show_fading = 1;		// fade animations == true

/* Simple Smooth Scroll */
function smoothScroll(t, callback) {
	var targetOffset = jQuery(t).offset().top - 12;
	jQuery('html,body').animate({scrollTop: targetOffset}, 420);
};

/* Pulse a page element max times */
function pulse(tgt, max) {
	$(tgt).animate({opacity: .12}, 210, function() {
		if (max > 0) {
			max -= 1;
			$(this).animate({opacity: 1.0}, 210, pulse(tgt, max));
		} else {
			$(this).animate({opacity: 1.0}, 210);
		}
	});
};

/* Eml Protect */
function emlWrt(dom, usr, output) { 
	if ((output.length == 0) || (output.indexOf('@')+1)) {
		document.write('<a href=' + '"mai' + 'lto:' + usr + '@' + dom + '">' + usr + '@' + dom + '<\/a>'); }
	else {
		document.write('<a href=' + '"mai' + 'lto:' + usr + '@' + dom + '">' + output + '<\/a>');
	}
};

/* Auto-clear & auto-fill form fields */
function inputClr(id, lbl) {
	$(id).css({ color: "#aaa" }).attr({ value: lbl }).focus(function(){
		if($(this).val() == lbl) {
			$(this).val("").css({ color: "#555" });
		}
	}).blur(function() {
		if($(this).val() == ""){
			$(this).css({ color: "#aaa" }).val(lbl);
		}
	});
};

/* RSS month -> nicename */
function monthParse(m) {
	if (m=="Jan") return "January";
	if (m=="Feb") return "February";
	if (m=="Mar") return "March";
	if (m=="Apr") return "April";
	if (m=="May") return "May";
	if (m=="Jun") return "June";
	if (m=="Jul") return "July";
	if (m=="Aug") return "August";
	if (m=="Sep") return "September";
	if (m=="Oct") return "October";
	if (m=="Nov") return "November";
	if (m=="Dec") return "December";
};

/* RSS Episodes XML Import & Parse */
function RSSParse(myFeed) {
	$.getFeed({
		url: myFeed,
		success: function(feed) {
			var html = "";
			var month = "";
			var first = true;
			for(var i=0; i<feed.items.length; i++) {
				var item = feed.items[i];
				
				var title = item.title.split(":");
				var day = item.updated.substr(5,2);
				if (day.substr(0,1) == "0") { day = day.substr(1,1); }
				var year = 	"'" + item.updated.substr(14,2);
			
				if (item.updated.substr(8,3) != month) {
					if (!first) {
						html += "</ul></li>";
					}
					month = item.updated.substr(8,3);
					if (!first) {
						html += "<li id=\"" + month + "-" + item.updated.substr(14,2) + "\"><a onclick=\"ulSlide(this);\">" + monthParse(month) + " " + item.updated.substr(12,4) + "</a><ul class=\"hide\">";
					} else {
						html += "<li class=\"first\" id=\"" + month + "-" + item.updated.substr(14,2) + "\"><a onclick=\"ulSlide(this);\">" + monthParse(month) + " " + item.updated.substr(12,4) + "</a><ul class=\"hide\">";
					}
					first = false;
				}
				var date = month + " " + day + " " + year;
				
				html += '<li>' + '<a href="' + item.link + '">' + title[title.length - 1] + '</a> ';
				html += '<em>' + date + '</em></li>';
			}

			$("#episode-list").html(html)
		}
	});
};

/* Sidebar Slider Toggle */
function ulSlide(a) {
	if (!$(a).parent().hasClass('current')) {
		$(a).parent().addClass('current');
	} else {
		$(a).parent().removeClass('current');
	}
	$(a).parent().find('ul').slideToggle('fast');
};

/* Comment Form Validation */
function commentValidate(theForm) {
	var isValid = true;
	var errStr = "";
	n = $("#author").val();
	e = $("#email").val();
	c = $("#comment").val();
	
	if ((n == '') || (n == 'Name')) {
		isValid = false;
		errStr = "It's not hard, bozo... enter your name.";
	}
	
	if ((e == '') || (e == 'Email')) {
		isValid = false;
		errStr = "Enter an email address, por favor.";
	} else {
		var str = e;
		var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (!filter.test(str)) {
			isValid = false;
			errStr = "C'mon dogg... a valid email.";
		}
	}
	
	if ((c == '') || (c == 'Your Comment...')) {
		isValid = false;
		errStr = "Empty comments are sad. You should write something.";
	}
	
	if (isValid) {
		$("form").submit();
	} else {
		$("#form-error").html(errStr);
		$("#form-error").css("display", "block");
	}
};

/* Textarea Autogrow */
(function(jQuery) {
	var self = null;
	jQuery.fn.autogrow = function(o) {	
		return this.each(function() {
			new jQuery.autogrow(this, o);
		});
	};
	
	jQuery.autogrow = function (e, o) {
		this.options		  	= o || {};
		this.dummy			  	= null;
		this.interval	 	  	= null;
		this.line_height	  	= this.options.lineHeight || parseInt(jQuery(e).css('line-height'));
		this.min_height		  	= this.options.minHeight || parseInt(jQuery(e).css('min-height'));
		this.max_height		  	= this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
		this.textarea		  	= jQuery(e);
		
		if(this.line_height == NaN)
			this.line_height = 0;
		
		this.init();
	};
	jQuery.autogrow.fn = jQuery.autogrow.prototype = {
    autogrow: '1.2.2'
};

jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
jQuery.autogrow.fn.extend({
	init: function() {			
		var self = this;			
		this.textarea.css({overflow: 'hidden', display: 'block'});
		this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() });
		this.checkExpand();	
	},
	startExpand: function() {				
		var self = this;
		this.interval = window.setInterval(function() {self.checkExpand()}, 400);
	},
	stopExpand: function() {
		clearInterval(this.interval);	
	},
	checkExpand: function() {
		if (this.dummy == null) {
			this.dummy = jQuery('<div></div>');
			this.dummy.css({
				'font-size'  : this.textarea.css('font-size'),
				'font-family': this.textarea.css('font-family'),
				'width'      : this.textarea.css('width'),
				'padding'    : this.textarea.css('padding'),
				'line-height': this.line_height + 'px',
				'overflow-x' : 'hidden',
				'position'   : 'absolute',
				'top'        : 0,
				'left'		 : -9999
				}).appendTo('body');
			}
			var html = this.textarea.val().replace(/(<|>)/g, '');
			if ($.browser.msie) { html = html.replace(/\n/g, '<BR>new'); }
			else { html = html.replace(/\n/g, '<br>new'); }
			if (this.dummy.html() != html) {
				this.dummy.html(html);
				if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height)) {
					this.textarea.css('overflow-y', 'auto');
				} else {
					this.textarea.css('overflow-y', 'hidden');
					if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height())) {	
						this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);	
					}
				}
			}
		}
	});
})(jQuery);

/* Fire When Ready! */
$(document).ready(function() {
	var needUpgrade = /(MSIE 6|MSIE 5.(\d+))/i.test(navigator.userAgent);
	if (needUpgrade) {
		$("body").css("background", "transparent");
		$("body h1").css("color", "#444");
		$("body p").css("color", "#666");
		$("body").css("text-align", "center");
		$("body").html("<h1>Huff &amp; Stapes</h1><p>Sorry chief... we have no love for IE6. Try <a href=\"http://www.microsoft.com/windows/internet-explorer/\">upgrading your browser to IE8</a>, <a href=\"http://www.mozilla.org\">use Firefox</a>, or <a href=\"http://www.apple.com\">get yourself a Mac</a>. All will be right with the world.</p>");
	}
	
	// Dropdowns
	var overItem = false;
	var overMenu = false;
	$("#main-nav>li").hover(
		function () {
			overItem = true;
			$(this).find(".drop").show();
			$(this).find("span.arrow").css("backgroundPosition", "right -15px");
		}, 
		function () {
			overItem = false;
			$(this).find(".drop").hide();
			$(this).find("span.arrow").css("backgroundPosition", "right 0px");
		}
	);
	
	$("div.drop").hover(
		function () {
			overItem = true;
			overMenu = true;
			$(this).show();
			$(this).find("span.arrow").css("backgroundPosition", "right -15px");
		}, 
		function () {
			overMenu = false;
			if (!overItem) {
				$(this).hide();
				$(this).find("span.arrow").css("backgroundPosition", "right 0");
			}
		}
	);
	
	// Some quick style adjustments for externally generated HTML
	$("#twitter li").addClass("clearfix");
	$("#twitter li:first").css({'border-top' : 'none', 'padding-top' : '0'});
	$("#twitter li:nth-child(3)").css("border-bottom","0");
	$("#twitter li a:last-child").before("<br />");
	$("#twitter li a:last-child").append(" &infin;");
	$("#twitter li a:last-child").addClass("tweet-stamp");
	
	// Autogrow textareas
	$('#footer textarea').autogrow({
		maxHeight: 210,
		minHeight: 30,
		lineHeight: 16
	});
	$('#commentform textarea').autogrow({
		maxHeight: 210,
		minHeight: 71,
		lineHeight: 16
	});
	
	// Define auto-clear/filled inputs
	inputClr($("#contact-n"), "Name");
	inputClr($("#contact-e"), "Email");
	inputClr($("#contact-m"), "Your Message...");
	inputClr($("#author"), "Name");
	inputClr($("#email"), "Email");
	inputClr($("#url"), "Website (optional)");
	inputClr($("#comment"), "Your Comment...");
});