var category;
var page;
var project;
var section;

var changing;
var shouldSave = false;

$(function () {
	
	
	
	$("#overlay").fadeTo(0, 0).addClass("hide");
	
	category = $('#content h2 .category').text();
	page = $('#content h2 .page').text();
	project = $('#content h2 .project').html();
	section = $('#content h2 .section').html();
		
	if (page == "In-Progress") {
		page = "Progress";		
	} else if (page == "Home Page") {
		page = "Home";	
	} else if (page == "About MDA") {
		page = "About";	
	}
	
	if (section == "Team Members") {
		section = "Member";	
	}
	if (section == "Branch Offices") {
		section = "Branch";	
	}
		
	//alert("category: " + category + "\nPage: " + page + "\nProject: " + project + "\nSection: " + section);
	
	if (page == "About") {
		swfobject.embedSWF("upload.swf", "about_img", "105", "32", "8.0.0", "", {sText:"Change\nImage", id:"about", windowID:"#about"});	
	}

	setDragDrop();	
	setButtons();
	
});

function setButtons() {
	
	/* * * * * * * * * * *  * COMMON FUNCTIONS * * * * * * * * * * * */
	
			
			// Sets the text area functionality
			$(".desc textarea").bind("keyup blur", function() {
				if ($(this).val() != $(this).text()) {
					shouldSave = true;
					$(this).text($(this).val());
				}
			})
					
			setAnchors();
			
			$("#live").click( function() {
				if (confirm("Are you sure?\n\nYour draft will now be visible to the world. It is recommended that you preview your draft if you have not.")) {
					$.post("save-data.php", {func:"makeDraftLive"}, function(r) {
						if (r.indexOf("false") != -1) {
							alert("There was an error while making the draft live. Please try again.");
						}
					}, "text");
				} else {
					return false;
				}
			});
			
			//$(".window textarea").width($(this).parent().parent().width());
	
	
	/* * * * * * * * * * * * HOME PAGE - PAGE FUNCTIONS * * * * * * * * * * * */
			
			$("table td.restore a").click( function() {
			
				var id = $(this).attr("href");
				id = id.substring(id.lastIndexOf("#") + 1, id.length);
				
				$.post("save-data.php", {name:id, func:"restoreDraft"}, function(r) {
					if (r.indexOf("false") != -1) {
						alert("There was an error while restoring the backup. Please try again.");
					} else {
						alert("Backup restored successfully!\n\nThe backup version is now the current draft.");	
					}
				}, "text");		
																 
			});
	
	
	
	/* * * * * * * * * * * * HOME PAGE - PAGE FUNCTIONS * * * * * * * * * * * */
		
			// Sets Save Button functionality to update the XML file
			$('input.updateHomeData').click( function() { saveHome(); });
			
			// Sets the functionality of the "Remove Image" button in each image list
			$("li.home_image .remove").click(function() {		
				shouldSave = true;
				$(this).parent().fadeTo(200, 0, function() {
					$(this).hide(200, function() {
						$(this).remove();
					});
				});
			});
			
			// Sets the "Add New Image" button functionality to allow users to upload new images
			$('input.add_home').click( function () {
				openNewHomeWindow();		
			});
			
			// Sets the functionality fo the "Change Image" button  in each image list
			$("li.home_image .change").click(function() {									  
				changing = $(this).parent();
				openChangeHomeWindow();		
			});
			
			// Sets the "Change Image" Applu and Cancel button functionality for the pop-up window
			$("#change_home input, #change_home .close-btn").click( function() {
				if ($(this).attr("value") == "Change") {
					if ($('#change_home img[src="uploads/no-image.gif"]').length > 0) {
						alert("You must upload an image before applying");
						return false;
					}
					shouldSave = true;
					changing.find('img.home').replaceWith($('#change_home img.home').clone());
					
					$("*").unbind();
					setDragDrop();	
					setButtons();
	
				}
				$("#change_home").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});
			
			// Sets the "Add Image" Applu and Cancel button functionality for the pop-up window
			$("#new_home input, #new_home .close-btn").click( function() {
				if ($(this).attr("value") == "Add") {
					if ($('#new_home img[src="uploads/no-image.gif"]').length > 0) {
						alert("You must upload an image before applying");
						return false;
					}
					shouldSave = true;

					var newHTML = '<li class="sortable home_image">';
					newHTML += '<img class="home" src="' + $("#new_home img.home").attr("src") + '" alt=" Home Page Image" title=" Home Page Image" height="54" width="81">';
					newHTML += '<div class="remove icons"><img src="close-icon.png" title="Remove this Image" alt="Remove Image" height="20" width="34"><p>Remove Image</p></div>';
					newHTML += '<div class="change icons"><img src="change-icon.png" title="Change this Image" alt="Change Image" height="20 title=" width="34"><p>Change Image</p></div></li>';

					$("#content ul").append(newHTML);
					$("*").unbind();
					setDragDrop();	
					setButtons();
					
				}
				$("#new_home").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});
	
	/* * * * * * * * * * * * PARTNERS - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('#content input.updatePartners').click( function() { savePartners(); });
			
			// Sets the add new member button functionality
			$('#content input.add_partner').click( function() {				
				shouldSave = true;
				var newhtml = '<li style="display: block;" class="sortable partner">	<p class="label">Partner<br>Info</p>';
				newhtml += '<fieldset><input class="first" name="name" value="Partner Name"><input name="url" value="http://www.partnerwebaddress.com"></fieldset>';
				newhtml += '<div class="remove icons"><img src="close-icon.png" title="Remove this Partner" alt="Remove Partner Button" height="20" width="34"><p>Remove Partner</p></div></li>';
				
				$('#content ul').append(newhtml);
				$("*").unbind();
				setDragDrop();	
				setButtons();
				
			});
			
			// Sets the functionality of the "Remove Image" button in each image list
			$("li.partner .remove").click(function() {		
				shouldSave = true;
				$(this).parent().fadeTo(200, 0, function() {
					$(this).slideUp(200, function() {
						$(this).remove();
					});
				});
			});
	
	
	/* * * * * * * * * * * * CONTACT > TEAM MEMBERS - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('#content input.updateContactMembers').click( function() { saveContactMembers(); });
			
			// Sets the add new member button functionality
			$('#content input.add_member').click( function() {				
				shouldSave = true;
				var newhtml = '<li class="sortable member">';
				newhtml += '<fieldset><label>Contact:</label><input class="member_name" value="Name">';
				newhtml += '<input class="member_email" value="Email"></fieldset>';
				newhtml += '<div class="remove icons"><img src="close-icon.png" title="Remove this Team Member" alt="Remove Team Member Button" height="20" width="34"><p>Remove</p></div></li>';
				$('#content ul').append(newhtml);
				$("*").unbind();
				setDragDrop();	
				setButtons();
				
			});
			
			// Sets the functionality of the "Remove Image" button in each image list
			$("li.member .remove").click(function() {		
				shouldSave = true;
				$(this).parent().fadeTo(200, 0, function() {
					$(this).slideUp(200, function() {
						$(this).remove();
					});
				});
			});
			
			
	/* * * * * * * * * * * * CONTACT > BRANCH OFFICES - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('#content input.updateContactBranches').click( function() { saveContactBranches(); });
			
			// Sets the add new member button functionality
			$('#content input.add_branch').click( function() {				
				shouldSave = true;
				var newhtml = '<li class="sortable branch"><p class="label">Branch<br>Office</p>';
				newhtml += '<fieldset><input class="branch_name" value="Branch Office Name">';
				newhtml += '<input class="address" name="address1" value="Street Address"><input name="phone" class="number" value="Phone">';
				newhtml += '<input class="address" name="address2" value="City, State Zip"><input name="fax" class="number" value="Fax"></fieldset>';
				newhtml += '<div class="remove icons"><img src="close-icon.png" title="Remove this Branch Office" alt="Remove Branch Office Button" height="20" width="34"><p>Remove Branch</p></div></li>';
				
				$('#content ul').append(newhtml);
				$("*").unbind();
				setDragDrop();	
				setButtons();
				
			});
			
			// Sets the functionality of the "Remove Image" button in each image list
			$("li.branch .remove").click(function() {		
				shouldSave = true;
				$(this).parent().fadeTo(200, 0, function() {
					$(this).slideUp(200, function() {
						$(this).remove();
					});
				});
			});
			
	
	
	/* * * * * * * * * * * * ABOUT - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('input.updateAboutData').click( function() { saveAbout(); });	
	
	
	/* * * * * * * * * * * * CATEGORY > FINISHED > PROJECT - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('input.updateFinishedProjectData').click( function() { saveFinishedProject(); });	
			// Sets the "Add New Image" button functionality to allow users to upload new images
			$('input.add_image').click( function () {
				openNewImageWindow();		
			});
			
			// Sets the functionality of the "Remove Image" button in each image list
			$("li.image .remove").click(function() {		
				shouldSave = true;
				$(this).parent().fadeTo(200, 0, function() {
					$(this).slideUp(200, function() {
						$(this).remove();
					});
				});
			});
			
			// Sets the functionality fo the "Change Image" button  in each image list
			$("li.image .change").click(function() {									  
				changing = $(this).parent();
				openChangeImageWindow();		
			});
			
			// Allow the user to also change the image by double clicking anywhere in the list element
			$("li.image").dblclick( function() {
				changing = $(this);
				openChangeImageWindow();									 
			});
			
			// Sets the name input field functionality
			$(".desc input.text").bind("keyup blur", function() {	
				shouldSave = true;
				$("h2 span.project").html($(this).val());
				$("#sidebar li a.selected").html("&raquo;&nbsp " + $(this).val());
			});
		
			 
			// Sets the "Change Image" Applu and Cancel button functionality for the pop-up window
			$("#change_image input, #change_image .close-btn").click( function() {
				if ($(this).attr("value") == "Change") {
					shouldSave = true;
					changing.find('img.img').replaceWith($('#change_image img.img').clone());
					changing.find('img.thumb').replaceWith($('#change_image img.thumb').clone());
					
					$("*").unbind();
					setDragDrop();	
					setButtons();
	
				}
				$("#change_image").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});
			
			// Sets the "Add Image" Apply and Cancel button functionality for the pop-up window
			$("#new_image input, #new_image .close-btn").click( function() {
				if ($(this).attr("value") == "Add") {
					if ($('#new_image img[src="uploads/no-image.gif"]').length > 0) {
						alert("You must upload a main image and thumbnail before applying");
						return false;
					}
					shouldSave = true;
					var newHTML = "";
					
					newHTML += '<li class="sortable image">';
					newHTML += '<img class="img" src="' + $("#new_image img.img").attr("src") + '" alt="' + project + ' Image: Main" title="' + project + ' Image: Main" height="54" width="81">';
					newHTML += '<p class="img_desc">Main<br>Image</p>';
					newHTML += '<img class="thumb" src="' + $("#new_image img.thumb").attr("src") + '" alt="' + project + ' Image: Thumbnail" title="' + project + ' Image: Thumbnail" height="40" width="60">';
					newHTML += '<p class="img_desc">Thumbnail<br>Image</p>';
					newHTML += '<div class="remove icons"><img src="close-icon.png" title="Remove this Image and Thumbnail" alt="Remove Image and Thumbnail Button" height="20" width="34">';
					newHTML += '<p>Remove Image</p></div>';
					newHTML += '<div class="change icons"><img src="change-icon.png" title="Change this Image and Thumbnail" alt="Change Image and Thumbnail Button" height="20 title=" width="34">';
					newHTML += '<p>Change Image</p></div></li>';

					$("#content ul").append(newHTML);
					$("*").unbind();
					setDragDrop();	
					setButtons();
					
				}
				$("#new_image").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});
				
	
	/* * * * * * * * * * * * CATEGORY > FINISHED & IN-PROGRESS - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets the functionality of the "Remove Image" button in each image list
			$("li.project .remove").click(function() {
				shouldSave = true;
				$(this).parent().fadeTo(200, 0, function() {
					$(this).slideUp(200, function() {
						$(this).remove();
						refreshNav();
					});
				});	
			});
	
	
	/* * * * * * * * * * * * CATEGORY > FINISHED - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('input.updateFinishedCategoryData').click( function() { saveFinishedCategory() });
			
			// Sets the "Add New Project" button functionality to allow users to upload new images
			$('input.add_project').click( function () {
				if ($("li.project").length < 8) {
					openNewProjectWindow();
				} else {
					alert("Eight is the maximun number of projects allowed. You must delete one before adding another.");	
				}
			});
				
			// Sets the functionality fo the "Change Project" button  in each image list
			$("li.finished .change").click(function() {									  
				if (shouldSave) {
					confirmSave("editor.php?category=" + category + "&page=" + page + "&project=" + $(this).parent().find("h3").html());
				} else {
					window.open("editor.php?category=" + category + "&page=" + page + "&project=" + $(this).parent().find("h3").text(),"_self");
				}
			});
			
			// Allow the user to select a project by double clicking anywhere in the list element
			$("li.finished").dblclick( function() {									 
				if (shouldSave) {
					if (confirmSave()) {
						window.open("editor.php?category=" + "Custom Residential" + "&page=" + "Finished" + "&project=" + $(this).find("h3").text(),"_self");
					}
				} else {
					window.open("editor.php?category=" + "Custom Residential" + "&page=" + "Finished" + "&project=" + $(this).find("h3").text(),"_self");
				}
			});
			
			// Sets the Applu and Cancel button functionality for the pop-up window
			$("#new_project .buttons input, #new_project .close-btn").click( function() {
				if ($(this).attr("value") == "Add") { // If the Add button was clicked, else just close the window
					if ($('#new_project .desc input').val() == "") {
						alert("You must provide a project name");
						return false;
					}
					shouldSave = true;
					
					var newHTML = "";
					var newDesc = $('#new_project textarea').val();
					
					newHTML += '<li class="sortable project">';
					newHTML += '<div class="project">';
					newHTML += '<h3>' + $('#new_project .desc input').val() + '</h3>';
					
					if (newDesc.length > 110) {
						newDesc = newDesc.substr(0, 110);
						newDesc = newDesc.substr(0, newDesc.lastIndexOf(" "));
						newDesc += " [...]";
					}
					
					newHTML += '<span class="hidden">' + newDesc + '</span>';
					newHTML += '<p>' + newDesc + '</p></div>';
					newHTML += '<div class="remove icons"><img src="close-icon.png" title="Remove this Image and Thumbnail" alt="Remove Image and Thumbnail Button" height="20" width="34"><p>Remove Project</p></div>';
					newHTML += '<div class="change icons"><img src="change-icon.png" title="Change this Image and Thumbnail" alt="Change Image and Thumbnail Button" height="20 title=" width="34"><p>Edit Project</p></div></li>';
	
					$("#content ul").append(newHTML);
					$("*").unbind();
					refreshNav();
					setDragDrop();	
					setButtons();
				}
				$("#new_project").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});
			
			
	
	/* * * * * * * * * * * * CATEGORY > IN-PROGRESS - PAGE FUNCTIONS * * * * * * * * * * * */
	
			// Sets Save Button functionality to update the XML file
			$('input.updateProgressData').click( function() { saveInProgress() });
			
			// Sets the "Add New Project" button functionality to allow users to upload new images
			$('input.add_progress').click( function () {
				if ($("li.project").length < 6) {
					openNewProgressWindow();
				} else {
					alert("Six is the maximun number of projects allowed. You must delete one before adding another.");	
				}
			});
				
			// Sets the functionality fo the "Change Project" button  in each image list
			$("li.progress .change").click(function() {									  
				changing = $(this).parent();
				openChangeProgressWindow();
			});
			
			// Allow the user to select a project by double clicking anywhere in the list element
			$("li.progress").dblclick( function() {									 
				changing = $(this);
				openChangeProgressWindow();
			});
			
			
			// Sets the "Change Image" Applu and Cancel button functionality for the pop-up window
			$("#change_progress .buttons input, #change_progress .close-btn").click( function() {
				if ($(this).attr("value") == "Change") {
					shouldSave = true;
					changing.find('img.progress').replaceWith($('#change_progress img.progress').clone());
					changing.find('h3').text($("#change_progress .desc input").val());
					
					var newDesc = $("#change_progress .desc textarea").val();
					changing.find('.hidden').text(newDesc);
					if (newDesc.length > 80) {
						newDesc = newDesc.substr(0, 80);
						newDesc = newDesc.substr(0, newDesc.lastIndexOf(" "));
						newDesc += " [...]";
					}
					changing.find('.progress p').text(newDesc);					
					
					$("*").unbind();
					refreshNav();
					setDragDrop();	
					setButtons();
				}
				$("#change_progress").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});
			
			// Sets the Applu and Cancel button functionality for the pop-up window
			$("#new_progress .buttons input, #new_progress .close-btn").click( function() {
				if ($(this).attr("value") == "Add") { // If the Add button was clicked, else just close the window
					if ($('#new_progress .desc input').val() == "") {
						alert("You must provide a project name");
						return false;
					}
					shouldSave = true;
					
					var newHTML = "";
					var newName = $("#new_progress .desc input").val();
					var newDesc = $('#new_progress textarea').val();
							
					newHTML += '<li style="" class="sortable project progress">';
					newHTML += '<img class="progress" src="' + $("#new_progress img.progress").attr("src") + '" alt="' + newName + ' Image" title="' + newName + ' Image" height="50" width="117">';
					newHTML += '<div class="project progress"><h3>' + newName + '</h3>';
					newHTML += '<span class="hidden">' + newDesc + '</span>';
					if (newDesc.length > 80) {
						newDesc = newDesc.substr(0, 80);
						newDesc = newDesc.substr(0, newDesc.lastIndexOf(" "));
						newDesc += " [...]";
					}
					newHTML += '<p>' + newDesc + '</p></div>';
					newHTML += '<div class="remove icons"><img src="close-icon.png" title="Remove this Image and Thumbnail" alt="Remove Image and Thumbnail Button" height="20" width="34"><p>Remove Project</p></div>';
					newHTML += '<div class="change icons"><img src="change-icon.png" title="Change this Image and Thumbnail" alt="Change Image and Thumbnail Button" height="20 title=" width="34"><p>Edit Project</p></div></li>';

					$("#content ul").append(newHTML);
					$("*").unbind();
					refreshNav();
					setDragDrop();	
					setButtons();
				}
				$("#new_progress").fadeOut(300);
				$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });						  
			});

}

function confirmSave(url) {
			
	$('#save').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
	
	$('#save .close-btn').click(closeSaveWindow);
	$('#save input[value="Cancel"]').click(closeSaveWindow);
	
	$('#dont_save').click(function() {
		window.open(url,"_self");																	  																			  
	});
	$('#save input[value="Save"]').click( function() {		
		if (page == "Finished") {
			if (project) {
				saveFinishedProject(url);	
			} else {
				saveFinishedCategory(url);	
			}
		} else if (page == "Progress") {
			saveInProgress(url);	
		} else if (page == "Home") {
			saveHome(url);	
		} else if (page == "About") {
			saveAbout(url);	
		} else if (page == "Contact") {
			if (section == "Branch") {
				saveContactBranches(url);
			}
			if (section == "Member") {
				saveContactMembers(url);
			}
		}else if (page == "Partners") {
			savePartners(url);	
		}									  
	});
}

function closeSaveWindow(url) {
	$("*").unbind();
	setDragDrop();	
	setButtons();
	$("#save").fadeOut(300);
	$("#overlay").fadeTo(500, 0, function() { $(this).addClass("hide") });	
}

function saveXML(data, url) {
	$.post("save-data.php", data, function(r) {
		if (r.indexOf("false") != -1) {
			alert("There was an error while trying to save. Please try again.");
			shouldSave = true;
		} else {
			shouldSave = false;
			if (url) {
				window.open(url,"_self");
				//alert("url: " + url);
			}
		}
	}, "text");		
}

function saveFinishedCategory(url) {		
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updateFinishedCategoryData";
	data['category'] = category;
	$('.sortable').each( function(i) {
		data['name' + i] = $(this).find('div.project h3').text();
		data['desc' + i] = $(this).find('div.project .hidden').text();
		
	});
	saveXML(data, url);
}

function saveFinishedProject(url) {						
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updateFinishedProjectData";
	data['category'] = category;
	data['project'] = project;
	data['name'] = $('.desc input.text').val();
	data['desc'] = $('.desc textarea').val();
	$('.sortable').each( function(i) {
		data['imgURL' + i] = $(this).children('.img').attr('src');
		data['thumbURL' + i] = $(this).children('.thumb').attr('src');
	});
	saveXML(data, url);
}

function saveInProgress(url) {		
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updateProgressData";
	data['category'] = category;
	$('.sortable').each( function(i) {
		data['name' + i] = $(this).find('div.project h3').text();
		data['desc' + i] = $(this).find('div.project .hidden').text();
		data['imgURL' + i] = $(this).find('img.progress').attr("src");
		
	});
	saveXML(data, url);
}

function saveHome(url) {	
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updateHomeData";
	data['category'] = category;
	data['description'] = $('#content .desc textarea').val();
	$('.sortable').each( function(i) {
		data['url' + i] = $(this).find('img.home').attr("src");					
	});
	saveXML(data, url);
}

function saveAbout(url) {	
	var data = {};
	data['func'] = "updateAboutData";
	data['section'] = section;
	data['description'] = $('#about textarea').val();
	data['imgURL'] = $('#about img.about').attr("src");
	saveXML(data, url);
}

function saveContactMembers(url) {	
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updateContactMembersData";
	$('.sortable').each( function(i) {
		data['name' + i] = $(this).find('input.member_name').val();
		data['email' + i] = $(this).find('input.member_email').val();
	});
	saveXML(data, url);
}

function saveContactBranches(url) {	
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updateContactBranchesData";
	$('.sortable').each( function(i) {
		data['name' + i] = $(this).find('input.branch_name').val();
		data['address1' + i] = $(this).find('input[name="address1"]').val();
		data['address2' + i] = $(this).find('input[name="address2"]').val();
		data['phone' + i] = $(this).find('input[name="phone"]').val();
		data['fax' + i] = $(this).find('input[name="fax"]').val();
	});
	saveXML(data, url);
}

function savePartners(url) {	
	var data = {}, elements = $('.sortable');
	data['length'] = elements.length;
	data['func'] = "updatePartnersData";
	data['desc'] = $("#content .desc textarea").val();
	$('.sortable').each( function(i) {
		data['name' + i] = $(this).find('input[name="name"]').val();
		var url = $(this).find('input[name="url"]').val();
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			url = "http://" + url;	
		}
		data['url' + i] = url;
	});
	saveXML(data, url);
}


function openNewProgressWindow() {
	$("#new_progress div.image").append('<div id="flash_new_progress"></div>');		
	swfobject.embedSWF("upload.swf", "flash_new_progress", "105", "32", "8.0.0", "", {sText:"Upload\nNew Image", id:"progress", windowID:"#new_progress"});								
	$("#new_progress img.progress").attr("src", "uploads/no-image.gif");
	$("#new_progress .desc input").val("Project Name");
	$("#new_progress .desc textarea").val("Project Description");
	$('#new_progress').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
}

function openChangeProgressWindow() {
	$("#change_progress div.image").empty().append(changing.find('img.progress').clone()).append('<div id="flash_change_progress"></div>');		
	swfobject.embedSWF("upload.swf", "flash_change_progress", "105", "32", "8.0.0", "", {sText:"Change Image", id:"progress", windowID:"#change_progress"});								
	$("#change_progress .desc input").val(changing.find('h3').text());
	$("#change_progress .desc textarea").val(changing.find('.hidden').text());
	$('#change_progress').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);	
}


function openNewHomeWindow() {
	$("#new_home div.image").append('<div id="flash_new_home"></div>');		
	swfobject.embedSWF("upload.swf", "flash_new_home", "105", "32", "8.0.0", "", {sText:"Upload Image", id:"home", windowID:"#new_home"});
	$("#new_home img.home").attr("src", "uploads/no-image.gif");
	$('#new_home').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
}

function openChangeHomeWindow() {
	$("#change_home div.image").empty().append(changing.find('.home').clone()).append('<div id="flash_change_home"></div>');		
	swfobject.embedSWF("upload.swf", "flash_change_home", "105", "32", "8.0.0", "", {sText:"Change Image", id:"home", windowID:"#change_home"});								
	$('#change_home').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
}

function openChangeImageWindow() {
	$("#change_image div.image").empty().append(changing.find('.img').clone()).append('<div id="flash_change_img"></div>');		
	swfobject.embedSWF("upload.swf", "flash_change_img", "105", "32", "8.0.0", "", {sText:"Change Main Image", id:"img", windowID:"#change_image"});
	$("#change_image div.thumb").empty().append(changing.find('.thumb').clone()).append('<div id="flash_change_thumb"></div>');
	swfobject.embedSWF("upload.swf", "flash_change_thumb", "105", "32", "8.0.0", "", {sText:"Change Thumbnail", id:"thumb", windowID:"#change_image"});														
	$('#change_image').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
}

function openNewImageWindow() {
	$("#new_image div.image").append('<div id="flash_new_img"></div>');		
	swfobject.embedSWF("upload.swf", "flash_new_img", "105", "32", "8.0.0", "", {sText:"Upload\nMain Image", id:"img", windowID:"#new_image"});
	$("#new_image div.thumb").append('<div id="flash_new_thumb"></div>');
	swfobject.embedSWF("upload.swf", "flash_new_thumb", "105", "32", "8.0.0", "", {sText:"Upload\nThumbnail", id:"thumb", windowID:"#new_image"});		
	
	$("#new_image img.img").attr("src", "uploads/no-image.gif");
	$("#new_image img.thumb").attr("src", "uploads/no-image.gif");
	
	$('#new_image').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
}

function openNewProjectWindow() {
	$("#new_project .desc input").val("Project Name");
	$("#new_project .desc textarea").val("Project Description");
	$('#new_project').fadeIn(300);
	$('#overlay').removeClass("hide").fadeTo(500, .6);
	
}

function refreshNav() {
	var links = $("#sidebar ul");
	links.empty();
	
	$("#content li.project h3").each( function() {
		var name = $(this).text();
		var href = 'editor.php?category=' + category + '&amp;page=' + page + '&amp;project=' + name;
		if (page == "Progress") {
			href += "#" + name;
		}
		links.append('<li><a href="' + href +  '" class="">&raquo;&nbsp; ' + name + '</a></li>');
	})
	if (page == "Progress") {
		links.find("li a").addClass("progress");		
	}
	setAnchors();
}

function setAnchors() {
		
	$("a:not(.progress)").unbind().click( function() {
		if (shouldSave) {
			confirmSave($(this).attr("href"));
			return false;
		}
	});
	
	$("#sidebar li a.progress").click( function() {
		var url = $(this).attr("href");
		url = url.substring(url.indexOf("#") + 1, url.length);
		changing = $("#content li.sortable:has(h3:contains('" + url +  "'))");
		openChangeProgressWindow();
		return false;
	});
	
}

function updateImage(id, imageID, windowID) {
	$("#" + windowID + " img." + id).attr("src", "uploads/" + imageID + ".jpg");
}

function setDragDrop() {
	$(".sortable:not(.home_image)")
		.bind( "dragstart", {not:"div, input"}, function( event ){
			//if ($(event.target).is("img")) {
				var $drag = $( this ), $proxy = $drag.clone();
				$proxy.width($drag.width());
				$drag.slideUp(200, function() { 
					$.dropManage({mode:"overlap", filter:".sortable"});
				});
				
				return $proxy.appendTo( document.body ).addClass("ghost");
			//} else {
			//	return false;	
			//}
		})
		.bind( "drag", {not:"div, input", distance:5}, function( event ){
			// update the "proxy" element position
			$( event.dragProxy ).css({
				left: event.offsetX, 
				top: event.offsetY
			});
		})
		.bind( "dragend", {not:"div, input"}, function( event ){
			// remove the "proxy" element
			$( event.dragProxy ).fadeOut(200, function(){
				$(this).remove();
			});
			$(event.dragTarget).slideDown(200);
			shouldSave = true;
			
			if ($(this).hasClass("project")) {
				refreshNav();	
			}
			
		});

	$(".sortable:not(.home_image)")
		.bind( "dropstart", {not:"div, input"}, function( event ){
				$(this).addClass("active");
		})
		.bind( "drop", {not:"div, input"},  function( event ){
			// if there was a drop, move some data...
			if (event.dragTarget != event.dropTarget) {
				if (event.offsetY >= $(this).position().top) {
					$(event.dragTarget).insertAfter(this);
				} else {
					$(event.dragTarget).insertBefore(this);
				}
			}
						
		})
		.bind( "dropend", {not:"div, input"}, function( event ){
			// deactivate the "drop" target element
			$( this ).removeClass("active");
		});
}