/* Rounded Boxes & PNG Fixes */
DD_roundies.addRule('#addquotelink,#deletequotelink,#whatsthis,.fancybutton,a.closebox','6px',true);
DD_roundies.addRule('#innermain','8px',true);
DD_roundies.addRule('#addquote h3','6px 6px 0 0',true);
DD_roundies.addRule('.modal','10px',true);
DD_roundies.addRule('#playlistcontrols a,#quotewrap');

var grabQuoteInterval;
var curQuote = 0;
var allQuotes = Array();

function initQuote(animate) {
	if(animate) {
		$("#quotewrap").animate({
			top: "43px"
		},1500,"easeOutQuad", function() {
			$("a:not('.hidden')",$("#quotewrap")).fadeIn(1000);
			grabQuotes();
			grabQuoteInterval = setInterval(grabQuotes, 7500);
		});
	} else {
		$("#quotewrap").css("top","43px");
		$("a:not('.hidden')",$("#quotewrap")).fadeIn(1000);
		grabQuotes();
		grabQuoteInterval = setInterval(grabQuotes, 7500);
	}
};

function grabQuotes() {
	if(curQuote == 0) {
		$.get("/v4/wp-content/themes/revmitczV2/includes/grabquote.php", function(data) {
			allQuotes = data.split('<span class="end"></span>');
		});
		showQuote();
	} else {
		showQuote();
	}
};

function showQuote() {
	$("#quote").fadeOut("slow", function() {
		$("#quote").html(allQuotes[curQuote]);
		$("#quote").fadeIn("slow");
		if(curQuote < (allQuotes.length-2)) {
			curQuote++;
		} else {
			curQuote = 0;
		}
	});
};

$(document).ready(function() {
	//Check all links on page
	$('a').filter(function() {
	//abort if we're dealing with an internal link
	return this.hostname && this.hostname !== location.hostname;
	}).addClass("externallink").attr({target: "_blank",title: "Opens in a new window"});

	var quoteForms = '<div id="addquote" class="modal"><a href="#" title="click to close" class="closebox">x</a><div class="innermodal" id="inneradd"><h3>Add A Quote</h3><form id="addquoteform" action="functions/addquote.php"><p>Add your very own quote about me, Rev. Mitcz. It shows up immediately (don\'t make me regret that decision) but I reserve the right to delete stupid crap.</p><ol><li class="clearfix"><label for="quotename">Your Name : </label><input type="text" id="quotename" name="quotename" maxlength="52" /><span class="formnote">(This is the name that will appear beneath the quote)</span></li><li class="clearfix"><label for="quotecredit">Your "title" : </label><input type="text" id="quotecredit" name="quotecredit" maxlength="52" /><span class="formnote">(Usually your occupation, but can also be silly,<br /> like "Ass Enthusiast". Appears below your name)</span></li><li class="clearfix"><label for="quotetext">Your Quote : </label><textarea id="quotetext" name="quotetext" maxlength="140" rows="10" cols="5"></textarea><span class="formnote">(Max of 140 characters. No HTML or other crazy shit.<br />Line breaks will be retained, however)</span></li><li class="hideline"><input type="hidden" name="formaction" value="addquote" /></li><li class="submitline"><button type="submit" class="fancybutton inline-block disabled" id="postquote" disabled="disabled">Post it!</button></li></ol></form><div class="successwrapper"></div></div></div><div id="deletequote" class="modal"><a href="#" title="click to close" class="closebox">x</a><div class="innermodal" id="innerdelete"><h3>Delete Your Quote</h3><form id="delquoteform" action="functions/addquote.php"><p>Click the delete button below to delete your quote (you can type a new one right away, though). To cancel, click that "X" at the top of this box.</p><ol><li class="hideline"><input type="hidden" name="formaction" value="deletequote" /></li><li class="submitline"><button type="submit" class="fancybutton inline-block" id="deletequotebtn">Delete My Quote</button></li></ol></form><div class="successwrapper"></div></div></div>';
	
	$("#container").append(quoteForms);

	$("#playlistStatus").html('<span>( click my mouth )</span><marquee class="statusmarquee" behavior="scroll" direction="left" scrollamount="4" width="116"></marquee>');
		
	$("#statusmarquee").marquee('statusmarquee');
	$("#statusmarquee").hide();

	var playItem = 0;

	var myPlayList = [
		{name:"chivalrous me",mp3:"/v4/wp-content/themes/revmitczV2/mp3/eatinpussy02.mp3",vol:40},
		{name:"in defense of my pierced dick",mp3:"/v4/wp-content/themes/revmitczV2/mp3/lovemydick.mp3",vol:60},
		{name:'why am i pierced?',mp3:'/v4/wp-content/themes/revmitczV2/mp3/whyimpierced.mp3',vol:37},
		{name:'"<i>didn\'t that hurt</i>?"',mp3:'/v4/wp-content/themes/revmitczV2/mp3/fuckhole.mp3',vol:37},
		{name:"suburbanites",mp3:"/v4/wp-content/themes/revmitczV2/mp3/suburban.mp3",vol:35},
		{name:"oh, facebook...",mp3:"/v4/wp-content/themes/revmitczV2/mp3/facebook.mp3",vol:85},
		{name:"barack hussein obama",mp3:"/v4/wp-content/themes/revmitczV2/mp3/hateyouassfuckmcgee.mp3",vol:35},
		{name:"sarah palin, the worthless cunt",mp3:"/v4/wp-content/themes/revmitczV2/mp3/sarahpalin.mp3",vol:35},
		{name:"scrawny with a belly",mp3:"/v4/wp-content/themes/revmitczV2/mp3/lowercaseb.mp3",vol:50},
		{name:"the importance of oral",mp3:"/v4/wp-content/themes/revmitczV2/mp3/eatmorepussy.mp3",vol:35},
		{name:"myspace is an old whore",mp3:"/v4/wp-content/themes/revmitczV2/mp3/myspace.mp3",vol:50},
		{name:"retards on youtube",mp3:"/v4/wp-content/themes/revmitczV2/mp3/youtube.mp3",vol:50}
	];


	// Audio player
	$("#audioplayer").jPlayer({
		ready: function() {
			$("#audioplayer").setFile(myPlayList[playItem].mp3);
			$("#audioplayer").volume(myPlayList[playItem].vol);
			$(".statusmarquee").html(myPlayList[playItem].name);
		}
	});

	$("#audioplayer").jPlayerId("play","playsound");
	$("#audioplayer").jPlayerId("pause","pausesound");
	$("#audioplayer").onProgressChange(function(loadPercent, playedPercentRelative,playedPercentAbsolute,playedTime,totalTime) {
		if(playedTime < 10) {
			$("#pausesound").css("visibility","hidden");
		} else {
			$("#pausesound").css("visibility","visible");
			$(".statusmarquee").show();
			$("#playlistStatus span").hide();
			if(playedTime >= (totalTime-100) && playedTime > 0) {
				playNext();
			};
		}
	})
	
	$("#playlistcontrols #track_next").click(function() {
		playNext();
		return false;
	});
	
	$("#playlistcontrols #track_prev").click(function() {
		playPrev();
		return false;
	});
	
	function setTrack(index) {
		playItem = index;
		$("#audioplayer").setFile(myPlayList[index].mp3);
		$("#audioplayer").volume(myPlayList[index].vol);
		$(".statusmarquee").html(myPlayList[index].name);
	}
	
	function playNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		setTrack(index);
		$("#audioplayer").play();
	}
	
	function playPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		setTrack(index);
		$("#audioplayer").play();
	}
	

	// For the quotes
	$("#addquotelink").bind("click", function() {
		$("h3","#addquote").html("Add A Quote");
		$("#inneradd .successwrapper").html("");
		$("#addquoteform").show();
		$("#quotetext").val("");
		$.blockUI({
			message: $("#addquote"),
			css: {
				border: 'none',
				background: 'transparent',
				top: '10%'
		     },
		     centerX: true,
		     centerY: true,
		     fadeIn: 500
		});
		return false;
	});
	
	$("#deletequotelink").bind("click", function() {
		$("h3","#deletequote").html("Delete Your Quote");
		$("#innerdelete .successwrapper").html("");
		$("#delquoteform").show();
		$.blockUI({
			message: $("#deletequote"),
			css: {
				border: 'none',
				background: 'transparent',
				top: '10%'
		     },
		     centerX: true,
		     centerY: true,
		     fadeIn: 500
		});
		return false;
	});
	
	$('textarea[maxlength]').keyup(function(){
		var limit = parseInt($(this).attr('maxlength'));
		var text = $(this).val();
		var chars = text.length;
	
		if(chars > limit){
			var new_text = text.substr(0, limit);
			$(this).val(new_text);
		}
		
		if($("#quotename").val().length > 2 && text.length > 5) {
			$("#postquote").removeAttr("disabled").removeClass("disabled");
		} else {
			$("#postquote").attr("disabled", true).addClass("disabled");
		}
	});
	
	$("#quotename").live("keyup", function() {
		if($("#quotename").val().length > 2 && $("#quotetext").val().length > 5) {
			$("#postquote").removeAttr("disabled").removeClass("disabled");
		} else {
			$("#postquote").attr("disabled", true).addClass("disabled");
		}
	});
	
	$("#addquoteform").bind("submit", function() {
		var dataString = $("#addquoteform").serialize();
		$("#postquote").attr("disabled", true);
	    $.ajax({
	      type: "POST",
	      url: "/v4/wp-content/themes/revmitczV2/functions/addquote.php",
	      data: dataString,
	      dataType: "html",
	      success: function(msg) {
	      	$("#addquoteform").fadeOut("slow", function() {
	      		$("#deletequotelink").show();
	      		$("#addquotelink").hide();
	      		$("h3","#addquote").html("Posted! Thank you.");
	      		$("#inneradd .successwrapper").html('<div class="postsuccess"><p>Thank you for your textual contribution.<br />Due to the random nature of the quote grabber, it might be a few minutes before you see it.</p><p><b>NOTE : </b>if you\'re unhappy with the way your quote looks, you can click the "delete my quote" button. That option will disappear once you leave my site, however.</p><a class="fancybutton" href="#">Schweeet!</a></div>');
				$("a.closebox, a.fancybutton").bind("click", function() {
					curQuote = 0;
					grabQuotes();
					$.unblockUI();
					return false;
				});	      		
	      	});
	      }
	    });
		return false;
	});
	
	
	$("#delquoteform").bind("submit", function() {
		var dataString = $("#delquoteform").serialize();
	    $.ajax({
	      type: "POST",
	      url: "/v4/wp-content/themes/revmitczV2/functions/addquote.php",
	      data: dataString,
	      dataType: "html",
	      success: function(msg) {
	      	$("#delquoteform").fadeOut("slow", function() {
	      		$("#deletequotelink").hide();
	      		$("#addquotelink").show();
	      		$("h3","#deletequote").html("Poof! All Gone!");
				$("#innerdelete .successwrapper").html('<div class="postsuccess"><p>Okie dokie. Your quote was deleted and you\'re free to<br />try again now if you so choose.</p><a class="fancybutton">Rightie-o!</a><br /></div>');
				$("a.closebox, a.fancybutton").bind("click", function() {
					curQuote = 0;
					grabQuotes();
					$.unblockUI();
					return false;
				});
	      	});
	      }
	    });
		return false;
	});
	
	
	$("a.closebox, a.fancybutton").bind("click", function() {
		$.unblockUI();
		return false;
	});
});