/*  
* Version: 2007-11-30  
* Author: Tomasz Ulanowski <tomasz@ulanowski.info>
* All rights reserved.
*/
var initialExpandedMenu;
var abortRestoration;
Element.addMethods('DIV', {
	initDynMenu: function(el) {
		var menuItems, subs, i, j;
		
		// save initially expanded  menu nodes
		initialExpandedMenu = $$('.expanded');
		
		menuItems = el.childElements();
		for(i=0; i < menuItems.length; i++) {
			// set event handlers

			menuItems[i].onmouseover = function () {
				// clear initially expanded node for the first time (only!) 
				var initialState = true;
				for(j=0; j<initialExpandedMenu.length; j++ ) {
					initialState &= initialExpandedMenu[j].hasClassName('expanded');
				}
				if ( initialState ) {
					for(j=0; j<initialExpandedMenu.length; j++ ) {
						initialExpandedMenu[j].removeClassName('expanded');
					}
				}
				// expand this node
				subs = this.childElements();
				for(j=0; j<subs.length; j++) { 
					if (subs[j].tagName=='DIV') {
						subs[j].addClassName('expanded');
						subs[j].previous('A').addClassName('expanded');
					}
				}
				// abort restoring to initial state
				abortRestoration = true;
			} // onmouseover
			
			menuItems[i].onmouseout = function () {
				// collapse this node
				subs = this.childElements();
				for(j=0; j<subs.length; j++) { 
					if (subs[j].tagName=='DIV') {
						subs[j].removeClassName('expanded');
						subs[j].previous('A').removeClassName('expanded');
					}
				}
				
				// is this the last collapsed node?
				if ( $$('.expanded').length == 0 ) {
					// schedule initially expanded nodes restoration (if nothing new happens)
					abortRestoration = false;
					setTimeout(function() {
							if ( !abortRestoration && $$('.expanded').length == 0 ) { 
								for(j=0; j<initialExpandedMenu.length; j++ ) {
									initialExpandedMenu[j].addClassName('expanded');
								}
							}
						}, 3000);
				}
			} // onmouseout
			
			// recursively init child nodes
			subs = menuItems[i].childElements();
			for(j=0; j<subs.length; j++) { 
				if (subs[j].tagName=='DIV') {
					subs[j].initDynMenu();
				}
			}
		}
	}
});

function initSelector(select, idPrefix) {
	select.onchange = function updateSelector () {
		select.childElements().each(function(item) {
			if(item.selected) 
				$(idPrefix + item.value).show(); 
			else 
				$(idPrefix + item.value).hide();
		}); 
	}
	select.onchange();
}

/* temporary copy title attribute to the text field value */
document.observe("dom:loaded", function() {
	$$('input[title]').each(function(item) {
		// copy from title to value
		if (item.value == '') {
			item.value = item.title;
			if (item.type == 'password') {
				item.type = 'text';
				item.revertToPassword = true;
			}
		} // if 
		// set focus and blur event handlers
		Event.observe(item, 'focus', function() {
			if (this.value == item.title) {
				this.value = '';
				if (this.revertToPassword) this.type = 'password';
			} 
			//new Effect.Highlight(this, {startcolor:'#eeeeee', endcolor:'#ffffff'})
		}); //Event.observe
		Event.observe(item, 'blur', function() {
			if (this.value == '') {
				this.value = item.title;
				if (this.revertToPassword) this.type = 'text';
			}
		}); //Event.observe
	});
});

/* dynamic tabs initialize */
var dyntabs = new Hash();
function xyz() {
	$$('a[rel]').each(function(item) {
		var tabDivId, tabId, tabsArray;
		if ( tabId = item.rel.match(/^dyntabs\[(.*)\]$/) ) if (tabId = tabId[1]) {
			if ( !(tabsArray = dyntabs.get(tabId)) ) tabsArray = dyntabs.set(tabId, new Array());
			tabsArray.push(item);

			if ( tabDivId = item.href.split('#') ) if (tabDivId = tabDivId[1]) {
				
				if ( item.hasClassName('selected') ) {
					if ( $(tabDivId) ) $(tabDivId).show();
				}
				else {
					if ( $(tabDivId) ) $(tabDivId).hide();
				}
	
				item.onclick = function () {
					dyntabs.get(tabId).each(function(sel) {
						var tab;
						if ( tab = sel.href.split('#') ) if (tab = tab[1]) {
							//alert(tab);
							if ( sel === item) {
								sel.addClassName('selected');
								if ( $(tab) ) $(tab).show();
							}
							else {
								sel.removeClassName('selected');
								if ( $(tab) ) $(tab).hide();
							}
						}
					});
					return false; 
				} // onclick

			
			} 
			
		} // if
	}); // each
}
document.observe("dom:loaded", xyz);

/* switching selected className */
Element.addMethods({
	setSelectedInGroup: function(el, group) {
		group.each(function (item) {
			if ( el === item ) {
				item.addClassName('selected');
			}
			else {
				item.removeClassName('selected');
			}
		});
	}
});

function ukryjZobaczWiecej(){
	lista = document.getElementsByTagName('a'); 
	ilosc = lista.length;
	ile=0;
	for(i=0;i<ilosc;i++){
		if( lista[i].rel=='lightbox[galeria]' )
			ile++;	
	}
	if(ile<=2)
		if(document.getElementById('zobaczWiecejId')!=null)
		{
			document.getElementById('zobaczWiecejId').style.display='none';
			document.getElementById('zobaczWiecejId').rel=' ';
		}

}


function ProlongSession() { 
	var Timeout = 1 * 60 * 1000;
	var oImage = new Image; oImage.src = "images/pixel.gif?" + Math.random(); 
	setTimeout("ProlongSession();", Timeout);
	
 }


function pop_dwa(plik,w,h){
			aw=800;
			ah=600;
			okno = null;
			if(window.screen){
				aw = screen.availWidth;
				ah = screen.availHeight;
			}
			dane="width="+w+",height="+h+",left="+(aw-w)/2+",top="+(ah-h)/2+",toolbar=no,location=no,directories=no,"
			+"status=no,menubar=no,scrollbars=yes,resizable=no";
			okno=window.open(plik,'pop',dane);
}
function openEditAvatar( url ){
	window.open(url, 'Edit AVATAR', "width=640,height=480,menubar=0,toolbar=0,location=0,status=0,scrollbars=0,directories=0,resizable=1,copyhistory=0,left=0,top=0");
	return false;
}
function openEditPassword( url ){
	window.open(url, 'Edycja hasła', "width=440,height=180,menubar=0,toolbar=0,location=0,status=0,scrollbars=0,directories=0,resizable=1,copyhistory=0,left=0,top=0");
	return false;
}
function searchSubmit() {
	var filed = document.getElementById('sbTools:sbToolsSearchCms:txtQuery');
	if (filed.value.length < 1) 
		return false
	return true;
}

function newsletterSubmit() {
	var filed = document.getElementById('sbTools:sbToolsNewsletterCms:txtQuery1');
	if (filed.value.length < 1) 
		return false
	return true;
}
function zmienMenu(idKanal){
		document.getElementById('menu_top_a_'+idKanal).className='expanded';
		document.getElementById('menu_top_div_'+idKanal).className+=' expanded'; 
}

