/* Copyright (c) 2011 Piotr Głodzik CRMSoftware piotr.glodzik@crmsoftware.pl
 * --------------------------------------------------
 * BIPSite, version 1.0.0
 * Data utworzenia pliku: 2011-04-11
 * Ostatnia modyfikacja:  2011-07-17
 */
(function(window){
	
	var BIPSite = function() {
		
	}
	
	BIPSite.prototype =  {
			ver : '1.0.0',				
			
			mInterval : null,
			
			// Pokazuje okno z rekordem historycznym
			showHistoricalRecord : function( id, SID ) {
				
				A.nW( { url : 'SiteHistoricalRecordPopup.php?id='+id,
				          name : 'archive_record',
				          height : '400px',
				          width : '600px',
				          left : '100px',
				          top  : '100px',
				          scrollbars : 'yes'
				        } );
			},
			
			// Zwija wszystkie moduły
			hideAllModules : function() {
				
				var allModules = document.getElementsByTagName('ul');
				
				for(var i=0;i<allModules.length;i++) {			
					
					if ( allModules[i].className == 'menu' || allModules[i].className == 'menu menuVisible') {						
						Arm(allModules[i]).style.height=0;
					}
				}
			},
			
			// Animacja - do przyszłego wykorzystania
			animateMenu : function( dModule, o, maxLen ) {
												
				var uList = dModule.getElementsByTagName('ul')[0];
								
				//var uHeight = parseInt(getComputedStyle( uList,null).getPropertyValue('height'));
				var uHeight = parseInt(uList.offsetHeight);
				
				// Żeby obliczyc wysokośc rozwiniętego ulam usże go gdzieś na początku wpakować w inne miejsce
				// Ale to będzie jakiś kompletny kosmos
				// Może jakoś policzyć wysokość wszystkich elementów?
								
				if ( uHeight<maxLen ) {
						uList.style.height=uHeight+1+'px';
						uHeight = parseInt(getComputedStyle( uList,null).getPropertyValue('height'));
				} else {

					clearInterval(o.t.mInterval);
				}
				
			},						
			
			// Rozwija/zwija moduły w menu
			toggleModule : function(e,o) {
				
				// Pobieram ul należące do modułu
				var uListCol = this.parentNode.getElementsByTagName('ul');
				
				if( uListCol.length > 0 ) {
					var uList = uListCol[0];
					var uHeight;
					if (uList.currentStyle) {
						uHeight = parseInt(uList.currentStyle['height']);						
					} else {
					 uHeight = parseInt(getComputedStyle( uList, null ).getPropertyValue('height'));
					}				    				
					
					// Rozwijam moduł
				   if ( uHeight == 0 ) {					   
					   // Zwijam wszystkie moduły
					   o.t.hideAllModules();
					   
					   // Rozwijam moduł kliknięty
					   uList.style.height = 'auto';					   					   					   					   					   										   
					   return;
					   					   					   					 
					
				   } else { // Zwijam moduł
					   
					   uList.style.height = 0;
				   }
				}
				
			},
			
			// Przypisuje do modułów w menu funkcje przełączające			
			assignToggleModule : function() {
				
				var leftModules = Arm('leftMenuContainer').getElementsByTagName('div');				
				for(var i=0;i<leftModules.length;i++) {
					
					if ( leftModules[i].className=='moduleHeader') {
						Arm(leftModules[i]).aE('click',this.toggleModule,'tmHndl',{t:this} );
						
					}
					
				}
				
				
				var rightModules = Arm('rightMenuContainer').getElementsByTagName('div');				
				for(var i=0;i<rightModules.length;i++) {
					
					if ( rightModules[i].className=='moduleHeader') {
						Arm(rightModules[i]).aE('click',this.toggleModule,'tmrHndl',{t:this} );
						
					}
					
				}
				
			},
			
			
			fontResize : function(e,o) {
			
				e.cancelBubble = true;
				var bd = document.getElementsByTagName('body')[0];
				if ( o.z==1)  {
					fS = 16;
				} else {
				
				if ( window.getComputedStyle ) {
					var fS = parseInt( getComputedStyle( bd,null).getPropertyValue('font-size') );
				} else {
						var fS = bd.currentStyle['fontSize'];
						if (fS == '12pt') {
							fS = 16;
						} else {
							fS = parseInt(fS);
						}
							
				}
												
				if ( this.id == 'fontUp' ) {
					fS = fS + 3;
				} else {
					fS = fS - 3;
				}		
				}

				bd.style.fontSize = fS + 'px';
				
			},
			
			// Przypisuje do przycisków funkcje resize tekstu
			assignFontResize : function() {
				Arm('fontUp').aE('click',this.fontResize,'fuHndl',{t:this,z:0} );
				Arm('fontDown').aE('click',this.fontResize,'fdHndl',{t:this,z:0} );
				Arm('fontZero').aE('click',this.fontResize,'fdHndl',{t:this,z:1} );
			},
			
			// Pobiera tabelki z listaim rekordow
			getRecordListTables : function() {
				
				var tabs = document.getElementsByTagName('table');
				var listTabs = new Array();
				var x = 0;
				for ( var i=0;i<tabs.length;i++) {
					if (tabs[i].className=='recordList' ) {
						listTabs[x] = tabs[i];
						x++;
					}
				}
				
				return listTabs;
			},
			
			// Pobiera tabelki z detalami rekordów
			getRecordDetailTables : function() {
				
				var tabs = document.getElementsByTagName('table');
				var listTabs = new Array();
				var x = 0;
				for ( var i=0;i<tabs.length;i++) {
					if (tabs[i].className=='recordDetail' ) {
						listTabs[x] = tabs[i];
						x++;
					}
				}
				
				return listTabs;
			},
						
			// Tworzy wersję do druku
			printHTML : function(e,o) {
								
				var listTabs = o.t.getRecordListTables();
				var detailTabs = o.t.getRecordDetailTables();
				
				var dPrint = '';
								
				if (listTabs.length>0) {
					
					for(var i=0;i<listTabs.length;i++){
					dPrint = dPrint + '<table class="recordList">' + listTabs[i].innerHTML + '</table>';	
					}
				}
				
				if (detailTabs.length>0) {
					
					for(var i=0;i<detailTabs.length;i++){
					dPrint = dPrint + '<table class="recordDetail">' + detailTabs[i].innerHTML + '</table>';	
					}
				}								
								
				// Dla IE musi byc pelny adres, bo inaczej sie wywala
				var nwUrl = document.getElementsByTagName( 'base' )[0].href + 'print.html'
				var nw = A.nW( { url : nwUrl,
								 name : 'print',
								 scrollbars : 'yes',
								 height : '700px',
								 width: '600x',
								 left : '50px',
								 top : '50px'} );
								
				var pC = '';
				
				pC = pC + dPrint;
								
				var dt = new Date();
				var mth = dt.getMonth() + 1;
				pC = pC + '<br /><br />Data wydruku: ' + dt.getFullYear() + '-' + mth + '-' + dt.getDate() + ', ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds(); 
				var pC = pC + '</div></body></html>';							
								
				var printOnLoad = function(e,o) {

					var printContainer = this.document.getElementById('printContentContainer');					
					printContainer.innerHTML = o.con;
					this.print();
				}

				Arm(nw).aE('load',printOnLoad,'fuHndl',{t:this,con:pC})
											
			},
		
			showPrintButton : function() {
				var pButton = Arm('printHTML');				
				pButton.style.display = 'inline';
				return pButton;
			},
			
			// Przypisuje do przycisku print funkcję drukowania
			assignPrint : function() {
			
				var listTabs = this.getRecordListTables();
				var detailTabs = this.getRecordDetailTables();
				
				if ( listTabs.length>0 || detailTabs.length>0) {			
					var pButton = this.showPrintButton();
					Arm(pButton).aE('click',this.printHTML,'pHHndl',{t:this});
				}
				
			},
			
			// Submituje formularz zamówień publicznych
			submitAuctionForm : function( year, month ) {
				
				var frm = Arm('auctionForm');
				var iYear = Arm('year');
				var iMonth = Arm('month');
				iYear.value = year;
				iMonth.value = month;
				
				frm.submit();
				
			},
			
			// Wraca do aktualnych przetargów
			returnToActual : function() {
				var frm = Arm('auctionForm');
				var iYear = Arm('year');
				var iMonth = Arm('month');
				iYear.value = '';
				iMonth.value = '';
				
				frm.submit();
			},
			
			toggleMenu : function(e,o) {
					
				var mOpen;
				var leftModules = Arm('leftMenuContainer').getElementsByTagName('div');
				
				for(var i=0;i<leftModules.length;i++) {
					
					// Pobieram ul należące do modułu
					var uListCol = leftModules[i].getElementsByTagName('ul');
									
					
					if( uListCol.length > 0 ) {						
						
						var uList = uListCol[0];
						var uHeight;
						if (uList.currentStyle) {
							uHeight = parseInt(uList.currentStyle['height']);						
						} else {
						 uHeight = parseInt(getComputedStyle( uList, null ).getPropertyValue('height'));
						}				    				
						
						
						// Rozwijam moduł
					   if ( uHeight == 0 ) {					   
						   
						   // Rozwijam moduł kliknięty
						   uList.style.height = 'auto';					   					   					   					   					   										   
						   						
					   } else { // Zwijam moduł
						   
						   uList.style.height = 0;
						   
					   }
					   
					}
					
				}
									
				var rightModules = Arm('rightMenuContainer').getElementsByTagName('div');				
				for(var i=0;i<rightModules.length;i++) {
					
					// Pobieram ul należące do modułu
					var uListCol = rightModules[i].getElementsByTagName('ul');
									
					
					if( uListCol.length > 0 ) {						
						
						var uList = uListCol[0];
						var uHeight;
						if (uList.currentStyle) {
							uHeight = parseInt(uList.currentStyle['height']);						
						} else {
						 uHeight = parseInt(getComputedStyle( uList, null ).getPropertyValue('height'));
						}				    				
						
						
						// Rozwijam moduł
					   if ( uHeight == 0 ) {					   
						   
						   // Rozwijam moduł kliknięty
						   uList.style.height = 'auto';					   					   					   					   					   										   
						   mOpen = 1;
						
					   } else { // Zwijam moduł
						   
						   uList.style.height = 0;
						   mOpen = 0;
					   }				
					
					}
				}

				// Ustawiam cookie
				document.cookie = 'mOpen=' + mOpen;
				
			},
			
			getCookie : function (c_name) {
			var i,x,y,ARRcookies=document.cookie.split(";");
			for (i=0;i<ARRcookies.length;i++)
			{
			  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			  x=x.replace(/^\s+|\s+$/g,"");
			  if (x==c_name)
			    {
			    return unescape(y);
			    }
			  }
			},
			
			assignToggleMenu : function(e) {
			
				Arm('entityHeader').aE( 'click', this.toggleMenu, 'tmaHNDL', {t:this});
				Arm('subjectHeader').aE( 'click', this.toggleMenu, 'tmbHNDL', {t:this});
				
				// Sprawdzam w cookie czy menu ma być rozwinięte
				// Jeżeli tak, to rozwijam
				var mOpen = this.getCookie('mOpen');
				
				if ( mOpen == 1 ) {
					this.toggleMenu(e,{t:this});
				}
			},
			
			
			linkToTychy : function(e,o) {
				
				if ( e.stopPropagation ) {
					e.stopPropagation()
				} else {
					window.event.cancelBubble = true;
				}
				window.location.href='http://umtychy.pl';
								
			},
			
			assignLinks : function() {
				
				Arm('headerLeft').aE( 'click', this.linkToTychy, 'tymaHNDL', {t:this});	
			}
			
								
			
	};
		
	this.BIPSite = new BIPSite;
	
	this.BIPSite.prototype = BIPSite.prototype;
	
}(window));

