$(document).ready(function(){
	$("#menu ul li").hover(
		function() { $(this).addClass("active"); },
		function() { $(this).removeClass("active"); }
	);

	$("#menu ul.sub").parent().hover(
		function() { $(this).find("ul.sub").fadeIn(200); },
		function() { $(this).find("ul.sub").fadeOut(200); }
	);

	$(".tabs h2").click(function() {
		toggleTabs(this);
	});

	toggleInputDefault("#field_terms");
	toggleOptionsTab();
	
	// highlight logical fields when first visiting the following screens
	$("#sign_in_email").select(); // sign in
	$("#sign_up_email").select(); // sign up
	$("#forgot_last_name").select(); // forgot password
	$("#forgot_ans").select(); // forgot password
	
	if( $("#edit_page").length > 0 ) {
		$("#entry_title").focus();
		editStart();
	}

	$("#featured td a").hover(
		function() { $(this).find(".feat_img").animate({ opacity: 0.8 }, 200); },
		function() { $(this).find(".feat_img").animate({ opacity: 0.5 }, 200); }
	);

	$(".soc_media_list").hover(
		function() { $(this).addClass("hover"); },
		function() { $(this).removeClass("hover"); }
	);

	$("#link_url").keyup(function(e) {
		// alert( $(this).attr("value") );

		if( $(this).attr("value") != "" ) {
			$("#content_editor").fadeOut();
		} else {
			$("#content_editor").fadeIn();
		}
	});
	
	$(".orderable_pics").sortable({
		stop: function(e, ui) {
			var imageIds = [];

			$(".orderable_pics a").each(function() {
				imageIds[imageIds.length] = $(this).attr("id").substr(4);
			});

			$.get("blog-action.php", {
				task: "order_blog_photo",
				blog_post_id: $("#field_blog_post_id").val(),
				pic_order: imageIds.join("|")
			});
		}
	}).disableSelection();

	// blog photos keyboard arrow navigation
	if( $(".blog_photos").length > 0 ) {
		var $picNav = $(".blog_photos");

		// setup next/prev picture nav via arrow keys
		$(document).keypress(function(e) {
			photosKeyNav(e);
		});

		// cancel nav if focus in editable field
		$picNav.find("textarea, input").focus(function() { $(document).unbind("keypress"); });
		$picNav.find("textarea, input").blur(function() { $(document).bind("keypress", function(e) { photosKeyNav(e); }); });

		// setup photo embed link
		$picNav.find(".embed").click(function() {
			$("#embed_code").fadeIn().find("input").focus();
		});
	}

	$(".focus_select").focus(function() {
		$(this).select();
	});
});

function photosKeyNav(e) {
	var whichKey = e.keyCode;
	var $picNav = $(".blog_photos");

	if( whichKey == "37" && $picNav.find(".prev").length > 0 ) { window.location.href = $picNav.find(".prev").attr("href"); } // prev
	if( whichKey == "39" && $picNav.find(".next").length > 0 ) { window.location.href = $picNav.find(".next").attr("href"); } // next
}

function toggleTabs(clickedTab) {
    $tab = $(clickedTab);
	var this_section_id = $tab.attr("id").split("_")[0];

	$(".tabs h2").removeClass("active"); // all tabs non-active
	$tab.addClass("active"); // set clicked tab to active

	$(".tabs div.box_content").removeClass("active");
	$("#"+this_section_id).addClass("active");
}

function toggleInputDefault(inputId) {
	$this_input = $(inputId);
	var default_text = $this_input.attr("value");

	$this_input.focus(function() {
		if( $this_input.attr("value") == default_text ) $this_input.attr("value", "");
	});
	$this_input.blur(function() {
		if( $this_input.attr("value") == "" ) $this_input.attr("value", default_text);
	});
}

function toggleOptionsTab() {
	$opt_tab = $("#opt_tab_01");
	$(".event_details").hover(function() {
		$opt_tab.fadeIn("fast");
	}, function() {
		$opt_tab.fadeOut("fast");
	});
}

// *****************************************

// Hide or show elements on a page
// nodeIds - ID of one or more (separated by "|") elements on a page
// visTo - takes a value of 0 or 1 to set visibility as either none or block (by default)
// visType (optional) - allows override of default block visibility type for other options (e.g. inline, table-row-group, etc.)
function hide_show(nodeIds, visTo, visType)
{
	var nodeIdArr = nodeIds.split("|");
	
	for(i=0; i<nodeIdArr.length; i++) {
		var nodeId = nodeIdArr[i];
		var $thisNode = $("#"+nodeId);
		
		if($thisNode) {
			if(visTo == 1)
				$thisNode.show();
			else
				$thisNode.hide();
		}
	}
}

function tc(entry_id, blog_post_id) {
	if( entry_id != "" )
		$.get("/action.php", { "task":"tc", "e":entry_id });
	else if( blog_post_id != "" )
		$.get("/action.php", { "task":"tc", "b":blog_post_id });
	return true;
}

function vc(vid_id) {
	$.get("/action.php", { "task":"vc", "v":vid_id });
	return true;
}


function getEventsByDate() {
	var evtDate = $("#evt_sdate").attr("value");

	if( evtDate != "") {
		$.get("action.php", { task: "ajax_event_by_date", evt_date: evtDate }, function(data) {
			$("#msg_area").html(data);
		});
	}
}

function showTip(tip) {
	var tipData = tip.split("|");
	var tipTitle = tipData[0];
	var tipText = tipData[1];

	$("#msg_area").html("<h3>" + tipTitle + "</h3><div class=box><p>" + tipText + "</p></div>");
}

function changeCalGrid(thisDate, mode, viewing) {
	$.get("action.php", { "task":"ajax_cal_grid", "dt":thisDate, "md":mode, "vw":viewing }, function(data) {
		$("#cal_grid").html(data);
	});
}

function updateForumBlotter() {
	$.get("action.php", { "task":"update_forum_blotter" }, function(data) {
		$("#forum_blotter").html(content);
	});
}

function signUpWindow() {
	window.open("http://www.feedtacoma.com/sign-up.php", "sign_up", "width=500,height=400,status=0,toolbar=0,location=0,menubar=0");
}

function shopTermsWindow() {
	window.open("http://shops.feedtacoma.com/terms-of-use.php", "terms", "width=500,height=400,status=0,toolbar=0,location=0,menubar=0,scrollbars=1");
}

function popUpWindow(loc) {
	window.open(loc, "something", "width=500,height=400,status=0,toolbar=0,location=0,menubar=0,scrollbars=1");
}


// *** SIGN IN / UP FUNCTIONS ***
function userSignIn() {
	var url = "action.php";
	var params = "task=sign_in";
	sendPostRequest(url, params);

	$("sign_in_area").innerHTML = http.responseText;
}

function securityQA(path) {
	var ques = $("ques_field").value;
	var ans = $("ans_field").value;

	var error = "";
	error += ( ques == "" ? "Please enter a security question.\n" : "" );
	error += ( ans == "" ? "Please enter an answer to your security question.\n" : "" );

	if( error != "" ) {
		alert(error);
	} else { // save security Q and A to db
		var url= path + "action.php?task=update_security_q_a&q=" + ques + "&a=" + ans;
		sendRequest(url);

		if( http.responseText == "success" ) {
			// show "thanks" button
			var saveBtn = $("security_submit");
			saveBtn.disabled = "true";
			saveBtn.className = "buttons submit";
			saveBtn.value = "Thanks!";

			setTimeout("$('container').removeChild($('security_need'));", 1500);
		}
	}
}


function forumCommentEdit(commentMsgId, type) {
	var $commentObj = $("#msg_" + commentMsgId);

	$.get("/action.php", { "task":"get_msg_text", "type":type, "msg_id":commentMsgId}, function(data) {
		var commentHtml = data;

		var editForm = "<p><textarea id=\"msg_edit_" + commentMsgId + "\" class=\"fill\">" + commentHtml + "</textarea></p>";
		editForm += "<p class=\"no_margin\"><input type=\"button\" class=\"buttons submit\" value=\"Save &raquo;\" onclick=\"this.disabled='true'; this.value='Saving...'; this.className='buttons'; forumCommentSave(" + commentMsgId + ", '" + type + "');\" /></p>";

		$commentObj.html(editForm);
	});
}

function forumCommentSave(commentMsgId, type) {
	var $commentObj = $("#msg_" + commentMsgId);
	var $commentTextarea = $("#msg_edit_" + commentMsgId);
	var commentText = $commentTextarea.val();

	$.get("/action.php", { "task":"save_msg_text", "type":type, "msg_id":commentMsgId, "msg_txt":commentText}, function(data) {
		$commentObj.html(data);
	});
}

function forumCommentDelete(commentMsgId, type) {
	if( confirm("You are about to remove this comment from the forums. Click OK to continue.\n\nNote: This action cannot be undone.") ) {
		$.get("/action.php", { "task":"delete_msg_text", "type":type, "msg_id":commentMsgId }, function(data) {
			if( data == "success" ) {
				var $commentObj = $("#msg_" + commentMsgId);
				$commentObj.parent().fadeOut(function() {
					this.remove();
				});
				// var thisRow = commentObj.parentNode;

				// thisRow.parentNode.removeChild(thisRow);
			}
		});
	}
}

function comicsPostDelete(stripId, postId) {
	if( confirm("You are about to remove this post from your comic strip. Click OK to continue.\n\nNote: This action cannot be undone.") ) {
		$.ajax({ url: "comics-action.php", data: "task=del_comic_post&strip_id="+stripId+"&post_id="+postId, async: false, success: function(msg) {
			if( msg == "success" ) {
				var $postRow = $("#comic_post_" + postId);
				$postRow.fadeOut(function() {
					$(this).remove();
				});
			}
		} });
	}
}


function shopsRemoveItem() {
	return confirm("You are about to remove this item from your store. Don't worry, past order records won't be effected. Click OK to continue.\n\nNote: This action cannot be undone.");
}

function settingsNewAvatar() {
	$("#current_avatar").hide();
	$("#new_avatar").show();
}

