var JadePagesheet = {
	_auto: function()	{
		var subDir = window.location.pathname;
		if(subDir.substr(-1)=='/'){ subDir=subDir.substr(0,subDir.length-1); }
		else{ subDir=subDir.substr(0,subDir.lastIndexOf('/')); }
		subDir=subDir.substr(subDir.lastIndexOf('/')+1);
		if(subDir!='install' && subDir!="licence") {
			//var urlPath = window.location.href;
			//if(urlPath.substr(-1)!='/' && urlPath.indexOf('index.php')==-1 && typeof(USER)=="undefined") { Jade.Redirect('index.php'); }
			Jade.Pagesheet._header();
			Jade.Pagesheet._foot();
		}
		else{
			
		}
	},
	_header:function(){
		var header = document.getElementById('header');
		Jade.DOM.Add(header,'div',null,'logo');
		var logoText=Jade.DOM.Add(header,'div',null,'logo_texto');
		logoText.innerHTML = Jade.Lang.Get("employee_portal");
		var zoomIn=Jade.DOM.Add(header,'div',null,'zoom_in');
		zoomIn.onclick = function() { Jade.TextZoom.ZoomIn(); }
		var zoomOut=Jade.DOM.Add(header,'div',null,'zoom_out');
		zoomOut.onclick = function() { Jade.TextZoom.ZoomOut(); }
		Jade.DOM.Add(header,'div',null,'menu_top');
		var menu=Jade.DOM.Add(header,'div',null,'menu');
		menu.drop_menus = new Array;
		
		//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		if(typeof(USER)!="undefined")	{
			var gpUser=Jade.Objects.Container();
			gpUser.Position(580,-2);
			gpUser.Size(225,39);
			gpUser.SetStyle('user');
			gpUser.Add(header);
			
			var codeP=new Jade.Objects.Text(USER.login);
			codeP.Location(10,8, '100%', 22);
			codeP.Add(gpUser);

			var userP = new Jade.Objects.Text();
			userP.Position(10,23, '100%', 22);
			if(USER.root=='1'){ userP.SetText(Jade.Lang.Get('administrator')); }
			else{ userP.SetText(USER.employee+' - '+USER.name+' '+USER.surnames); }
			userP.Add(gpUser);
			
			var pass_change=$.Objects.Link($.lng('change_password'));
			pass_change.Location(815, 8, 160, 20);
			pass_change.SetHref('?user_password')
			pass_change.Add(header);
			
			Jade.Pagesheet._menu_add(menu,Jade.Lang.Get('resume'),'?resume');
			if(Jade.Utils.PrivilegeValue('empl_priv')!=0){
				if(Jade.Utils.Privilege('employee_data') || Jade.Utils.Privilege('employee_profile') || Jade.Utils.Privilege('employee_calendar') || Jade.Utils.Privilege('employee_duty_time') || Jade.Utils.Privilege('employee_hiring_dismissals') || Jade.Utils.Privilege('employee_notes')){
					Jade.Pagesheet._menu_add(menu,Jade.Lang.Get('employees'),'?employees');
				}
				if(Jade.Utils.Privilege('log_punches') || Jade.Utils.Privilege('log_access_punches') || Jade.Utils.Privilege('log_daily_log') || Jade.Utils.Privilege('log_weekly_accruals') || Jade.Utils.Privilege('log_monthly_accruals') ||	Jade.Utils.Privilege('log_payroll') || Jade.Utils.Privilege('log_weekly_payroll')){
					Jade.Pagesheet._menu_add(menu,Jade.Lang.Get('history'),'?history');
				}
			}
			if(Jade.Utils.Privilege('checks_maintenance')){ Jade.Pagesheet._menu_add(menu,Jade.Lang.Get('checks'),'?punches'); }
			if(Jade.Utils.PrivilegeValue('empl_priv')!=0){ Jade.Pagesheet._menu_add(menu,Jade.Lang.Get('petitions'),'?petitions'); }
			
			/*
			var mnu_petitions = Jade.Pagesheet._drop_menu_add(menu, Jade.Lang.Get('petitions'));
			Jade.Pagesheet._drop_sub_menu_add(mnu_petitions, Jade.Lang.Get('petitions_creation'),'petition_simple.php');
			Jade.Pagesheet._drop_sub_menu_add(mnu_petitions, Jade.Lang.Get('petitions_management'),'petitions.php');
			*/
			
			if(Jade.Utils.Privilege('config_priv')){
				var mnu_config = Jade.Pagesheet._drop_menu_add(menu, Jade.Lang.Get('configurations'));
				Jade.Pagesheet._drop_sub_menu_add(mnu_config, Jade.Lang.Get('profile_management'),'?profiles');
				Jade.Pagesheet._drop_sub_menu_add(mnu_config, Jade.Lang.Get('calendar_management'),'?calendars');
				Jade.Pagesheet._drop_sub_menu_add(mnu_config, Jade.Lang.Get('workday_management'),'?workdays');
			}
			if(Jade.Utils.Privilege('reports_checks') || Jade.Utils.Privilege('reports_timecost')){
				var mnu_report = Jade.Pagesheet._drop_menu_add(menu,Jade.Lang.Get('reports'));
				if(Jade.Utils.Privilege('reports_checks')){ Jade.Pagesheet._drop_sub_menu_add(mnu_report, Jade.Lang.Get('reports_checks'),'?report_checks'); }
				if(Jade.Utils.Privilege('reports_timecost')){ Jade.Pagesheet._drop_sub_menu_add(mnu_report, Jade.Lang.Get('reports_time_payroll'),'?report_payroll'); }
				if(Jade.Utils.Privilege('reports_timecost') && Jade.Lang.language=="hr"){ Jade.Pagesheet._drop_sub_menu_add(mnu_report, 'Izvještaj evidencije radnog vremena','?report_aam'); }
			}
			if(Jade.Utils.Privilege('report_formulas')){ Jade.Pagesheet._drop_sub_menu_add(mnu_report, Jade.Lang.Get('manage_report_formulas'),'?report_formulas'); }
			
			Jade.Pagesheet._menu_add(menu,Jade.Lang.Get('disconnect'),'?logout');
			
		}
		
		//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		var cmbLanguage = new Jade.Objects.ComboBox();
		cmbLanguage.Position(810,25);
		cmbLanguage.Size(160,17);
		cmbLanguage.SetStyle('language');
		
		cmbLanguage.Add(document.getElementById('header'));
		for(var i=0,iLen=Jade.Lang.languages.length; i<iLen; i++){
			var language=Jade.Lang.languages[i];
			cmbLanguage.AddItem(language.name,language.code,language.code);
			if(Jade.Lang.language==language.code){ cmbLanguage.SetSelectedIndex(i); }
		}

		cmbLanguage.onchange = function() {
			Jade.Lang.SetLanguage(this.selectedKey);
			Jade.Redirect(window.location);
		}
	},
	_menu_add:function(menu,text,href){
		var link=document.createElement('a');
		link.className='text-size';
		link.innerHTML=text;
		link.href=href;
		Jade.Events.NoSelect(link);
		menu.appendChild(link);
	},
	_drop_menu_add:function(menu,text){
		var link=document.createElement('a');
		link.className = 'text-size';
		menu.appendChild(link);
		
		var arrow = document.createElement('span');
		arrow.innerHTML='▼';
		link.appendChild(arrow);
		
		var texto = document.createTextNode(text);
		link.appendChild(texto);
		Jade.Events.NoSelect(link);
		
		var object = new Jade.Objects.ContextMenu();
		link.onmouseclick = link.onmouseover;
		link.onmouseover = function(ev) {
			object.onmouseover();
			for(var i in menu.drop_menus){
				menu.drop_menus[i].Hide();
			}
			
			var coords = Jade.Css.RealPosition(this);
			object.Pop(coords[0]+4,coords[1]+15);
		}
		link.onmouseout = function(){
			object.onmouseout();
		}
		
		menu.drop_menus.push(object);
		return object;
	},
	_drop_sub_menu_add:function(menu,text,link){
		var index = menu.length;
		menu.AddItem(text,index);
		menu.items[index].onclick=function(){ Jade.Redirect(link); }
	},
	_foot:function(){
		var foot = document.getElementById("footer");
		
		Jade.DOM.Add(foot,'div','start');
		var foot_body = Jade.DOM.Add(foot,'div',null,'foot');
		Jade.DOM.Add(foot,'div','end');
		
		var innerHTMLFoot = '';
		var obj;
		
		Jade.DOM.Add(foot_body,'div','ico up');
		obj = Jade.DOM.Add(foot_body,'div');
		obj = Jade.DOM.Add(obj,'a',null,null,Jade.Lang.Get("up"));
		obj.href = '#top';obj.target = '_self';
		Jade.DOM.Add(foot_body,'div','sep');
		
		Jade.DOM.Add(foot_body,'div','ico print');
		obj = Jade.DOM.Add(foot_body,'div');
		obj =Jade.DOM.Add(obj,'a',null,null,Jade.Lang.Get("print"));
		obj.href = 'javascript: window.print();'; obj.target = '_self';
		Jade.DOM.Add(foot_body,'div','sep');
		
		Jade.DOM.Add(foot_body,'div','ico favourites');
		obj = Jade.DOM.Add(foot_body,'div');
		Jade.Pagesheet.Create_Bookmark(obj,Jade.Lang.Get("add_favourites"));
		
		Jade.DOM.Add(foot_body,'div','sep');
		Jade.DOM.Add(foot_body,'div',null,null,'Copyright © 2011 · PSD Software · '+Jade.Lang.Get("copyright"));
	},
	Create_Bookmark: function(parent,text){
		var title=document.title;
		var url=window.location;
		var link=document.createElement('a');
		link.appendChild(document.createTextNode(text));
		link.href='';
		link.target='_self';
		if(typeof(window.sidebar)!="undefined")
		{
			link.onclick = function() { window.sidebar.addPanel(title, url,""); }
		}
		else if (window.external)
		{
			link.onclick = function() { window.external.AddFavorite( url, title); }
		}
		else if(window.opera && window.print)
		{
			link.title=title;
			link.href=url;
			link.rel='sidebar';
		}
		
		parent.appendChild(link);
	},
	FormatIncidence: function(text){
		if(text==0) { return '';	}
		else if(text<=9) { return '<span class="incidence system">['+text+']</span>';	}
		else { return '<span class="incidence user">['+text+']</span>';	}
	}
}
Jade.LoadLibrary('Pagesheet',JadePagesheet);