mapInit = function(){
	$("form tr.blockLabel input.mp").bind('click',function() {
		if($(this).attr('disabled')){ //jsme disabled
			return false;
		}
		
		if($(this).attr("checked")) {
			//$(this).attr("checked","checked");
			document.getElementById($(this).val()).style.display = "block";
		} else {
			//$(this).attr("checked","");
			$("form input.celapraha").removeAttr("checked");
			document.getElementById($(this).val()).style.display = "none";
		}
		$(this).change();
		return true;
	});
	

	$("form map.imgmapa area.mp").bind('click',function() {
		
		$(this).blur();
		
		
		if($("form input.celapraha").attr('disabled')){ //jsme disabled
			return false;
		}
		
		tclassname = $(this).attr("class").substr(3,4);
		tclassname = tclassname.replace(/\s/g,'');
		
		if (document.getElementById(tclassname).style.display == "block") {
			document.getElementById(tclassname).style.display = "none";
			document.getElementById("c"+tclassname).checked = "";
			//if($(this).not('.incp').length > 0){ // WTF?
				$("form input.celapraha").removeAttr("checked");
			//}
		} else {
			document.getElementById(tclassname).style.display = "block";
			document.getElementById("c"+tclassname).checked = "checked";
		}
		$(document.getElementById("c"+tclassname)).change();
		return false;
	});
	
	$("form input.celapraha").bind('click',function(evt){
		if($(this).attr("checked")) {
			$(this).attr("checked","checked");
			$("form tr.blockLabel input.incp").attr("checked","checked");
			if (($.browser.msie) && ($.browser.version <= 6)) {
				ajaxBubble(evt);
			}
			$("#map div.overlay img.incp").css("display","block");
		} else {
			$(this).attr("checked","");
			$("form tr.blockLabel input.incp").removeAttr("checked");
			$("#map div.overlay img.incp").css("display","none");
			if (($.browser.msie) && ($.browser.version <= 6)) {
				ajaxBubble(evt);
			}
		}
		return true;
	});
	
	$("form tr.blockLabel input.mp").each(function(){
			if ($(this).attr("checked") || $(this).attr("checked") == "checked") {
				checkerid = $(this).attr("value");
				$("#"+checkerid).css("display","block");
			}
	});
}


ajaxBubble = function(evt){

	if(typeof gmapsPresent != 'undefined'){
		$('input.vyhledatbutton').attr('src',base_web_dir+'images/button-vyhledat-pulse.gif');
	}
	
	if((typeof cplxform != 'undefined') && (cplxform.offerType == null)){
		$('#bubble').remove();
		return true;
	}
	$(evt.target.form).ajaxSubmit({
		url: evt.target.form.action+'?ajax=1',
		type:'post',
		success: function(data){
			if(data != ''){
				if($('#bubble').get().length == 0){
					$('#surrogate').append('<div id="bubble">Loading...</div>');
					$('#bubble').show();
				}
				$('#bubble').html(data);
			}
		}
	});
	return true;
}

locationPartInit = function(){
	$('#locationPart').focus(
		function(){
			$("#locationPartSurrogate").append('<div id="tooltipinfo">'+
			'<div id="tooltiparrow"></div>'+
			'<h1>Nápověda k poli obec, katastr, okres</h1>'+
			'<p>Zadáním názvu obce nebo čtvrti <strong>omezíte</strong> vyhledávanou lokalitu. Názvy oddělujte čárkou.</p>'+
			'<p><strong>Příklad:</strong> <em>zb,dej</em> hledá objekty ze Zbraslavi a Dejvic.</p>'+
			'</div>'); 
		}
	);
	$('#locationPart').blur(
		function(){
			$('#tooltipinfo').remove();
		}
	);
}

ajaxBubbleInit = function(parentElm){
	if ($.browser.msie) {
		$('input:checkbox',parentElm).bind('click',function(evt){
			ajaxBubble(evt);
			return true;
		});
		// IE odesila eventy jeste driv, nez updatne atribut "checked"
		$('input:radio',parentElm).bind('click',function(evt){
			ajaxBubble(evt);
			return true;
		});
		$('input:text',parentElm).bind('change',ajaxBubble);
	} else {
		$('input:text,input:checkbox,input:radio',parentElm).bind('change',ajaxBubble);
	}
	$('select',parentElm).bind('change',ajaxBubble);
}

tableListInit = function(){
	$("table.list tbody tr").each(function(){
		link = $("a.rowLink",this).attr("href");
		$(this).children(".link").css("cursor","pointer");
		$(this).children(".link").bind('click',function(){
			document.location.href = $('a.rowLink', $(this).parent()).attr('href');
		});
		if ($.browser.msie) {
			$(this).hover(function(){
				$(this).children("td").toggleClass("hover");
			},
			function(){
				$(this).children("td").toggleClass("hover");
			});
		}
	});
	
	$("input.refreshButton").parent().css("display","none");

	$("select.submitonchange").bind('change',function(){
		$('#senddemandhidden').attr('value','0');
		if(this.name.indexOf(':_') > -1){
			this.form.bottomSubmit.click();
		}else{
			this.form.topSubmit.click();
		}
		
		//this.form.submit();
	});
	
	$("div.inInfoBox",$('div.infoBox').not('.brokerTip,.reservation')).hover(
		function(){
			$(this).css("background-color","#FFFAE3"); 
		},
		function(){ 
			$(this).css("background-color",'#FFF');
		}
	);
	$("div.brokerTip div.inInfoBox").hover(
		function(){
			$(this).css("background-color","#FFFAE3"); 
		},
		function(){ 
			$(this).css("background-color",'#fde9c8'); 
		}
	);
	$("div.reservation div.inInfoBox").hover(
		function(){
			$(this).css("background-color","#FFFAE3"); 
		},
		function(){ 
			$(this).css("background-color",'#f8f8f8'); 
		}
	);
}

function getscrollpos() {
	 var scrollPos = window.pageYOffset; // Mozilla
	 if (scrollPos == undefined) {
		 var doc = window.document;
		 if (doc.compatMode && doc.compatMode != "BackCompat")
			  scrollPos = doc.documentElement.scrollTop;
		 else
			  scrollPos = doc.body.scrollTop;
	 }
	 return scrollPos;
}
function scPos() {
	if (getscrollpos() > "128") {
		$('#rightbanner').css("top",getscrollpos());
	}
}

init = function() {
	
	if(typeof loadTableList != 'undefined'){
		tableListInit();
	}
	
	if(typeof loadForm != 'undefined'){
		mapInit();
		ajaxBubbleInit($('form.withbubble .bubbleActive'));
	}
	$('#locationPart').each(locationPartInit);

	if(homepage == false) {
		$('#hideshowaf').bind('click',function(evt){
			$('#afilter').slideToggle(function(){
					if(document.getElementById("hideshowaf").innerHTML.indexOf("Zobrazit") > -1) {
						document.getElementById("hideshowaf").innerHTML = "Skrýt podrobné vyhledávání";
					} else {
						document.getElementById("hideshowaf").innerHTML = "Zobrazit podrobné vyhledávání";
					}
					this.blur();
			});
			evt.stopPropagation();
			evt.preventDefault();
			return false;
		});
	}
	
	document.getElementById("logo").style.cursor = "pointer";
	$('#logo').bind('click',function(){
		document.location.href = base_web_dir;
	});
	
	if(homepage == false) {
		$('a.plainPopup').click(function(){
			ploc = $(this).attr("href");
			popup = window.open(ploc,'popupwin','width=800,height=500,toolbar=0,resizable=1,location=0,status=0,menubar=0,directories=0,scrollbars=1');
			popup.focus();
			return false;
		});
	}
	
	if(typeof page != 'undefined' &&  page == 'kariera'){
		$('a.karieraform').click(function(){
			ploc = $(this).attr("href");
			popupk = window.open(ploc,'popupwin','width=450,height=518,toolbar=0,resizable=1,location=0,status=0,menubar=0,directories=0,scrollbars=1');
			popupk.focus();
			return false;
		});
		
		
	}
	
	if (getscrollpos() >= "128") {
		scPos();
	}
	$(window).scroll( function() {
			if (getscrollpos() > "128") {
				n = setTimeout("scPos()",200);
			} else {
				$('#rightbanner').css("top","128px");
			}
	});

	$('#fblink, #fblink2').click(function(){
		if(typeof(fbTracker) != 'undefined'){
			fbTracker._trackEvent('kliknuti-na-fb', $(this).attr('id'));
		}
	});

}


$(document).ready(init);

$(document).ready(function(){

	if(ajaxSendToFriend ==""){
		ajaxSendToFriend = TRUE;
	}

	// SEND LINK
	var form = '<div id="modal-contact-form">'+
	'	<h3>Doporučit známému</h3>'+
	'	<p id="ajax-message"></p>'+
	'	<form id="sendToFriendForm" action="'+ajaxSendToFriend+'" method="post">'+
	'		<p><label for="stf:email">Cílový e-mail</label> <input id="stf:email" value="@" type="text" name="stf:email" class="inputText required email"/></p>'+
	'		<p><label for="stf:note">Vzkaz</label> <textarea id="stf:note" name="stf:note" rows="3" cols="20"></textarea></p>'+
	'		<input id="stf:link" type="hidden" name="stf:link" value="'+requestURI+'" />'+
	'		<p class="submitBtn"><input type="image" class="inputImage" title="Odeslat" alt="Odeslat" src="'+base_web_dir+'images/button-odeslat.gif"/>'+
	'	</form>'+
	'</div>';


	$('#share li.email a, #share-small li.email a').click(function(){		
		$('body').komodal({
			content: form,
			closeBackground: "#BE4219",
			//padding: "30px 35px 0",
			height: "220",
			width: "300"

		})

		
		return false;
	})

	// LINK IN A NEW WINDOW
	$('a.new-window, #share li:not(#send-email) a, #share-small li:not(#send-email) a').click(function(){
		window.open(this.href);
		return false;
	});

	// REQUEST BOX
	$('#show-full-request').click(function(){
		$('#half-request-box').hide();
		$('#full-request-box').fadeIn('slow');
		$('#request-box').addClass('expanded-request-box');

		return false;
	});
    $('.autofill')
        .each(function(){
            $(this).data('value',$(this).val());
        })
        .change(function(){
            if($(this).val()!=$(this).data('value')){
                $(this).removeClass('autofill-default');
            }
        })
        .focus(function(){
            if($(this).val()==$(this).data('value')){
                $(this)
                    .val('')
                    .removeClass('autofill-default')
                ;
            }
        })
        .blur(function(){
            if($.trim($(this).val())==''){
                $(this)
                    .val($(this).data('value'))
                    .addClass('autofill-default')
                ;
            }
        })
        .addClass('autofill-default')
    ;
	$('form.smallH.withbubble').submit(function(){
		$('.autofill',this).each(function(){
            if($(this).val()==$(this).data('value')){
                $(this)
                    .val('')
                    .removeClass('autofill-default')
                ;
            }
		});
	});

});