// javascript functions
// simon.allante.kane[at]bleps.com 2005, 2006, 2007, 2008, 2009

//--[ajax]------------------------------------------------------------------------------------------

  // adds new row under parent row. loads specified clip. toggles expand icon.
  // will toggle control_icon_id icon if present
	function open_subrow(event, parent_row_id, clip_type, data_id, perform_close)
	{	
	  // get div ids
		child_id = parent_row_id + 'child';
		childcontent_id = parent_row_id + 'childcontent';
		control_icon_id = parent_row_id + '-i';

	  // modifier keys
		shift_pressed = event.shiftKey;
		alt_pressed = event.altKey;
		ctrl_pressed = event.ctrlKey;

	  // check if a child row exists, if not - create one
		if ( $('#'+child_id).length === 0 )
		{
		  // close all other expanded rows
			if (perform_close) { $(".expandedrow").remove(); }

	  	  // insert child row below the parent_row
	  	 	rows_in_parent = $('#'+parent_row_id).attr("cells").length;
			$('#'+parent_row_id).after('<tr id="' + child_id + '" class="expandedrow"><td colspan="' + rows_in_parent + '" id="' +childcontent_id + '"><img src="http://provost.utdallas.edu/images/ajax-loader.gif" /></td></tr>');

		  // load clip file into child
		  	$('#'+childcontent_id).load('http://provost.utdallas.edu/facultyjobs/clips/'+clip_type+'.zog', { id: data_id, div: childcontent_id, modshift: shift_pressed, modalt: alt_pressed, modctrl: ctrl_pressed }, function() { load_callback(clip_type, data_id); } );

		  // change +/- control to minus
		  	$('#'+control_icon_id).attr({ src: "http://provost.utdallas.edu/images/foldicon-minus.gif"});
		}
	  // remove the child row
		else
		{
		  // remove child row
			$('#'+child_id).remove();
			
		  // change +/- control to plus
		  	$('#'+control_icon_id).attr({ src: "http://provost.utdallas.edu/images/foldicon-plus.gif"});
		}
	}

	function load_callback(clip_type, data_id)
	{
		//alert(clip_type);
		switch(clip_type)
		{
			case 'clip-applicants':
				$(".applicant-table tr:odd").addClass("liteodd");
				$(".applicant-table tr").mouseover( function() { $(this).addClass("hilite").css("cursor","pointer"); } ).mouseout( function() { $(this).removeClass("hilite"); } );

	  		  // kill event propagation
				$(".stopbubble").click( function(e){ e.stopPropagation();} );
				break;
				
			case 'clip-applicant':

			  // buttons: add button styles 
			  	$(".gbutton")
			  	.mouseover( function() { $(this).css("backgroundPosition","0 -16px").css("cursor","pointer"); } )
			  	.mouseout( function() { $(this).css("backgroundPosition","0 0"); } )
			  	.mousedown( function() { $(this).css("backgroundPosition","0 -32px"); } )
			  	.mouseup( function() { $(this).css("backgroundPosition","0 -16px"); } );

			  // activate stars
				//$("#stars-wrapper1").rating();
				$('#'+data_id+'-stars').stars({
					  callback: function(ui, type, value){ update_rating(data_id, value); }
					  });

				break;
			default:
				break;
		}
		
	}

	
	function toggle_application_visibility(data_id)
	{
		$('#'+data_id+'-sh').load('http://provost.utdallas.edu/facultyjobs/clips/clip-applicant_visibility.zog', { id: data_id, div: childcontent_id } );
	}
	
	 

  //--[dox upload]------------------------------------------------------------------------------------------

  // called when the upload form on applicant page changes
	function upload_form_change(data_id, starting_type)
	{

		if ( $('#'+data_id+'-doxuploadtype').length )
		{
		  // get local values
			upload_type = $('#'+data_id+'-doxuploadtype').val();
		}
		else
		{
			upload_type = starting_type;
		}
		
	  // reload form
		$('#'+data_id+'-doxupload').load('http://provost.utdallas.edu/facultyjobs/clips/clip-fileuploadform.zog', { upload_type: upload_type, upload_appcode: data_id  }, function() { callback_uploadform_load(data_id); } );
	}


  // called after the upload form has been regenerated
	function callback_uploadform_load(data_id)
	{		
	  // apply live validation to this form
		$('#'+data_id+'-doxuploadform').validate({
			submitHandler: function(form) { $('#'+data_id+'-doxuploadform').ajaxSubmit({  success: function(data, status_code){ upload_success(data, status_code, data_id); }   }); }
			//submitHandler: function(form) { form.submit(); }			
		});

		// function(html){ $("#error").append(html); }
		//$("#error").append('[loaded]');		
	}
	
	function upload_success(data, status_code, data_id)
	{	
	  // clear document type - reload the upload form
		$('#'+data_id+'-doxuploadtype').val('');
		upload_form_change(data_id);

	  // refresh document list
		$('#'+data_id+'-doxlist').load('http://provost.utdallas.edu/facultyjobs/clips/clip-upload_list.zog', { upload_appcode: data_id } );

		//$("#documentstatus_div").load('http://www.zzz.com');

	  // display error/success message
	 	$('#'+data_id+'-doxuploadstatus').html(data);
	}
	

  //--[ratings]------------------------------------------------------------------------------------------

	function update_rating(data_id, rating)
	{
		$('#'+data_id+'-stars-results').load('http://provost.utdallas.edu/facultyjobs/clips/clip-process_rating.zog', { data_id: data_id, rating: rating} );
	}

	function update_filter(data_id)
	{
	  // get local values
		selected_filter = $('#'+data_id+'-filter').val();

		$('#'+data_id+'childcontent').load('http://provost.utdallas.edu/facultyjobs/clips/clip-applicants.zog', { id: data_id, filter: selected_filter} );
	}

  //--[printcoversheet]------------------------------------------------------------------------------------------

  // called when the upload form on applicant page changes
	function doxcoversheet_form_change(data_id)
	{
	  // get local values
		coversheet_type = $('#'+data_id+'-coversheettype').val();

	  // reload form
		$('#'+data_id+'-doxcoversheet').load('http://provost.utdallas.edu/facultyjobs/clips/clip-doxcoversheet_form.zog', { coversheet_type: coversheet_type, data_id: data_id  }, function() { callback_doxcoversheetform_load(data_id); } );
	}
	
	
  // called after the doxcoversheet form has been regenerated
	function callback_doxcoversheetform_load(data_id)
	{		
	  // apply live validation to this form
		$('#'+data_id+'-doxcoversheetform').validate({
			submitHandler: function(form) { $('#'+data_id+'-doxcoversheetform').ajaxSubmit({  success: function(data, status_code){ doxcoversheet_success(data, status_code, data_id); }   }); }
		});
	}	

	function doxcoversheet_success(data, status_code, data_id)
	{	
	  // clear document type - reload the upload form
		$('#'+data_id+'-coversheettype').val('');
		doxcoversheet_form_change(data_id);

	  // display error/success message
	 	$('#'+data_id+'-doxcoversheetstatus').html(data);
	}


//--[other]------------------------------------------------------------------------------------------

  //popular links menu
	function jumplink(popform) 
	{
		loc=document.popform.poplist.options[document.popform.poplist.selectedIndex].value;
		location.href = loc;
	}