

/**
 * klasa tworząca i zarządzająca panelem do edycji danych
 */
function EditPanel ()
{
	//wskaznik na rodzica panelu
	this.hParent = null;
	//unikalny identyfikator panleu
	this.id = null;
	//odległość w px od lewej krawędzi okna przeglądarki
	this.left = 10;
	//odległość w px od górnej krawędzi okna przeglądarki
	this.top = 10;
	//wysokość głównego okna dialogu
	this.width = 400;
	//wysokość głównego okna dialogu
	this.heigth = 300;
	//opis wyświetlany na belce tytulowej
	this.opis = null;
	//tabela stron TabCtrl
	this.strony = new Array();
	this.PageCount = null;
	//flaga określajaca czy wyszarzyć zawartosć strony podczas wyświetlania dialogu
	this.gray = false;

	//kolory wykorzystywane w dialogu
	this.bkColor = '#f0efe6';	
	this.darkBorderColor = '#e0e0e0';
	this.brightBorderColor = '#ffffff';
	this.borderWidth = '2';
	
	//główne okno panelu
	this.window = null;
	//panel grupujacy właściwyy panel usera oraz przyciski OK i Anuluj
	this.hPanelField = null;
	
	//akcja kontrolera wywoływana w momencie zatwierdzenia dialogu (formularza) przyciskiem OK
	this.formAction = null;
	//unikalny identyfikator formularza
	this.formID = null;
	this.form = null;
	
	this._3DFlag = true;
	

	//div wyszerzenia strony
	this.Shadow = null;

	//wskaźnik na właściwyy panel na którym można umieszczać dane
	this.hClientPanel = null;
	//rozmiary panelu klienta
	this.clientWidth;
	this.clientHeight;

	//flaga informująca czy wywołano już metodę init
	this.initFlag = false;
	
	this.BTN_OK = false;
	
	this.formCloseAction = false;
	this.editID = false;
	
	//zmienna określajaca poziom panelu w stosunku do innych Paneli. Czym wyższy poziom panelu tym wyżej w hierarhi z-roder znajduje się dany Panel	
	this.poziom = 0;
	
	this.saveParams = false;

	this.init = function( hParent, id, left, top, width, height, opis, gray )
	{
		this.initFlag = true;
		left == null ? left = getElemLeft(hParent) : false;
		top == null ? top = getElemTop(hParent) : false;

		//ukryj wszystkie przyciski klasy startEditButton
		var edity = document.getElementsByClassName('startEditButton');
		for (var i=0; i<edity.length; i++)
			edity[i].style.display = 'none';
			
		//jesli chcemy aby dialog zostać wyśrodkowany
		if (top == 'center')
			top = (document.documentElement||document.body).scrollTop + 
					parseInt(((document.documentElement||document.body).clientHeight/2) - (height/2));
		if (left == 'center')
			left = (document.documentElement||document.body).scrollLeft + 
					 parseInt(((document.documentElement||document.body).clientWidth/2) - (width/2));

		//określ maxymalne położenie w pionie aby zmieścił na ekranie
		maxtop = (document.documentElement||document.body).scrollTop + ((document.documentElement||document.body).clientHeight - height - 20);
		top > maxtop ? top = maxtop - 5 : false;
		top < 0 ? top = 0 : false;
		//określ maxymalne położenie w pionie aby zmieścił na ekranie
		maxleft = (document.documentElement||document.body).scrollLeft + ((document.documentElement||document.body).clientWidth - width - 5);
		left > maxleft ? left = maxleft - 5 : false;
		left < 0 ? left = 0 : false;

		//ustal nowe wymiary głównego okna uwzględniające border
		width -= 2*this.borderWidth;
		height -= 2*this.borderWidth;
		
		//stworz główne okno aplikacji
		this.window = document.createElement('div');
		this.window.id = 'paneltestowyid';
		//setOpacity(this.window,1);
		var pomPoz = 100 + (this.poziom*3) + 2;
		SetElemStyle(this.window,"z-index:"+pomPoz+"; position:absolute; background-color:" + this.bkColor + "; " + GetCSSSize(left, top, width, height) );
		this.Set3DBorder(this.window,true);
		document.body.appendChild(this.window);

		if (this._3DFlag == true)
		{			
			var dystans = 8; 
			this._3D = document.createElement('div');
			this._3D.id = 'testowypanelID_3D';
			var pomPoz = 100 +(this.poziom*3) + 1;
			SetElemStyle(this._3D,"z-index:"+pomPoz+"; position:absolute; background-color: black; " + GetCSSSize(left+dystans, top+dystans, width+4, height+4) );			
			document.body.appendChild(this._3D);
			setOpacity(this._3D,0.2);
		}
		
		//stwórz belką tytułową panelu
		var belka =  document.createElement('div');
		belka.className = 'menu_panel';
		belka.id= 'editbelka' + id;
		this.window.appendChild(belka);
		
		//stwórz krzyżyk zamykający panel
		var btn_Close = document.createElement('div');
		btn_Close.className = 'menu_panel_button close';
		belka.appendChild(btn_Close);
		
		//stwórz krzyżyk maksymalizacji panel
		var btn_Max = document.createElement('div');
		btn_Max.className = 'menu_panel_button maxim';
		belka.appendChild(btn_Max);
		
		//stwórz opis panelu
		var pom = document.createElement('div');
		pom.className = 'menu_panel_opis';
		pom.appendChild(document.createTextNode(opis));
		belka.appendChild(pom);
		
		//uwzględnij wysokość belki oraz wysokość panelu z przyciskami
		
		//PrintObject(head,1,1,20, true);
		//PrintObject(head,1,21,40, true);
		//PrintObject(head,1,41,60, true);
		//PrintObject(head,1,61,80, true);
		
		
		height -= getElemHeight(belka);
		//uwzględnij border panelu
		width -= (2*this.borderWidth);
		height -= (2*this.borderWidth);
		//stworz główny panel
		this.hPanelField =  document.createElement('div');
		SetElemStyle(this.hPanelField, GetCSSSize(0, 0, width, height));
		this.Set3DBorder(this.hPanelField,false);		
		this.window.appendChild(this.hPanelField);


		//stwórz przyciski ok i Anuluj
		var btn_ok = CreateButton(this.hPanelField, width - 150, height - 4, 70, 25, null, 'OK');
		this.BTN_OK = btn_ok; 
		var btn_Cancel = CreateButton(this.hPanelField, width - 73, height - 4, 70, 25, null, 'Anuluj');
		//zapamietaj we wszystkich przyciskach uchwyt do panelu (aby można było łatwo wyłączyć)		
		btn_Max.panel = btn_Close.panel = btn_Cancel.panel = btn_ok.panel = this;
		//funkcja zamykania dialogu
		btn_Cancel.onclick = btn_Close.onclick = function()
		{
			this.panel.destroy(true);			
		}
		//funkcja zatwierdzania dialogu
		btn_ok.onclick = function()
		{
			if (this.panel.formAction != null)
			{						
				var form = document.getElementById(this.panel.formID);
				form.submit();
			}
			
			this.panel.destroy(false);
	   };
	  
	  
	   //stworz separator pomiędzy belką a panelem klienckim
	   var pomoc = document.createElement('div');
	   SetElemStyle(pomoc,'height: 5px;');
	   this.hPanelField.appendChild(pomoc);
	   height -= 5;
	   
	   //oblicz rozmiar panelu klienckiego
	   this.clientWidth = width - 10;	//-10 ponieważ chcemy ustalić odstep między lewą i prawa granicą okna obejmującego po 5px
	   this.clientHeight = height -35; //-35 szer przycisków
	   
	   //stwórz panel klienta do zamieszczania wszystkich pól edycyjnych
	   this.hClientPanel = document.createElement('div');
	   SetElemStyle(this.hClientPanel,"margin:auto; width:"+this.clientWidth+"px; height:"+this.clientHeight+"px; overflow:hidden;");
	   this.hPanelField.appendChild(this.hClientPanel);
	   
	   //sprawdź czy ustawiono akcję kontrolera która powinna zostać wywołana w momencie zatwierdzenie dialogu
		if (this.formAction != null)			
			this.form = CreateForm(this.hClientPanel,this.formID,this.formAction,'POST',"multipart/form-data");		
	   
	  //alert(this.form.innerHTML);
	  
	   if (gray == true)
		{
			this.Shadow = document.createElement('DIV');
			this.Shadow.id = 'paneltestowyid' + 'Shadow';
			var fullHeight = (document.documentElement||document.body).scrollTop + (document.documentElement||document.body).clientHeight;			
			var poz = 100 + (this.poziom*3); //określ poziom cienia
			SetElemStyle(this.Shadow,"background-color:black; position: absolute; left:0px; top:0px; width:100%; height:"+fullHeight+"px; z-index:"+poz+";");
			setOpacity(this.Shadow,0.7);
			//shadow.appendChild(this.window);
			document.body.appendChild(this.Shadow);

			//zapamietaj wskaznik na obiekt wyszarzenia
			btn_Close.Shadow = btn_Cancel.Shadow = btn_ok.Shadow = this.Shadow;

		}
	  
	  
		
    	//var start = new Date().getTime();
	 	//while ((new Date().getTime() - start) < 1500){ /*nic nie rób*/ };
		
  		
		
	  
	   
		return;
	}
	
	this.Show = function()
	{
		 var elements = new Array();
	 	elements.push(document.getElementById('paneltestowyid'));
	   //elements.push(document.getElementById('paneltestowyidShadow')); 
	   elements.push(document.getElementById('testowypanelID_3D'));
	  
	  	var myFx = new Fx.Elements
	  	(
	  		elements, 
			{								
				duration: 1000,				
				onComplete: function(){  }
			}
		);
		
		myFx.start
		({
   		"0":{ "opacity" : [0,1] },   		
   		"1":{ "opacity" : [0,0.2] }
		});
		
	}
	
	this.setPoziom = function (poz)
	{
		this.poziom = poz;
	}
	
	this.setFormAction = function(action)
	{
	  
		if (this.initFlag == true)
		{
			alert('Dialog zostal już zainicjowany. Nie mogą dodac akcji Kontrolera do zainicjowanego dialogu.');
			this.destroy();
			return false;
		}
		var pom = explode('/',action);
		this.formID = 'formID_' + pom[pom.length-1];
		this.formAction = action;
		return true;
	}

	this.initTabCtrl = function()
	{		
		this.PageCount = 1;
		//stwórz panel na TabCtrlButton
		this.TabCtrlButtonPanel =  document.createElement('div');
		SetElemStyle(this.TabCtrlButtonPanel,"height:22px; display:block; " );
		
		if (this.formAction == null)
			this.hClientPanel.appendChild(this.TabCtrlButtonPanel);
		else
			this.form.appendChild(this.TabCtrlButtonPanel);
	}

	this.createTabCtrlPage = function(opis)
	{
		if (this.PageCount == null)
		{
			alert('Nie zainicjowano obiektu TabCtrl. Skontaktuj się z autorem oprogramowania.');
			return false;
		}
		
		var pageButton =  document.createElement('div');
		
		this.PageCount == 1 ? pageButton.className = 'TabCtrlPageButton_select' : pageButton.className = 'TabCtrlPageButton_normal';
		pageButton.name = 'TabCtrlButton';
		pageButton.index = this.PageCount;
		pageButton.parent = this.hClientPanel;
		pageButton.onclick = function()
		{
			var buttons = new Array();
			var pages = new Array();
			getAllChildrenByName(this.parent,'TabCtrlButton',buttons);
			getAllChildrenByName(this.parent,'TabCtrlPage',pages);
						
			for (var i=0; i<buttons.length; i++)
			{
				buttons[i].index == this.index ? buttons[i].className = 'TabCtrlPageButton_select' : buttons[i].className = 'TabCtrlPageButton_normal';
				buttons[i].index == this.index ? pages[i].style.display = 'block' : pages[i].style.display = 'none';
			}
		};



		this.TabCtrlButtonPanel.appendChild(pageButton);
		pageButton.innerHTML = opis;

		//stworz główny panel TabCtrl
		var page =  document.createElement('div');
		page.name = page.className = 'TabCtrlPage';
		//this.Set3DBorder(page,true);
		var pagePadding = 4;
		var pageWidth = this.clientWidth - 2*this.borderWidth - 2*pagePadding;
		var pageHeight = this.clientHeight - 22 - 2*this.borderWidth - 2*pagePadding;

		SetElemStyle(page, "padding:" + pagePadding + "px; width:" + pageWidth + "px; height:" + pageHeight + "px;");
		this.PageCount == 1 ? SetElemStyle(page,"display: block;") : SetElemStyle(page,"display: none;");

		page.pageWidth = pageWidth;
		page.pageHeight = pageHeight;
		

		if (this.formAction == null)
			this.hClientPanel.appendChild(page);
		else
			this.form.appendChild(page);
		//page.innerHTML = opis + ' - page';
		this.strony[this.PageCount] = page;
		this.PageCount++;
		return page;
	}
	
	this.getPage = function(ktora)
	{
		if (ktora > this.PageCount || ktora < 1)
		{
			alert('Podana strona nie jest zdefiniowana.');
			return false
		} 	
		
		return this.strony[ktora];	
	}
	

	this.Set3DBorder = function(object, wypukly)
	{
		var bright, dark;
		wypukly == false ? bright = this.brightBorderColor : bright = this.darkBorderColor;
		wypukly == false ? dark = this.darkBorderColor : dark = this.brightBorderColor;

		SetElemStyle(object, "border-bottom: solid " + bright + " " + this.borderWidth + "px;");
		SetElemStyle(object, "border-right: solid " + bright + " " + this.borderWidth + "px;");
		SetElemStyle(object, "border-top: solid " + dark + " " + this.borderWidth + "px;");
		SetElemStyle(object, "border-left: solid " + dark + " " + this.borderWidth + "px;");
	}
	
	this.setFormCloseAction = function(akcja, posID)
	{			
		this.editID = posID;		
		this.formCloseAction = akcja;		
	}
	
	//funkcja dodaje do panlu informację o tym które pozycje były na nim edytowane aby w razie pomyłek 
	//i naciśnięcia przycisku anuluj można było przywrócić oryginalne ustawienia
	this.addSaveOryginalPos = function(posID)
	{		
		if (this.savePos == undefined)
		{
			this.savePos = new Array(posID);
			var ile = 0;
			
			this.saveParams = new Array;
			var elem = document.getElementById('param' + posID);
			if (elem != undefined)
			{
				this.saveParams[0] = new Array;
				this.saveParams[0]['id'] = posID;
				this.saveParams[0]['value'] = elem.value;				
			}				
			return;
		}
		else
		{
			for (var i=0; i<this.savePos.length; i++)
			{
				if (this.savePos[i] == posID)
					return;
			}
		}
		this.savePos.push(posID);
				
		var elem = document.getElementById('param' + posID);
		if (elem != undefined)
		{
			this.saveParams[this.saveParams.length] = new Array;
			this.saveParams[this.saveParams.length-1]['id'] = posID;
			this.saveParams[this.saveParams.length-1]['value'] = elem.value;
			
		}						
	}

	//funkcja niszcząca dialog 
	this.destroy = function (refresh)
	{
		if (this.poziom == 0 && refresh)
		{
			var pomform = CreateForm(this.window,'refreshForm',this.formCloseAction,'POST',"multipart/form-data");
			if (this.editID != 0)
				CreateInput('hidden',pomform,null,'posID',this.editID,'');
			
			if (this.saveParams != false)			
				for (var i=0; i<this.saveParams.length; i++)
					CreateInput('hidden',pomform,null,'oldValue' + this.saveParams[i]['id'],this.saveParams[i]['value'],'');			
			
			pomform.submit();
		}
		
		if (this.window != null)
			document.body.removeChild(this.window);			
		if (this._3D != null)
			document.body.removeChild(this._3D);			
		if (this.Shadow != undefined && this.Shadow != null)
			document.body.removeChild(this.Shadow);
		//pokaż wszystkie przyciski klasy startEditButton
		var edity = document.getElementsByClassName('startEditButton');
		for (var i=0; i<edity.length; i++)
		   edity[i].style.display = 'block';
		   
		this.initFlag = false;
	}

}

