(function(h){h.fn.hoverAccordion=function(a){function m(d,f,b){var e=h(o).find("."+a.classOpen).closest("li").find("ul:first");if(false===e.is(":animated")){if(a.keepHeight==true)b=k;if(f.hasClass(a.classOpen)==false){d.children().show();d.animate({height:b},{step:function(c){d.height(b-c)},duration:a.speed});e.animate({height:0},{step:function(c){d.height(b-c)},duration:a.speed}).children().hide();f.addClass(a.classOpen).removeClass(a.classClosed);e.closest("li").removeClass(a.classActive).find("a:first").addClass(a.classClosed).removeClass(a.classOpen)}}} a=jQuery.extend({speed:"fast",activateItem:true,keepHeight:false,onClickOnly:false,classActive:"active",classHeader:"header",classHover:"hover",classOpen:"opened",classClosed:"closed"},a);var o=this,g=window.location.href,l=0,n=0,k=0;h(this).children("li").each(function(){var d=h(this),f=false;n++;var b=d.find("a:first").addClass(a.classHeader);if(b.length>0){b.hover(function(){b.addClass(a.classHover)},function(){b.removeClass(a.classHover)});var e=b.attr("href");if(e=="#")b.click(function(){this.blur(); return false});else if(a.activateItem==true&&g.indexOf(e)>0&&g.length-g.lastIndexOf(e)==e.length){f=true;d.addClass(a.classActive);b.removeClass(a.classClosed).addClass(a.classOpen)}}var c=d.find("ul:first");if(c.length>0){var i=c.height();if(k<i)k=i;a.onClickOnly==true?b.click(function(){m(c,b,i)}):b.hover(function(){l=setInterval(function(){m(c,b,i);clearInterval(l)},400)},function(){clearInterval(l)});if(a.activateItem==true)c.children("li").each(function(){var j=h(this).find("a").attr("href"); if(j)if(g.indexOf(j)>0&&g.length-g.lastIndexOf(j)==j.length){f=true;d.addClass(a.classActive);b.removeClass(a.classClosed).addClass(a.classOpen)}});else if(parseInt(a.activateItem,10)==n){f=true;d.addClass(a.classActive);b.removeClass(a.classClosed).addClass(a.classOpen)}}if(!f){b.removeClass(a.classOpen);if(c.length>0){c.children().hide();b.addClass(a.classClosed)}}});return this}})(jQuery);

$(document).ready(function(){
	
	/*$('#lang').hover(function(){
		$('#lang').stop().animate({top: 0}, 200);
	}, function(){
		$('#lang').stop().animate({top: -36}, 200);
	});*/
	setImage('Web dizainai/Advokato pagalba', 'Advokato pagalba', this, 6);
	$('#slideshow').nivoSlider({
		effect:'slideInLeft',
        slices:15,
        boxCols: 8,
        boxRows: 4,
        animSpeed:500,
        pauseTime:5000,
        startSlide:0,
        directionNav:false,
        directionNavHide:false,
        controlNav:true,
        keyboardNav:true,
        pauseOnHover:true,
        manualAdvance:false,
        captionOpacity:0.5,
        prevText: '',
        nextText: '',
	});
	
	$('#akordijonas').hoverAccordion({
		keepHeight : false,
		activateItem: 1,
		onClickOnly : true,
	});
	
	fader('#msg.ok', 4, 200);
	fader('#msg.error', 6, 100);
	fader('#msg.info', 4, 200);
	
	$('#work #hover').fadeTo(0, 0.7);
	/*$('#work').hover(function(){
		$('#work #hover').stop().fadeIn(250);
		
	}, function(){
		$('#work #hover').stop().fadeOut(250);
	});*/
	
    $('#subscribe').click(function(){
        subscribe($('#subscribe_val').val());
    });
    
    if($(window).height()<800) {
            $('#paslaugos_pic').hide();
        } else {
            $('#paslaugos_pic').show();
        }
    $(window).resize(function(){
        if($(window).height()<800) {
            $('#paslaugos_pic').hide();
        } else {
            $('#paslaugos_pic').show();
        }
    });
    
});

function subscribe() {
        $.ajax({
        url: '/prenumerata.php',
        type: 'POST',
        data: {
            email: $('#submail').val()
        },
        success: function(data) {
            if(data == "1") {
                alert('Dėkui, kad užsiprenumeravote.');
            } else {
                alert('Tai nepanašu į elektroninio laiško adresą.');
            }
        },
        error: function() {
            alert('Įvyko klaidą.');
        }
    });
}

function remove(){
	$("ul li ul li").removeClass('selected');
}

function removeIMG(){
	$("#count ul li").removeClass('selected');
}

function check_seo(a) {
    $(a).hide(0);
	if ($('#f_domain').val() == "") {
        if($('#f_domain').val() == "") {
            $('#msg_show').html('<div id="msg" class="error">Adresas būtinas.</div>');
            $('#f_domain').css({backgroundColor: '#FDC'});
        }
    } else {
        $('.dmm, .dmn').fadeOut(300, function(){
            $('.dmm').html('<strong>Kraunasi...</strong>');
            $('.dmm').fadeIn(300);
        });
		var values = {};
		var values = $('#ff').serialize();

        $.ajax({
            url: '/seo.php',
            type: 'POST',
            data: values,
            success: function(data) {
                if(data !== undefined) {
                    data = $.parseJSON(data);
                    console.log(data);
					$('.dmm, .dmn').fadeOut(300, function(){
                        $('.dmm').html($('#f_domain').val() + '<br><br><strong>Page rank: ' + data.pr + '/10</strong>');
                        if(data.keywords != "") {
                            $('.dmn').html('<table width="100%"><tr><td><strong>Raktažodis</strong></td><td><strong>Pozicija</strong></td></tr>' + data.keywords + '</table>');
                        } else {
                            $('.dmn').html('');
                        }
                        $('.dmm, .dmn').fadeIn(300);
                    });
                } else {
                    $('#msg_show').html('<div id="msg" class="error">Įvyko klaida. Pabandykite vėliau.</div>');
                }
                
            },
            error: function() {
                alert('Šiuo metu serveris per daug apkrautas. Prašom pabandyti vėliau.');
            }
        }, "json");
    }
}

function seo_field(xc) {
	var zz = 0;
	$('#seo_input :input').each(function(i, b){
		if($(b).val() == "") { zz = 1; }
	});
	if($(xc).val() != "" && zz == 0) {
		$('#seo_input').append('<input type="text" name="keyword[]" style="width: 300px;margin-left:0px" id="f_keyword" onChange="seo_field(this)"><br><br>');
	}
}

function setImage(url, title, a, id){
	remove();
    $('#count ul').html('');
	$(a).addClass('selected');
	//$("#work img").fadeOut(100, function(){
        $("#work #what").html(title);
		$("#work img").attr('src', '/works/' + url + '/' + id + '.jpg');
        //for(z=1;z<=count;z++) {
         //  $('#count ul').html($('#count ul').html() + '<li onClick="gotoIMG(\'' + cat + '\', \'' + name + '\', \'this\', ' + z + ')" style="cursor:pointer">' + z + '</li>');
        //}
		//$("#work img").fadeIn(100);
	//});
}

function gotoIMG(cat, name, a, id) {
    removeIMG();
	$(this).addClass('selected');
	$("#work img").fadeOut(100, function(){
		$("#work img").attr('src', 'works/' + cat + '/' + name + '/images/' + id + '.jpg');
		$("#work img").fadeIn(100);
	});
}

function fader(item, i, speed){
	if(i != 0){
		$(item).fadeTo(speed, 0.5, function(){
			$(item).fadeTo(speed, 1, function(){
				i = i - 1;
				fader(item, i, speed)
			})
		});
	}
}

function upload(){
    $('#ui').html('<div id="msg" class="info" style="width:300px">Failas siunčiamas į duombazę</div>');
    return true;
}

function kiek_kainuos_lt() {
    if ($('#f_name').val() == "" || $('#f_tel').val() == "") {
        if($('#f_name').val() == "") {
            $('#msg_show').html('<div id="msg" class="error">Vardas būtinas.</div>');
            $('#f_name').css({backgroundColor: '#FDC'});
        }
        if($('#f_tel').val() == "") {
            if($('#msg').html() == 'Vardas būtinas.') {
                $('#msg').html($('#msg').html() + '<br>' + 'Telefono numeris būtinas.')
            } else {
                $('#msg_show').html('<div id="msg" class="error">Telefono numeris būtinas.</div>');
            }
            $('#f_tel').css({backgroundColor: '#FDC'});
        }
    } else {
        $.ajax({
            url: '/kiek_kainuos.php',
            type: 'POST',
            data: {
                name: $('#f_name').val(),
                company: $('#f_company').val(),
                email: $('#f_email').val(),
                tel: $('#f_tel').val(),
                site: $('#f_site').val(),
                title: $('#f_title').val(),
                text: $('#f_text').val()
            },
            success: function(data) {
                if(data == 1) {
                    $('#msg_show').html('<div id="msg" class="ok">Jūsų žinutę gavome. Artimiausiu metu susisieksim.</div>');
                } else {
                    $('#msg_show').html('<div id="msg" class="error">Įvyko klaida. Pabandykite vėliau.</div>');
                }
            },
            error: function() {
                alert('Įvyko klaidą.');
            }
        });
    }
}

function kontaktai_lt() {
    if ($('#f_name').val() == "" || $('#f_mail').val() == "" || $('#f_text').val() == "") {
        $('#msg_show').html('<div id="msg" class="error"></div>');
        if($('#f_name').val() == "") {
            $('#msg').html($('#msg').html() + 'Vardas būtinas<br>');
            $('#f_name').css({backgroundColor: '#FDC'});
        }
        if($('#f_mail').val() == "") {
            $('#msg').html($('#msg').html() + 'El. pašto adresas būtinas<br>');
            $('#f_mail').css({backgroundColor: '#FDC'});
        }
        if($('#f_text').val() == "") {
            $('#msg').html($('#msg').html() + 'Tekstas būtinas');
            $('#f_text').css({backgroundColor: '#FDC'});
        }
    } else {
        $('#msg_show').html('');
        $.ajax({
            url: '/kontaktai.php',
            type: 'POST',
            data: {
                name: $('#f_name').val(),
                email: $('#f_mail').val(),
                tel: $('#f_tel').val(),
				imone: $('#f_imone').val(),
                text: $('#f_text').val()
            },
            success: function(data) {
                if(data == 1) {
                    $('#msg_show').html('<div id="msg" class="ok">Jūsų žinutę gavome. Artimiausiu metu susisieksime.</div>');
                } else {
                    $('#msg_show').html('<div id="msg" class="error">Įvyko klaida. Pabandykite vėliau.</div>');
                }
            },
            error: function() {
                alert('Įvyko klaidą.');
            }
        });
    }
}


function kiek_kainuos_en() {
    if ($('#f_name').val() == "" || $('#f_tel').val() == "") {
        if($('#f_name').val() == "") {
            $('#msg_show').html('<div id="msg" class="error">Name is required.</div>');
            $('#f_name').css({backgroundColor: '#FDC'});
        }
        if($('#f_tel').val() == "") {
            if($('#msg').html() == 'Vardas būtinas.') {
                $('#msg').html($('#msg').html() + '<br>' + 'Telephone number is required.')
            } else {
                $('#msg_show').html('<div id="msg" class="error">Telephone number is required.</div>');
            }
            $('#f_tel').css({backgroundColor: '#FDC'});
        }
    } else {
        $.ajax({
            url: '/kiek_kainuos.php',
            type: 'POST',
            data: {
                name: $('#f_name').val(),
                company: $('#f_company').val(),
                email: $('#f_email').val(),
                tel: $('#f_tel').val(),
                site: $('#f_site').val(),
                title: $('#f_title').val(),
                text: $('#f_text').val()
            },
            success: function(data) {
                if(data == 1) {
                    $('#msg_show').html('<div id="msg" class="ok">Your message received. In the near future contact.</div>');
                } else {
                    $('#msg_show').html('<div id="msg" class="error">There was an error. Please try again later.</div>');
                }
            },
            error: function() {
                alert('Įvyko klaidą.');
            }
        });
    }
}

function kontaktai_en() {
    if ($('#f_name').val() == "" || $('#f_mail').val() == "" || $('#f_text').val() == "") {
        $('#msg_show').html('<div id="msg" class="error"></div>');
        if($('#f_name').val() == "") {
            $('#msg').html($('#msg').html() + 'Name is required.<br>');
            $('#f_name').css({backgroundColor: '#FDC'});
        }
        if($('#f_mail').val() == "") {
            $('#msg').html($('#msg').html() + 'E-mail address is required<br>');
            $('#f_mail').css({backgroundColor: '#FDC'});
        }
        if($('#f_text').val() == "") {
            $('#msg').html($('#msg').html() + 'Message is required');
            $('#f_text').css({backgroundColor: '#FDC'});
        }
    } else {
        $('#msg_show').html('');
        $.ajax({
            url: '/kontaktai.php',
            type: 'POST',
            data: {
                name: $('#f_name').val(),
                email: $('#f_mail').val(),
				 imone: $('#f_imone').val(),
                tel: $('#f_tel').val(),
                text: $('#f_text').val()
            },
            success: function(data) {
                if(data == 1) {
                    $('#msg_show').html('<div id="msg" class="ok">Your message received. In the near future contact.</div>');
                } else {
                    $('#msg_show').html('<div id="msg" class="error">There was an error. Please try again later.</div>');
                }
            },
            error: function() {
                alert('Įvyko klaidą.');
            }
        });
    }
}

function getCat(el) {
    $.ajax({
        url: '/add.php',
        type: 'POST',
        data: {
            group: $(el).val()
        },
        success: function(data) {
            $('#add_grp').html(data);
            $('#add_about, #add_grp, #new_grp, #add_up, #add_ok').show();
        }
    });
}
function goTop() { $(window).scrollTop(0);}
function goBack() { history.go(-1);}


function openThisGallery(id){
	$("#darbai ul li").each(function(key, val){
		$(this).removeClass('active');
	});
	$("#link_"+id).parent().addClass('active');
	$(".darbaiGallery").each(function(key, val){
		//if($(this).attr('id') != id){
			$(this).animate({
			opacity: 0.0,
			}, 500, function(){
				if($(this).attr('id') != id){
					$(this).hide();
				}else{
					$(this).css('display', 'block');
					//console.debug(id+" "+$("#"+id).css('display'));
					$(this).animate({
						opacity: 1.0,
					  }, 500, function(){
						//$("#"+id).show();
					  });
				}
			});
		//}
	});
	
	
}

$(document).ready(function(){
	$('#darbai .darbaiGallery .gal-item').mouseover(function(){
		var offset = $(this).offset();
		if(offset.top+100 >= $(window).height()){
            $(this).children('.vid_block').css('top', -$(this).children('.vid_block').height()+163-4);
        }
       // console.log(offset.left);
       // console.log($(window).width());
        if(offset.left > $(window).width()/2){
			$(this).children('.vid_block').css('left', '-500px');
		}
		$(this).children('.vid_block').show();
		//$(this).parent().parent().css('background-color', '#c6001c');
	});
	$('#darbai .darbaiGallery .gal-item').mouseout(function(){
		$(this).children('.vid_block').hide();
		//$(this).parent().parent().css('background-color', '#c7c7c7');
	});
	openThisGallery('298');
     

});
