$.extend({
	getUrlVars: function() {
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; });
		return vars;
	}
});

function checkHover() { 
	if (!obj) $('#blogMenu').fadeOut('slow');
	else setTimeout("checkHover()", 900);
}

$(document).ready(function() {

	var c = $.getUrlVars()['c'];
	
	if ($.getUrlVars()['commentsPage']) {
		var commentsPage = $.getUrlVars()['commentsPage'];
	} else {
		var commentsPage = '1';
	}
	
	if (c) {
		var toggleLink = $('a#'+c);
		var container = toggleLink.parent().parent().parent().children(".designContainer");
		$.get("web_ajax/pullThreadAjax.php", { c: toggleLink.attr("id"), t: "checkup", commentsPage: commentsPage }, function(data) {
  			$('.result').html(data);
  				container.append(data);
  				container.addClass("dataLoaded");
  				container.slideToggle("slow");
  				
  				container.find("a.img_thumb").lightBox();
    			container.find("a.attachedImage").lightBox();
  				
  				$(".delMessage").click(function(){						
					var currMsg = $(this).parent().parent().parent();
					var msgID = $(this).attr('id');
					if (confirm("Delete this message? \n\n"+$('.'+msgID+'.messageText').html().replace(/(<([^>]+)>)/ig,""))) {
						$.post("/web_ajax/delMessageAjax.php", { messageID: $(this).attr('id') },
							function(data){
								if (data.success)
									currMsg.fadeOut();
								else 
									alert(data.msg);
									
							}, "json"
						);
			
					}
					return(false);
				});
				
				$("a.toggleViewComment").click(function () {
					var containerComment = $(this).parent().parent().parent().parent().parent().parent().children(".designContainer");
					containerComment.slideToggle("slow");
					return false;
				});
				
				$("div.topBottom").children().animate({"opacity": "0"}, "fast");
				
				$("div.topBottom").hover(
					function() {
						$(this).children().animate({"opacity": "1"}, "slow");
					},
					function() {
						$(this).children().animate({"opacity": "0"}, "slow");
					}
				);
				
				$(".leaveComment").click(function () {
					var targetNode = $(this).parent().prev();
					if (!targetNode.hasClass("visible")) {
						var commentBoxHTML = $("div#commentBoxWrapper").html();
						targetNode.append(commentBoxHTML);
						targetNode.slideDown();
						targetNode.addClass("visible");

						$(".submitComment").click(function () {
							var threadID = $(this).parent().parent().parent().parent().parent().attr("id");
							var textareaNode = $(this).prev();
							var commentBoxArea = $(this).parent().parent().parent();
							var destNode = $(this).parent().parent().parent().parent().children(".thoughts_field");
							$(this).parent().next().children("input").attr("name", "imageComment"+threadID);
							$(this).parent().next().children("input").attr("id", "imageComment"+threadID);
							$.ajaxFileUpload({
								url:'/web_ajax/comment2Ajax.php?req=add&type=2&destID='+threadID+'&comment='+urlEncode(textareaNode.val())+'&template=lm_s_thread_commentNew&imageFileObjName=imageComment'+threadID+'&pAvatar=true',
								secureuri:false,
								fileElementId:'imageComment'+threadID,
								dataType: 'passThrough',
								beforeSend:function(){
								},
								complete:function(){
									commentBoxArea.slideUp();
									textareaNode.val('');
									$("#imageComment"+threadID).val('');
								},
								success: function (data, status){
									if (data == 'error')
										alert('There was an error uploading your comment.  If uploading an image, please make sure that it is less that 1MB and a .jpg, .png or .gif and try again, otherwise please refresh this page and try again.');
									else {
										destNode.prepend(data);
										
										$(".newData").slideDown();
										
										$(".newData").children("div.topBottom").children().animate({"opacity": "0"}, "fast");
										
										$(".newData").children("div.topBottom").hover(
											function() {
												$(this).children().animate({"opacity": "1"}, "slow");
											},
											function() {
												$(this).children().animate({"opacity": "0"}, "slow");
											}
										);
										
										$(".newData").find(".delMessage").click(function(){
											var currMsg = $(this).parent().parent().parent();
											var msgID = $(this).attr("id");
											if (confirm("Delete this message? \n\n"+$('.'+msgID+'.messageText').html().replace(/(<([^>]+)>)/ig,""))) {
												$.post("/web_ajax/delMessageAjax.php", { messageID: $(this).attr('id') },
													function(data){
														if (data.success)
															currMsg.fadeOut();
														else 
															alert(data.msg);
															
													}, "json"
												);
									
											}
											return(false);
										});
										
										$(".newData").find("a.attachedImage").lightBox();
									}
								},
								error: function (data, status, e){ window.location.reload(); }
							});
							
						});
					} else {
						targetNode.slideDown();
					}
				});
			});
	}

	$('a.img_thumb').lightBox();
    $('a.attachedImage').lightBox();
	$("div.designContainer").hide();
	var currPane=$('#currentPane').val();
	$('#'+currPane+'').show(function() {
	
	});
	$("a.toggleView").click(function () {
		var container = $(this).parent().parent().parent().children(".designContainer");
		if (!container.hasClass("dataLoaded")) {
			$.get("web_ajax/pullThreadAjax.php", { c: $(this).attr("id"), t: "checkup", commentsPage: commentsPage }, function(data) {
  			$('.result').html(data);
  				container.append(data);
  				container.addClass("dataLoaded");
  				container.slideToggle("slow");
  				
  				container.find("a.img_thumb").lightBox();
    			container.find("a.attachedImage").lightBox();
  				
  				$(".delMessage").click(function(){						
					var currMsg = $(this).parent().parent().parent();
					var msgID = $(this).attr('id');
					if (confirm("Delete this message? \n\n"+$('.'+msgID+'.messageText').html().replace(/(<([^>]+)>)/ig,""))) {
						$.post("/web_ajax/delMessageAjax.php", { messageID: $(this).attr('id') },
							function(data){
								if (data.success)
									currMsg.fadeOut();
								else 
									alert(data.msg);
									
							}, "json"
						);
			
					}
					return(false);
				});
				
				$("a.toggleViewComment").click(function () {
					var containerComment = $(this).parent().parent().parent().parent().parent().parent().children(".designContainer");
					containerComment.slideToggle("slow");
					return false;
				});
				
				$("div.topBottom").children().animate({"opacity": "0"}, "fast");
				
				$("div.topBottom").hover(
					function() {
						$(this).children().animate({"opacity": "1"}, "slow");
					},
					function() {
						$(this).children().animate({"opacity": "0"}, "slow");
					}
				);
				
				$(".leaveComment").click(function () {
					var targetNode = $(this).parent().prev();
					if (!targetNode.hasClass("visible")) {
						var commentBoxHTML = $("div#commentBoxWrapper").html();
						targetNode.append(commentBoxHTML);
						targetNode.slideDown();
						targetNode.addClass("visible");

						$(".submitComment").click(function () {
							var threadID = $(this).parent().parent().parent().parent().parent().attr("id");
							var textareaNode = $(this).prev();
							var commentBoxArea = $(this).parent().parent().parent();
							var destNode = $(this).parent().parent().parent().parent().children(".thoughts_field");
							$(this).parent().next().children("input").attr("name", "imageComment"+threadID);
							$(this).parent().next().children("input").attr("id", "imageComment"+threadID);
							$.ajaxFileUpload({
								url:'/web_ajax/comment2Ajax.php?req=add&type=2&destID='+threadID+'&comment='+urlEncode(textareaNode.val())+'&template=lm_s_thread_commentNew&imageFileObjName=imageComment'+threadID+'&pAvatar=true',
								secureuri:false,
								fileElementId:'imageComment'+threadID,
								dataType: 'passThrough',
								beforeSend:function(){
								},
								complete:function(){
									commentBoxArea.slideUp();
									textareaNode.val('');
									$("#imageComment"+threadID).val('');
								},
								success: function (data, status){
									if (data == 'error')
										alert('There was an error uploading your comment.  If uploading an image, please make sure that it is less that 1MB and a .jpg, .png or .gif and try again, otherwise please refresh this page and try again.');
									else {
										destNode.prepend(data);
										
										$(".newData").slideDown();
										
										$(".newData").children("div.topBottom").children().animate({"opacity": "0"}, "fast");
										
										$(".newData").children("div.topBottom").hover(
											function() {
												$(this).children().animate({"opacity": "1"}, "slow");
											},
											function() {
												$(this).children().animate({"opacity": "0"}, "slow");
											}
										);
										
										$(".newData").find(".delMessage").click(function(){
											var currMsg = $(this).parent().parent().parent();
											var msgID = $(this).attr("id");
											if (confirm("Delete this message? \n\n"+$('.'+msgID+'.messageText').html().replace(/(<([^>]+)>)/ig,""))) {
												$.post("/web_ajax/delMessageAjax.php", { messageID: $(this).attr('id') },
													function(data){
														if (data.success)
															currMsg.fadeOut();
														else 
															alert(data.msg);
															
													}, "json"
												);
									
											}
											return(false);
										});
										
										$(".newData").find("a.attachedImage").lightBox();
									}
								},
								error: function (data, status, e){ window.location.reload(); }
							});
							
						});
					} else {
						targetNode.slideDown();
					}
				});
			});
		} else {
			container.slideToggle("slow");
		};
		
		return false;
	});
	$('#loading')
    .hide()  // hide it initially
    .ajaxStart(function() {
        $(this).show();
    })
    .ajaxStop(function() {
        $(this).hide();
    });
	$("div.designContainer:not('div#"+currPane+"')").hide();
	/* $("div.designContainer:last").show(); */
    $('.indx').click(function() {
    	$('#start'+$(this).attr('name')).next().children(".designContainer").show();
    	$.scrollTo('#start'+$(this).attr('name'));
    	return false;
    });
});