function Job(){
	var currentTab = 'printJobTab';
	
    this.init = function() {  
    	self.registerOnclick();
    }
    
    this.registerOnclick = function() {
    	if(dijit.byId("jobTabContainer")){
	    	var tabs = dijit.byId("jobTabContainer");
	    	
	    	if(tabs.tablist._currentChild.id != currentTab) {
	    		currentTab = tabs.tablist._currentChild.id;
	    		self.showModule(currentTab);
	    	}
	    	switch(tabs.tablist._currentChild.id){
	    	case "displayJobTab":
	    		dijit.byId('listingFilter').attr('href', '/job/show-listing-filter/display');
	    		break;
	    	default:
	    		dijit.byId('listingFilter').attr('href', '/job/show-listing-filter/print');	
	    		break;
	    	}
    	}
    }
    
    this.showModule = function(tabName) {
    	switch(tabName) {
    		case "printJobTab":
    			dijit.byId('jobListWrapper').attr('href', '/job/show-user-jobs/');
			break;
    		case "displayJobTab":
    			dijit.byId('jobListWrapper').attr('href', '/job/show-user-displays/');
    		break;
    		default:
    			//alert("default");
    		break;
    	}
    }
    
    this.commUpload = function(path){
    	
        dojo.xhrPost({
            url: "/job/set-comm-upload-folder/",
            postData: "path=" + path ,
            load: function(response, ioArgs){
    			errorHandler.show();
    			
    			dojo.byId('commUploadDialogIframe').src = '/job/add-comm-files'; 
    			dijit.byId('commUploadDialog').show();
    			
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });    	
    }
    
    this.showChart = function() {
    	if(dojo.byId('jobStatChart'))
    	dojo.byId('jobStatChart').destroyRecursive();
    	
    	var flashvars = {
    		  library_path: "/charts/charts_library",
    		  xml_source: "/job/statistics-xml",
  		};
  		
    	var params = {
        		wmode: "transparent",
        		scale: 'noscale',
    			salign: 'TL',
        };
        	    		
        var attributes = {
      			id: "jobStatChart",
      			name: "jobStatChart",
     			width: '260',
    			height: '200',
    			menu: 'true',
    			allowFullScreen: 'true',
    			allowScriptAccess: 'sameDomain',
    			quality: 'high',
    			pluginspage: 'http://www.macromedia.com/go/getflashplayer',
    			play: 'true',
    			devicefont: 'false'
        };
        
        if(dojo.byId('jobStatistics'))
  	    swfobject.embedSWF("/charts/charts.swf", "jobStatistics", "260", "200", "9.0.0", "", flashvars, params, attributes);
    }
    
    this.showAddChart = function() {
    	if(dojo.byId('addJobStatChart'))
        	dojo.byId('addJobStatChart').destroyRecursive();
        	
        	var flashvars = {
        		  library_path: "/charts/charts_library",
        		  xml_source: "/job/additional-statistics-xml",
      		};
      		
        	var params = {
            		wmode: "transparent",
            		scale: 'noscale',
        			salign: 'TL',
            };
            	    		
            var attributes = {
          			id: "addJobStatChart",
          			name: "addJobStatChart",
         			width: '260',
        			height: '200',
        			menu: 'true',
        			allowFullScreen: 'true',
        			allowScriptAccess: 'sameDomain',
        			quality: 'high',
        			pluginspage: 'http://www.macromedia.com/go/getflashplayer',
        			play: 'true',
        			devicefont: 'false'
            };
            
            if(dojo.byId('additionalJobStatistics'))
      	    swfobject.embedSWF("/charts/charts.swf", "additionalJobStatistics", "260", "200", "9.0.0", "", flashvars, params, attributes);        
    }
    
    this.updateChart = function() {
    	if(!document.jobStatChart) {
    		self.showChart();
    	}
    	else {
	        // the update url to download the xml update from
	        url = "/job/statistics-xml";
	     
	        // show a spinning wheel while downloading the update
	        spinning_wheel = false; 
	     
	        // number of seconds to wait before a download times out
	        timeout = 30; 
	     
	        // number of times to try downloading before displaying an error
	        retry = 2; 
	        
	        // the update mode (see the update function)
	        mode = "data";
	     
	        document.jobStatChart.Update_URL( url, spinning_wheel, timeout, retry, mode );
    	}
    }
    
    this.updateAddChart = function() {
    	if(!document.addJobStatChart) {
    		self.showAddChart();
    	}
    	else {
	        // the update url to download the xml update from
	        url = "/job/additional-statistics-xml";
	     
	        // show a spinning wheel while downloading the update
	        spinning_wheel = false; 
	     
	        // number of seconds to wait before a download times out
	        timeout = 30; 
	     
	        // number of times to try downloading before displaying an error
	        retry = 2; 
	        
	        // the update mode (see the update function)
	        mode = "data";
	     
	        document.addJobStatChart.Update_URL( url, spinning_wheel, timeout, retry, mode );
    	}
    }    
    
    this.changeAddStatType = function(value){
        dojo.xhrPost({
            url: "/job/change-add-stat-type/",
            postData: "value=" + value ,
            load: function(response, ioArgs){
   				self.updateAddChart();
    			errorHandler.show();
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });    	
    }
    
    this.changeStatType = function(value){
        dojo.xhrPost({
            url: "/job/change-stat-type/",
            postData: "value=" + value ,
            load: function(response, ioArgs){
   				self.updateChart();
    			errorHandler.show();
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });    	
    }    
    
    this.showDisplayChart = function() {
    	if(dojo.byId('jobDisplayStatChart'))
    	dojo.byId('jobDisplayStatChart').destroyRecursive();
    	
    	var flashvars = {
    		  library_path: "/charts/charts_library",
    		  xml_source: "/job/display-statistics-xml",
    	};
  		
    	var params = {
          		wmode: "transparent",
          		scale: 'noscale',
      			salign: 'TL',
    	};
          	    		
    	var attributes = {
        		id: "jobDisplayStatChart",
        		name: "jobDisplayStatChart",
       			width: '566',
      			height: '349',
      			menu: 'true',
      			allowFullScreen: 'true',
      			allowScriptAccess: 'sameDomain',
      			quality: 'high',
      			pluginspage: 'http://www.macromedia.com/go/getflashplayer',
      			play: 'true',
      			devicefont: 'false'
        };
        
    	if(dojo.byId('jobDisplayStatistics'))
  	    swfobject.embedSWF("/charts/charts.swf", "jobDisplayStatistics", "576", "349", "9.0.0", "", flashvars, params, attributes);
    }
    
    this.updateDisplayChart = function() {
    	if(!document.jobDisplayStatChart) {
    		self.showDisplayChart();
    	}
    	else {
	        // the update url to download the xml update from
	        url = "/job/display-statistics-xml";
	     
	        // show a spinning wheel while downloading the update
	        spinning_wheel = false; 
	     
	        // number of seconds to wait before a download times out
	        timeout = 30; 
	     
	        // number of times to try downloading before displaying an error
	        retry = 2; 
	        
	        // the update mode (see the update function)
	        mode = "data";
	
	     
	        document.jobDisplayStatChart.Update_URL( url, spinning_wheel, timeout, retry, mode );
    	}
    }

    this.changeCategory = function(level, add_id, parent_id) {
    	if(parseInt(level) < 7) {
	        dojo.xhrPost({
	            url: '/job/set-user-category/'+level+'/'+add_id+'/'+parent_id,
	            load: function(response, ioArgs){
	    			dijit.byId('jobListWrapper').attr('href', '/job/show-user-jobs/');
	    			
	    			//dijit.byId('jobBreadcrumbs').attr('href', '/job/show-breadcrumbs/');
	    			job.updateChart();
	    			job.updateAddChart();
	    			dijit.byId('listingAdditionalSignallightsWrapper').refresh();
	    			errorHandler.show();
	                return response;
	            },
	            error: function(response, ioArgs){
	            	errorHandler.show();
	                return response;
	            },
	            handleAs: "text"
	        });
    	}
    	else {
    		
    	}
    }
    
    this.doCopyCategoryStructure = function(catName){
    	selectedStructureId = dijit.byId('categoryStructure').getValue();
    	
    	if(selectedStructureId > 0 && catName != ""){
	        dojo.xhrPost({
	            url: '/job/copy-category-structure/',
	            postData: "selectedStructureId=" + selectedStructureId + "&catName=" + catName ,
	            load: function(response, ioArgs){
	    			dijit.byId('jobListWrapper').refresh();
	
	    			errorHandler.show();
	                return response;
	            },
	            error: function(response, ioArgs){
	            	errorHandler.show();
	                return response;
	            },
	            handleAs: "text"
	        });
    	}
    }
    
    this.changeDisplayCategory = function(level, add_id, parent_id) {
    	if(parseInt(level) < 7) {
	        dojo.xhrPost({
	            url: '/job/set-user-display-category/'+level+'/'+add_id+'/'+parent_id,
	            load: function(response, ioArgs){
	    			dijit.byId('jobListWrapper').attr('href', '/job/show-user-displays/');

	    			job.updateDisplayChart();
	    			errorHandler.show();
	                return response;
	            },
	            error: function(response, ioArgs){
	            	errorHandler.show();
	                return response;
	            },
	            handleAs: "text"
	        });
    	}
    	else {
    		
    	}
    }
    
    this.setUser = function(userId) {
        dojo.xhrPost({
            url: '/job/set-user/',
            postData: "user_id=" + userId,
            load: function(response, ioArgs){
        	
				dijit.byId('jobListWrapper').attr('href', '/job/show-user-jobs/');
				dijit.byId('jobBreadcrumbs').attr('href', '/job/show-breadcrumbs/');
    			
        		errorHandler.show();
    			job.updateDisplayChart();
    			
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }
    
    this.selectDisplay = function(displayId) {
        dojo.xhrPost({
            url: '/job/set-display/',
            postData: "display_id=" + displayId,
            load: function(response, ioArgs){
        		if(dijit.byId('displayBreadcrumbs')) {
	    			dijit.byId('displayBreadcrumbs').attr('href', '/job/display-breadcrumbs/');
	    			
        		}
        		
        		dijit.byId('jobListWrapper').attr('href', '/job/show-user-displays/');
    			
        		errorHandler.show();
    			job.updateDisplayChart();
    			
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }

    this.selectMeasurement = function(measurementId) {
        dojo.xhrPost({
            url: '/job/select-measurement/',
            postData: "measurement_id=" + measurementId,
            load: function(response, ioArgs){
        		//if controlModule
        		if(dojo.byId('lowerAreaControl'))
        		{
          			control.showEvaluationHolder();
        			control.updateCgas();
        			errorHandler.show();
        		}
        		else
        		{
        			document.location.href = '/control/print';
        		}
        		
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }
    
    this.selectMeasurementDisplay = function(measurementDisplayId) {
        dojo.xhrPost({
            url: '/job/select-measurement-display/',
            postData: "measurementDisplay_id=" + measurementDisplayId,
            load: function(response, ioArgs){
        		//if controlModule
        		if(dojo.byId('lowerAreaControl'))
        		{
        			controlDisplay.showDisplayEvaluationHolder();
        			dijit.byId('controlDisplayBreadcrumbs').refresh();
        			control.updateDisplayCgas();
        			errorHandler.show();
        		}
        		else
        		{
        			document.location.href = '/control/display';
        		}
        		
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }
    
    this.selectDisplayDisplay = function(DisplayId) {
        dojo.xhrPost({
            url: '/job/select-display-display/',
            postData: "Display_id=" + DisplayId,
            load: function(response, ioArgs){
        		//if controlModule
        		if(dojo.byId('lowerAreaControl'))
        		{
        			controlDisplay.showDisplayEvaluationHolder();
        			dijit.byId('controlDisplayBreadcrumbs').refresh();
        			controlDisplay.updateDisplayChas();
        			errorHandler.show();
        		}
        		else
        		{
        			document.location.href = '/control/display';
        		}
        		
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }    
    
    this.selectCt4 = function(categoryId) {
        dojo.xhrPost({
            url: '/job/select-category4/',
            postData: "category_id=" + categoryId,
            load: function(response, ioArgs){
        		//if controlModule
        		if(dojo.byId('lowerAreaControl'))
        		{
          			control.showEvaluationHolder();
        			control.updateChas();
        			errorHandler.show();
        		}
        		else
        		{
        			document.location.href = '/control/print';
        		}

                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }    
    
    this.selectCt5 = function(categoryId) {
        dojo.xhrPost({
            url: '/job/select-category5/',
            postData: "category_id=" + categoryId,
            load: function(response, ioArgs){
        		//if controlModule
        		if(dojo.byId('lowerAreaControl'))
        		{
        			control.showEvaluationHolder();
        			control.updateChas();
        			errorHandler.show();
        		}
        		else
        		{
        			document.location.href = '/control/print';
        		}        		
        		
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }      
    
    this.selectForZert = function(id,level) {
        dojo.xhrPost({
            url: '/job/select-for-zert/',
            postData: "id=" + id + "&level=" + level,
            load: function(response, ioArgs){
        		//if controlModule
        		if(dojo.byId('lowerAreaZert'))
        		{
        			//zertPrinthistorized.updateZertPrinthistorizedChart();
        			zertPrinthistorized.slideAllIn();
        			
            		if(dijit.byId('printhistorizedBreadcrumbs')) {
    	    			dijit.byId('printhistorizedBreadcrumbs').refresh();
    	    			
            		}
        				
        			errorHandler.show();
        		}
        		else
        		{
        			document.location.href = '/zert/printhistorized';
        		}        		
        		
                return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }
    
    this.openAttachedFiles = function(id){
    	dojo.toggleClass(id, "showFiles");
    	dojo.query(".showFiles").forEach(function(item){
    		if (item.id != id)
    			dojo.removeClass(item.id, "showFiles");
        });    	
    }
    
    this.changeCategory2 = function(level, parentCateory) {
    	dijit.byId('listWrapper').attr('href', '/job/show-user-jobs/'+level+'/'+parentCateory);
    }
    
    this.updateFilterValue = function(field, value){
    	
    	switch(field){
	    	case 'listingFilterFrom':
	    	case 'listingFilterTo':
	    		if(value != null)
	    			value = tools.JSDate2TS(value);
	    	break;
    	}
    	
        dojo.xhrPost({
	            url: "/job/update-session-value/",
	            postData: "field=" + field + "&value=" + value ,
	            load: function(response, ioArgs){
        			dijit.byId('jobListWrapper').refresh();
        			self.updateChart();
        			self.updateAddChart();
        			dijit.byId('listingAdditionalSignallightsWrapper').refresh();
        			errorHandler.show();
        			
	                return response;
	            },
	            error: function(response, ioArgs){
	            	errorHandler.show();
	                return response;
	            },
	            handleAs: "text"
	    });
    }
    
    this.deleteSignaturePdf = function(category_id){
    	dojo.byId("signaturePdfCategoryId").value = category_id;
    	dijit.byId('removeSignaturePdfDialog').show();    	
    }
    
    this.doDeleteSignaturePdf = function(category_id){
        dojo.xhrPost({
            url: "/job/delete-signature-pdf",
            postData: "category_id=" + category_id,
            load: function(response, ioArgs){
            	dijit.byId('jobListWrapper').refresh();
        		errorHandler.show();
        		return response;
            },
            error: function(response, ioArgs){
            	errorHandler.show();
                return response;
            },
            handleAs: "text"
        });
    }    
    
    var self = this;
}

var job = new Job();

dojo.addOnLoad(
  function(){
    job.init();
  }
);



