$(document).ready(function() {
	initButtons();
	initPlayList();
	initCurvyCorners();
	maturePopup();
})

// general functions

function showContent(obj) {
	exc = $("#"+obj.id);
	cont = $("#content_"+obj.id).html();
	exc.html(cont);
}

function maturePopup() {
	if (!$.cookie("PKmature")) {
		var ok = confirm("This site is intended for adults!\nBy clicking OK you're consenting to experience some of the filthiest language on the planet.");
		if (ok) {
			$.cookie("PKmature", true, {expires:30});
		}
		else {
			redirect_to("http://www.google.com");
		}		
	};
}

function initDialogs() {
	$(".newComment").unbind("click");
	$(".commentBubble").unbind("click");
	$(".excerpt").unbind("click");
	$(".excerpt").click(function(e) {showContent(this)});
	$(".newComment").click(function(e){showComments(this)});
	$(".commentBubble").click(function(e){focusInput(this)});
}

function submitComment(obj, id) {
  $.ajaxSetup({error: function(e) {alert("Comment not posted.  Comments may be closed for this post.");}});
	$.ajaxSetup({ beforeSend: function(e) {startComment(obj)}, complete: function(e) {stopComment(obj)}});
	obj = $("#"+obj.id);
	$.post("/wp-comments-post.php", obj.serialize(), function(e){getPostFromID(id)});
	return false;
}

function startComment(obj) {
	$('.commentBubble_submit').attr("src", "/wp-content/themes/modernman/images/submit_comment_anim.gif");
}

function stopComment(obj) {
	$('.commentBubble_submit').attr("src", "/wp-content/themes/modernman/images/submit_comment.png");
}

function getComments(obj) {
	$("#commentDump_"+obj.id).load("/wp-content/plugins/blip_tv/ajax-comments.php?loop_name=load_comment_loop&id="+obj.id);
}

function showComments(obj) {
	$('#commentDump_'+obj.id).slideToggle();
}

function focusInput(obj) {
	$("#"+obj.id+" > .commentBubble_input").focus();
}

// document.ready Functions

function initButtons() {
	$(".button").unbind("click");
	$("menu a").unbind("click");
	$("#subscribe").unbind("click");
	$(".button").click(function(e){buttonClick(this)});
	$('#subscribe').click(function(e){$('#subscribeMenu').slideToggle();});
	$('.menu a').click(function(e){$('#subscribeMenu').hide(); buttonClick(this)});
}

function initCurvyCorners() {
  $(".float_left, .float_right").equalizeCols();
  $('div.sponsor_button,div.title_img,div#footerContain').pngFix();
  $('.vidWindow').corner({tl: { radius: 8 }, tr: { radius: 8 }, bl: { radius: 8 }, br: { radius: 8 }, antiAlias: true, autoPad: false, validTags: ["div"] }); 
}

function initPlayList() {
  $('#mycarousel').jcarousel({ scroll: 1, animation: 1000, itemLoadCallback: playListLoad });
}

// BlipTV Helper Functions

function getPost(vid) {
	$('#vidInfo').load("/wp-content/plugins/blip_tv/ajax-posts.php?video_id="+vid, null, updateTitle);
}

function getPostFromID(id) {
	$('#post_'+id).load("/wp-content/plugins/blip_tv/ajax-posts.php?loop_name=load_loop&id="+id, null, updateTitle);
}

function makeThumb(index, imageurl, title) {
	return "<li class='videoThumb'><a href=\"javascript:playShow("+index+")\"><div class='videoThumbImg'><img src=\""+imageurl+"\" width=\"82\" height=\"45\" /></div></a><p style=\"font-size:.6em;\">"+modernmanize(title)+"</span></p></li>";
}

function modernmanize(title) {
	words = title.split(" ");
	first_word = words[0];
	words.shift();
	other_words = words.join(" ")
	return first_word+" <span class=\"orange\">"+other_words+"</span>";
}

function num_to_word(num) {
	switch (num) {
		case 1: return "one"; break;
		case 2: return "two"; break;
		case 3: return "three"; break;
		case 4: return "four"; break;
		case 5: return "five"; break;
		case 6: return "six"; break;
		case 7: return "seven"; break;
		case 8: return "eight"; break;
		case 9: return "nine"; break;
		case 10: return "ten"; break;
		case 11: return "eleven"; break;
		case 12: return "twelve"; break;
		case 13: return "thirteen"; break;
		case 14: return "fourteen"; break;
		case 15: return "fifteen"; break;
		case 16: return "sixteen"; break;
	}
}

function updateTitle() {
	document.title = "Modern Man - " + stripSillyShit(postTitle);
	initDialogs();
}

function stripSillyShit(text) {
	text = text.replace("&#8217;", "'");
	text = text.replace("&#8230;", "...");
	return text;
}

function buttonClick(obj) {
	url = false;
	switch(obj.id) {
		case "next": getNextShow(); break;
		case "back": getPrevShow(); break;
		case "itunes": url = "itpc://"+blipSubDomain+".blip.tv/rss/itunes/"; break;
		case "download": url = currentBlipDownloadableURL; break;
		case "miro": url = "http://subscribe.getmiro.com/?url1=http%3A//"+blipSubDomain+".blip.tv/rss"; break;
		case "rss": url = "http://feeds.feedburner.com/ModernManComedy/?format=xml"; break;
		case "technorati": url = "http://technorati.com/faves/?add="+postPermaLink; break;
		case "digg": url = "http://digg.com/submit?phase=2&url="+postPermaLink+"&title="+postTitle; break;
		case "reedit": url = "http://reddit.com/submit?url="+postPermaLink+"&title="+postTitle; break;
		case "newsvine": url = "http://www.newsvine.com/_tools/seed?popoff=0&u="+postPermaLink+"&title="+postTitle; break;
		case "stumbleupon": url = "http://www.stumbleupon.com/submit?url="+postPermaLink+"&title="+postTitle; break;
		case "delicious": url = "http://del.icio.us/post?url="+postPermaLink+"&title="+postTitle; break;
		case "bloglines": url = "http://www.bloglines.com/sub/"+postPermaLink+"&title="+postTitle; break;
		case "facebook": url = "http://www.facebook.com/sharer.php?u="+postPermaLink+"&t="+postTitle; break;
		case "donate": $("#donateForm").submit(); break;
		case "join": $("#joinForm").submit(); break;
		case "vote": url = "/vote-for-us"; break;
		case "tellafriend": url = "/tell-a-friend"; break;
		case "login": redirect_to("/wp-login.php");
	}
	if (url) popout_to(url);
}



function redirect_to(loc) {
	window.location.href = loc;
}

function popout_to(loc) {
	window.open(loc);
}