;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);
		function loadApp(param){
            hash = document.location.hash;
            $.ajax({
				url: "/l.php" + param,
				cache: false,
				success: function(html){
					$(".application").html(html);
				},
                complete: function(){
                    document.location.hash = hash;
                }
			});
		}
		function loadBreadCrumb(){
			$("#breadcrumb").html($("#hiddenBreadcrumb").html());
		}
		
		function loadSurvey(surveyNr){
			$.ajax({
				url: "/l.php/diskusijos.sv",
				cache: false,
				success: function(html){
					$("#surveyBox").html(html);
				}
			});
		}
		function report(href){
			rmsg = 'Vai J\u016Bs patie\u0161\u0101m v\u0113laties inform\u0113t par ne\u0113tisku zi\u0146ojumu?';
			if(confirm(rmsg)){
				$.ajax({
					url: "/l.php" + param,
					cache: false,
					success: function(html){
						$(".application").html(html);
					}
				});
			}
		}

$(document).ready(function(){
	$(".checkCtrl").livequery(function(){$(this).checkboxes()});
	$(".toggle-block").livequery(function(){
		$(this).hide();
	});
	$(".toggle").livequery(function(){
		$(this).css({'text-decoration': 'underline', 'cursor': 'pointer'})
	});
	$(".toggle").livequery(function(){
		$(this).hover(function(){ $(this).css('text-decoration', 'none') }, function() { $(this).css('text-decoration', 'underline') })
	});
	$(".toggle").livequery(function(){
		$(this).click(function(){
			$(this).next(".toggle-block").toggle()
		});
	});
	$("blockquote").append('<img src="css/images/blockquote2.gif" alt="quote" width="15" height="16" />');
	$('input[type=checkbox]').shiftClick();

// open popups
	$("a.minipopup").live('click', function(){
		pop($(this).attr('href'),'minipopup',300,280);
		return false;
	});
	$("a.external").livequery(function(){
		$('<img src="/html/css/images/ico.external.gif" class="ico-external" alt="external" width="12" height="10" />').insertAfter($(this));
	});
	$("div.svarbu").livequery(function(){
		$('<div class="end"></div>').appendTo($(this));
	});
	$("a.pdf").livequery(function(){
		$('<img src="/html/css/images/ico.pdf.gif" class="ico-pdf" alt="pdf" width="11" height="12" />').insertAfter($(this));
	});
	$(".button").livequery(function(){
		$(this).after('<div class="button-end"></div>');
	});
	$("button").livequery(function(){
		$(this).after('<div class="button-end"></div>');
	});
	$("a.pdf, a.external").live('click', function(){
		externLink($(this).attr('href'));
		return false;
	});

	//submit for startpage selector
    $("#go").change(function(){
        window.open($(this).val());
    });

    $("#goform").submit(function(){
        window.open($("#go").val());
        return false;
    });
    
	//added for meelespea ajax-functionality
	// loading legacy-app-links into .application div
	$(".application #text a:not(.noquery), .application #left_side a:not(.noquery)").live('click', function(event, element) {
		if($(this).attr('target')=='_blank'){
//			var forumlink = window.open();
//			forumlink.location.href = $(this).attr('href');
			return true;
		}else{
			var Suche = $(this).attr('href').indexOf(".php") + 4;
			link = $(this).attr('href').substring(Suche);
			loadApp(link);
			return false;
		}
	});
	$("a.report").live('click',function(){
		report($(this).attr('href'));
		return false;
	});
	$(".calday").live('click',function(){
		$.get("/l.php/cal.kalendorius/" + $(this).attr('id'),'',
				function (data) {
					$(".application").html(data);
				}
			);
		return false;
	});

	$("a.tigra").live('click',function(){
		var cal1 = new calendar1(document.forms['calCiklas'].elements['START_']);
		cal1.year_scroll = true;
		cal1.popup();
		return false;
	});

	$("a.reload").live('click', function(){
		$.get($(this).attr('href'),
			function (data) {
				window.location.reload();
			}
		);
		return false;
	});

	$("#userlogin").live('submit', function(){
		$.post("/l.php",$(this).serialize(),
			function (data) {
				loginsuccess = true;
				$(data).each(function(i,el){
					if($(el).is(".error")){
						loginsuccess = false;
						alert($(el).text());
					}
				});
				if(loginsuccess)
//					window.location.href = login_url;
					window.location.reload();
			}
		);
		return false;
	});

	// submit handler for legacy-app
	$("#searchForum").live('submit', function(){
		if($("ko").val()==''){
			return false;
		}else{
			$.get("/l.php/diskusijos.search", $(this).serialize(),
				function (data) {
					$(".application").html(data);
				}
			);
			return false;
		}
	});

	$('#goMonth_,#goYear_').live('change', function(){
		$.get("/l.php/cal.kalendorius",
			$("#changeDate").serialize(),
			function (data) {
				$(".application").html(data);
            }
        );
	});
	
	$("#pollVote").live('submit', function(){
		pollID = $("#poll_id").val();
			$.ajax({
				url: "/l.php",
				type: "POST",
				data: $(this).serialize(),
				success: function (data) {
					window.location.href = '/scripts/pages/lt/bendruomene/apklaus_rezultatai.php?apage=/poll.results/'+ pollID;
				}
			});
//		}
		return false;
	});
	
	$("#deletMsgForm, #forgotform, #searchUser, #contactList, #MsgForm, #msgReply, #newpassword, #setnewpassword, #newregistration, #editProfile, #registerMobile, #myQuotes").live('submit', function(){
		$.post("/l.php", $(this).serialize(),
			function (data) {
				$(".application").html(data);
			}
		);
		return false;
	});

	$("#formDay").live('submit', function(){
		dayToShow = $("#dayToShow").val();
		$.post("/l.php", $(this).serialize(),
			function (data) {
				$.get("/l.php/cal.kalendorius/" + dayToShow,
				function(data){
					$(".application").html(data);
				});
			}
		);
		return false;
	});

	$("#calCiklas").live('submit', function(){
		$.post("/l.php", $(this).serialize(),
			function (data) {
				window.location.href = '/scripts/pages/lt/kalendorius/mano_kalendorius/index.php';
			}
		);
		return false;
	});

	$("#editForm, #newMsgForm").live('submit', function(){
		if(validMsgForm(document.forms[$(this).attr('id')])){
			$.post("/l.php", $(this).serialize(),
				function (data) {
					$(".application").html(data);
				}
			);
			return false;
		}else
			return false;
	});
	$("#newTemaForm").live('submit', function(){
		if(validTemaForm(document.forms[$(this).attr('id')])){
			$.post("/l.php#create", $(this).serialize(),
				function (data) {
					$(".application").html(data);
				}
			);
			return false;
		}else
			return false;
	});

	$("#recommendToFriend, #abiinfo, #sendCalendar, #askTheDoctor").live('submit', function(){
		if(validateForm(document.forms[$(this).attr('id')])){
			$.post("/l.php", $(this).serialize(),
				function (data) {
					$(".application").html(data);
				}
			);
			return false;
		}else
			return false;
	});

	$("#unRegister").live('submit', function(){
		if(validRegForm(document.forms[$(this).attr('id')])){
			$.post("/l.php", $(this).serialize(),
				function (data) {
					$(".application").html(data);
				}
			);
			return false;
		}else
			return false;
	});

	$("#search_form").submit(function(){
		$("#WT\\.oss").val($("#q").val());
	});


	//formDay


	if ($("li#ciklas a").hasClass("ison")){
		$("ul#top-menu li#ciklas").css({backgroundPosition: "0 -35px"});
	}

		function checkCtrl2()
		{
				if ($('input[type=checkbox][checked]', 'table.checkCtrl2').size()>0)
				{
						$('#delAll').next("div.button-end").removeClass("disabled");
						$('#delAll').removeClass("disabled");
						$('#delAll').attr("disabled", false);
				}
				else
				{
						$('#delAll').next("div.button-end").addClass("disabled");
						$('#delAll').addClass("disabled");
						$('#delAll').attr("disabled", true);
				}

				if ($('tr.deleted input[type=checkbox][checked]', 'table.checkCtrl2').size()>0)
				{
						$('#revert').next("div.button-end").removeClass("disabled");
						$('#revert').removeClass("disabled");
						$('#revert').attr("disabled", false);
				}
				else
				{
						$('#revert').next("div.button-end").addClass("disabled");
						$('#revert').addClass("disabled");
						$('#revert').attr("disabled", true);
				}
		}
		$('table.checkCtrl2 input[type=checkbox]').click(function() {
				checkCtrl2();
		});
		checkCtrl2();
	if($('tr.moderator')){
		next = $('tr.moderator').not('.author').next().next();
		if ( $(next).hasClass('moderator') ) $('td', next).addClass('nobt');
	}

	// ---------- start (dl.slider) ----------
	if ($('dl.slider').size() > 0) {
		$('dl.slider dd').hide();
		$('dl.slider dt').addClass('collapsed');
		var lang = $('body').attr('class');
	 	var translations = {
			lt	: [" I\u0161skleisti visus atsakymus", "Suskleisti visus atsakymus"], 
			ee	:	["Ava k&otilde;ik", "Sule k&otilde;ik"],
			lv	: ["Expand all", "Collapse all"]
		};
		$('dl.slider').before('<a id="sliderButton" href="javascript:void(0)"><span>' + translations[lang][0] + '</span></a>');
		$('dl.slider dt').click(function() {
			$(this).next('dd').slideToggle('fast');
			$(this).toggleClass('expanded');
			check();
		});
		$('#sliderButton').click(function() {
			var expandedSize = $('dl.slider dt:not(.expanded)').size();
			if (expandedSize > 0) {
				$('dl.slider dd').slideDown("fast");
				$('dl.slider dt').addClass('expanded');
			} else {
				$('dl.slider dd').slideUp("fast");
				$('dl.slider dt').removeClass('expanded');
			}
			check();
		});
		var sliderButton = document.getElementById("sliderButton");
		function check() {
			var expandedSize = $('dl.slider dt:not(.expanded)').size();
			if (expandedSize > 0) {
				sliderButton.innerHTML = "<span>" + translations[lang][0] + "</span>";
			} else {
				sliderButton.innerHTML = "<span>" + translations[lang][1] + "</span>";
			}
		}
	}
	// ---------- end (dl.slider) ----------

	$("#searchFirst").click(function(){
		$(document).scrollTo('#forumSearchBox',500);
		$("#forumSearchBox").find("input").focus();
		return false;
		});


});

$.fn.DefaultValue = function(text){
		return this.each(function(){
				if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
						return;
				var fld_current=this;
				if(this.value=='') {
						this.value=text;
				} else {
						return;
				}

				$(this).focus(function() {
						if(this.value==text || this.value=='')
								this.value='';
				});

				$(this).blur(function() {
						if(this.value==text || this.value=='')
								this.value=text;
				});

				$(this).parents("form").each(function() {
						$(this).submit(function() {
								if(fld_current.value==text) {
										fld_current.value='';
								}
						});
				});
		});
};

$.fn.checkboxes = function() {
	var $checks = this.find("input[type=checkbox]").not(":disabled"),
			$control = this.find("a.checkAll"),
			$action = this.find("button"),
			$inputs = this.find("input.button"),
			total = $checks.length,
			on = false,
			current = 0;

	$checks.each(function() {
		if (!!this.checked) current++;
	});

	if (total === 0) $control.css({display: "none"});

	toggleAction();

	function toggleSingle(e) {
		if (e.target.type != "checkbox") return;
		!!e.target.checked ? current++ : current--;
		toggleAction();
	}

	function toggleAll() {
		on =! on;
		$checks.attr("checked", on);
		$action.attr("disabled", on);
		current = on ? total : 0;
		toggleAction();
	}

	function toggleAction() {
		$action.each(function() {
			var $current = $(this);
			if (current === 0) {
				$current.next("div.button-end").addClass("disabled");
				$current.addClass("disabled");
				$current.attr("disabled", true);
			} else {
				$current.next("div.button-end").removeClass("disabled");
				$current.removeClass("disabled");
				$current.attr("disabled", false);
			};
		});
		$inputs.each(function() {
			var $current = $(this);
			if (current === 0) {
				$current.next("div.button-end").addClass("disabled");
				$current.addClass("disabled");
				$current.attr("disabled", true);
			} else {
				$current.next("div.button-end").removeClass("disabled");
				$current.removeClass("disabled");
				$current.attr("disabled", false);
			};
		});
	}

	this.bind("click", function(e) { toggleSingle(e); });
	$control.bind("click", function(e) {
		toggleAll();
		return false;
	});
};

$.fn.shiftClick = function() {
	var lastSelected;
	var checkBoxes = $(this);

	this.each(function() {
		$(this).click(function(ev) {
			if (ev.shiftKey) {
				var last = checkBoxes.index(lastSelected);
				var first = checkBoxes.index(this);

				var start = Math.min(first, last);
				var end = Math.max(first, last);

				var chk = lastSelected.checked;
				for (var i = start; i < end; i++) {
					checkBoxes[i].checked = chk;
				}
			} else {
				lastSelected = this;
			}
		})
	});
};

function windowPrn(url) {
	if (url=='') {return;}
	var win=null;
	var popW=800;
	var popH=600;
	var left = (screen.width-popW)/2;
	var top = (screen.height-popH)/2;
	var settings ='height='+popH+',';
	settings +='width='+popW+',';
	settings +='top='+left+',';
	settings +='left='+top+',';
	settings +='toolbar=no,';
	settings +='scrollbars=yes,';
	settings +='resizable=yes';
	settings +='status=no';
	win=window.open(url,'print',settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
					oldonload();
			}
			func();
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	}
	else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

var onload_events=new Array();
var home_url="";
var lang="";

function loading(){
 for(i=0;i<onload_events.length;i++) eval(onload_events[i]);
}
function onloading(s){
	onload_events[onload_events.length]=s;
}

function pop(url,name,width,height){
	if (url=='') {return false;}
	if (name=='') name='';
	if (!(width>0)) width=300;
	if (!(height>0)) height=200;
//	url=home_url+url;
	var win=null;
	var settings ='height='+height+',';
	settings +='width='+width+',';
	settings +='toolbar=no,';
	settings +='scrollbars=yes,';
	settings +='resizable=yes';
	settings +='status=no';
	win=window.open(url,name,settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function popimg(ifile,ix,iy,ititle,stop) {
	var win;
	var sWidth;
	var sHeight;
	var NS = (document.layers) ? 1 : 0;
	win = window.open("","imageviewer","width="+ix+",height="+iy+",menubar=no,toolbar=no");
	win.document.open();
	win.document.write("<html><head><title>"+ititle+"</title>");
	win.document.write("</head><body>");
	win.document.write('<div style="position:absolute;width:'+ix+'px;height:'+iy+'px;left:0px;top:0px">');
	win.document.write("<img src="+ifile+"></div></body></html>");
	win.document.close();
	if (NS) {
		sWidth = win.innerWidth;
		sHeight = win.innerHeight;
	} else {
		sWidth = win.document.body.clientWidth;
		sHeight = win.document.body.clientHeight;
	}
	if(stop!=true && (sWidth!=ix || sHeight!=iy)) {
		win.close();
		setTimeout("popimage('"+ifile+"',"+ix+","+iy+",'"+ititle+"',true)", 250);
		return;
	}
	win.focus();
}

function load_editor(url){
	var width=600;
	var height=540;
	url=home_url+url;
	var weditor=window.open(url,'wineditor',"toolbar=no,scrollbars=yes,resizable=no,width="+width+",height="+height);
	weditor.focus();
}

var zymek=true;
function checkAll(forma){
	var elem=forma.elements;
	for(i=0;i<elem.length;i++){
		if (elem[i].name=="it[]") elem[i].checked=zymek;
	}
	zymek=!zymek;
}
function externLink(url) {
	var fenster = window.open(url);
}

function to_old_win(url)
{
	opener.location.href = url;
}

function dialog(message, url)
{
    if (confirm(message))
    {
	var Suche = url.indexOf(".php") + 4;
	link = url.substring(Suche);
        loadApp(link);
    }
    else
    {
        return false;
    }
}