function param(id) {if(gtsp(id)) {return (gtsp(id).innerHTML.indexOf('<param')!=-1 || gtsp(id).innerHTML.indexOf('<PARAM')!=-1 || gtsp(id).innerHTML.indexOf('<A')!=-1 || gtsp(id).innerHTML.indexOf('<a')!=-1);}}

function D22D(id){D2D('t'+id,'r'+id)}
function D2D(d1id,d2id){if(gtsp(d2id)){vtmp=gtsp(d1id).innerHTML;gtsp(d1id).innerHTML='';el(d2id,'none');gtsp(d2id).innerHTML=vtmp;ObjReplace(d2id);el(d2id,'');}}



function gtsp(id){get=document.getElementById(id); return get}
function el(id,dis){
    if(gtsp(id)) gtsp(id).style.display=dis
}
function zapal(id)        {
	gtsp(id).className="menu_gif_r on";
}
function zgas(id)        {
	gtsp(id).className="menu_gif_r off";
}

function ONW(str_url,sz,wy){str_param="width="+sz+",height="+wy+",top="+((screen.availHeight-wy)/2)+",left="+((screen.availWidth-sz)/2)+",location=0,menubar=0,resizable=1,scrollbars=1";nokn=window.open(str_url,null,str_param);if(nokn) nokn.focus();}

function checkPlatSMS(obj)	{
	if(obj.options[obj.selectedIndex].value==102)	{
		gtsp("kwota").disabled=true;
		el("kwota","none");
		gtsp("kwota2").disabled=false;
		el("kwota2","");
	}	else	{
		gtsp("kwota").disabled=false;
		el("kwota","");
		gtsp("kwota2").disabled=true;
		el("kwota2","none");
	}
}

function moj_zapal(ob0,ob1,ob2,ob3,ob4) {
	if(ob0.checked==true){
		document.getElementById(ob1).style.visibility='hidden';
		document.getElementById(ob2).style.visibility='hidden';
		document.getElementById(ob3).style.display='';
		document.getElementById(ob4).style.display='none';
	}else{
		document.getElementById(ob1).style.visibility='';
		document.getElementById(ob2).style.visibility='';
		document.getElementById(ob3).style.display='none';
		document.getElementById(ob4).style.display='';
	}
}

if(typeof($) != "undefined")	{
	$(function() {                  
	  $("div.lista_firm_logo a > img").lazyload({
	     placeholder : "layout_images/nowydom/ajax.gif",
	     effect      : "fadeIn"
	  });
	});
$(document).ready(
	function() {
		if(readCookie("moj_logedin") == 1)	{
		$("#div_moj").load("/get_menu.php",{},function() {
			$('#link_moj').attr('href','http://moj.dom.pl/' +  readCookie('moj_logedin_username') + '/');	
			TB_init();
		});
	}

	}
);	
}

function zapal_check()
{
	var ile=0;
	var objs=document.getElementsByTagName('input');
	for(i=0;i<objs.length;i++)
	{
		if(objs[i].name=="tem_kat[]" && objs[i].disabled)
		{
			objs[i].disabled = false;
		}
	}
}

function zgas_check()
{
//	alert("Możesz zaznaczyć tylko 3 kategorie tematyczne");
	var ile=0;
	var objs=document.getElementsByTagName('input');
	for(i=0;i<objs.length;i++)
	{
		if(objs[i].name=="tem_kat[]" && !objs[i].checked)
		{
			objs[i].disabled = true;
		}
	}
}
function check_sprawdz()
{
	var ile=0;
	var objs=document.getElementsByTagName('input');
	for(i=0;i<objs.length;i++)
	{
		if(objs[i].name=="tem_kat[]" &&objs[i].checked)
		{
			ile=parseInt(ile+1);
		}
	}
	if(ile >= 3)	
	{
		zgas_check();
	}
	else
	{
		zapal_check();
	}
}
window.onload=function()	{
check_sprawdz();
}

function readCookie(name) {
    var nameEQ = name + "=";var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[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;
}

function ajaxFunction(url,params,met,akcja,ret){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		if (ajaxRequest.overrideMimeType) { 
            ajaxRequest.overrideMimeType('text/xml; charset=utf-8'); 
		} 
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Twoja przeglądarka zawiodła!");
				return false;
			}
		}
	}
	if(met=='post')	{
		ajaxRequest.open("POST", url, true);
	
		//Send the proper header information along with the request
		ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close"); 
	
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(ret)	{
				wynik=ajaxRequest.responseXML;
				eval(akcja);	
			}	else	{
				wynik=ajaxRequest.responseText;
				setTimeout('eval("'+akcja+'")',800);
			}
		}
	}
	if(met=='get')	{
		ajaxRequest.open("GET", url+'?'+params, true);
		ajaxRequest.send(null);
	}
	if(met=='post')	{
		ajaxRequest.send(params); 
	}
}

/* moj.dom.pl - ulubione */
function moj_ulubione(ad_id) {
	var search_url=location.protocol+"//"+location.host+"/moj_ulubione.php";
	var search_string="ad_id="+ad_id;
	var akcja="do_moj_ulubione(wynik)";
	ajaxFunction(search_url,search_string,'get',akcja);
}

function do_moj_ulubione(kom)	{
	var zalogowany_moj=readCookie('moj_logedin');
	if(zalogowany_moj)	{
		if(kom==1)	{
			alert("Zapisano do ulubionych!");
		}	else	{
			alert("Błąd zapisu. Spróbuj jeszcze raz!");
		}
	}	else	{
		window.location="http://moj.dom.pl/user_home.php?"+kom;	
	}
	
}
/*dopisane z moj.dom.pl*/

TB_WIDTH = 0;
TB_HEIGHT = 0;

var TB_doneOnce = 0;
var TB_ready = false;
var TB_useReady = true;


// add smoothbox to href elements that have a class of .smoothbox
function TB_init()
{
  TB_ready = true;
	$("a.smoothbox").each(function(el){el.onclick=TB_bind});
}

//window.addEvent('domready', TB_init);
//window.addEvent('load', TB_init);



function TB_bind(event)
{
  if( TB_useReady && !TB_ready ) return;
  
	var event = new Event(event);
	// stop default behaviour
	event.preventDefault();
	// remove click border
	this.blur();
	// get caption: either title or name attribute
	var caption = this.title || this.name || "";
	// get rel attribute for image groups
	var group = this.rel || false;
	// display the box for the elements href
	TB_show(caption, this.href, group);
    this.onclick = TB_bind;
    return false;
}

// called when the user clicks on a smoothbox link
function TB_show(caption, url, rel, loading, disable_close)
{
  if( TB_useReady && !TB_ready ) return;
  
  // set default closing mechanism
  if(!disable_close) { disable_close = 0; }

  // create iframe, overlay and box if non-existent
  if (!$("#TB_overlay").length)
  {
  	if($('#smoothbox_container').length)
    	$(document.createElement('div')).attr('id', 'TB_overlay').appendTo($('#smoothbox_container'));
    else
    	$(document.createElement('div')).attr('id', 'TB_overlay').appendTo(document.body);
    $('#TB_overlay').css('opacity','0.6');
    TB_overlaySize();
  }

  if (!$("#TB_window").length)
  {
  	if($('#smoothbox_container').length)
    	$(document.createElement('div')).attr('id', 'TB_window').appendTo($('#smoothbox_container'));
    else
    	$(document.createElement('div')).attr('id', 'TB_window').appendTo(document.body);
    $('#TB_window').css('display','none');
  	
  }
  else 
  {
    $('#TB_window').remove(); //$('TB_window').dispose();
   	if($('#smoothbox_container').length)
    	$(document.createElement('div')).attr('id', 'TB_window').appendTo($('#smoothbox_container'));
    else
    	$(document.createElement('div')).attr('id', 'TB_window').appendTo(document.body);
    //$('#TB_window').css('opacity','0');
  }
 
  if(disable_close == 0) { $("#TB_overlay").click( function () { TB_remove()}); } else { $("#TB_overlay").click(); }
  window.onscroll = TB_position;
 
  // check if a query string is involved
  var baseURL = url.match(/(.+)?/)[1] || url;
  
  // CODE TO SHOW IFRAME
  var queryString = url.match(/\?(.+)/)[1];
  var params = TB_parseQuery(queryString);

  TB_WIDTH = (params['width'] * 1) + 30;
  TB_HEIGHT = (params['height'] * 1) + 40;
  
  var ajaxContentW = TB_WIDTH - 30, ajaxContentH = TB_HEIGHT - 45;
  
  
  if (url.indexOf('TB_iframe') != -1)
  {
    urlNoQuery = url.split('TB_');
    $("#TB_window").html($("#TB_window").html() + "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>X</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "&in_smoothbox=true' id='TB_iframeContent' name='TB_iframeContent' scrolling='auto' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' onload='TB_showWindow()'> </iframe>");
  }
  else
  {
    $("#TB_window").html($("#TB_window").html() + "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>X</a></div></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>");
  }
  
  $("#TB_closeWindowButton").click(function () {TB_remove()});

  if (url.indexOf('TB_inline') != -1)
  {
    $("#TB_ajaxContent").html($(params['inlineId']).html());
    TB_position();
    TB_showWindow();
  }
  else if (url.indexOf('TB_iframe') != -1)
  {
    //alert("UHOH1");
    TB_position();
    if (frames['TB_iframeContent'] == undefined) {//be nice to safari
      $(document).keyup(function(e){
                    var key = e.keyCode;
                    if (key == 27) {
                        TB_remove()
                    }
      });
      TB_showWindow();
    }
  }
  else
  {
    //alert("UHOH2");
    var handlerFunc = function(){
          TB_position();
          TB_showWindow();
    };
    new Request.HTML({
      method: 'get',
      update: $("#TB_ajaxContent"),
      onComplete: handlerFunc
    }).get(url);
  }

  window.onresize = function()
  {
    TB_position();
    TB_overlaySize();
  }

  document.onkeyup = function(event)
  {
    var event = new Event(event);
    if (event.code == 27) { // close
        TB_remove();
    }
  }

}

//helper functions below

function TB_showWindow()
{
  if( TB_useReady && !TB_ready ) return;
  
  if (TB_doneOnce == 0) {
     TB_doneOnce = 1;
      
      $('#TB_window').fadeIn(1000);
  }
  else {
      $('#TB_window').css('opacity', '1');
  }
//  $('TB_window').setStyle('opacity', 1);
}

function TB_remove()
{
  if( TB_useReady && !TB_ready ) return;
  
  //$("#TB_overlay").click();
  document.onkeyup = null;
  document.onkeydown = null;
  
//  if ($('#TB_closeWindowButton')) 
//     $("#TB_closeWindowButton").click();

  $('#TB_window').fadeOut(250, function(){
  	$('#TB_window').remove(); //$('TB_window').dispose();
  });
  
  $('#TB_overlay').fadeOut(400, function(){
  	$('#TB_overlay').remove(); //$('TB_overlay').dispose();
  });
  
  window.onscroll = null;
  window.onresize = null;
  TB_init();
  TB_doneOnce = 0;
  return false;
}

function TB_position()
{
  if( TB_useReady && !TB_ready ) return;
  
  $('#TB_window').animate({
    width: TB_WIDTH + 'px',
    left: ($(window).scrollLeft() + ($(window).width() - TB_WIDTH) / 2) + 'px',
    top: ($(window).scrollTop() + ($(window).height() - TB_HEIGHT) / 2) + 'px'
	},150);	
}

function TB_overlaySize()
{
  if( TB_useReady && !TB_ready ) return;
  
  // we have to set this to 0px before so we can reduce the size / width of the overflow onresize 

  $("#TB_overlay").css({
    "height": '0px',
    "width": '0px'
  });

  $("#TB_overlay").css({
    "height": ($(document).height() + 'px'),
    "width": ($(document).width() + 'px')
  });

}


function TB_parseQuery(query)
{
  if( TB_useReady && !TB_ready ) return;
  
  // return empty object
  if (!query)  return {};
  var params = {};
  
  // parse query
  var pairs = query.split(/[;&]/);
  for (var i = 0; i < pairs.length; i++)
  {
    var pair = pairs[i].split('=');
    if (!pair || pair.length != 2) continue;
    // unescape both key and value, replace "+" with spaces in value
    params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
  }
  return params;
}


var open_menu;
var current_timeout = new Array();

function showMenu(id1)
{
  if($('#' + id1).length)
  {
    if($('#' + id1).css('display')=='none')
    {
      //alert(open_menu);
      if($('#' + open_menu).length) {hideMenu($('#' + open_menu)); }
      $('#' + id1).css('display','inline');
      startMenuTimeout($('#' + id1));
      $('#' + id1).bind("mouseover",function(e) { killMenuTimeout($('#' + id1)); });
      $('#' + id1).bind("mouseout",function(e) { startMenuTimeout($('#' + id1)); });
      open_menu = id1;
    }
  }
}

function showMenu(id1, id2)
{
  if($('#' + id1).length)
  {
    if($('#' + id1).css('display')=='none')
    {
      //alert(open_menu);
      if($('#' + open_menu).length) {hideMenu($('#' + open_menu)); }
      $('#' + id1).css('display','inline');
     //startMenuTimeout($('#' + id1));

	$('#' + id2).bind("mouseleave",function(e) {startMenuTimeout($('#' + id1));});
	$('#' + id1).bind("mouseover",function(e){killMenuTimeout($('#' + id1)); });
	$('#' + id1).bind("mouseout",function(e) {startMenuTimeout($('#' + id1)); });
	  open_menu = id1;
    }
  }
}


function killMenuTimeout(divEl)
{
  clearTimeout(current_timeout[divEl.id]);
  current_timeout[divEl.id] = '';
}

function startMenuTimeout(divEl)
{
//	alert(divEl.id);
  if(current_timeout[divEl.id] == '') {
    current_timeout[divEl.id] = setTimeout(function() { hideMenu(divEl); }, 1000);
  }
}

function hideMenu(divEl)
{
  divEl.css('display','none');
  current_timeout[divEl.id] = '';
  divEl.unbind("mouseover");
  divEl.unbind("mouseout");
}

function SwapOut(id1) {
  $('#' + id1).src = Rollarrow1.src;
  return true;
}

function SwapBack(id1) {
  $('#' + id1).src = Rollarrow0.src;
  return true;
}
function urldecode( str ) {
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}
function user_moj_log()	{
	if(readCookie('moj_logedin'))	{
		var username = readCookie('moj_logedin_username');
		var displayname = readCookie('moj_logedin_displayname');
		gtsp('user_moj_log_a').href='http://moj.dom.pl/' + username;
		if(displayname)	{
			gtsp('user_moj_log_a').innerHTML='Witaj, ' + urldecode(displayname) + '!';
		}	else	{
			gtsp('user_moj_log_a').innerHTML='Witaj, ' + urldecode(username) + '!';
		}
		el('user_moj_not_log','none');
		el('user_moj_log','');
	}
}
function user_moj_log2()	{
	if(readCookie('moj_logedin'))	{
		var username = readCookie('moj_logedin_username');
		var displayname = readCookie('moj_logedin_displayname');
		gtsp('profil_link').href='http://moj.dom.pl/' + username;
		gtsp('loged_profil').href='http://moj.dom.pl/' + username;
		username_short = username;
		if(username.length > 15)
			var username_short = username.substr(0,11)+'...';
		gtsp('loged_profil').innerHTML=username_short;
		gtsp('profil_tresc').innerHTML='...<a href="http://moj.dom.pl/zdjecia_z_budowy.html">zdjęcia z budowy</a><span class="moj_or">&nbsp;</span><br/>...<a href="http://moj.dom.pl/internetowy_dziennik_budowy.html">dziennik budowy</a><span class="moj_or">&nbsp;</span>';
		el('user_profil_not_log','none');
		el('user_profil_log','');
	}
}