var NEWSLETTER_ONFOCUS = 0;

$(document).ready(function () { 
	$('a[rel]').lightBox();

	$('.easy_slideshow').each(function(){
		speed = parseInt($(this).attr('speed'));
		$(this).find('a').fadeOut(0);
		$(this).find('.index_0').fadeIn(1000);
		$(this).everyTime(parseInt($(this).attr('speed')), function(i){ index=i%$(this).find('a').length; $(this).find('a').fadeOut(1000); $(this).find('.index_'+index).fadeIn(1000); });
	}); 
	
	$('#menu li').mouseover(function(){ $(this).find('ul').css({'left':$(this).position().left+15+'px','height':'auto','top':$(this).position().top+23+'px'}); $(this).find('ul').stop().slideDown(500); });
	$('#menu li').mouseout(function(){ $(this).find('ul').stop().slideUp(500); });
	
	$('#menu li ul li').mouseover(function(){ $(this).find('ul').stop().slideDown(500); });
	$('#menu li ul li').mouseout(function(){ $(this).find('ul').stop().slideUp(500); });

	$('#newsletters input').focus(function(){ NEWSLETTER_ONFOCUS=1; });
	$('#newsletters input').blur(function(){ NEWSLETTER_ONFOCUS=0; });
	
	$('#newsletters').hover(function(){ $(this).attr('class','newsletters_a'); }, function(){ $(this).attr('class',''); if(NEWSLETTER_ONFOCUS!=1){ $('#newsletter_form').slideUp(500); } });
	
} );

$(document).click(function(){
	$('.nuolaidos').hide(0);
});

function loadEventsCalendar(date, direction, lng){
	$.ajax({
		url: 'ajax.php?content=events&ajax=1&date='+date+'&lng='+lng,
		cache: false,
		success: function(html){
			old_html = $("#events_content_container").html();
			if(direction==-1){
				new_html = '<table cellpadding="0" cellspacing="0"><tr><td>'+old_html+'</td><td>'+html+'</td></tr></table>';
			}else{
				new_html = '<table cellpadding="0" cellspacing="0"><tr><td>'+html+'</td><td>'+old_html+'</td></tr></table>';
			}
			if(direction==1){
				$("#events_content_container").css({'left':-1*$('#events_content').width()+'px'});
				$("#events_content_container").html(new_html);
				$("#events_content_container").animate({left:'0px'}, 3000, function(){ $("#events_content_container").html(html); $("#events_content_container").css({'left':'0px'}); });
			}else{
				$("#events_content_container").html(new_html);
				$("#events_content_container").animate({left:-1*$('#events_content').width()+'px'}, 1500, function(){ $("#events_content_container").html(html); $("#events_content_container").css({'left':'0px'}); });
			}
		}
	});
}

function show_more(id){
	$('#e_short_'+id).slideUp(500); 
	$('#e_more_'+id).slideDown(500);
}

function show_back(id){
	$('#e_more_'+id).slideUp(500);
	$('#e_short_'+id).slideDown(500); 
}

function show_nuolaida(id, obj){
	$('#'+id).css({'left':obj.position().left-517+obj.width()+'px','top':obj.position().top-90+obj.height()+'px'});
	$('#'+id).show(0);
}


function submitSearchForm(url, form){
	location = url + document.forms['search'].elements['q'].value;
}

function getMouseXY(e) {
  if (document.all) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return { x:tempX, y:tempY }
}

function ajax(a_url, a_obj, top_pos){
			$.ajax({
			  async: true,
			  url: a_url,
			  cache: false,
			  beforeSend: function(){
			  	//$("#"+a_obj).slideUp(1000, function(){  });
			  },
			  success: function(html){
			    $("#"+a_obj).html(html);
			    if(top_pos) $("#"+a_obj).animate({height:"150px", top:top_pos}, 1000);
			  },
			  //timeout: 20,
			  complete: function(html){
			    
			  }
			});
}

function post(a_url, a_obj, form){
			$.ajax({
			  type: "POST",
			  async: true,
			  url: a_url,
			  cache: false,
			  data: formData2QueryString(form),
			  beforeSend: function(){
			  	$("#"+a_obj).fadeOut(500, function(){  });
			  },
			  success: function(html){
			    $("#"+a_obj).html(html);
			    $("#"+a_obj).fadeIn(1000);
			  },
			  //timeout: 20,
			  complete: function(html){
			    
			  }
			});
}

function submit_email(email, lng, wrong_email){
	if(checkMail(email)){
		$.ajax({
		  type: "GET",
		  async: true,
		  url: "ajax.php?content=newsletters&lng="+lng+"&email="+email,
		  cache: false,
		  beforeSend: function(){
		  	$("#newsletter_status").show(500);
		  },
		  success: function(html){
		    $("#newsletter_status").html(html);
		    //$("#newsletter_form").hide(500);
		  },
		  //timeout: 20,
		  complete: function(html){
		    
		  }
		});
		
	}else{
		$("#newsletter_status").html(wrong_email);
		$("#newsletter_status").show(500);
	}
}

function setSelected(obj, val){
	for(i=0; i<obj.options.length; i++){
		if(obj.options[i].value==parseInt(val)) index=i;
	}
	if(index) obj.options[index].selected=true;
}

function formData2QueryString(docForm) {

        var strSubmit       = '';
        var formElem;
        var strLastElemName = '';
        
        for (i = 0; i < docForm.elements.length; i++) {
                formElem = docForm.elements[i];
                switch (formElem.type) {
                        // Text, select, hidden, password, textarea elements
                        case 'text':
                        case 'hidden':
                        case 'password':
                        case 'textarea':
				formElem.value = formElem.value.replace(/&/g, '%26');
				formElem.value = formElem.value.replace(/\+/g, '%2B');
				strSubmit += formElem.name + '=' + formElem.value + '&';
                        break;
                        case 'select-one':
                        	strSubmit += formElem.name + '=' + escape(formElem.options[formElem.selectedIndex].value) + '&';
                        break;
                        case 'checkbox':
                        	if(formElem.checked == true) strSubmit += formElem.name + '=' + escape(formElem.value) + '&';
                        break;
                        case 'radio':
	                        if(formElem.checked == true) strSubmit += formElem.name + '=' + escape(formElem.value) + '&';
                }
        }
        return strSubmit;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return { x:curleft, y:curtop };
}

function showPostForm(obj){
	document.getElementById('NL_overlay').style.display = 'block';
	document.getElementById(obj).style.display = 'block';
	try{
		document.getElementsByTagName("html")[0].style.overflow = "hidden";
	}catch(e){}
}

function closePostForm(obj){
	
	clearPostForm(obj);
	
	document.getElementById('NL_overlay').style.display = 'none';
	document.getElementById(obj).style.display = 'none';
	try{
		document.getElementsByTagName("html")[0].style.overflow = "";
	}catch(e){}
}

function clearPostForm(obj){
	document.getElementById(obj + '_form').reset();
}

function format_float(number){
	var str = new String();
	str = number + "";
	arr = str.split(/\./);
	if(arr.length>1){
		if((arr[1]).length<2)
			str = str + "0";
		if((arr[1]).length>2)
			str = Math.round(str*100)/100;
	}else{
		str = str + ".00";
	}
	return str;
}

function checkInt(x){
	var filter  = /^[0-9]*$/;
	if (filter.test(x)) return true;
	else return false;
}

function valid_email(email){
	if(!checkMail(email)) alert('Neteisingai įvestas el. paštas.');
	else return true;
}

function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function valid_number(x)
{
	var filter  = /^([0-9])*$/;
	if (filter.test(x)) return true;
	else return false;
}

function inputFocus(field){
	if(document.getElementById(field.name + '_').value != 1) 
		field.value='';
}

function inputBlur(field, text){
	if(document.getElementById(field.name + '_').value != 1 && field.value != '') 
		document.getElementById(field.name + '_').value = 1;
	if(document.getElementById(field.name + '_').value != 1) 
		field.value = text;
}

function show_hide(id){
	var obj = document.getElementById(id);
	if(obj.style.display=='none'){
		//obj.style.visibility='visible';
		//obj.style.position='static';
		obj.style.display = 'block';
	}else{
		//obj.style.visibility='hidden';
		//obj.style.position='absolute';
		obj.style.display = 'none';
	}
	
}

