$(document).ready(function() {
	
	
	
	/*Setup the general dialog boxes*/
	$(function() {
		jQuery.each($(".jquery_dialog_window"), function(i, val) {
			$("#" + $(val).attr('id')).dialog(eval('options_' + $(val).attr('id')));	
    	});
	});		
	$('.jquery_dialog').click(function(){$('#' + $(this).attr('id') + '_dialog').dialog('open');});
	
	
	/* Set-up the email confirmation */
	confirmEmail();
	$("#Email").blur(function () {confirmEmail();});	
	
	/* Set-up the opt out function for the technical comp section*/
	$('.optout').click(function (){
		$('#' + $(this).attr("id") + '-box').slideDown();
		$('#' + $(this).attr("id") + '-link').hide();
	});
	$('.optout-cancel').click(function (){
		$('#' + $(this).attr("rel") + '-box').hide();
		$('#' + $(this).attr("rel") + '-link').show();
	});
	
	/* Start the timer so we can notify the user if there session is about to expire */
	$('#readi').everyTime(1000,function(i) {
		var timeout = 2100;
		var warningTime = 60;
		// the current application timeout is 35 minutes, which is 2100 seconds....
		if(i >= timeout-warningTime){
			// create the dialog 
			$("#timeout-dialog").dialog({
					bgiframe: true,
					resizable: false,
					height:200,
					width:400,
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						Cancel: function() {
							$('#readi').stopTime();
							$(this).dialog('close');
						}
					}
				});
			$('#timout-seconds-remaining').html(timeout - i);
			// if the time has reached 0 seconds..., stop the timer and show the expired message.
			if(timeout - i < 0){
				$('#readi').stopTime();
				// now show the session expired message
				$('#timeout-warning').hide();
				$('#timeout-expired').show();
				$("#timeout-dialog").dialog("moveToTop"); 
			}
			
			// now open the dialog
			$("#timeout-dialog").dialog('open');
			
		}
	});
	
	
	
	/*Send report functions*/
	$(function() {			
		$('#email-report-dialog').dialog({
			height: 460,
			width: 600,
			modal: true,
			draggable: false,
			resizable: false,
			autoOpen: false,
			buttons: {
				'Send Report': function() {
					var str = $("#email-report-form").serialize();
					$('#email-report-form-container').hide();
					$('#email-report-sending').show();						
				  $.getJSON("/index.cfm?do=report.sendReport&" + str, function(json){
				  	var success = json.data[0].success;
						var message = json.data[0].message;
						if(success == 0){
						  $("#email-report-result").attr("class","errorsMsg");
						  $("#email-report-result").html(message);
						  $("#email-report-result").fadeIn('slow');
						}else{
							$("#email-report-result").attr("class","StatusMessage");
						  $("#email-report-result").html(message);
						  $("#email-report-result").fadeIn('slow');	
						  $('form')[0].reset();	
						}
						  $('#email-report-form-container').show();
							$('#email-report-sending').hide();			
				  });
				},
				Close: function() {
					$(this).dialog('close');
					$("#email-report-result").hide();
					$("#email-report-form-container").hide();
					$("#email-report-form").hide();
				}
			}
		});
	});
	$('#email-report-dialog-open-btn').click(function (){
		$('#email-report-form').ajaxForm();
		$("#email-report-dialog").dialog('open');
		$('#email-report-form-container').show();
		$("#email-report-form").show();		
	});
	
	
/* CONVERT TO PDF DIALOG */
$(function() {			
		$('#pdf-report-dialog').dialog({
			height: 300,
			width: 500,
			modal: true,
			draggable: false,
			resizable: false,
			autoOpen: false,
			buttons: {
				Close: function() {
					$(this).dialog('close');
					$("#pdf-report-result").hide();
					$("#pdf-report-form").hide();
				}
			}
		});
	});
	$('#pdf-report-dialog-open-btn').click(function (){
		$("#pdf-report-dialog").dialog('open');
		;
		// fire off the ajax request to generate the pdf report...
		$.getJSON($('#pdf-link').val(), {userid:$('#userid').val(), pdf:'true'}, function(json){
			$('#pdf-report-processing').hide()
			$('#pdf-report-complete').show()
			$('#pdf-report-dialog').dialog('option', 'title', 'Process Complete...');
		});
	});	
	
	
/* Final Report Functions */
	// initalize the tabs
		$(function() {$("#MyTabs").tabs();});
		$(function() {$("#learning-styles").tabs();});
		
	/*Section Reset Functions*/
	$('.reset-confirm-button').click(function(){
			var id = $(this).attr('id');
			// show the confirmation message
			$("#" + id + "_confirm").slideDown();
			$("#" + id + "_cancel").bind("click", function(e) {$("#" + id + "_confirm").slideUp();});				
	  });	  
	
	
		
	// info window
	$(".info-button").bind("click", function(e) {
		var userid = $(this).attr('id');
		var curRow = '#user_' + userid;
		var curRowClass = $(curRow).attr('class');
		// first give the bubble some loading text
		$('#bubble').html("Loading information...<br/><br/><img src='/assets/images/ajax-loader.gif' border='0'/>");
		// send the request to get the details
		$.get("/AdministrationReports.AssessmentSnapshot/userid/" + userid, function(data){
	  		$('#bubble').html(data);	 		
	  });
		// add the highlighted class
		$(curRow).attr('class', 'active');
		
		
		$("#bubble").css({
						left:jQuery(this).offset().left-30, 
						top:jQuery(this).offset().top-jQuery("#bubble").height()-40 
					}).show();
		e.stopPropagation(); // Stops the following click function from being executed
		$(document).one("click", function(f) {
			// return the row back to the original state
			
			$(curRow).attr('class', curRowClass);
			$("#bubble").hide();
		});
	});
	// STOP the menu from disappearing when it is clicked
	$("#bubble").bind("click", function(e) { e.stopPropagation(); $(curRow).attr('class', curRowClass); });	
	
	/* START :  READING Communcations */
	
	
	
	
	/* END :  READING Communcations */
			
	});

/*
function processResponse(json, statusText) { 
	var success = json.data[0].success;
	var message = json.data[0].message;
	if(success == 0){
	  $("#sendResult").attr("class","errorsMsg");
	  $("#sendResult").html(message);
	  $("#sendResult").fadeIn('slow');
	}else{
		$("#sendResult").attr("class","StatusMessage");
	  $("#sendResult").html(message);
	  $("#sendResult").fadeIn('slow');	
	  $('form')[0].reset();	
	}
	  $("#send-info").show();
	  $("#sending-message").hide();	
}

function clearMsg(){
	$("#sendResult").removeAttr("class");
	$("#sendResult").css({display:"none"});
	$("#sendResult").html("");
}

*/

function confirmEmail(){
	var emailVal = jQuery.trim($('#Email').val());
	// make sure the email address has length, if so we are going to verify it
	if(emailVal != ''){						
		$.getJSON("/services/assessment.cfc?method=CheckEmail", {email:emailVal}, function(json){
		  if(json.returncode != '0'){
		  	// first make sure you clear any message that might already be set.
			$('#email-error-message').remove();
		  	$('#EmailMsg').slideDown();
		  	$('#Email').parent().attr('class','ctrlHolder error');
		  	$('#Email').parent().append('<span id="email-error-message">' + json.message + '</span>');
		  }else{
		  	$('#EmailMsg').slideUp();
		  	$('#Email').parent().attr('class','ctrlHolder');
		  	$('#email-error-message').remove();
		  }
		});
	}else{
		$('#EmailMsg').slideUp();
		$('#email-error-message').remove();
		$('#Email').parent().attr('class','ctrlHolder');
	}
}



