// Sitewide Admin scripts using jquery
$(document).ready(function() {
// auto-focus on the login username input field
	$("input:text:visible:first").focus();													 
													 
//password recovery display
	$("#recover_pw").click(function() {
		$("#recover_box").toggle();
		return false;
	});

//jquery tablesorter
	$(".tablesorter").tablesorter({widgets: ["zebra"]});
	$(".tablesorter tbody tr").hover(function() {$(this).addClass("over");}, function() {$(this).removeClass("over");});
	
//Table Row Drag and Drop Sorter
	$("#tablednd tbody tr").hover(function() {
          $(this.cells[0]).addClass('show_drag_handle');
    }, function() {
          $(this.cells[0]).removeClass('show_drag_handle');
    });
	$("#tablednd").tableDnD({
        onDrop: function(table, row) {
			var serial = $.tableDnD.serialize();
			$.post("db_ajax.php?type=sort", serial, function() {
				$(".tablesorter").trigger("update").trigger("applyWidgets", {widgets: ["zebra"]});
			});
        },
		dragHandle: "drag_handle"
    });
	
//checkbox toggle
	$(".checkbox").live("click", function() {
		var name = $(this).attr("name"); //get name
		var checkval = ""; //default the value to nothing
		if ($(this).is(':checked')) {
			checkval = 1;
		}
		$.post("db_ajax.php", { type: "checkbox", name: name, value: checkval });
	});

//update text box on keyup
	var delayed;
	$(".text_ajax").live("keyup", function() {
		clearTimeout(delayed);
		var value = $(this).val();
		var name = $(this).attr("name"); //get name attribute
		
		if (value) {
			delayed = setTimeout(function() {
				$.post("db_ajax.php", { type: "text", name: name, value: value });
			}, 600);
		}
	});

//update select on change with ajax
	$(".select_ajax").change(function() {
		var name = $(this).attr("name"); //get name attribute
		var value = $(this).val();
		//make ajax call
		$.post("db_ajax.php", { type: "text", name: name, value: value });
	});	

//jquery Datepicker
	Date.firstDayOfWeek = 7; //show sunday through saturday
	Date.format = "mm/dd/yyyy"; //show desired date format
	$(".form_date").datePicker({startDate:"01/01/1996"});
	//validate date using validate_date.js
	$(".form_date").blur(function() {
		checkdate(this);
	});

//multiple fileupload
	$(".multifile").multifile();
	
//encrypt password with md5 hash
	$("a.encrypt").click(function() {
		var input = $(this).prev("input");
		var text = input.val();
		var encrypt = $.md5(text);
		input.val(encrypt);
		return false;
	});

//Confirm Delete
	$(".delete_item").click(function() {
		var answer = confirm("Are you sure you want to delete?");
		$link = $(this);
		if (answer) {
			$.get($link.attr("href"), function() {
				$link.parent().parent().remove();
				$(".tablesorter").trigger("update").trigger("applyWidgets", {widgets: ["zebra"]});
			});
		}
		return false;
	});

//Create New Category
	$(".create_cat").click(function () {
		$(this).after(" <input type='text' /> <a href='#' class='new_cat'>add</a>");
		
		var newcat = function() {
			$(".new_cat").click(function () {
				var text = $(this).prev().val(); //get category name typed in
				//remove any selected option
				$(this).prev().prev().prev("select").children().each(function () {
					$(this).removeAttr("selected");
				})
				//append the text into the menu
				$(this).prev().prev().prev("select").append("<option value='" + text + "' selected='selected'>" + text + "</option>");	
			});
		}
		newcat();
	});
	
//Block Slider Toggle
	$(".block_options").hide();
	$(".block_toggle").toggle(
		function() {
			$(this).addClass("block_toggle_open").removeClass("block_toggle_collapse").next().show();
		}, function() {
			$(this).addClass("block_toggle_collapse").removeClass("block_toggle_open").next().hide();
		}
	);

//WYSIWYG Rich Text Editor using HtmlBox
/*	$(".form_rte").each(function() {
		$(this).htmlbox({
			toolbars:[
				 [
				  // Bold, Italic, Underline
				  "bold","italic","underline","strike",
				  // Left, Right, Center, Justify
				  "separator","left","center","right","justify",
				  // Ordered List, Unordered List, Indent, Outdent
				  "separator","ol","ul","indent","outdent",
				  // Hyperlink, unlink, Image
				  "separator","link","unlink","image",
				  //remove format, strip tags, HTML code view
				  "separator","removeformat","striptags","code"
				  ]
			],
			idir:"images/htmlbox/",
			icons:"silk",
			skin:"blue",
			about:false,
			css: "body{background:#F9F9F9}"
		});
	});*/
	
//WYSIWYG Rich Text Editor using HtmlBox
	$(".form_rte").ckeditor({
		extraPlugins : 'uicolor',
		uiColor: '#A2B8E8',
    toolbar:
		[
			['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
			['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'],
      ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
      ['Link','Unlink','Anchor'],
      ['Image','Table','HorizontalRule','SpecialChar'],
      '/',
      ['Format', '-', 'Undo','Redo','-','Find','Replace','SelectAll', 'ShowBlocks','RemoveFormat'],
      ['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker'],
      ['Maximize','-','Source']
		]
	});
	
/* 
-----------------------------------------------------------------------------
File Upload and Delete
-----------------------------------------------------------------------------*/
//folder select for fileupload
	$("#folder_select").change(function() {
		$(this).parent("form").submit();
	});

//reload folders for fileupload
	$("#reload").click(function() {
		location.reload(true);
		return false;
	});

//upload window for fileupload
	$("#upload_window a").click(function() {
		window.open(this.href, "Upload", "width=550,height=200,scrollbars=1,resizable=1,status=0,toolbar=0");
		return false;
	});
	
//show upload graphic
	$("#upload").submit(function() {
		$("#progress").show();
	});

//refresh file admin page when upload is complete
	if ($("div").hasClass("uploaded")) {
		window.opener.location.reload(true);
	}

//add tooltip to files to show preview
	$("a.preview").filetip(true);

//uploadify multifile uploader
	$("#upload_files").uploadify({
		"uploader"  : "swf/uploadify.swf",
		"script"    : "includes/uploadify.php",
		"cancelImg" : "images/cancel.png",
		"folder"    : "/db_files"
	});
	
});
