// manages private switch on module edit pages
var privateSwitch = {
	state : 1,
	label : null,
	init : function() {
		privateSwitch.label = $(".privateWrap label");
		if (0 < privateSwitch.label.length) {
			privateSwitch.state = $(privateSwitch.label).siblings("input")[0].value;

			$(privateSwitch.label).click(function(){
				if (1 == privateSwitch.state) {
					$(privateSwitch.label).removeClass("locked");
					$(privateSwitch.label).text("public");
					privateSwitch.state = false;
					$(privateSwitch.label).siblings("input")[0].value = 0;
				} else {
					$(privateSwitch.label).addClass("locked");
					$(privateSwitch.label).text("private");
					privateSwitch.state = true;
					$(privateSwitch.label).siblings("input")[0].value = 1;
				}
				return false;
			});
		}
	}
}

var popTip = {
	currentTip : null,
	open : function(url,elm,popClass,callback) {
		popTip.remove();

		$("body").append("<div class='popTip'><span class='loading'>Loading...</span></div>");
		popTip.currentTip = $(".popTip")[0];

		if (popClass) {
			$(popTip.currentTip).addClass(popClass);
		}

		var linkPos = $(elm).offset();
		$(popTip.currentTip).css("top",linkPos["top"]).css("left",linkPos["left"]).show();

		$(popTip.currentTip).load(url,null,callback);

	},
	replace : function(html,callback) {

	},
	remove : function() {
		$(".popTip").remove();
		popTip.currentTip = null;
	}
}

var flagThis = {
	activeTip : null,
	inProgress : null,
	init : function() {
		$(".flag a").live("click",function() {
			if (!flagThis.inProgress) {
				popTip.open(this.href,this,"popFlag",flagThis.initConfirm);
				flagThis.inProgress = true;
			}
			return false;
		});
	},
	initConfirm : function() {
		// add even handlers to buttons
		$(".popFlag a.btn").bind("click",function() {
			popTip.remove();
			flagThis.inProgress = false;
			return false;
		});

		$(".popFlag form").bind("submit",function() {
			flagThis.activeTip = $(this).parent();
			$.post($(this).attr("action"),$(this).serializeArray(),function(data) {
				$(flagThis.activeTip).html(data);
				flagThis.initConfirm();
			},"html");
			return false;
		});

	}
}

var resolutions = {
	store: new Object(),
	swapDiv : null,
	activeField : null,
	reqUrl : null,
	inProgress : false,
	init : function() {
		if (0 < $("div.entry.type-r,div.entry.type-gr").length) {

			// initialize weekly 'pagination' -- swap only content of bottom portion of module
			$("div.entry.type-r .shuffleNav a,div.entry.type-gr .shuffleNav a").live("click",function() {
				if (!resolutions.inProgress) {
					resolutions.inProgress = true;
					resolutions.swapDiv = $(this).closest("div.resWeek");
					resolutions.requestUrl = $(this).attr("href");


					$(resolutions.swapDiv).fadeTo("def", 0.01, function() {
						$(resolutions.swapDiv).load(resolutions.requestUrl,null,function() {
							$(resolutions.swapDiv).fadeTo("def",1);
							resolutions.inProgress = false;
						});
					});
				}

				return false;
			});

			// initialize group user interaction - swapping entire module contents
			$("div.entry.type-gr td .participants a,div.entry.type-gr a.back").live("click",function() {
				if (!resolutions.inProgress) {
					resolutions.inProgress = true;
					resolutions.swapDiv = $(this).closest("div.entry");
					resolutions.requestUrl = $(this).attr("href");

					$(resolutions.swapDiv).fadeTo("def", 0.01, function() {
						$(resolutions.swapDiv).load(resolutions.requestUrl,null,function() {
							$(resolutions.swapDiv).fadeTo("def",1);
							resolutions.inProgress = false;
						});
					});
				}
				return false;
			});


			// initialize note item interaction

			$("div.entry.type-r td a.resNote,div.entry.type-gr td a.resNote").live("click",function() {
				if (!resolutions.inProgress) {
					resolutions.inProgress = true;
					resolutions.activeField = this;
					popTip.open(this.href,this,'popNote',resolutions.initNoteEdit);
				}
				return false;
			});

			// initialize completion item interaction

			$("div.entry.type-r td a.resComplete,div.entry.type-gr td a.resComplete").live("click",function() {
				if (!resolutions.inProgress) {
					resolutions.inProgress = true;
					resolutions.activeField = this;

					$.getJSON(this.href, function(data) {
						if(resolutions.store[data.id] == undefined || resolutions.store[data.id][data.result.date] == undefined) {
							resolutions.store[data.id] = new Array();
							resolutions.store[data.id][data.result.date] = data.result.state;
						}

						if (1 == resolutions.store[data.id][data.result.date]) {
							$(resolutions.activeField).blur().addClass("resCompleteOn");
							$(resolutions.activeField).closest("div").find(".resPctWeekly").text(data.result.weekly);
							$(resolutions.activeField).closest("div").find(".resPctAll").text(data.result.all);
						} else {
							$(resolutions.activeField).blur().removeClass("resCompleteOn");
						}
						resolutions.inProgress = false;
						resolutions.store[data.id][data.result.date] = data.result.state ? 0 : 1;
						resolutions.activeField.href = resolutions.activeField.href.replace(/(state=)(\d)/, "$1"+resolutions.store[data.id][data.result.date]);
					});
				}

				return false;
			});


		}
	},
	initNoteEdit : function() {
		$(".popTip a.btn2").bind("click",function() {
			popTip.remove();
			resolutions.inProgress = false;
			return false;
		});

		$(".popTip form").bind("submit",function() {
			$.post($(this).attr("action"),$(this).serializeArray(),function(data) {
				if (1 == data.result.state) {
					$(resolutions.activeField).blur().addClass("resNoteOn");
				} else {
					$(resolutions.activeField).blur().removeClass("resNoteOn");
				}
				resolutions.inProgress = false;
				popTip.remove();
			},"json");
			return false;
		});



	},
	handleNoteEdit : function() {

	}
}

var toolDescription = {
	init : function() {
		$('div.extendedDescription').append('<a href="#" class="hideDescription">Hide</a>');
		$("a.hideDescription").bind("click", function(e) {
			toolDescription.hide();
			e.preventDefault();
		});
		$("a.extendDescription").bind("click", function(e) {
			toolDescription.show();
			e.preventDefault();
		});
	},
	show : function() {
		$('a.extendDescription').hide();
		$('div.extendedDescription').slideDown();
	},
	hide : function() {
		$('div.extendedDescription').slideUp(function(){
			$('a.extendDescription').show();
		});
	}
}


$(document).ready(function(){

	// read more link on tools pages
	toolDescription.init();

	$("form.filter select").bind("change",function(){
		$(this).closest("form").submit();
	});

	// Flag this post Links
	flagThis.init();

	// Resolution Interface
	resolutions.init();

});
