function e(id) {
	return document.getElementById(id);
}
function set_cookie(name,value) {
	t = new Date();
	t.setTime(t.getTime()+100*60*60*24*7);
	document.cookie = name+"="+value+";expires="+t.toGMTString()+" path=/";
}
function get_cookie(name) {
	var nameEQ = name + "=";
	var all_cookies = document.cookie.split(';');
	for(var i=0;i<all_cookies.length;i++) {
		var c = all_cookies[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

var LAY_COUNT = 0;
var TAB_COUNT = new Array(0, 0);
var hidden_floats = new Array();
var float_order = new Array();

var OFERTA_COUNT = 0;
var OFERTA_TIMEOUT = 5000;

var TAB_FIELDS = new Array(new Array('mm_tab', 'mm_layer'));
var mini_tab1_active = 0;
var mini_tab2_active = 0;

//window.onload = make_interface;

function make_interface() {

	layers_float = new Array();
	layer_moves = new Array();
	layers_oferta = new Array();
	layers = document.getElementsByTagName('div');
	for(i=0;i<layers.length;i++) {
		for(c=0;c<TAB_FIELDS.length;c++) {
			if(layers[i].className == TAB_FIELDS[c][1]) {
				TAB_COUNT[c]++;
			}
		}
		if(layers[i].className == 'sm_layer') {
			layers_float[LAY_COUNT] = layers[i];
			layer_moves[LAY_COUNT++] = 0;
			hidden_floats[LAY_COUNT] = false;
			float_order[LAY_COUNT] = LAY_COUNT;
			activate_float_links(layers[i]);
		}
		else if(layers[i].className == 'oferta_content_div') {
			OFERTA_COUNT++;
		}
		else if(layers[i].className == 'oferta_button') {
			
		}
	}
	
	for(c=0;c<TAB_FIELDS.length;c++) {
		name = TAB_FIELDS[c][0];
		e(name+'1').onclick = function() { return false; };
		for(i=2;i<=TAB_COUNT[c];i++) {
			eval("e(name+i).onclick = function() { change_tab("+c+", this) };");
		}
		old_tab = get_cookie('oldtab'+c);
		if(old_tab != null) {
			tab(c, old_tab);
		}
	}

	hidden_floats_c = get_cookie('hiddenf');
	if(hidden_floats_c != null && hidden_floats_c != '') {
		fs = hidden_floats_c.split('|');
		for(i=0;i<fs.length;i++) {
			name = 'sm_layer'+fs[i];
			float_hide(e(name));
		}
	}

	float_order_c = get_cookie('orderf');
	if(float_order_c != null) {
		fs = float_order_c.split('|');
		for(z=0;z<fs.length;z++) {
			num1 = fs[z];
			num2 = float_order[z+1];
			if(num1 != num2) {
				indx = -1;
				for(j=z+2;j<=LAY_COUNT;j++) {
					if(float_order[j] == num1) {
						indx = j;
						break;
					}
				}
				for(j=indx;j>z+1;j--) {
					float_up(e('sm_layer'+num1));
				}
			}
		}
	}
	
	box_oferta();
	
}
function tab(module, tabnum) {
	tabname = TAB_FIELDS[module][0];
	layername = TAB_FIELDS[module][1];
	for(i=1;i<=TAB_COUNT[module];i++) {
		e(layername+i).style.display = 'none';
		obj = e(tabname+i);
		eval("obj.onclick = function() { change_tab("+module+", this) };");
		obj.className = 'tab_active';		
	}
	obj = e(tabname+tabnum);
	// ######################## LINK DO ZAKLADKI ############################
	eval("obj.onclick = function() { window.location.href = 'http://"+obj.innerHTML.toLowerCase()+".lca.pl'; return false; };");
	obj.className = 'tab_inactive';
	e(layername+tabnum).style.display = 'block';
}
function change_tab(module, obj) {
	obj = obj.id;
	tabnum = obj.substr(obj.length-1, 1);
	tab(module, tabnum);
	set_cookie('oldtab'+module, tabnum);
	return false;
}
function activate_float_links(layer) {
	divs = layer.childNodes;
	for(x=0;x<divs.length;x++) {
		if(divs[x].className == 'sm_layer_bottom') {
			divs = divs[x].childNodes;
			break;
		}
	}
	for(x=0;x<divs.length;x++) {
		if(divs[x].className == 'sm_layer_controls') {
			links = divs[x].getElementsByTagName('span');
			for(y=0;y<links.length;y++) {
				if(links[y].className == 'sm_layer_link_down') {
					links[y].onclick = float_down_handler;
				}
				else if(links[y].className == 'sm_layer_link_up') {
					links[y].onclick = float_up_handler;
				}
				else if(links[y].className == 'sm_layer_link_hide') {
					links[y].onclick = float_hide_handler;
				}
			}
			break;
		}
	}
}

function float_is_hidden(box) {
	divs = box.childNodes;
	content = null;
	for(x=0;x<divs.length;x++) {
		if(divs[x].className == 'sm_layer_content') {
			content = divs[x];
			break;
		}
	}
	return content.style.display == 'none';
}
function float_hide_handler() {
	float_hide(this.parentNode.parentNode.parentNode);
	float_hide_save();
}
function float_hide_img_handler() {
	float_hide(this.parentNode.parentNode.parentNode.parentNode);
	float_hide_save();
}
function float_hide(box) {
	divs = box.childNodes;
	num = box.id;
	num = num.substr(num.length-1, 1);
	content = null;
	link = null;
	for(x=0;x<divs.length;x++) {
		if(divs[x].className == 'sm_layer_content') {
			content = divs[x];
			break;
		}
	}
	spans = box.getElementsByTagName('span');
	for(x=0;x<spans.length;x++) {
		if(spans[x].className == 'sm_layer_link_hide') {
			link = spans[x];
			break;
		}
	}
	if(content.style.display != 'none') {
		content.style.display = 'none';
		hidden_floats[num] = true;
		link.childNodes[0].src = 'http://img.lca.pl/max.gif';
		
	}
	else {
		content.style.display = 'block';
		hidden_floats[num] = false;
		link.childNodes[0].src = 'http://img.lca.pl/min.gif';
	}	
}
function float_hide_save() {
	str = '';
	for(i=0;i<LAY_COUNT;i++) {
		if(hidden_floats[i+1]) {
			str += ('|'+(i+1));
		}
	}
	str = str.substr(1);
	set_cookie('hiddenf', str);
}
function previousObject(obj) {
	do {
		obj = obj.previousSibling;
	}
	while (obj != null && obj.nodeType == 3);
	return obj;
}
function nextObject(obj) {
	do {
		obj = obj.nextSibling;
	}
	while (obj != null && obj.nodeType == 3);
	return obj;
}
function isFirst(obj) {
	return previousObject(obj) == null;
}
function isLast(obj) {
	return nextObject(obj) == null;
}
function float_up_handler() {
	float_up(this.parentNode.parentNode.parentNode);
	float_order_save();
}
function float_up_img_handler() {
	float_up(this.parentNode.parentNode.parentNode.parentNode);
	float_order_save();
}
function float_down_handler() {
	float_down(this.parentNode.parentNode.parentNode);
	float_order_save();
}
function float_down_img_handler() {
	float_down(this.parentNode.parentNode.parentNode.parentNode);
	float_order_save();
}
function change_link_state(box, link) {
	cls = 'sm_layer_link_'+link;
	elements = box.getElementsByTagName('span');
	for(x=0;x<elements.length;x++) {
		if(elements[x].className == cls) {
			if(elements[x].style.display == 'none') {
				elements[x].style.display = 'inline';
			}
			else {
				elements[x].style.display = 'none';
			}
			break;
		}
	}
}
function getLayerNumber(box) {
	id = box.id;
	return Number(id.substr(id.length-1, 1));
}
function swap_order(box1, box2) {
	num1 = getLayerNumber(box1);
	num2 = getLayerNumber(box2);
	for(i=1;i<=LAY_COUNT;i++) {
		if(float_order[i] == num1) {
			indx1 = i;
		}
		else if(float_order[i] == num2) {
			indx2 = i;
		}
	}
	float_order[indx1] = num2;
	float_order[indx2] = num1;
}
function float_up(box) {
	prev_box = previousObject(box);
	if(prev_box != null) {
		e('sub_menu_center').insertBefore(box, prev_box);
		swap_order(box, prev_box);
		if(isFirst(box)) {
			change_link_state(box, 'up');
			change_link_state(prev_box, 'up');
		}
		if(isLast(prev_box)) {
			change_link_state(box, 'down');
			change_link_state(prev_box, 'down');
		}
	}
	
}
function float_down(box) {
	next_box = nextObject(box);
	if(next_box != null) {
		e('sub_menu_center').insertBefore(next_box, box);
		swap_order(box, next_box);
		if(isLast(box)) {
			change_link_state(box, 'down');
			change_link_state(next_box, 'down');
		}
		if(isFirst(next_box)) {
			change_link_state(box, 'up');
			change_link_state(next_box, 'up');
		}
	}
}
function float_order_save() {
	str = '';
	for(i=1;i<=LAY_COUNT;i++) {
		str += ('|'+float_order[i]);
	}
	str = str.substr(1);
	set_cookie('orderf', str);
}
function minitab(tab) {
	if(tab == 'mail') {
		test_var = mini_tab1_active;
		test_val = 0;
		lay_one = 'static_box2_content';
		lay_two = 'static_box2_content_mail';
	}
	else if(tab == 'blog') {
		test_var = mini_tab1_active;
		test_val = 1;
		lay_one = 'static_box2_content_mail';
		lay_two = 'static_box2_content';
	}
	else if(tab == 'katalog') {
		test_var = mini_tab2_active;
		test_val = 0;
		lay_one = 'static_box3_content';
		lay_two = 'static_box3_content_katalog';
	}
	else if(tab == 'ogloszenia') {
		test_var = mini_tab2_active;
		test_val = 1;
		lay_one = 'static_box3_content_katalog';
		lay_two = 'static_box3_content';
	}
	if(test_var == test_val) {
		e(lay_one).style.visibility = 'hidden';
		e(lay_two).style.visibility = 'visible';
		if(tab == 'mail' || tab == 'blog') {
			mini_tab1_active = !mini_tab1_active;
		}
		else if(tab == 'katalog' || tab == 'ogloszenia') {
			mini_tab2_active = !mini_tab2_active;
		}
		if(navigator.appName == "Microsoft Internet Explorer") {
			return event.returnValue = false;
		}
		else {
			return false;
		}
	}
	else {
		if(navigator.appName == "Microsoft Internet Explorer") {
		return event.returnValue = true;
		}
		else {
			return true;
		}
	}
}

var oferta_active = 0;
T_ID = -1;

function box_oferta() {
	oferta_layer(oferta_active+1, true);
}

function oferta_switch_link(num) {
	link = e('oferta_button_'+num);
	link_a = link.childNodes[0];
	if(link.className == 'oferta_button') {
		link.className = 'oferta_button_active';
	}
	else {
		link.className = 'oferta_button';
	}
}

function oferta_layer(n, go) {
	
	if(oferta_active > 0) {
		e('oferta_content_'+oferta_active).style.visibility = 'hidden';
		oferta_switch_link(oferta_active);
	}
	oferta_active = n;
	if(oferta_active > OFERTA_COUNT) {
		oferta_active = 1;
	}
	
	if(e('oferta_content_'+oferta_active).style.visibility != 'visible') {
		e('oferta_content_'+oferta_active).style.visibility = 'visible';
		oferta_switch_link(oferta_active);
	}
	
	if(T_ID > -1) {
		clearTimeout(T_ID);
	}
	
	if(go) {
		T_ID = setTimeout('box_oferta()', OFERTA_TIMEOUT);
	}
	else {
		T_ID = -1;
	}
	
	return false;
}

function link_return(logic) {
	if(navigator.appName == "Microsoft Internet Explorer") {
		return event.returnValue = logic;
	}
	else {
		return logic;
	}
}

function okno(url, w, h) {
	window.open(url, '', 'width='+w+', height='+h);
	return link_return(false);
}
function okno_s(url, w, h) {
	window.open(url, '', 'scrollbars=1, width='+w+', height='+h);
	return link_return(false);
}

make_interface();