/*
	ARS REGIA - JavaScripts
	Grzegorz Kaliciak, grzegorz@kaliciak.net / etrust.pl
	v1.0.0, 04.08.2007
*/

/*
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function inputLabelFromLabel(input_id,label_id) {
	if (!document.getElementById) return false;
	if (!document.getElementById(input_id)) return false;
	if (!document.getElementById(label_id)) return false;
	var textInput = document.getElementById(input_id);
	var label = document.getElementById(label_id);
	var text = label.innerHTML;
	if (!textInput.value) textInput.value = text;
	textInput.onfocus = function() {
		if (this.value == text) {
			this.value = '';
		}
	}
	textInput.onblur = function() {
		if (this.value == '') {
			this.value = text;
		}
	}
}

addLoadEvent(function(){
	inputLabelFromLabel("search","search_l");
});


*/
var reco_hide = function() {
    var popup = jQuery('#side-reco');
    jQuery('.text,textarea', popup).val('').removeClass('error');
    jQuery('label.error', popup).remove();
    jQuery('p.status', popup).hide();
    jQuery('p.submit', popup).show();
    popup.hide();
};

(function($){

var handle_facebook = function() {
    if ($('#fb-root').length == 0)
        return;

    window.fbAsyncInit = function() {
        FB.init({appId: '2530096808', status: true, cookie: true, xfbml: true});
    };

    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/pl_PL/all.js';
    document.getElementById('fb-root').appendChild(e);
};

var handle_reco = function() {
    var form = jQuery('#side-reco form');
    var popup = jQuery('#side-reco');

    form.validate({});
    jQuery('a.submit', form).click(function(ev) {
        ev.preventDefault();
        if (!form.valid())
            return false;

        var data = {
            name_from: jQuery('input[name=name_from]', form).val(),
            email_from: jQuery('input[name=email_from]', form).val(),
            content: jQuery('textarea[name=content]', form).val(),
            title: jQuery('input[name=title]', form).val(),
            url: location.href
        };

        jQuery('p.status', form).hide();
        jQuery('p.wait', form).show();

        jQuery.post('mail', data, function(resp) {
            if (resp == 'err') {
                jQuery('p.status', form).hide();
                jQuery('p.error', form).show();
            }
            else if (resp == 'ok') {
                jQuery('p.status', form).hide();
                jQuery('p.ok', form).show();
                setTimeout('reco_hide();', 2000);
            }
        });
        return false;
    });

    jQuery('#side-reco-show').click(function(ev) {
        ev.preventDefault();
        popup.show();
        return false;
    });

    jQuery('a.hide', popup).click(function(ev) {
        ev.preventDefault();
        reco_hide();
        return false;
    });

    if (popup.css('position') == 'absolute') {
        jQuery(window).scroll(function() {
            var me = jQuery(this);
            popup.css('top', me.scrollTop() + parseInt((me.height() - popup.height()) / 2));
        });
    };
}

$(function() {
	var maxh = 0;
	var maxh2 = 0;
	$('.box-prewrap').each(function() {
		var h = $(this).height();
		if (h > maxh) maxh = h;
	});
	maxh1 = maxh + 67;
	$('.box-prewrap').css('height', maxh1 + 'px');
	maxh2 = maxh - 113;
	$('.box-prewrap2').css('height', maxh2 + 'px');

	$('#advsearch-filter').change(function() {
		var v = $('option:selected', this).val();
		$('#advsearch-woj').parents('.field-t0').css('display', v == 'woj' ? 'inline-block' : 'none');
		$('#advsearch-adr').parents('.field-t0').css('display', v == 'adr' ? 'inline-block' : 'none');
	}).blur(function() { $(this).change(); }).change();

	$('a.ico-print').click(function(ev) {
		ev.preventDefault();
		window.print();
		return false;
	});

	handle_facebook();
	handle_reco();
});

})(jQuery);

