var hm_buttons = ['hm_portal','hm_forum','hm_ginger','hm_phones', 'hm_sms'];
var search_defvalue = 'szukany tekst';
var main_domain;
var current_domain;

function btnOver()
{
	this.src = hm_buttons[this.hm_idx].src;
}

function btnOut()
{
	this.src = this.org_src;
}

function btnPrepare(hm_idx)
{
	var img = $(hm_buttons[hm_idx]);
	img.org_src = img.src;
	img.hm_idx = hm_idx;
	hm_buttons[hm_idx] = new Element('img', {src: img.src.replace('_std', '_hl')});
	img.observe('mouseover', btnOver);
	img.observe('mouseout', btnOut);
}

function pasjaInit()
{
	pasjaInitDOM();
	pasjaInitLayout();
}

function pasjaInitDOM()
{
	for (var i = 0; i < hm_buttons.length; i++) {
		btnPrepare(i);
	}

	var searchf = $('head_search_field');
	if (searchf) {
		if (searchf.value === '' || searchf.value == search_defvalue) {
			searchf.value = search_defvalue;
		}
		searchf.observe('focus', function(){
			if (searchf.value == search_defvalue) {
				searchf.value = '';
			}
			searchf.style.background = '#FDFACC';
		});
		searchf.observe('blur', function(){
			if (searchf.value === '') {
				searchf.value = search_defvalue;
			}
			searchf.style.background = '';
		});
	}
	var cc = $('contents_clear');
	if (cc != null && !cc.hasClassName('forum')) {
		$('contents').insert(new Element('div', {id: 'contents_clear'}).setStyle({clear:'both'}));
	}
}

function pasjaInitLayout()
{
	var obj = $('adv_banner');
	if (obj && obj.contentDocument) {
		obj.style.height = obj.contentDocument.body.scrollHeight+'px';
	}
}

function linksInit()
{
	var col_left = $('col_left');
	if (!col_left) {
		return;
	}
	var parents = col_left.select('.justify', '.postbody', 'td.quote');
	for (var i = 0; i < parents.length; i++) {
		var as = parents[i].select('a');
		for (var j = 0; j < as.length; j++) {
			var href = as[j].readAttribute('href');
			if (!href || href.indexOf('.pasjagsm.pl/') != -1
					|| (href.substring(0, 7) != 'http://' && href.substring(0, 8) != 'https://')) {
				continue;
			}
			as[j].href_org = as[j].href;
			as[j].href = main_domain + '/link.php?url=' + encodeURIComponent(as[j].href_org);
		}
	}
}

function pollInit()
{
	var obj = $('block_poll');
	if (!obj || obj.tagName.toLowerCase() != 'form') {
		return;
	}

	var submit = obj.down('input[name=submit_poll_vote]');
	obj.enable();
	submit.enable();

	obj.observe('submit', function(event) {
		event.stop();
		var values = this.serialize();
		this.disable();
		submit.disable();
		submit.addClassName('disabled');
		var loading = new Element('div', {'class': 'box_loading'})
			.setStyle({position: 'absolute'})
			.clonePosition(obj);
		this
			.setOpacity(0.35)
			.insert({after: loading});
		new Ajax.Updater(this, this.action, {
			encoding: 'ISO-8859-2',
			postBody: values,
			onComplete: function() {
				loading.remove();
				obj.setOpacity(1);
			}
		});
	});
	// some browsers can restore form value after page refresh
	var value = null;
	var radios = obj.select('input[type=radio]');
	radios.each(function(el){value = value || $F(el)});
	if (!value) {
		submit.disable();
		submit.addClassName('disabled');
		radios.each(function(el) {
			el.observe('click', function() {
				submit.enable();
				submit.removeClassName('disabled');
			});
		});
	}
}

document.observe('dom:loaded', pasjaInitDOM);
document.observe('dom:loaded', linksInit);
document.observe('dom:loaded', pollInit);
Event.observe(window, 'load', pasjaInitLayout);