$(document).ready( function() {

    // simbols counter on textarea
    
		function counter() {
			var id = $(this).attr("id");
			var val = $(this).val();
			var max = parseInt($("#"+id+"_max").html());
			var cur = val.length;

			if (cur>=max) {
				var new_val = val.substring(0,max);
				$(this).val(new_val);
				cur = max;
			}
			$("#"+id+"_count").html(cur.toString());
			
			return true;
		}
		
		$('#iesnieguma-forma textarea').each(function(){
				$(this).keyup(counter);
		});
		
		$('#iesnieguma-forma input').keyup(counter);
		
		
		// aptaujas bloki show-hide

		var showTitle = 'Hide';
		var hideTitle = 'Show';
		$('.filter .toggler a').click(function() {
			var contentBlock = $(this).parent().next()
			
			if(contentBlock.is(':hidden')) {                   // aizveerts
				$('.filter .text:visible')[$.browser.msie?'hide':'slideUp']();
				$('.filter .toggler a').attr('title',hideTitle);
				// contentBlock.slideDown(); - for all browsers
				contentBlock[$.browser.msie?'show':'slideDown']();
				$(this).attr('title', showTitle);
			}
			else {                                            // atveerts
				contentBlock[$.browser.msie?'hide':'slideUp']();
				$(this).attr('title', hideTitle);
			}
			return false;
		});


		// opens print window
		
		$('#druka').click(function() {
				window.print();
		});
		
		$('.draugiem_say').click(function(){
			window.open(this.href, '', 'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400');
			return false;
		});


		// close pop-up window
		
		$('#close').click(function() {
				window.close();
		});


		// open pop-up page to print
		
		$("a[rel='pop-up']").click(function() {
					var features = "height=700,width=800,scrollTo,resizable=1,scrollbars=1,location=0";
					newwindow = window.open(this.href, 'Popup', features);
					return false;
		});

		// hide show text in input and textarea

		$('.hidde').focus(function() {
			// clear value if current value is the default
			if($(this).val() === this.defaultValue) {
				$(this).val("").removeClass('default');
			}
		})
		.blur(function() {
			// restore to the default value if current value is empty
			if($(this).val() == "") {
				$(this).addClass('default').val(this.defaultValue);
			} else {
				$(this).removeClass('default');
			}
		});

		// strukturas plus/minus list
		
		$('.struktura a.icon').click(function(){
			var toggleParentClass = true;

			// to close most top node;
			if($(this).parent().parent().parent().hasClass('struktura') && $(this).parent().hasClass('active')){
				toggleParentClass = false;
			}
			if(!$(this).parent().parent().parent().hasClass('active')){
				$(".struktura ul").parent("li").removeClass('active');
			}
			if(toggleParentClass){
				$(this).parent().toggleClass('active');
			}
			return false;
		});
		
		$(".struktura ul").parent("li").addClass('ico-plus');
		
});



