	$(document).ready(function() {
        $('.input :radio').focus(updateSelectedStyle);
        $('.input :radio').blur(updateSelectedStyle);
        $('.input :radio').change(updateSelectedStyle);
		$('.target').attr('target', '_blank');

		$('.art_view_true_source a[rel="nofollow"]').attr('target', '_blank');

		$('.snapback').attr('target', '_blank');
		$("#okresy_toggle").click(function() {
			$('#okresy').toggle('slow');
		});
		$("#commentForm").hide();
		$("#hide_more_comments").hide();
		$("#add_comment_button").click(function () {
			$("#commentForm").toggle("slow");
		});
		$("#hide_comment_button").click(function () {
			$("#commentForm").toggle("slow");
		});
		$("#expand_firm_edit").click(function () {
			$("#ul_firm_edit_box").toggle("slow");
		});
		$("#more_comments").hide();
		$("#show_more_comments").click(function () {
			$("#more_comments").toggle("slow");
			//$("#show_more_comments").toggle();
			delayone=setTimeout('$("#show_more_comments").toggle()',500);
			delay=setTimeout('$("#hide_more_comments").toggle()',500);
			delayleft=setTimeout('setleft();',500);
			delayright=setTimeout('setright();',500);
		});
		$("#hide_more_comments").click(function () {
			$("#more_comments").toggle("slow");
			delayone=setTimeout('$("#hide_more_comments").toggle()',500);
			delay=setTimeout('$("#show_more_comments").toggle()',500);
			delayleft=setTimeout('setleft();',500);
			delayright=setTimeout('setright();',500);
		});
		$("#add_comment_button").click(function () {
			delayone=setTimeout('$("#add_comment_button").toggle()',500);
			delay=setTimeout('$("#hide_comment_button").toggle()',500);
			delayleft=setTimeout('setleft();',500);
			delayright=setTimeout('setright();',500);
		});
		$("#hide_comment_button").click(function () {
			delayone=setTimeout('$("#hide_comment_button").toggle()',500);
			delay=setTimeout('$("#add_comment_button").toggle()',500);
			delayleft=setTimeout('setleft();',500);
			delayright=setTimeout('setright();',500);
		});
		$('#gallery a').lightBox({fixedNavigation:true});

		$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
		$('#gallery a').lightBox(); // Select all links in object with gallery ID
		$('a.lightbox').lightBox(); // Select all links with lightbox class
		// This, or...
		//$('a').lightBox(); // Select all links in the page
		// ... The possibility are many. Use your creative or choose one in the examples above
		$("#adv_search_options").hide();

		$('li.s_radio_li').click( function(){
			$('li.s_radio_li_change').addClass('s_radio_li');
			$('li.s_radio_li_change').removeClass('s_radio_li_change');
			$(this).addClass('s_radio_li_change');
			$(this).children("input[type=radio]").click();
		});


		//$('div#tabs li a').corner("top 5px");
		
		$('#sample-menu-border').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#left_advert_1').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#left_advert_2').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#left_advert_3').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#firm_login').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('.content_rounded').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#right_advert_2').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#left_project_list_seo_box').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#left_most_search').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#right_project_list_seo_box').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#left_list_seo_box1').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#right_forum_seo_box').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
			
		$('#left_bottom_search_SEO_MostRead').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#random_projects').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
		$('#right_recent_topics').corner("round 5px").parent().css('padding', '1px').corner("round 5px");
//		$('.sf-menu-seo li.sf-cut-seo a span').each(function(){$(this).text($(this).text().substr(0,35) + '...');});
		$('.onlynumeric').numeric();
		$('.numeric').numeric({allow:"&"});
		$('.alphanumeric_firm_name').alphanumeric({allow:"\\. ,!?%&-\\)\\(\\]\\[\\&\\^\\%"});
		$('.numericopenttime').numeric({allow:":-"});
		$('.alphanumeric').alphanumeric();
		$('.alphanumericspace').alphanumeric({allow:" "});
		$('.postalcode').numeric({allow:"-"});
		$('.phonenumber').numeric({allow:"-"});
		$('.email_chars').alphanumeric({allow:"\\.@_-"});
		$('.sample6').alphanumeric({ichars:'.1a'});

		$('select#address_voivodship').change(function(){
			$.ajax({
					async: true,
					data: {'param' : $(this).val()},
					dataType: 'html',
					beforeSend: function (XMLHttpRequest) {
						$('#address_voivodship').attr("disabled","disabled");
						$('#address_district').attr("disabled","disabled");
						$('#address_community').attr("disabled","disabled");
						$('#address_city').attr("disabled","disabled");
						$('#address_postal_code').attr("disabled","disabled");
						$('#address_street_building_number').attr("disabled","disabled");
						$('#address_street_name').attr("disabled","disabled");
						$('#send').attr("disabled","disabled");
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						$("#address_voivodship").removeAttr("disabled")
						$('#address_district').removeAttr("disabled");
						$('#address_community').removeAttr("disabled");
						
					},
					success: function(data, textStatus) {
						$("#address_district").html(data);
						$('#address_district option:first').attr('selected', 'selected');
						$('#data_container').html(data);
						optionsN = '<option value="NO">-wybierz powiat-</option>';
						$("#address_community").html(optionsN);
						$("#address_city").html(optionsN);
						$("#address_voivodship").removeAttr("disabled")
						$('#address_district').removeAttr("disabled");
						$('#address_community').removeAttr("disabled");
						$('#address_city').removeAttr("disabled");

						if(districtVal!='NO') {
							$("#address_district").val(districtVal);
							$("#address_district").trigger("change");
						}
					},
					type: 'POST',
					url: baseURLFirmy+"ajax-firmy.html"
			});
		});

		$('select#business_area_voivodship').change(function(){
			var param = '';
			var iterator = 0;
			$('#business_area_voivodship option').each(function(){
				if($(this).attr('selected')==true && $(this).val()!="NO") param = (param=='' ? '' : param + '&') + 'param['+(iterator++)+']='+$(this).val();
			});			
			$.ajax({
					async: true,
					data: param,
					dataType: 'html',
					beforeSend: function (XMLHttpRequest) {
						$('#business_area_district').attr("disabled","disabled");

					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {


					},
					success: function(data, textStatus) {
						$("#business_area_district").html(data);
						$('#business_area_district option:first').attr('selected', 'selected');
						$('#data_container').html(data);
						$('#business_area_district').attr("size","8");
						$("#business_area_voivodship").removeAttr("disabled")
						$('#business_area_district').removeAttr("disabled");
					},
					type: 'POST',
					url: baseURLFirmy+"ajax-firmy-ba.html"
			});
		});


		$('select#address_district').change(function(){
			$.ajax({
					async: true,
					data: {'param' : $(this).val()},
					dataType: 'html',
					beforeSend: function (XMLHttpRequest) {
						$('#address_voivodship').attr("disabled","disabled");
						$('#address_district').attr("disabled","disabled");
						$('#address_community').attr("disabled","disabled");
						$('#address_city').attr("disabled","disabled");
						$('#address_postal_code').attr("disabled","disabled");
						$('#address_street_building_number').attr("disabled","disabled");
						$('#address_street_name').attr("disabled","disabled");
						$('#send').attr("disabled","disabled");
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						$("#address_voivodship").removeAttr("disabled")
						$('#address_district').removeAttr("disabled");
						$('#address_community').removeAttr("disabled");
						
					},
					success: function(data, textStatus) {
						$("#address_community").html(data);
						$('#address_community option:first').attr('selected', 'selected');
						$("#address_voivodship").removeAttr("disabled")
						$('#address_district').removeAttr("disabled");
						$('#address_community').removeAttr("disabled");
						optionsN = '<option value="NO">-wybierz gminę-</option>';
						$("#address_city").html(optionsN);
						$('#address_city').removeAttr("disabled");
						if(communityVal!='NO') {
							$("#address_community").val(communityVal);
							$("#address_community").trigger("change");
							//communityVal='NO';
						}
					},
					type: 'POST',
					url: baseURLFirmy+"ajax-firmy.html"
			});
		});

		$('select#address_community').change(function(){
			$.ajax({
					async: true,
					data: {'param' : $(this).val()},
					dataType: 'html',
					beforeSend: function (XMLHttpRequest) {
						$('#address_voivodship').attr("disabled","disabled");
						$('#address_district').attr("disabled","disabled");
						$('#address_community').attr("disabled","disabled");
						$('#address_city').attr("disabled","disabled");
						$('#address_postal_code').attr("disabled","disabled");
						$('#address_street_building_number').attr("disabled","disabled");
						$('#address_street_name').attr("disabled","disabled");
						$('#send').attr("disabled","disabled");
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						

					},
					success: function(data, textStatus) {
						$("#address_city").html(data);
						$('#address_city option:first').attr('selected', 'selected');
						$("#address_voivodship").removeAttr("disabled")
						$('#address_district').removeAttr("disabled");
						$('#address_community').removeAttr("disabled");
						$('#address_city').removeAttr("disabled");
						if(cityVal!='NO') {
							$("#address_city").val(cityVal);
							$("#address_city").trigger("change");
							//cityVal='NO';
						}
						
					},
					type: 'POST',
					url: baseURLFirmy+"ajax-firmy.html"
			});
		});




		$('select#address_city').change(function(){
			$('#address_postal_code').removeAttr("disabled");
			$('#send').removeAttr("disabled");
			$('#address_street_building_number').removeAttr("disabled");
			$('#address_street_name').removeAttr("disabled");
		});
		if($('#address_voivodship').val()!='NO' )	{
//			alert($('#address_district').val());
			$('#address_voivodship').trigger('change');
		}

		if($('#address_voivodship').val()!='NO' )	{
//			alert($('#address_district').val());
			$('#address_voivodship').trigger('change');
		}
		if($('#business_area_voivodship').val()!='NO') {
			$('#business_area_voivodship').trigger('change');
		}

		if($('#business_area_voivodship').val()=='NO') {
			$('#business_area_district').attr("disabled","disabled");
		}


		$("#business_area_type_global").click(function() {
			$('#business_ares').hide();
			$('#business_area_district').attr("disabled","disabled");
			$("#business_area_voivodship").attr("disabled","disabled");
		});

		$("#business_area_type_local").click(function() {
			$("#business_area_voivodship").removeAttr("disabled")
			$('#business_area_district').removeAttr("disabled");
			$('#business_ares').show('slow');
		});

		if(typeof(commentAdded)=='undefined') {
			showCommentBlock=0;
		} else {
			showCommentBlock=1;
		}

		if(showCommentBlock) {
			$("#commentForm").show("slow");
		}

		if(typeof(startCatsAnimate)=='undefined') {
			startCatsAnimate=0;
		}

		if (startCatsAnimate) {
			$("#categories_firms").asmSelect({
				addItemTarget: 'bottom',
				animate: true,
				highlight: true,
				sortable: true
			})
		}

		if(typeof(startMapGoogle)=='undefined') {
			startMapGoogle=0;
		}

		if (startMapGoogle) {
			var geo;
			var mapa;
			mapaStart(startMapGoogle,startMapGoogleInfo,startMapGoogleInfoDwa,'first');
		}
		$("#nr_of_t_2").hide();
		$("#nr_of_t_3").hide();
		$(".page_navigation_gallery").click(function () {
			nrP=$('.tb_nr_p').attr('id').split('_');
			for (i = 0; i < nrP[1]; i++) {
				if(i==0) {
					$(this).removeClass('page_navigation_gallery').addClass('gallery_selected_nav');
				}
				if($(this).text()!=(1+i)) {
					$("#nr_of_t_" + (1+i)).hide();
					$('#nr_gall_p_'+ (1+i)).removeClass('gallery_selected_nav').addClass('page_navigation_gallery');
					$('#nr_gall_top_p_'+ (1+i)).removeClass('gallery_selected_nav').addClass('page_navigation_gallery');
				} else {
					$("#nr_of_t_" + (1+i)).show();
					$('#nr_gall_p_'+ (1+i)).removeClass('page_navigation_gallery').addClass('gallery_selected_nav');
					$('#nr_gall_top_p_'+ (1+i)).removeClass('page_navigation_gallery').addClass('gallery_selected_nav');
				}
			}
		});
    })

    function updateSelectedStyle() {
        $('.input :radio').removeClass('focused').next().removeClass('focused');
        $('.input :radio:checked').addClass('focused').next().addClass('focused');
    }

	function in_array( needle, haystack ){
		var a=false;
		for(var i=0;i<haystack.length;i++) {
			if(needle == haystack[i]) {
				a=true;
				break;
			}
		}
		return a;
	}

	function setleft() {
		rightHeight=$('.right').height();
		middleHeight=$('.middle_nrborder').height();
		leftheight=$('#left_h').height();
		if(middleHeight > rightHeight) {
				$("#left_min_h").height(middleHeight-leftheight-7);
		} else if(middleHeight < rightHeight) {
				$("#left_min_h").height(rightHeight-leftheight-2);
		}
	}

	function setright() {
		leftHeight=$('.right').height();
		middleHeight=$('.middle_nrborder').height();
		rightheight=$('#right_h').height();
		if(middleHeight > leftHeight) {
				if(middleHeight-rightheight) {
					$("#right_min_h").height(middleHeight-rightheight-17);
				}
		} else if(middleHeight < leftHeight) {
				if(leftHeight-rightheight) {
					$("#right_min_h").height(leftHeight-rightheight-12);
				}
		}
	}

	var pageSize;
	function loadPopup()
	{
		$('#popup_box_opacity').width(pageSize[0]);
		$('#popup_box_opacity').height(pageSize[1]);
		$('#popup_box_content').css('width','0px').css('height','0px');
		$('#popup_box_content > div').hide();
		$('#popup_box_row').css('top','155px').show();
		$('#popup_box_opacity').show();
	}

	function waitPopup()
	{
		$('#popup_loading').show();
	}

	function fillPopup(sourcediv)
	{
		var elem = $('#'+sourcediv);
		var newHeigh = $(elem).height(); // $(elem).css('height');
		var newWidth = $(elem).width(); // $(elem).css('width');
		scrollpopup(true,newHeigh);
		if(elem)
		{
			$('#popup_loading').hide();			
			$('#popup_box_content').animate({'width' : newWidth, 'height' : newHeigh}, 300, 'linear', function(){
				//sprawdzamy czy jest juz w odpowiednim miejscu
				if($('#popup_box_content div#'+sourcediv).length == 0)
				{
					var htmlData = $(elem).html();
					$('#'+sourcediv).remove();
					$('#popup_box_content').append("<div id='"+sourcediv+"'>"+htmlData+"</div>");
				}
				$('#popup_box_content .help_button_hide').click(function(){hidePopup();});
				$('#popup_box_content div#'+sourcediv).show();
				if(!(navigator.appName=='Microsoft Internet Explorer' && (' ' + navigator.appVersion).toString().indexOf('MSIE 6')))
					$('#popup_box_content').css('width','auto').css('height','auto');				
			});
		}
	}

	function hidePopup()
	{
		$('#popup_box_row').hide();
		$('#popup_box_opacity').hide();		
	}
	
	var lastscrolltime = new Date();

	var myScrollOffset = 150;

	function scrollpopup(force,height)
	{
		if(typeof(force)=='undefined') force = false;
		if(typeof(height)=='undefined') height = $('#popup_box_content').height();		
		var curtime = new Date();
		var diff = curtime - lastscrolltime;
		if(force || diff > 200)
		{
			myScrollOffset = (pageSize[3]-height)/2;
			var pos = getScrollXY();
			var top = pos[1]+myScrollOffset;
			$('#popup_box_row').css('top',top+'px');
			$('#popup_loading').css('top',top+30+'px');
		}
	}

	$(document).ready(function(){
		pageSize = getPageSize();
		$(window).scroll(function(){
			if($('#popup_box_opacity').css('display')=='block')
			{
				lastscrolltime = new Date();
				setTimeout('scrollpopup();',500);
			}
		});
		$(window).resize(function(){
			pageSize = getPageSize();
			$('#popup_box_opacity').width(pageSize[0]);
			$('#popup_box_opacity').height(pageSize[1]);
			scrollpopup(true);
		});
		$('#popup_box_opacity, #popup_box_row').click(function(){hidePopup();});
		$('#popup_box_content').click(function(e){e.stopPropagation();});
		var raterClick = function(){
			var star = this;
			var rel = $(star).attr('rel') ? $(star).attr('rel') : ';';
			var vote = rel.split(';')[0];
			var id = rel.split(';')[1];
			var ratingBlock = $(star).parents('div.ratingblock:first')[0];
			$.ajax({
				async: true,
					data: {vote : vote, id : id},
					dataType: 'html',
					beforeSend: function (XMLHttpRequest) {							
						$('li.active-rating',$(star).parents('ul:first')[0]).hide(); 
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {	
						$('li.active-rating',$(star).parents('ul:first')[0]).show(); 
					},
					success: function(data, textStatus)	{
						$(ratingBlock).html($(data).html());						
						$('div.ratingblock, a.rater').click(raterClick);
						$(ratingBlock).attr('title','Twoja ocena: '+vote);
					},
					type: 'POST',
					url: baseURL+"p-vote.html"
			});
			return false;
		};
		$('div.ratingblock a.rater').click(raterClick);
	});

