// DOM loaded
$(document).ready(function() {
    
	// reset
	jQuery.fn.reset = function () {
	  $(this).each (function() { this.reset(); });
	}
	
	// custom fade animation
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	
	$('#uzsakymas_submit').click(function() {
		process_uzsakymas();
		return false;
	});
	
	$('#uzsakymas_reset').click(function() {
		$('#forma_uzsakymas').reset();
		return false;
	});
	
	// pasikeite gatves pavadinimas
	$('#gatve').change(function() {
		$('#namas').load('/content/ajax_namai.tpl', {'gatve':$('#gatve').val()}, function(){
			$("#namas option:first").attr("selected", true);
		});
		return false;
	});
	
	// visuose trijuose jau padarytas pasirinkimas
	$('#gatve, #namas, #make').change(function() {
		var gatve = $('#gatve').val();
		var namas = $('#namas').val();
		var vart = $('#make').val();
		if(gatve!="" && namas!="" && vart!="") {
			$('#type').load('/content/ajax_tipas.tpl', {'gatve':gatve,'namas':namas,'vart':vart}, function(){
				$("#type option:first").attr("selected", true);
			});
		}
		return false;
	});
	
	// visuose trijuose jau padarytas pasirinkimas
	$('#gatve, #namas, #make').change(function() {
		var gatve = $('#gatve').val();
		var namas = $('#namas').val();
		var vart = $('#make').val();
		if(gatve!="" && namas!="" && vart!="") {
			$('#type').load('/content/ajax_tipas.tpl', {'gatve':gatve,'namas':namas,'vart':vart}, function(){
				$("#type option:first").attr("selected", true);
			});
		}
		return false;
	});
	
	// galimi planai
	$('#type, #make').change(function() {
		var tipas = $('#type').val();
		var vart = $('#make').val();
		if(tipas!="" && vart!="") {
			$('#model').load('/content/ajax_planai.tpl', {'tipas':tipas,'vart':vart}, function(){
				$("#model option:first").attr("selected", true);
			});
		}
		return false;
	});
	
	// kaina
	$('#model').change(function() {
		var planas = $('#model').val();
		var tipas = $('#type').val();
		var vart = $('#make').val();
		if(planas!="") {
			$.post('/content/ajax_kaina.tpl', {'tipas':tipas,'vart':vart,'planas':planas}, function(kainos){
				$("#kaina").val($.trim(kainos));
			});
		}
		return false;
	});

});




// tikrinam el pasta
function check_email(str) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	(filter.test(str)) ? testresults=true : testresults=false;
	return (testresults)
}

// karjeros forma
function process_uzsakymas() {
	var forma = $('#forma_uzsakymas');
	var geri = true;

	forma.children(":input").each(function(i, e) {
		el = $(e);
		if(el.attr('name')!='asmens_kodas' && el.attr('name')!='text') {
			if($.trim(el.val()) == '') {
				alert('Užpildykite visus laukelius.');
				el.focus();
				geri = false;
				return false;
			} else if(el.attr('name') == "email" && check_email(el.val()) == false) {
				alert('Netinkamas el. paštas.');
				el.focus();
				geri = false;
				return false;
			}
		}
		
	});

	if (geri) forma.submit();
	return false;
}





function prisijungimo_forma(){
if (document.search.query.value=="") {document.search.query.value=document.search.query.defaultValue;}
}
function prisijungimo_forma2(){
if (document.naujienos.email.value=="") {document.naujienos.email.value=document.naujienos.email.defaultValue;}
}
//easing equation, borrowed from jQuery easing plugin
        //http://gsgd.co.uk/sandbox/jquery/easing/
        $.easing.easeOutQuart = function (x, t, b, c, d) {
            return -c * ((t=t/d-1)*t*t*t - 1) + b;
        };

        jQuery(function( $ ){
            /**
             * Most jQuery.serialScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
             * @see http://flesler.webs.com/jQuery.ScrollTo/
             * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.serialScroll.
             */

            /**
             * The plugin binds 6 events to the container to allow external manipulation.
             * prev, next, goto, start, stop and notify
             * You use them like this: $(your_container).trigger('next'), $(your_container).trigger('goto', [5]) (0-based index).
             * If for some odd reason, the element already has any of these events bound, trigger it with the namespace.
             */

            /**
             * IMPORTANT: this call to the plugin specifies ALL the settings (plus some of jQuery.ScrollTo)
             * This is done so you can see them. You DON'T need to specify them all.
             * A 'target' is specified, that means that #screen is the context for target, prev, next and navigation.
             */


            /**
             * No need to have only one element in view, you can use it for slideshows or similar.
             * In this case, clicking the images, scrolls to them.
             * No target in this case, so the selectors are absolute.
             */

            $('#slideshow_0').serialScroll({
                items:'li',
                prev:'#screen_0 a.prev',
                next:'#screen_0 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });
            $('#slideshow_1').serialScroll({
                items:'li',
                prev:'#screen_1 a.prev',
                next:'#screen_1 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });
            $('#slideshow_2').serialScroll({
                items:'li',
                prev:'#screen_2 a.prev',
                next:'#screen_2 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });
            $('#slideshow_3').serialScroll({
                items:'li',
                prev:'#screen_3 a.prev',
                next:'#screen_3 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });

            $('#slideshow').serialScroll({
                items:'li',
                prev:'#screen a.prev',
                next:'#screen a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });

            $('#slideshow2').serialScroll({
                items:'li',
                prev:'#screen2 a.prev',
                next:'#screen2 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });

            $('#slideshow3').serialScroll({
                items:'li',
                prev:'#screen3 a.prev',
                next:'#screen3 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });

            $('#slideshow4').serialScroll({
                items:'li',
                prev:'#screen4 a.prev',
                next:'#screen4 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });

            $('#slideshow5').serialScroll({
                items:'li',
                prev:'#screen5 a.prev',
                next:'#screen5 a.next',
                axis:'x',
                offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
                start:0, //as we are centering it, start at the 2nd
                duration:1200,
                force:true,
                stop:true,
                lock:false,
                cycle:false, //don't pull back once you reach the end
                easing:'easeOutQuart', //use this easing equation for a funny effect
                jump: false //click on the images to scroll to them
            });
            /**
             * The call below, is just to show that you are not restricted to prev/next buttons
             * In this case, the plugin will react to a custom event on the container
             * You can trigger the event from the outside.
             */

            var $news = $('#news-ticker');//we'll re use it a lot, so better save it to a var.
            $news.serialScroll({
                items:'div',
                duration:700,
                force:true,
                axis:'y',
                lazy:true,//NOTE: it's set to true, meaning you can add/remove/reorder items and the changes are taken into account.
                interval:5000, //yeah! I now added auto-scrolling
                step:2 //scroll 2 news each time
            });

            /**
             * The following you don't need to see, is just for the "Add 2 Items" and "Shuffle"" buttons
             * These exemplify the use of the option 'lazy'.
             */
            $('#add-news').click(function(){
                var
                    $items = $news.find('div'),
                    num = $items.length + 1;

                $items.slice(-2).clone().find('h4').each(function(i){
                    $(this).text( 'News ' + (num + i) );
                }).end().appendTo($news);
            });
            $('#shuffle-news').click(function(){//don't shuffle the first, don't wanna deal with css
                var shuffled = $news.find('div').get().slice(1).sort(function(){
                    return Math.round(Math.random())-0.5;//just a random number between -0.5 and 0.5
                });
                $(shuffled).appendTo($news);//add them all reordered
            });
        });


function prideti_balus(kanalo_id,balas){
    var kanalo_id2 = "#kanalas_"+kanalo_id;
    $(kanalo_id2).load("/content/star.tpl",{kanalo_id:kanalo_id,balas: balas });
    var oho = "#oho_"+kanalo_id;
    $(oho).load("/content/star2.tpl",{kanalo_id:kanalo_id,balas: balas });

}

function submit2() {
    document.client2.code.value = document.naujienos.email.value;
    document.client2.submit();
}





