/********************************************************************************************
 *
 * 					            CLASS TRANSLATION
 *
 ********************************************************************************************
 *
 *
 *  @author Rhonda Kammer
 *
 ********************************************************************************************
 *
 * NOTES:
 *
 ********************************************************************************************/


function edit_translation_class(){


	/** ****** private class variables *********** */	
	var _session_id 			= ec.get_session_id();
	var _object_property 		= ec.get_property_name();
	var _jq_orig_element_id 	= ec.get_jq_original_element_id();
	var _edit_object 			= ec.get_edit_object();
	var _edit_trans_height 		= ec.get_callout_height();
	var _edit_trans_width  		= ec.get_callout_width();
	var _script_uri 			= ec.get_script_uri();	
	var _property_name 			= ec.get_property_name();
	var _object_id 				= ec.get_object_id();
	var _user_language			= ec.get_user_language();
	var _nav_template			= ec.get_nav_template();	
	var _ws_timestamp0 			= '';
	var _ws_timestamp1			= '';	
	var _timestamp_result 		= '';	
	var _translated_text		= '';
	var _orig_text 				= $(_jq_orig_element_id).html();
	var _text_diff				= '';
	var _text_length			= '';
	var _web_string_english		= '';
	var _web_string_language	= '';
	var _browser_lang_diff		= '';
	var _english_lang_diff		= '';
	var _ajax_test_url			= "";	


	//*************************************
	//*****  constructor actions  *********
	//*************************************
	
	//set diff of current language in browser
	set_history_diff( );		
	
	//set english diff
	set_history_diff(3);


	//*******************************************************************
	//********************  PUBLIC METHODS  *****************************
	//*******************************************************************
	
	edit_translation_class.prototype.compare_timestamps = function( response ){
		
		//degug (2) lines
		var test_url = ec.get_ajax_test_url();		
		//ec.show_ajax_debug( test_url, response );
		
		//NOTE: response element [0] is the english and element [1] is the user language
		var full_ts = '';
		if( _user_language == 3 ){
			if( is_set(response[0]) ){
				full_ts = response[0].timestamp;
			}
     	}else{
	     	if( is_set(response[1]) ){
	     		full_ts = response[1].timestamp;	     		
	     	}else{
	     		full_ts = response[0].timestamp;
	     	}
     	}
		
		configure_tabs(response);		
     	
     	if( is_set( full_ts ) ){
			var parts = full_ts.split(".");
			_ws_timestamp0 = parts[0];
			_ws_timestamp1 = parts[1];
			do_collision_actions(response);			
			return true;
		}else{
			_ws_timestamp0 = '';
			_ws_timestamp1 = '';
			do_collision_actions(response);
			return false;
		}
	};
	
	
	/**
	 * Method attempts to save a translated text to the database. Ajax
	 * call is made to edit_ajax_processor then callback to get_save_response	
	 */
	edit_translation_class.prototype.process_tab_save = function(){

		_translated_text = $("#editCallout_translatedText").val();
		if( _translated_text != '' ){
			save_translation();
	        $("#editCallout_translatedText").val('');
		}else{
			$("#editTool_Message").html('<font color="red">Please enter a translation.</font>');
		}
		return true;
	};//fun pts
    
	/**
	 * Depricated: will be erased soon. Was called by edit_callout but
	 * now configure tabs is called by public set_diff
	 */
	edit_translation_class.prototype.configure_tabs = function(){		
		configure_tabs();		
	};//ct


	/**
	 * Method processes save attempt by process_tab_save. If translated webstring
	 * cannot be saved user is informed to refresh and try again
	 */
	edit_translation_class.prototype.get_save_response = function( response ){
		
		//ec.show_ajax_debug( _ajax_test_url, response );

		 if( ec.get_user_language() == 3 ){
			 _timestamp_result = response[0].ts_result;
			 _timestamp_actual = response[0].timestamp;
		 }else{
			 _timestamp_result = response[0].ts_result;
			 _timestamp_actual = response[1].timestamp;
		 }

   //alert( "translation save 1: ts db: resp 0: " + _timestamp_result + " resp 1: " + _timestamp_result + " user lang: " + ec.get_user_language() );
		 

		if( _timestamp_result == "timestamp_different_db" && response[0].db_result == undefined ){			
			do_collision_actions( response );
		}

		if( _timestamp_result == "timestamp_same_db" ){
			$(ec.get_jq_original_element_id()).text(response[1].webstring_text);
			$(_jq_orig_element_id).css({ 'color':'green' });			
			close_edit_callout();
		}
		if(_nav_template == 'building'){
			var donothing = '';
		}else{
			window.location.href = document.URL;
		}

	};//fun gsr


	edit_translation_class.prototype.set_ws_timestamp0 = function( timestamp_0 ){
		return _ws_timestamp0 = timestamp_0;
	};

	edit_translation_class.prototype.set_ws_timestamp1 = function( timestamp_1 ){
		return _ws_timestamp1 = timestamp_1;
	};
	
	
	edit_translation_class.prototype.get_history = function(){
		
		//ec.show_ajax_debug( _ajax_test_url, response );
		
		var url = '';
		url =  _script_uri + "?";
		url += 'pub_task=edit_callout';
		url += '&session_id=' + encodeURIComponent(_session_id);
		url += '&edit_object=translation';
		url += '&tool_purpose=webstring';		
		url += '&property_name=' + encodeURIComponent(_property_name);
		url += '&tool_task=history';
		url += '&user_lang=' + encodeURIComponent(_user_language);
		
		_ajax_test_url = url;
		
		//alert( "translation: get history called." + url );
		$('#editCallout_tabsContainer_historyTab').html("");
		ajax_launcher( url, '', 'edit_translation', 'translation.show_history' );
		
		
	};//gh
	
	edit_translation_class.prototype.show_history = function( response ){
		
		//ec.show_ajax_debug( _ajax_test_url, response );
		
		var row_count = 1;
		var display = '<table width="100%">';

		if(is_set(response)){
			for( each in response ){
	
				var newDate = new Date( );
				var unixts  = response[each].t_import_data_time_entry;
				newDate.setTime( unixts * 1000 );
				dateString = newDate.toUTCString( );
	
			    display += '<tr><td bgcolor="#BFD2FF"><font color="white">' + dateString + '</font></td></tr>';
			    
				display += '<tr><td>' + response[each].value + '</td></tr>';
						
			}
			display += '</table>';
			$('#editCallout_tabsContainer_historyTab').html( display );
		}else{
			$('#editCallout_tabsContainer_historyTab').html('');
		}		
	};
	
	edit_translation_class.prototype.set_diff = function( response ){
		
		//ec.show_ajax_debug( _ajax_test_url, response );
		
		//alert( 'translation: ' + _web_string_english );
					
		if( is_set(response[0]) && is_set(response[1])){
			
			//alert('translation: old: ' + response[1].value + ' new: ' + response[0].value );
			_text_diff = content_diff_string( response[1].t_import_data_value_text, response[0].t_import_data_value_text );
						
			//fill upper container div with text to be translated			
			if( _orig_text == '[label to edit]' ){ 
				_text_diff = 'no text diff to show'; 
			}
			if( response[0].t_language_id == 3 ){ 
				_english_lang_diff = _text_diff;				
			}
			if( response[0].t_language_id != 3 ){ 
				_browser_lang_diff = _text_diff;				
			}
		}				
		return true;		
	};	
	
	// *******************************************************************
	// ********************  PRIVATE METHODS  ****************************
	// *******************************************************************
	
	function configure_tabs( response ){
		
		//ec.show_ajax_debug( _ajax_test_url, response );						
		
		var text_to_embed = '';
		$("#editCallout_translatedText").val('');		
				
		//these two do not work but must eventually find out how
		//$('#editCallout_tabsContainer_editTab').resizable();
		//$('#editCallout_tabsContainer_editTab').resizable({ alsoResize: '.other' });
		
		if( is_set(response[1]) && is_set(response[0])){
			_web_string_language = response[1].webstring_text;
			_web_string_english  = response[0].webstring_text;
		}else if( is_set( response[0] ) && !is_set(response[1]) ){
			_web_string_language = response[0].webstring_text;
			_web_string_english  = response[0].webstring_text;
		}		
								
	    if( BrowserDetect.browser == 'Chrome' || BrowserDetect.browser == 'Safari' ){        		
    		_edit_trans_height = _edit_trans_height - 105;
    		ec.resize_edit_callout( _edit_trans_height, _edit_trans_width );
    	}else{
    		ec.resize_edit_callout( _edit_trans_height, _edit_trans_width );
    	}
	    
	  //make edit tab selected
	    $("#editCallout_tabsContainer").tabs( 'select', 1 );
							
		$("#editCallout_tabsContainer_editTabTitle").html("translate");		
		$("#editCallout_tabsContainer_editTabValue0").html('<b><font color="blue">' + _object_property + '</font></b>');
		
		//fill upper div with english diff text
		if( is_set(_english_lang_diff)){			
			$("#editCallout_translation").html(_english_lang_diff).css({'overflow':'auto'});		
		}else{			
			$("#editCallout_translation").html(_web_string_english).css({'overflow':'auto'});
		}
		
		//fill lower textarea where tranlation is edited
		$("#editCallout_tabsContainer_editTabLabel1").html("Translation: <br><br>");						
		$("#editCallout_tabsContainer_editTabValue1").hide();	    	
		if( is_set( _web_string_language )){
			$("#editCallout_translatedText").val(_web_string_language);			
		}else{
			$("#editCallout_translatedText").val('');
		}		

		//size and show translation textarea
		$("#editCallout_translatedText").css({ 'height':'160px' });
		$("#editCallout_translatedText").css({ 'width':'235px' });
		$("#editCallout_translatedText").css({'display':'block'});
		
		//alert('translation: ' + is_set( _web_string_language ) );
		
		//size and show source textarea	- Release 5
//		$("#editCallout_tabsContainer_editTabLabel1").css({ 'width':'155px' });
//		$("#editCallout_tabsContainer_editTabLabel0").css({ 'height':'25px' });		
//		$('#editCallout_tabsContainer_editTabLabel2').show();
//		
//		$("#editCallout_tabsContainer_editTabSource1").css({ 'height':'55px' });
//		$("#editCallout_tabsContainer_editTabSource1").css({ 'width':'235px' });
//		$('#editCallout_tabsContainer_editTabSource1').show();
				
		$("#editCallout_translatedText").focus();		
	}//ct
	
	
	function do_collision_actions( response ){
		
		//ec.show_ajax_debug( _ajax_test_url, response );

		//alert( "translation: collision action: " + response[0].ts_result );
		
		if( response[0].ts_result == "timestamp_same_nodb" ){
			//alert( "Timestamp check ok " );
			//ok to translate but hurry
		}

		if( response[0].ts_result == "timestamp_different_db" ){
			message = '<font color="red">Your translation could not be saved.</font> <br> The value has changed. <br> Close callout and try again if you wish.</font>';
			$('#editTool_Message').html(message);
			$('#editCallout_translation').text(response[1].webstring_text);
			$(_jq_orig_element_id).text(response[1].webstring_text);			
			$(_jq_orig_element_id).css({ 'color':'red' });
			$("#editCallout_translatedText").css({'display':'none'});
			//alert( "translation coll act: " + _close_callout );
		}
		
		if( response[0].ts_result == "timestamp_different_nodb" ){
		
			//first check if value user has on page is the same as in response[1].webstring_text
			//if same then this the user that changed and the message should not be sent
			var val_on_page = $(ec.get_jq_original_element_id()).text();			
			
			if( val_on_page != response[1].webstring_text){
				message = '<font color="red">Please note, text has changed since browser opening.</font>';
				$('#editTool_Message').html(message);
				$(_jq_orig_element_id).css({ 'color':'red' });
			}

			$(_jq_orig_element_id).text(response[1].webstring_text);
			$('#editCallout_translation').text(response[1].webstring_text);
			//_ws_timestamp0 = ec.get_ws_timestamp0();
		    //_ws_timestamp1 = ec.get_ws_timestamp1();
			ec.set_ws_timestamp0( response[1].timestamp );
			ec.set_ws_timestamp1( response[1].timestamp );
		}
		
		//alert( 'Translation: res: ' + response[1].timestamp + ' page: ' + val_on_page + ' ajax: ' + response[1].webstring_text );

	}//fun dca
	
	function close_edit_callout(){
		$("#editCallout_innerContainer").dialog( "destroy" );
		$(_jq_orig_element_id).css({ "borderBottom":"0px solid #183961" });
        $(_jq_orig_element_id).css({ "borderRight":"0px solid #183961" });
        $(_jq_orig_element_id).css({ "borderLeft":"0px solid #819DC0" });
        $(_jq_orig_element_id).css({ "borderTop":"0px solid #819DC0" });
        $(_jq_orig_element_id).css({ "backgroundColor":"#FFFFFF" });
	}//cec

	function save_translation(){
		
		//send translated text as post variable
		var text_data = {};
		text_data['translated_text'] = $('#editCallout_translatedText').val();
		text_data['translated_text'] = encodeURIComponent(text_data['translated_text']);
		//text_data['translated_text'] = text_data['translated_text'];
		
		var url = _script_uri + "?";
		url += 'pub_task=edit_callout';
		url += '&session_id=' + encodeURIComponent(_session_id);
		url += '&edit_object=translation';
		url += '&property_name=' + encodeURIComponent(_property_name);
		url += '&object_id=' + encodeURIComponent(_object_id);
		url += '&web_string_ts_0=' + encodeURIComponent(_ws_timestamp0);
		url += '&web_string_ts_1=' + encodeURIComponent(_ws_timestamp1);		
		url += '&user_lang=' + encodeURIComponent(_user_language);
		url += '&tool_task=process';

		_ajax_test_url = url;

		ajax_launcher( url, text_data, 'edit_translation', 'translation.get_save_response' );

		//alert( "translation tab save: " + url );
		
	}//st
	
    /**
	 * Used to retrieve and store label/translation history independant of tab click
	 */			
	function set_history_diff( language ){
		
		var url =  _script_uri + "?";
		url += 'pub_task=edit_callout';
		url += '&session_id=' + encodeURIComponent(_session_id);
		url += '&edit_object=translation';
		url += '&tool_purpose=webstring';		
		url += '&property_name=' + encodeURIComponent(_object_property);
		url += '&tool_task=history';
		if( is_set(language) ){
			url += '&user_lang=' + encodeURIComponent(language);
		}else{
			url += '&user_lang=' + encodeURIComponent(_user_language);
		}
		
		_ajax_test_url = url;
		
		//alert( "translation: get history diff called." + url );		
		ajax_launcher( url, '', 'edit_translation', 'translation.set_diff' );							
	}//ghd

}














