var certinterpURL = 'includes/certifyinterpretation_server.php';
var certURL = 'includes/content/certify/cert_server.php';
var critiqueid = "";
var dreamcomment = "";
var intcomment = "";
var apprenticelevel = "";
var status = "";
var dataset = "";
var from_interp = false;
$(document).ready(function() 
	{
		$('#critiqueid').change(function()
			{
				critiqueid = $('#critiqueid').val();
			});
		$('#button-removehold').click(function() {
				RemoveHold(interpid);
				})
			.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#a-apprenticelevel').change(function()
			{
				apprenticelevel = $('#apprenticelevel').val();
			});
		$('#a-status').change(function()
			{
				status = $('#status').val();
			});
		$('#button-updatecert').click(function() {
				dreamcomment = $('#dreamercomment').val();
				intcomment = $('#intcomment').val();
				critiqueid = $('#critiqueid').val();
				validate = true;
				
				if(critiqueid == 0)
					{
						validate = false;
						$('#critiqueid').addClass('ui-state-error');
						updateTips("You must critique the interpretation",$('#tips-cert'));
					}
				
				if(validate)
					{
						UpdateCertification("updatecert",dreamid,heldbyid,interpid,critiqueid,intcomment,dreamcomment);
					}
					})
			.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dialog-mandatory').dialog(
			{
				bgiframe: true,
				autoOpen: false,
				resizable: false,
				width:600,
				modal: true,
				closeOnEscape:false,
				overlay: 
				{
				  backgroundColor: '#000',
				  opacity: 0.5
				 },
				buttons: 
					{
					'Submit': function() 
						{
							$('#dreamtitle').removeClass('ui-state-error'); 
							$('#dreamtext').removeClass('ui-state-error');
							$('#status').removeClass('ui-state-error');
							$('#intfocus').removeClass('ui-state-error');
							$('#inttext').removeClass('ui-state-error');
							$('#apprenticelevel').removeClass('ui-state-error');
							validate = true;
							if(!canapprove)
								{
									var dreamtitle = $('#dreamtitle').text(); 
									var dreamtext = $('#dreamtext').text();
									var status = 0;
								}
							else if(canapprove)
								{
									var dreamtitle = $('#dreamtitle').val(); 
									var dreamtext = $('#dreamtext').val();
									status = $('#status').val()
								validate = validate && FillCheck(dreamtitle,"You must enter a Dream Title",$('#dreamtitle'),$('#tips-mandatory'));
								validate = validate && FillCheck(dreamtext,"Dream Text cannot be blank",$('#dreamtext'),$('#tips-mandatory'));
								validate = validate && FillCheck(status,"Please select a status",$('#status'),$('#tips-mandatory'));
								}
							var intfocus = $('#intfocus').val();
							var inttext = $('#inttext').val();
							var apprenticelevel = $('#apprenticelevel').val();
							
							validate = validate && FillCheck(intfocus,"You must enter a correct focus",$('#intfocus'),$('#tips-mandatory'));
							validate = validate && FillCheck(inttext,"Correct Interpretation cannot be blank",$('#inttext'),$('#tips-mandatory'));
							validate = validate && FillCheck(apprenticelevel,"Please assign this dream to an Interpreter Level",$('#apprenticelevel'),$('#tips-mandatory'));

						if(validate)
							{
								
								Mandatory("submitmandatory",dreamid,interp_id,ilevel,dreamtitle,dreamtext,status,intfocus,inttext,apprenticelevel);
								
								//document.write("request=submitmandatory&dreamid="+dreamid+"&interp_id="+interp_id+"&ilevel="+ilevel+"&info="+dataset);
							}
						
						},
					'Cancel': function() 
						{
							$(this).dialog('close');
						}
					},
				close:function()
					{
					}
			});
		$('#button-suggestmandatory').click(function() 
			{
				Mandatory("suggestmandatory",dreamid,interp_id,ilevel,"");
				})
			.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		//$("#cert-accordion").accordion({ autoHeight: false,  active: false });
		$('#dialog-success-cert').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				height:100,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor:'#000',
					opacity:0.5
				},
				buttons:
				{
					'Ok':function()
						{
							$(this).dialog('close');
						}
				},
				close:function()
					{
					}
			});
		$("#cert-accordion").addClass("ui-accordion ui-widget ui-helper-reset")
		.find("h3")
			.addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom")
			.prepend('<span class="ui-icon ui-icon-triangle-1-e"/>')
			.click(function() {
				$(this).toggleClass("ui-accordion-header-active").toggleClass("ui-state-active")
					.toggleClass("ui-state-default").toggleClass("ui-corner-bottom")
				.find("> .ui-icon").toggleClass("ui-icon-triangle-1-e").toggleClass("ui-icon-triangle-1-s")
				.end().next().toggleClass("ui-accordion-content-active").toggle();
				return false;
			})
			.next().addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom").hide();

	});
function RemoveHold(interpid)
	{
		blockForDataLoad();
		$.post(certURL,
			{
				request:"removehold",
				interp_id:interpid
			},
		function(res)
			{
				var info = $.json.deserialize(res);
				var info_result = "";

				for(key in info)
					{
						info_result = info[key].confirmation;	
					}
				 
				if(info_result)
					{
						//this.target = "_self";
						window.open('certification.php',"_self");

					}
				else
					{
						unblockForDataLoad();
						alert("error");
					}
			});
	}
function Mandatory(request,x_dreamid,interp_id,ilevel,dreamtitle,dreamtext,status,intfocus,inttext,apprenticelevel)
	{
		//alert(request+' ,    '+dreamid+' ,    '+interp_id+' ,    '+ilevel+' ,    '+dreamtitle+' ,    '+dreamtext+' ,    '+status+' ,    '+intfocus+' ,    '+inttext+' ,    '+apprenticelevel);
		dreamid = x_dreamid;
		$('#a-dreamtitle').empty(); 
				$('#a-dreamtext').empty();
				$('#a-status').empty();
				$('#a-intfocus').empty();
				$('#a-inttext').empty();
				$('#a-apprenticelevel').empty();
		$.post(certinterpURL,
		   {
			   request:request,
			   dreamid:x_dreamid,
			   interp_id:interp_id,
			   ilevel:ilevel,
			   dreamtitle:dreamtitle,
			   dreamtext:dreamtext,
			   status:status,
			   intfocus:intfocus,
			   inttext:inttext,
			   apprenticelevel:apprenticelevel
			},
		function(res)
			{
				//alert(res);
				var mandatory = $.json.deserialize(res);
				var title_result = "";
				var drmtext_result = "";
				var inttext_result = "";
				var intfocus_result = "";
				var level_result = "";
				var status_result = "";
				for(key in mandatory)
					{
						 title_result += mandatory[key].title;
						 drmtext_result += mandatory[key].drmtext;
						 intfocus_result += mandatory[key].intfocus;
						 inttext_result += mandatory[key].inttext;
						 level_result += mandatory[key].level;
						 status_result += mandatory[key].status;
					}
				if(request == "suggestmandatory")
					{
						$('#a-dreamtitle').append(title_result);
						$('#a-dreamtext').append(drmtext_result);
						$('#a-status').append(status_result);
						$('#a-intfocus').append(intfocus_result);
						$('#a-inttext').append(inttext_result);
						$('#a-apprenticelevel').append(level_result);
						$('#dialog-mandatory').dialog('open');
					}
				else if(request == "submitmandatory")
					{
						if(title_result)
							{
								$('#button-suggestmandatory').hide();
								$('#dialog-mandatory').dialog('close');
								updateTips("Mandatory Dream Successfully Suggested",$('#tips-success-cert'));
								if(from_interp)
									{
										$('#dialog-int-mandatory').dialog('open');
									}
								else
									{
										$('#dialog-success-cert').dialog('open');
									}
								
							}
						else
							{
								updateTips("There was a problem suggesting this a mandatory. Please try again or contact customer support at support@interpretmydream.com",$('#tips-error'));
								$('#dialog-error').dialog('open');
							}
					}
			});
	}
function UpdateCertification(request,dreamid,held_by_id,interp_id,critiqueid,intcomment,dreamcomment)
	{
		$.post(certinterpURL,
		   {
			   request:request,
			   dreamid:dreamid,
			   held_by_id:held_by_id,
			   interp_id:interp_id,
			   critiqueid:critiqueid,
			   intcomment:intcomment, 
			   dreamercomment:dreamercomment
			},
		function(data)
			{
				disp_alert(data);
			});
	}
function disp_interpreter(username)
	{
		alert("The interpreter is " + username);
	}
function disp_alert(data)
	{
	//alert("This interpretation has been certified"+data);
	window.open("http://www.interpretmydream.com/certification.php","_self");
	}
function blockForDataLoad()
	{
		$.blockUI(
				  {
					  message: '<img src="images/processing_bar.gif" />', 
				   css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					'-ms-filter': 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)',
					'filter': 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)',
					opacity: '.5' 
					} 
				  });
	}
function unblockForDataLoad()
	{
		$.unblockUI();
	}