
/*!
 * jQuery UI Widget-factory plugin boilerplate (for 1.8/9+)
 * Author: @addyosmani
 * Further changes: @peolanha
 * Licensed under the MIT license
 *http://jqueryui.com/demos/widget/
 TIMER
 http://bililite.com/blog/extending-jquery-ui-widgets/ GUTE ADDRESSE
 */




/*
<div id="progress_bar">
				
			</div>
			<span id="progress_bar_text">noch ca. 75min - Konzentrieren sie sich auf den Prozess...</span>
			<div id="progress_overview">
				<span class="progress_overview_done">1 | Login</span>
				<span class="progress_overview_done">2 | Ziel</span>
				<span class="progress_overview_done">3 | Erklärung</span>
				<span class="progress_overview_aktiv">4 | Parallele Bewertung</span>
				<span class="progress_overview_open">5 | Meeting</span>
				<span class="progress_overview_open">6 | Nacharbeit</span>
				<span class="progress_overview_open">7 | Fragebogen Teil 1</span>
		 		<span class="progress_overview_open">8 | Auswertung der Ergebnisse</span>
		 		<span class="progress_overview_open">9 | Fragebogen - Teil 2</span>
		 	</div>
		 	<div id="progress_time_all">
		 		<span>Gesamtzeit ca. <span id="progress_time_all_in_min">97</span> min</span>
		 	</div>

*/

;(function ( $, window, document, undefined ) {

    $.widget( "CE.SessionStep_Progressbar.js" ,$.CE.DataElement ,{

        options: { 
                ObjName: 'SessionStep_Progressbar',
                ActivSessionStepID: 0,
        		SessionSteps:[],
        		ParticipantID: 0,
        		StartTime:0,
        		EstimatedTimeAll_in_s:0
        },
        _init: function(){
			
			var w=this, e=w.element, o=w.options;
 			
 			w._initDataElement();
 			w._initProgressbar();
 					

        },
        _initProgressbar: function()
        {
        	var w=this, e=w.element, o=w.options, ss=o.SessionSteps;
        	e.addClass('CE_Session_Progressbar'); 	
        		
        	e.append('<div id="progress_bar"></div>');
        	e.append('<div id="progress_bar_text"></div>');
			
			e.append('<div id="progress_overview"></div>');      
			
			for(var i=0; i< ss.length; i++)
			{
			  	e.find('#progress_overview').append('<span class="progress_overview_open" ss_id="'+ss[i].attr('data_DataID')+'" bar_text="'+ss[i].attr('data_progressbar_comment')+'" estimated_time="0">'+i+'.'+ss[i].attr('data_step_name')+'</span>');	
			}  	
        	
        	e.append('<div id="progress_time_all"><span>Gesamtzeit ca. <span id="progress_time_all_in_min"></span> min</span></div>');
        		
        	//debug(o.ObjName+' >>>>>> Init with SS('+o.SessionSteps.length+') of Participant('+o.ParticipantID+') and Active('+o.ActivSessionStepID+')',true,o.debug_it);
        		
        	w._computeEstimatedTimes();
        	w._useActiveSessionStepID();
        	
        	
        },
        _useActiveSessionStepID:function()
        {
        	var w=this, e=w.element, o=w.options,  ss=o.SessionSteps;
        	
        	if(o.ActivSessionStepID==0) return false; 
        	
        	var beforActive=true;
        	
        	for(var i=0; i< ss.length; i++)
        	{
        		var tmp_related_span="";
        		tmp_related_span=e.find('span[ss_id="'+ss[i].attr('data_DataID')+'"]');
        		
        		if(ss[i].attr('data_DataID')==o.ActivSessionStepID)
        		{
        			beforActive=false;
        			tmp_related_span.removeClass('progress_overview_done').removeClass('progress_overview_open').addClass('progress_overview_aktiv');
        			
        			var bar_text=tmp_related_span.attr('bar_text');
        			
        			
        				e.find('#progress_bar_text').text(bar_text);
        				
        				w._update_progressbar();   
        			
        		}else
        		{
        			if(beforActive==true)
        			{
        				tmp_related_span.removeClass('progress_overview_aktiv').removeClass('progress_overview_open').addClass('progress_overview_done');	
        			
        			}else
        			{
        				tmp_related_span.removeClass('progress_overview_aktiv').removeClass('progress_overview_done').addClass('progress_overview_open');
        			}
        		}
        	}
        	
        	//debug(o.ObjName+' >>>>>> _useActiveSessionStepID('+o.ActivSessionStepID+')',true,o.debug_it);
        	
        	     	
        },
        _computeEstimatedTimes: function()
        {
        	var w=this, e=w.element, o=w.options,  ss=o.SessionSteps;
        	
        	var estimated_time_all_in_s=0;
        	var count_steps_with_estimated_time=0;
        	
        	var MIN_TIME_PER_STEP=120;
        	
        	// Estimated Time auslesen und Gesamtzeit berechnen
        	for(var i=0; i< ss.length; i++)
        	{
        		var tmp_estimated_time_of_step=ss[i].attr('data_time_estimated');
        		
        		var tmp_h=0;
        		var tmp_min=0;
        		var tmp_s=MIN_TIME_PER_STEP;
        		
        		if(tmp_estimated_time_of_step!="00:00:00" && tmp_estimated_time_of_step!=undefined)
        		{
        			tmp_h=parseInt(tmp_estimated_time_of_step.slice(0,2));
        			tmp_min=parseInt(tmp_estimated_time_of_step.slice(3,5));
        			tmp_s=parseInt(tmp_estimated_time_of_step.slice(6,8));
        		}
        		
        		//debug(o.ObjName+'_computeEstimatedTimes('+tmp_h+'-'+tmp_min+'-'+tmp_s+')',false,o.debug_it);
        			
        		tmp_time_for_step_in_s=tmp_s+(tmp_min*60)+(tmp_h*60*60);        			
        		e.find('span[ss_id="'+ss[i].attr('data_DataID')+'"]').attr('estimated_time', tmp_time_for_step_in_s);
        			
        		estimated_time_all_in_s=estimated_time_all_in_s+tmp_time_for_step_in_s;
        		count_steps_with_estimated_time++;
        	}
        	
        	o.EstimatedTimeAll_in_s=estimated_time_all_in_s;
        	        	
        	//debug(o.ObjName+'_computeEstimatedTimes('+estimated_time_all_in_s+' von '+count_steps_with_estimated_time+'/ '+ss.length+' Steps)',true,o.debug_it);
        	
        	e.find('#progress_time_all_in_min').text(Math.round(estimated_time_all_in_s/60));
        },
        _update_progressbar: function()
        {
        	var w=this, e=w.element, o=w.options;
        	
        	var seconds=w._getgoneSecondsOfTimeForActiveSS();
        	var seconds_for_this_step=parseInt(e.find('span[ss_id="'+o.ActivSessionStepID+'"]').attr('estimated_time'));
        	
        	
        	var mins_left=(o.EstimatedTimeAll_in_s-seconds)/60;
        	
        	e.find('#progress_bar_text').prepend('<span>Noch ca. '+Math.round(mins_left)+' min - </span>')
        	
        	var percent_befor_to_this_step=(seconds*100/o.EstimatedTimeAll_in_s);
        	var percent_befor_with_this_step=((seconds+seconds_for_this_step)*100/o.EstimatedTimeAll_in_s);
        	
        	
     		var MIN_PERCENT=2;
        	if(percent_befor_to_this_step<MIN_PERCENT)
        	{
        		percent_befor_to_this_step=MIN_PERCENT;
        	}
        	
        	e.find('#progress_bar').stop();
        	        	
        	var breite_in_px_to_this_step=e.width()*percent_befor_to_this_step/100;
        	var breite_in_px_with_this_step=e.width()*percent_befor_with_this_step/100;
        	
        	
        	e.find('#progress_bar').animate({'width': breite_in_px_to_this_step+'px'}, 'slow', function()
        	{
        			e.find('#progress_bar').animate({'width': breite_in_px_with_this_step+'px'}, seconds_for_this_step*1000);
        	
        	});
        	
        	
        	//debug(o.ObjName+'_getPercentOfTimeForActiveSS('+breite_in_px_to_this_step+')',true,o.debug_it);
        },
        _getgoneSecondsOfTimeForActiveSS: function()
        {
        	var w=this, e=w.element, o=w.options,  ss=o.SessionSteps;
        	
        	var left_time_in_s=0;
        	
        	for(var i=0; i< ss.length; i++)
        	{
        		if(ss[i].attr('data_DataID')==o.ActivSessionStepID)
        		{
        			break;
        		}
        		
        		var tmp_left_time_in_s=e.find('span[ss_id="'+ss[i].attr('data_DataID')+'"]').attr('estimated_time');
        		
        		left_time_in_s=left_time_in_s+parseInt(tmp_left_time_in_s);   		
        	}
        	
        	if(o.EstimatedTimeAll_in_s==0)
        	{
        		return 0;
        	} 
        	
        	return left_time_in_s;
        },        
        setOption: function ( key, value, animate, commitToDatabase ) 
        {
			var w=this, e=w.element, o=w.options;
        	
        	if(key!=null && key!=undefined)
            {
            	//debug(o.ElementID+' setValue '+key+' = '+value,false);
            	switch (key) 
            	{     
            		case "ActivSessionStepID":
            			 	o.key = value;
               	 			o[key] = value; 
            				w._useActiveSessionStepID();            		
            			break;
            		   	
            		default:
               	 		o.key = value;
               	 		o[key] = value;   
                		break;
           		}
           		
           		
           		if(commitToDatabase) w._setOption(key, value);
            }
            $.Widget.prototype._setOption.apply( this, arguments );
        }
    });
  
  
 
})( jQuery, window, document );
