//--------------------------------------------------
// Global variable declarations
//--------------------------------------------------

var ctrl = null;

//--------------------------------------------------
// Check for specific dropdown selected values and
// perform some operations according to the value
//--------------------------------------------------

function CheckDropdown(form, input) {
	var idx = document.forms[form].elements[input].selectedIndex;

	// From which form to check the dropdown value?

	if (form == "display") {
		if (idx != 0)
			document.forms[form].submit();
	} else if (form == "comments") {
		if (idx != 0)
			// Deleting comments?

			if (idx == 2){
				var question = document.forms[form].elements['question'].value;
				var op = confirm(question);

				if (op) {
					document.forms[form].submit();
				}
			} else {
				document.forms[form].submit();
			}
	} else if (form == "settings") {
		// Which dropdown to check?

		if (input == "thumbresample") {
			// Which value was selected?

			if (idx == 0) {
				document.forms[form].elements['thumbwidth'].disabled = false;
				document.forms[form].elements['thumbheight'].disabled = false;
			} else if (idx == 1) {
				document.forms[form].elements['thumbwidth'].disabled = false;
				document.forms[form].elements['thumbheight'].disabled = true;
			} else if (idx == 2) {
				document.forms[form].elements['thumbwidth'].disabled = true;
				document.forms[form].elements['thumbheight'].disabled = false;
			}
		} else if (input == "resizeimages") {
			// Which value was selected?

			if (idx == 0) {
				document.forms[form].elements['maximgsize'].disabled = true;
			} else if (idx == 1) {
				document.forms[form].elements['maximgsize'].disabled = false;
			}
		} else if (input == "recentimages") {
			// Which value was selected?

			if (idx == 0) {
				document.forms[form].elements['numrimages'].disabled = true;
			} else if (idx == 1) {
				document.forms[form].elements['numrimages'].disabled = false;
			}
		}
	}

	return false;
}

//--------------------------------------------------
// Confirms the operation for changing photo options
//--------------------------------------------------

function ModifyPhotoOptions() {
	var question = document.forms['settings'].elements['qmodimgsopts'].value;
	var op = confirm(question);

	// Should we submit the form?

	if (op) {
		document.forms['settings'].elements['action'].value = "updatenotify";
		document.forms['settings'].submit();
	}

	return false;
}

//--------------------------------------------------
// Confirms the operation for regenerating thumbs
//--------------------------------------------------

function RegenerateThumbs() {
	var question = document.forms['settings'].elements['qregenthumbs'].value;
	var op = confirm(question);

	// Should we submit the form?

	if (op) {
		document.forms['settings'].elements['action'].value = "regenthumbs";
		document.forms['settings'].submit();
	}

	return false;
}

//--------------------------------------------------
// Check all comments from the list
//--------------------------------------------------

function CheckAllComments() {
	var field = document.forms['comments'].elements['comment[]'];

	// Only one field?

	if (!field.length) {
		field.checked = true;
	} else {
		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
		}
	}
}

//--------------------------------------------------
// Swap an element display property
//--------------------------------------------------

function SwapElementVisibility(type, id) {
	var element = document.getElementById(id);

	if (type == "hide"){
		element.style.display = "none";
	} else if (type == "show") {
		element.style.display = "block";
	}
}

//--------------------------------------------------
// Display a success popup on screen
//--------------------------------------------------

function PopupDisplayCheck(msg) {
	var query = window.location.search.substring(1);

	var params = query.split("&");

	for (var i=0; i<params.length; i++) {
		var param = params[i].split("=");

		// Print the popup on the page

		if (param[0] == "success") {
			document.write("<div id=\"popup-success-bg\"><div id=\"popup-success-msg\">");
            document.write("<div class=\"popup-icon\"><img src=\"success.gif\" height=\"32\" width=\"32\" alt=\"\" /><\/div>");
	        document.write("<div class=\"popup-text\">" + msg + "<\/div>");
            document.write("<\/div><\/div>");

			break;
		}
	}
}

//--------------------------------------------------
// After the page has finished loading...
//--------------------------------------------------

window.onload = function() {
	// Hide loader popup

	SwapElementVisibility("hide", "popup-loader-bg");

	// Run the following functions if we are at the settings section

	if (document.forms['settings']) {
		CheckDropdown("settings", "thumbresample");
		CheckDropdown("settings", "resizeimages");
		CheckDropdown("settings", "recentimages");
	}

	// Create an array with all the hooked element ids

	var hook_elements = ["slct-commentsdisplay", "btn-changecomments", "btn-modimgsopts", "btn-regenthumbs", "btn-checkallcomments", "slct-thumbresample", "slct-resizeimages", "slct-recentimages", "popup-success-msg", "btn-upload"];

	// Process all hooked elements

	for (i in hook_elements) {
		if (document.getElementById(hook_elements[i])) {
			x = document.getElementById(hook_elements[i]);

			// Which one should we hook to a function?

			switch(x.id) {
				case "slct-commentsdisplay": x.onchange = function() { CheckDropdown("display", "pid"); }
											 break;
				case "btn-changecomments":   x.onclick = function() { CheckDropdown("comments", "action"); }
											 break;
				case "btn-modimgsopts":      x.onclick = function() { ModifyPhotoOptions(); }
											 break;
				case "btn-regenthumbs":      x.onclick = function() { RegenerateThumbs(); }
											 break;
				case "btn-checkallcomments": x.onclick = function() { CheckAllComments(); }
											 break;
				case "slct-thumbresample":   x.onchange = function() { CheckDropdown("settings", "thumbresample"); }
											 break;
				case "slct-resizeimages":    x.onchange = function() { CheckDropdown("settings", "resizeimages"); }
											 break;
				case "slct-recentimages":    x.onchange = function() { CheckDropdown("settings", "recentimages"); }
											 break;
				case "popup-success-msg":    x.onclick = function() { SwapElementVisibility("hide", "popup-success-bg"); }
											 break;
				case "btn-upload":			 x.onclick = function() { SwapElementVisibility("show", "popup-uploading-bg"); ctrl = "up_on"; }
											 break;
			}
		}
	}
}

//--------------------------------------------------
// After the page started to unload...
//--------------------------------------------------

window.onbeforeunload = function() {

	// Is the upload popup displayed?

	if (ctrl != "up_on") {
		// Show loader popup

		SwapElementVisibility("show", "popup-loader-bg");
	}
}