var project_toggle = 'true';

$(document).ready(function(){$("#client_projects_header").click(function () {
	$("#client_projects").slideToggle("fast");
	if (project_toggle) {
		project_toggle = false;
		$("#client_projects_header .right").replaceWith('<span class="right">[+]</span>');
	} else {
		project_toggle = true;
		$("#client_projects_header .right").replaceWith('<span class="right">[-]</span>');
	}
});});

$(document).ready(function(){$("#client_projects h3:not(.project_completed)").click(function () {$("~ ul:first", this).slideToggle("fast");});});

var notifications_toggle = false;
$(document).ready(function(){$("#notifications_header").click(function () {
	$("#notifications_header + div:first").slideToggle("fast");
	if (notifications_toggle) {
		notifications_toggle = false;
		$("#notifications_header .right").replaceWith('<span class="right">[+]</span>');
	} else {
		notifications_toggle = true;
		$("#notifications_header .right").replaceWith('<span class="right">[-]</span>');
	}
});});

$(document).ready(function(){$("#client_projects h4").click(function () {
	var parts = $(this).attr('id').split('_');
	var project_id = parts[3];
	var height = $("#project_comments_container_"+project_id).css("height");
	if (height == 'auto') {
		$("#project_comments_header_"+project_id+" .right").replaceWith('<span class="right">[-]</span>');
	} else {
		$("#project_comments_header_"+project_id+" .right").replaceWith('<span class="right">[+]</span>');
	}
	$("#project_comments_container_"+project_id).slideToggle("fast");
});});

$(document).ready(
	function () {
		$('#projects_list').Sortable(
			{
				accept : 		'sortableitem',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		1,
				fx:				200,
				axis:			'vertically',
				revert:			true
			}
		)
	}
);									
	
$(document).ready(					
	function() {
		$(".project_status").click(
			function () {
				var parts = this.id.split('_');
				var project_id = parts[2];
				$.ajax({
					type: "POST",
					url: "manage_projects.php",
					data: "project_id="+project_id+"&action=toggle_project",
					success: function(msg) {
						if (msg == 'completed') {
							$('#project_header_'+project_id+' ~ ul:first').hide()
							$('#project_header_'+project_id).addClass('project_completed');
							$('#project_header_'+project_id).removeClass('warning');
							$('#project_header_'+project_id).unbind();
						} else if (msg == 'open') {
							$('#project_header_'+project_id).removeClass('project_completed');
							$('#project_header_'+project_id).click(function () {$("~ ul:first", this).slideToggle("fast");});
						}
					}
				});
			}
		);
	}
);

$(document).ready(					
	function() {
		$(".todo_status").click(
			function () {
				var parts = this.id.split('_');
				var todo_id = parts[2];
				$.ajax({
					type: "POST",
					url: "manage_projects.php",
					data: "todo_id="+todo_id+"&action=toggle_todo",
					success: function(msg) {
						if (msg == 'completed') {
							$('#todo_list_item_'+todo_id+' .todo_description').addClass('todo_completed');
						} else if (msg == 'open') {
							$('#todo_list_item_'+todo_id+' .todo_description').removeClass('todo_completed');
						}
					}
				});
			}
		);
	}
);

function remove_todo(todo_id) {
	if (confirm('Are you sure you wish to delete this todo item?')) {
		$.ajax({
			type: "POST",
			url: "manage_projects.php",
			data: "todo_id="+todo_id+"&action=remove_todo",
			success: function() {
				$('#todo_list_item_'+todo_id).remove()
				$('#edit_todo_list_item_'+todo_id).remove();
			}
		});
	}					
	return false;
}

function remove_project(project_id) {
	if (confirm('Are you sure you wish to delete this project?')) {
		$.ajax({
			type: "POST",
			url: "manage_projects.php",
			data: "project_id="+project_id+"&action=remove_project",
			success: function() {
				$('#project_header_'+project_id).remove();
				$('#toggle_project'+project_id).remove()
				$('#project_todo_list_'+project_id).remove();
				$('#remove_project_'+project_id).remove();
			}
		});
	}					
	return false;
}

function add_todo(div_id) {
	$('#add_todo_link_'+div_id).hide("fast");
	$('#add_todo_'+div_id).show("fast");
	return false;
}

function cancel_add_todo(div_id) {
	$('#add_todo_link_'+div_id).show("fast");
	$('#add_todo_'+div_id).hide("fast");
	return false;
}

function edit_todo(div_id) {
	$('#todo_list_item_'+div_id).hide("fast");
	$('#edit_todo_list_item_'+div_id).show("fast");
	return false;
}

function cancel_edit_todo(div_id) {
	$('#todo_list_item_'+div_id).show("fast");
	$('#edit_todo_list_item_'+div_id).hide("fast");
	return false;
}


$(document).ready(function(){$('#date').datepicker();});

$(document).ready(function(){$("#add_project_button").click(function () {$("#add_project").show("fast");$("#add_project_button").hide("fast");});});

function cancel_add_project() {
	$("#add_project").hide("fast");
	$("#add_project_button").show("fast");
	return false;
}

$(function(){
	$('#start_date').datepicker();
	$('#end_date').datepicker();
	$('#expiration_date').datepicker();
	$('#include_dates').datepicker();
	$('#exclude_dates').datepicker();
});

function change_repeat_interval() {
	var indexNum = document.getElementById('repeat_interval').selectedIndex;
	var selected_value = document.getElementById('repeat_interval')[indexNum].value;
	if (selected_value == "days of week") {
		$('#these_relative_days').hide(500);
		$('#repeat_options').show(500);
		$('#days_of_week').show(500);
	} else if (selected_value == "these relative days") {
		$('#days_of_week').hide(500);
		$('#repeat_options').show(500);
		$('#these_relative_days').show(500);	
	} else {
		$('#repeat_options').hide(500);
	}
}

function add_include_date() {
	var new_date = $('#include_dates').attr('value');
	if (new_date == null) return false;
	var include_yearly = $('#include_every_year').attr('checked');
	if (include_yearly) {
		new_date = new_date + ' 1';
	} else {
		new_date = new_date + ' 0';
	}
	
	var optn = document.createElement("option");
	optn.text = new_date;
	document.getElementById('include_dates_select').options.add(optn);
	$('#include_dates').attr('value', '');
	$('#include_every_year').attr('checked', false);
	return false;
}

function add_exclude_date() {
	var new_date = $('#exclude_dates').attr('value');
	if (new_date == null) return false;
	var include_yearly = $('#exclude_every_year').attr('checked');
	if (include_yearly) {
		new_date = new_date + ' 1';
	} else {
		new_date = new_date + ' 0';
	}
	
	var optn = document.createElement("option");
	optn.text = new_date;
	document.getElementById('exclude_dates_select').options.add(optn);
	$('#exclude_dates').attr('value', '');
	$('#exclude_every_year').attr('checked', false);
	return false;
}

function remove_include_date() {
	var indexNum = document.getElementById('include_dates_select').selectedIndex;
	document.getElementById('include_dates_select').remove(indexNum);
	return false;
}

function remove_exclude_date() {
	var indexNum = document.getElementById('exclude_dates_select').selectedIndex;
	document.getElementById('exclude_dates_select').remove(indexNum);
	return false;
}

function add_tag() {
	var tag_name = $('#new_tag').attr('value');
	if (tag_name == null) return false;
	$.ajax({
		type: "POST",
		url: "add_tag.php",
		data: "tag_name="+tag_name,
		success: function(msg){
			if (msg != 0) {
				var temp = msg.split('|');
				var tag_id = temp[0];
				var color = temp[1];
				$('#tags').append('<label for="tag_'+tag_id+'"><span class="entry_tag" style="background: #'+color+';"></span>'+tag_name+'</label><input type="checkbox" name="tag_'+tag_id+'" id="tag_'+tag_id+'" />');
			} else {
				alert('invalid tag name or color');
			}
		}
	});
}


function submit_entry() {
	var error_msg = '';
	if ($('#title').attr('value') == null) {
		error_msg += "You must enter a title\n";
	}
	if ($('#start_date').attr('value') == null) {
		error_msg += "You must enter a start date\n";
	}
	if ($('#end_date').attr('value') == null) {
		error_msg += "You must enter an end date\n";
	}

	var indexNum = document.getElementById('repeat_interval').selectedIndex;
	var selected_value = document.getElementById('repeat_interval')[indexNum].value;
	if (selected_value == '') {
		error_msg += "You must select a repeat interval\n";
	} else if (selected_value == 'days of week') {
		if (!document.getElementById('monday').checked && !document.getElementById('tuesday').checked && !document.getElementById('wednesday').checked && !document.getElementById('thursday').checked && !document.getElementById('friday').checked && !document.getElementById('saturday').checked && !document.getElementById('sunday').checked) {
			error_msg += "You must select at least one day of the week\n";	
		}
	} else if (selected_value == 'these relative days') {
		if (!document.getElementById('first').checked && !document.getElementById('second').checked && !document.getElementById('third').checked && !document.getElementById('fourth').checked && !document.getElementById('fifth').checked && !document.getElementById('last').checked) {
			error_msg += "You must select at least one relative day\n";	
		}
	}
	if (error_msg == '') {
		var selectBox = document.getElementById('include_dates_select');
		$('#include_dates_select').attr('multiple', 'multiple');
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = true;
		}
	
		selectBox = document.getElementById('exclude_dates_select');
		$('#exclude_dates_select').attr('multiple', 'multiple');
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = true;
		}
		document.new_entry.submit();
	} else {
		alert(error_msg);
		return false;
	}
}

	$(document).ready(function() {
		$('#tag_list .entry_tag').each(
			function() {
				var id = $(this).attr('id');
				$('#'+id).click(function () {
					$.farbtastic('#colorpicker').linkTo(function(color) {
						$('#'+id).css('backgroundColor', color);
						$('#'+id).attr('value', color);
					});
					$("#colorpicker").show("slow");
				});
				$('#'+id).blur(function () {
					var color = $('#'+id).attr('value');
					$("#colorpicker").hide("slow");
					$.ajax({
						type: "POST",
						url: "edit_tag.php",
						data: "tag_color="+color+'&tag_id='+id,
						success: function(msg){
							$('#tag_list .entry_tag').each(
								function() {
									$('.'+id).css('background', color);
								}
							);
						}
					});
				});
				jQuery.farbtastic('#colorpicker').linkTo('#'+id);
			}
		);

	});

	$(function(){


		var link = false;
	
		$(".large_calendar td a").click(function() {
			link = true;
		});
	
		$(".large_calendar td").click(function() {
			if (link) return;
			var id = $(this).attr("id");
			var date = id.split('-');
			var month = date[1];
			var day = date[2];
			var year = date[3];
			window.location = 'manage_entry.php?height=650&width=400&modal=true&month='+month+'&day='+day+'&year='+year;
		});

	});