function my_kwicks()
{
    $('.kwicks').kwicks({
		duration: 200,
        max: 150,
        spacing:  0
    });
}

function my_flexbox()
{
	$('#select_emp_id').flexbox
	(
		'./flex_employees.php',
		{
			showArrow: true,
			showResults: true,
			noResultsText: 'No matching employees',
    		width: 275,
    		maxVisibleRows: 0,
    		watermark: 'Enter employee name or ID',
    		method: 'GET',
    		autoCompleteFirstMatch: true,
    		//queryDelay: 200,
    		//minChars: 1,
    		highlightMatches: true,
    		//highlightMatchesRegExModifier: 'i',
    		maxCacheBytes: 32768,
    		paging: {
			    style: 'input',             // 'input' or 'links'
			    //cssClass: 'paging',         // prefix with containerClass (e.g. .ffb .paging)
			    pageSize: 8,               // acts as a threshold.  if <= pageSize results, paging doesn't appear
			    showSummary: true,          // whether to show 'displaying 1-10 of 200 results' text
			    //summaryClass: 'summary',    // class for 'displaying 1-10 of 200 results', prefix with containerClass
			    summaryTemplate: 'Results {start}-{end} of {total}', // can use {page} and {pages} as well
			    maxPageLinks: 4             // used only if style is 'links'
			}
		}
	);
}

// Setup JS Vars for Image Toggling
/*ctrl1 = new Image();
ctrl2 = new Image();
ctrl1.src = "./images/batch_toggle_exp.jpg";
ctrl2.src = "./images/batch_toggle_col.jpg";*/

// Toggle "Batch Tools" Section
function toggle_batch() {
    // Get Selectors
    //ctrl = $("#batch_toggle_img");
    toggleDiv = $("#batch_hidden");

    // Make sure elements exist
    if (toggleDiv.length > 0) {
        // Toggle Section Show/Hide
        toggleDiv.slideToggle();

        // Toggle Image
        //if (ctrl.attr('src').indexOf('_exp') == -1) ctrl.attr('src', ctrl1.src)
        //else ctrl.attr('src', ctrl2.src);
    }
}

function soft_alert(my_text){
	var txt = '<h3>Portal System Alert</h3><p>'+ my_text +' </p>';

	$.prompt(txt,{
		buttons:{Ok:true}
	});
}

function change_app_status(appid, app_uid, old_status, owner_id, js_is_admin, atype) {
	// Build Appropriate Form Based on Current Status, Using AJAX POST
	$.post(
		"./ajax/appStatusChange_buildForm.php",
		{ app_id:appid, uid:app_uid, prev_status:old_status, eid:owner_id, is_admin:js_is_admin, action_type:atype },
		function(data){
			$.prompt(data,{
				buttons:{Update:true, Cancel:false},
				callback: function(v,m,f){
					if(v){
						$.post(
							"./ajax/appStatusChange_processForm.php",
							{ uid:f.uid, app_id:f.app_id, prev_status:f.prev_status, new_status:f.new_status, status_notes:f.status_notes, eid:f.eid, is_admin:f.is_admin, action_type:f.action_type },
							function(data){
								if (!data || data.result == 'false') { $.prompt('<h1>Error Encountered</h1>I\'m sorry, an error occurred while attempting to update this application\'s status. Please try again...'); }
								else if (data.result == 'changeOK') {
									// Inform User of Success
									$.prompt('<h1>Status Updated</h1>' + data.message);

									// Update Applicant Data by Re-Sumbitting Form
									$("#appSearchForm").submit();
								}
								else
								{
									$.prompt(data.result,{
										buttons:{Process:true, Cancel:false},
										callback: function(v,m,f){
											if(v){
												$.post(
													"./ajax/appNewHire_processForm.php",
													{
														uid:f.uid,
														app_id:f.app_id,
														prev_status:f.prev_status,
														new_status:f.new_status,
														status_notes:f.status_notes,
														eid:f.eid,
														is_admin:f.is_admin,
														hire_month:f.hire_month,
														hire_day:f.hire_day,
														hire_year:f.hire_year,
														new_eid:f.new_eid,
														access_level:f.access_level,
														job_position:f.job_position,
														dept_code:f.dept_code,
														location:f.location,
														emp_type:f.emp_type,
														reports_to:f.reports_to,
														send_welcome:f.send_welcome
													},
													function(data2){
														if (data2.result == 'newHireOK') {
															// Inform User of Success
															$.prompt(data2.message);

															// Update Applicant Data by Re-Sumbitting Form
															$("#appSearchForm").submit();
														}
														else {
                                                            myError = (data2.errorMessage) ? data2.errorMessage : '';
                                                            $.prompt('<h1>Error Encountered</h1>We\'re sorry, an error occurred while attempting to update this application\'s status.<br><br>'+myError);
                                                        }
													},
													"json"
												);
											}
										}
									});
								}
							},
                            "json"
						);
					}
				}
			});
		}
	);
}

function showAnimation() {
	$("#searchResults").html('<div style="padding:0; margin: 12px auto; text-align: center; font-size: 0.7em; font-style: italic;"><img src="./images/ajax-loader.gif" style="padding: 4px;" /><br />Searching Applications...</div>').slideDown();
	return true;
}

function showStars() {
	if ($("input.star_rating").length > 0) {
		$("input.star_rating").rating({
			callback: function(value, link){ updateRating(this.name, value); }
		});
	}
}

function updateRating(appid, rating)
{
	//alert('AppID: '+appid+' Rating: '+rating);
	$.post(
		"./ajax/appRating_update.php",
		{ app_id:appid, new_rating:rating },
		function(data){
			if (data == 'false') { $.prompt('<h1>Error Encountered</h1>I\'m sorry, an error occurred while attempting to update this application\'s rating. Please try again...'); }
		}
	);
}

function preLoader() {
	showAnimation();
	return true;
}

function postLoader() {
	$("#searchResults").slideDown();
	showStars();
	return true;
}

function refreshSearch(page_num, sessid) {
	showAnimation();
	$("#searchResults").load(
		"./ajax/appSearch_displayResults.php",
		{
			pageno:page_num,
			PHPSESSID:sessid
		},
		function(){
			showStars();
		}
	);
}

$(document).ready
(
	function()
	{
		// Load Kwicks Menu Functionality
		my_kwicks();

		// Set jQuery Prompt Defaults
		$.prompt.setDefaults({
			show: 'fadeIn',
			overlayspeed: 'fast',
			opacity: 0.7
		});

		// Set Options for AJAX Form
		var options = {
			target:			"#searchResults",		// target element(s) to be updated with server response
			beforeSubmit:	preLoader,				// pre-submit callback
			success:		postLoader				// post-submit callback
		};

		// Activate AJAX Form Plugin, and Submit to Fetch Initial Results
		if ($("#appSearchForm").length > 0) { $("#appSearchForm").ajaxForm(options).submit(); }

		// Load jqtransform Plugin
		if ( $("#jqtransform").length > 0 ) { $("#jqtransform").jqTransform(); }

		// Put Cursor in First Form Field on Page
		//if ( $("input:visible:enabled:first").length > 0 ) { $("input:visible:enabled:first").focus(); }
		if ( $("input:first").length > 0 ) { $("input:first").focus(); }

		// Load FlexBox Functionality
		my_flexbox();

		// Beautify Start Rating in Header if Set
		if ($("input.header_star_rating").length > 0) {
			$("input.header_star_rating").rating({
				callback: function(value, link){ updateRating(this.name, value); }
			});
		}

	}
);
