/*
* funkcja usuwa wszystkie wlasne menu kontekstowe o id zaczynajacym sie od cm_ z okna przegladarki
*/
function removeScrollMenu(){
	var d=document.getElementsByTagName('div');
	for(var i=0; i < d.length; i++){
		if(d[i].getAttribute('id') != null){
			var test=d[i].getAttribute('id').search('sr_');
			if(test == 0){
				$(d[i]).style.display='none';
			}
		}
	}
}

/*
* funkcja usuwa wszystkie wlasne menu kontekstowe o id zaczynajacym sie od cm_ z okna przegladarki
*/
function closeScrollMenu(e,id){
	var obj=$(id);
	if(!e){
		if( window.event ) {
     		e = window.event;
     	}
	}
	var pos=new MousePos(e);
	pos.init(e);
	var y=pos.getY().replace("px","");
	var x=pos.getX().replace("px","");
	if(!IsInside(obj, x, y)){
		removeScrollMenu();
	}
}

/*
* funkcja pokazuje menu kontekstowe dla okna
*/
function showScrollMenu(id_btn){
	var parent=$('scrm_'+id_btn);
	var t=$('sr_'+id_btn);
	if(t){
		if(parent){
			t.style.display='block';
			t.style.position='relative';
			if(parent.style.width==""){
				var w=GetWidth(parent);
				var buttons=t.getElementsByTagName("input");
				for(i=0; i<buttons.length; i++){
					buttons[i].style.width=w+'px';
				}
				parent.style.width=w+'px';
			}
		}
	}
}

/*
* funkcja usuwa wszystkie wlasne menu kontekstowe o id zaczynajacym sie od cm_ z okna przegladarki
*/
function removeContectsMenu(){
	var d=document.getElementsByTagName('div');
	for(var i=0; i < d.length; i++){
		if(d[i].getAttribute('id') != null){
			var test=d[i].getAttribute('id').search('cm_');
			if(test == 0){
				$(d[i]).style.display='none';
			}
		}
	}
}

/*
* funkcja pokazuje menu kontekstowe dla okna
*/
function showContectsMenu(id_window){
	var parent=$('p_'+id_window);
	var t=$('cm_'+id_window);
	if(t){
		if(parent){
			t.style.display='block';
			t.style.position='absolute';
			var menuSize=GetSize(t);
			var wndSize=GetSize(parent);
			t.style.right=5+'px';
			var b=wndSize.height - 22;
			if(b<0)b=0;
			t.style.bottom=b+'px';
			t.style.top=20+'px';
		}
	}
}

/*
* funkcja pokazuje podmenu dla menu
*/
function showSubMenu(id){
	var smenu=$(id);
	smenu.style.display='block';
	mint.fx.Fade(smenu, 95, 10, 500);
}

/*
* funkcja chowa podmenu dla menu
*/
function hideSubMenu(id){
	var smenu=$(id);
	OnDone=function(){
		smenu.style.display='none';
	}
	mint.fx.Fade(smenu, 0, 10, 500, null, OnDone);
}


/*
* funkcja pokazuje okienko z opisem dla elementu
*/
function showToolTip(ev, button){
	if(button){
		if(button.id==""){
			var bname=button.name;
		}
		else{
			var bname=button.id;
		}
		//sprawdzenie czy jest juz w tablicy obiektów. jak nie to dodanie
		if(ttps.hasByName(bname)){
			if(!ttps.getStateByName(bname)){
				return false;
			}
		}
		else{
			ttps.addTtip(bname,true);
		}
		moveElementToBody('tt_'+bname);
		var t=document.getElementById('tt_'+bname);
		if(t){
			if(!ev){
				if( window.event ) {
     				 ev = window.event;
     			}
			}
			if(ev){
				var c=new MousePos(ev);
				c.init(ev);
				var y=c.getY().replace("px","");
				var x=c.getX().replace("px","");
				var wndSize=windowSize();
				var wx=Math.round(x)+Math.round(250);
				var wy=Math.round(y)+Math.round(100);
				if(wx > wndSize[0]){
					var px=Math.round(wx)-Math.round(wndSize[0])+1;
					x=Math.round(x)-px;
				}
				if(wy > wndSize[1]){
					var py=Math.round(wy)-Math.round(wndSize[1])+1;
					y=Math.round(y)-py;
				}
				t.style.display='block';
				t.style.top=0+'px';
				t.style.left=0+'px';
				t.style.position='absolute';
				t.style.zIndex='150';
				t.style.top=Math.round(y)+"px";
				t.style.left=Math.round(x)+"px";
				mint.fx.Fade('tt_'+bname,80,10,100);
				button.onmousemove=moveToolTip;
			}
		}
	}
	return true;
}

/*
* funkcja ukrywa tooltip na 4 sekundy dla zdarzenia klikniecia na obiekcie do ktorego on nalezy 
*/
function pauseShowTooltip(button){
	if(button.id==""){
		var bname=button.name;
	}
	else{
		var bname=button.id;
	}
	if(!ttps.hasByName(bname)){
		ttps.addTtip(bname,false);
	}
	else{
		ttps.changeStateByName(bname,false);
	}
	hideToolTip(button);
	tout=window.setTimeout("unlockTTip('"+bname+"');",4000);
}

function unlockTTip(name){
	ttps.changeStateByName(name,true);
	window.clearTimeout(tout);
}

var tout=null;

/*
* funkcja przemieszcza okienko z opisem za kursorem myszy
*/
function moveToolTip(ev){
	if(!ev){
		if( window.event ) {
     		ev = window.event;
     	}
	}
	if(ev){
		var c=new MousePos(ev);
		c.init(ev);
		if(this.id==""){
			var bname=this.name;
		}
		else{
			var bname=this.id;
		}
		if(!ttps.getStateByName(bname)){
			return false;
		}
		var t=document.getElementById('tt_'+bname);
		var y=c.getY().replace("px","");
		var x=c.getX().replace("px","");
		var wndSize=windowSize();
		var wx=Math.round(x)+Math.round(250);
		var wy=Math.round(y)+Math.round(100);
		if(wx > wndSize[0]){
			var px=Math.round(wx)-Math.round(wndSize[0])+1;
			x=Math.round(x)-px;
		}
		if(wy > wndSize[1]){
			var py=Math.round(wy)-Math.round(wndSize[1])+1;
			y=Math.round(y)-py;
		}
		t.style.top=Math.round(y)+"px";
		t.style.left=Math.round(x)+"px";
	}
	return true;
}

/*
* funkcja chowa okienko z opisem dla elementu
*/
function hideToolTip(button){
	if(button){
		if(button.id==""){
			var bname=button.name;
		}
		else{
			var bname=button.id;
		}
		var t=document.getElementById('tt_'+bname);
		if(t){
			mint.fx.Fade('tt_'+bname,0,2,10);
			t.style.display='none';
		}
	}
}

/*
* klasa zarzadzajaca tooltipami
*/
function ToolTips(){
	//tablica zawierajaca obiekty tooltipow
	this.ttips=Array();
	
	//metoda dodajaca nowy tooltip do tablicy tooltipow
	this.addTtip=function(name,state){
		if(this.hasByName(name)){
			this.changeStateByName(name,state);
		}
		else{
			this.ttips.push(new TTip(name,state));
		}
	}
	
	//metoda zwraca true jezeli w tablicy istnieje obiekt o podanej nazwie
	this.hasByName=function(name){
		for(var i=0; i<this.ttips.length; i++){
			if(this.ttips[i].name==name){
				return true;
			}
		}
		return false;
	}
	
	//metoda pobiera stan tooltipa i zwraca go w postaci boolean
	this.getStateByName=function(name){
		for(var i=0; i<this.ttips.length; i++){
			if(this.ttips[i].name==name){
				return this.ttips[i].getState();
			}
		}
		return false;
	}
	
	//metoda umozliwia zmiane statusu tooltipa (aktywny, nieaktywny)
	this.changeStateByName=function(name,state){
		for(var i=0; i<this.ttips.length; i++){
			if(this.ttips[i].name==name){
				this.ttips[i].changeState(state);
			}
		}
	}
	
	this.removeAll=function(parent){
		//wyszukanie wszystkich nazw ttipow umieszczonych wewnatrz obiektu parent
		//jezeli parent=null to parent =body
		
		//usuniecie obiektow z tablicy zawartych w parent
	}
}

/*
* klasa opisuje pojedynczy tooltip
*/
function TTip(name,state){
	this.name=name;
	this.state=state;
	
	this.changeState=function(state){
		this.state=state;
	}
	
	this.getState=function(){
		return this.state;
	}
}

/*
* klasa zarzadzajaca okienkami (odswierzanie cykliczne zawartosci, przesuwanie, zamykanie, minimalizacja)
*/
function Windows(){
	//tablica zawierajaca obiekty definiujace dane okienka
	this.objects=Array();
	
	//metoda inicjalizujaca element DIV o nazwie name jako okienko
	this.addWindow=function(name){
		if(!this.hasByName(name)){
		 	if($('p_'+name)){
		 		var wnd=new virtualWindow(name);
		 		wnd.init();
		 		this.objects.push(wnd);
			}
		}
		else{
			var wnd=this.getWindow(name);
		}
		return wnd;
	}
	
	//pusty obiekt okna - uzywany dla parametru name=mainWND
	this.initNoInstanceWindow=function(){
		var wnd=new virtualWindow('mainWND');
		this.objects.push(wnd);
	}
	
	//metoda zwraca obiekt klasy virtualWindow, w innym przypadku zwraca null
	this.getWindow=function(name){
		for(var i=0; i<this.objects.length; i++){
			if(this.objects[i].name==name){
				return this.objects[i];
			}
		}
		return null;
	}
	
	//metoda zwraca true jezeli w tablicy istnieje obiekt o podanej nazwie
	this.hasByName=function(name){
		for(var i=0; i<this.objects.length; i++){
			if(this.objects[i].name==name){
				return true;
			}
		}
		return false;
	}
	
	//metoda czysci wszystkie dane dotyczace usuwanego okienka 
	//(zamkniecie interwalow, odrejestrowanie elementu DIV jako przesuwalnego, usuniecie obiektu z tablicy)
	this.removeWhenClose=function(name){
		for(var i=0; i<this.objects.length; i++){
			if(this.objects[i].name==name){
			    this.objects[i].remove();
			    delete this.objects[i];
			    this.objects.splice(i,1);
				break;
			}
		}
	}
}

/*
* klasa definiujaca dane okienko div
*/
function virtualWindow(name){

	// pole zawierajace nazwe elementu
	this.name=name;
	
	// pole zawierajace identyfikator interwalu
	this.IntervalHandler=null;
	
	// pole zawierajace link do odswiezania zawartosci okienka
	this.refreshLink=null;
	
	// pole zawierajace czas interwalu odswiezajacego zawartosc okienka [ms]
	this.refreshInterval=null;
	
	// pole zawierajace obiekt DragObject umozliwiajacy poruszanie obiektu w oknie
	this.MoveHandler=null;
	
	// pole zawierajace status elementu (zminimalizowane = 0, normalne = 1)
	this.WNDStat=1;
	
	// pole zawierajace wysokosc okienka
	this.heightW=0;
	
	// pole zawierajace szerokosc okienka
	this.widthW=0;
	
	// pole zawierajace wysokosc zawartosci okienka
	this.heightC=0;
	
	// pole zawierajace szerokosc zawartosci okienka
	this.widthC=0;
	
	// pole okresla roznice w wysokosci na aktywator rozszezania okienka
	this.offsetHeight=0;
	
	// pole stwierdzajace o skalowaniu elementu
	this.isResizable=false;
	
	// pole zawierajace informacje o aktualizacji zawartosci - blokuje mozliwosc jednoczesnych zapytan
	this.isRequesting=false;
	
	//pole zawiera informacje o odswiezaniu automatycznym zawartosci okienka (0 = wylaczone, 1 = wlaczone)
	this.isRefreshing=0;
	
	// pole przechowuje uchwyt do interwalu ponawiajacego zapytanie w przypadku jednoczesnego wysylania 2 zapytan
	this.intervalSend=null;
	
	this.showContMenu=false;
	
	//pole zawiera obiekt zarzadzajacy tooltipami danego okna
	//this.tooltips=new ToolTips();
	
	this.evalSize=function(){
		//pobranie aktualnych wymiarow okna
		var sizew=GetSize('p_'+this.name);
		//ustawienie wymiarow okienka wg typu przegladarki i zapisanie danych widthW i heightW
		//ustawienie wymiarow panelu
		var sizebut=GetSize('wndbut_'+this.name);
		//pobranie danych o przegladarce
		var nav=navigator.userAgent;
		if(nav.indexOf("MSIE")!=-1){ //dla ie
			SetSize('pan_'+this.name,sizew.width-sizebut.width,20);
			//ustawienie wymiarow zawartosci
			SetSize(this.name,sizew.width-2,sizew.height-this.offsetHeight-22);
			//ustawienie wymiarow warstwy ladowania - te same co zawartosci
			SetSize('load_'+this.name,sizew.width-2,sizew.height-this.offsetHeight-22);
			//zapisanie wymiarow
			this.widthW=sizew.width;
			this.heightW=sizew.height;	
			this.widthC=sizew.width-2;
			this.heightC=sizew.height-this.offsetHeight-22;	
		}
		else{ //dla geko i opery (reszta swiata)
			SetSize('pan_'+this.name,sizew.width-sizebut.width,20);
			//ustawienie wymiarow zawartosci
			SetSize(this.name,sizew.width-4,sizew.height-this.offsetHeight-24);
			//ustawienie wymiarow warstwy ladowania - te same co zawartosci
			SetSize('load_'+this.name,sizew.width-4,sizew.height-this.offsetHeight-24);
			//zapisanie wymiarow
			this.widthW=sizew.width;
			this.heightW=sizew.height;	
			this.widthC=sizew.width-4;
			this.heightC=sizew.height-this.offsetHeight-24;	
		}
	}
	
	// metoda inicjalizujaca element
	this.init=function(){
		this.evalSize();
	}
	
	this.setOffsetHeight=function(offset){
		this.offsetHeight=offset;
	}
	
	this.setMove=function(){
		if(this.MoveHandler==null){
			this.MoveHandler=mint.gui.RegisterDragObject('p_'+this.name);
			this.MoveHandler.lockX=true;
			this.MoveHandler.lockY=true;
			this.MoveHandler.SetBBox("panelWnd");
			if($('pan_'+this.name)){
				this.MoveHandler.AddGrip('pan_'+this.name);
			}
			this.MoveHandler.OnDrag=function(){ //w trakcie przeciagania

			}
			this.MoveHandler.OnDragStart=function(){ //w momencie rozpoczecia
			
			}
			this.MoveHandler.OnDragStop=function(){ //w momencie upuszczenia
			
			}
		}
	}
	
	this.setRefresh=function(link,time){
		this.refreshLink=link;
		this.refreshInterval=time;
		this.isRefreshing=1;
		if(this.IntervalHandler){
			window.clearInterval(this.IntervalHandler);
		}
		this.IntervalHandler=window.setInterval('SendRequest("'+link+'","'+this.name+'",null);',time);
	}
	
	this.refreshOn=function(){
		if(this.IntervalHandler){
			window.clearInterval(this.IntervalHandler);
		}
		this.IntervalHandler=window.setInterval('SendRequest("'+this.refreshLink+'","'+this.name+'",null);',this.refreshInterval);
		this.isRefreshing=1;
		$('refbtn_'+this.name).style.backgroundImage='url(img/refon.jpg)';
	}
	
	this.refreshOff=function(){
		if(this.IntervalHandler){
			window.clearInterval(this.IntervalHandler);
		}
		this.isRefreshing=0;
		$('refbtn_'+this.name).style.backgroundImage='url(img/refoff.jpg)';
	}
	
	this.refreshChange=function(){
		if(this.isRefreshing==0){
			this.refreshOn();
		}
		else{
			this.refreshOff();
		}
	}
	
	// metoda ukazujaca element w oknie
	this.pokaz=function(){
 		$(this.name).style.display = 'block';
 		$(this.name).style.visibility = 'visible';
	}

	// metoda ukrywajaca element w oknie
	this.ukryj=function(){
 		$(this.name).style.display = 'none';
 		$(this.name).style.visibility = 'hidden';
	}
	
	
	// metoda przywracajaca normalna wysokosc elementu
	this.max=function(){
		var el=this.name;
		var wndY=GetY('p_'+el);
		var maxPoint=GetY('panelWnd')+GetHeight('panelWnd');
		if((wndY+this.heightW)>=maxPoint){
			mint.fx.Move('p_'+el, null, (maxPoint-this.heightW-3), 10, 500);
		}
		mint.fx.Size('p_'+this.name, null, this.heightW, 10, 500);
		var s=mint.fx.Size(this.name, null, this.heightC, 10, 500);
		s.OnDone=function(){
			$(el).style.display = 'block';
 			$(el).style.visibility = 'visible';
			mint.fx.Fade(el, 100, 10, 500);
	  		$('minbtn_'+el).style.backgroundImage='url(img/minimize_on.jpg)';
	  		$('load_'+el).style.visibility='visible';
	  		if($('mbtn_'+el)){
	  			$('mbtn_'+el).style.visibility='visible';
	  		}
	  		if($('resbtn_'+el)){
	  			$('resbtn_'+el).style.display='block';
	  			$('resbtn_'+el).style.visibility='visible';
	  		}
		}
		this.WNDStat=1;
	}
	
	// metoda minimalizujaca element
	this.min=function(){
		var el=this.name;
		var fad=function(){
			mint.fx.Size('p_'+el, null, 20, 10, 500);
			var s=mint.fx.Size(el, null , 0, 10, 500);
			s.OnDone=function(){
				$(el).style.display = 'none';
	 			$(el).style.visibility = 'hidden';
	  			$('minbtn_'+el).style.backgroundImage='url(img/minimize_off.jpg)';
	  			$('load_'+el).style.visibility='hidden';
	  			if($('resbtn_'+el)){
	  				$('resbtn_'+el).style.display='none';
	  				$('resbtn_'+el).style.visibility='hidden';
	  			}
			}
		}
		if($('mbtn_'+el)){
	  		$('mbtn_'+el).style.visibility='hidden';
	  		removeContectsMenu();
	  	}
		mint.fx.Fade(el, 0, 10, 500, null, fad);
		this.WNDStat=0;
	}	
	
	this.minmax=function(){
		if(this.WNDStat==0){
			this.max();
		}
		else{
			this.min();
		}
	}
	
	//obliczanie maksymalnej wielkosci dla okienka
	this.setMaxSize=function(){
		var wndSize=GetSize('panelWnd');
		var wndPos=GetPos('panelWnd');
		var objPos=GetPos('p_'+this.name);
		this.MoveHandler.maxWidth=parseInt(wndSize.width-(objPos.x-wndPos.x)-6); //dla geko
		this.MoveHandler.maxHeight=parseInt(wndSize.height-(objPos.y-wndPos.y)-4); //dla geko
	}
	
	this.resizable=function(){
		this.setMove();
		if(this.MoveHandler){
			this.MoveHandler.minWidth=parseInt(this.widthW);
			this.MoveHandler.minHeight=parseInt(this.heightW);
			this.setMaxSize();
			this.MoveHandler.lockWidth=true;
			this.MoveHandler.lockHeight=true;
			this.MoveHandler.AddGrip('resbtn_'+this.name);
			this.MoveHandler.OnResize=function(obj){
				// rozszezanie wszystkich pozostalych elementow okienka i zapisanie nowych wartosci do widthW i heightW
				if((obj)&&(obj.id)){
					var name=obj.id.replace('p_','')
					windowsObj.getWindow(name).evalSize();
				}
			}
			this.isResizable=true;
		}
		else this.isResizable=false;
	}
	
	this.resizeOn=function(){
		if(!this.isResizable){
			this.resizable();
		}
		if(this.isResizable){
			this.setMaxSize();
			this.MoveHandler.resize=true;
			this.MoveHandler.lockWidth=false;
			this.MoveHandler.lockHeight=false;
		}
	}
	
	this.resizeOff=function(){
		if(!this.isResizable){
			this.resizable();
		}
		if(this.isResizable){
			this.MoveHandler.resize=false;
			this.MoveHandler.lockWidth=true;
			this.MoveHandler.lockHeight=true;
		}
	}
	
	this.moveOn=function(){
		if(this.MoveHandler){
			this.resizeOff();
			this.MoveHandler.lockX=false;
			this.MoveHandler.lockY=false;
			this.MoveHandler.SetBBox('panelWnd');
		}
	}
	
	this.moveOff=function(){
		if(this.MoveHandler){
			this.MoveHandler.lockX=true;
			this.MoveHandler.lockY=true;
		}
	}

	this.loadOn=function(){
		if($('load_'+this.name)){
			var obj=$('load_'+this.name);
			mint.fx.Fade(obj, 80, 10, 200, null, function(obj){ if(obj){ obj.style.display = 'block'; obj.style.zIndex = 150;}});
		}
	}
	
	this.loadOff=function(){
		if($('load_'+this.name)){
			var obj=$('load_'+this.name);
			mint.fx.Fade(obj, 0, 10, 200, null, function(obj){ if(obj){ obj.style.display = 'none'; obj.style.zIndex = 1;}});
		}
	}
	
	this.updateContentOn=function(){
		//loading on
		if(!this.isRequesting){
			this.loadOn();
		}
		this.isRequesting=true;		
	}
	
	this.updateContentOff=function(){
		//loading off
		if(this.isRequesting){
			this.loadOff();
		}
		this.isRequesting=false;
	}
	
	this.remove=function(){
		if($(this.name))mint.fx.Stop(this.name);
	    if($('p_'+this.name))mint.fx.Stop('p_'+this.name);
	    if($('load_'+this.name))mint.fx.Stop('load_'+this.name);
		if(this.MoveHandler){
			mint.gui.UnregisterDragObject('p_'+this.name);
		}
		if(this.IntervalHandler){
			window.clearInterval(this.IntervalHandler);
		}
		var parentEl=$('p_'+this.name).parentNode;
		parentEl.removeChild($('p_'+this.name));
	}
	
	this.showAndResizeMenu=function(){
		if(!this.showContMenu){
			showContectsMenu(this.name);
			var obj=$('cm_'+this.name);
			var w=GetWidth(obj);
			var nav=navigator.userAgent;
			if(nav.indexOf("MSIE")!=-1){
				w=w-50;
			}
			var buttons=obj.getElementsByTagName("input");
			for(i=0; i<buttons.length; i++){
				buttons[i].style.width=w+'px';
			}
			$('cm_'+this.name).style.display='none';
			this.showContMenu=true;
		}
	}
}

/*
* utworzenie pustego obiektu klasy zarzadzajacej okienkami
*/
var windowsObj=new Windows();
windowsObj.initNoInstanceWindow();
var ttps=new ToolTips();