function log(mssg){
    if(typeof(console) == 'object'){
        console.log(mssg);
    }
    return true;
}

var onPageLoadExecuted = false;

//main image
var mainImageIndex = 0;

//sukis tituliniame
var sukiaiIndex = 0;
var sukiaiTotal = 0;

//atlikti darbai tituliniame
var adIndex = 0;
var adTotal = 0;

$(document).ready(function() {
    if($("#intro").size() == 0  || $("#intro").css('display') == 'none'){
        DoOnPageLoad();
    }

    $("a.lightboxLink").fancybox({
        'hideOnContentClick': false,
        'showNavArrows': false,
        'cyclic': true
    });
    $(".next3 a").click(function(event) {
        event.preventDefault();
        $.fancybox.next();
    });
    $(".prev3 a").click(function(event) {
        event.preventDefault();
        $.fancybox.prev();
    });
});

function DoOnPageLoad(){

    var selection_timer = 1;
    var click_timer = 2;
    var selection_time = 5000;
    var selection = $(".selection a");
    var selection_count = selection.length;
    var ct = false;

    $("#contactForm, .selection a").hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        });

    $(".next").hover(
        function () {
            $(this).addClass("hover2");
        },
        function () {
            $(this).removeClass("hover2");
        });

    $(".prev").hover(
        function () {
            $(this).addClass("hover3");
        },
        function () {
            $(this).removeClass("hover3");
        });

    function play_selection(){
        selection_timer = setTimeout(function(){
            mainImageIndex = mainImageIndex + 1;
            if(mainImageIndex == selection_count){
                mainImageIndex = 0;
            }
            ShowMainImage();
            play_selection(true);
        }, selection_time);
    }

    selection.click(function(event) {
        event.preventDefault();
        clearTimeout(selection_timer);

        var current_index = $(".selection a").index(this);
        mainImageIndex = current_index;
        ShowMainImage();
        clearTimeout(click_timer);
        if(ct == false){
            click_timer = setTimeout(function(){
                ct = true;
                play_selection();

            }, 5000);
        }
    });

    ShowMainImage();
    //play_selection();
    
    setTimeout(function(){
		$("#contactForm, .selection a").eq(2).trigger('click');
	}, 5000);

    $("#contSukis a.prev").click(function(event) {
        event.preventDefault();
        if(sukiaiIndex > 0){
            sukiaiIndex = sukiaiIndex-1;
        } else {
            sukiaiIndex = sukiaiTotal-1;
        }
        ShowSukis();
    });

    $("#contSukis a.next").click(function(event) {
        event.preventDefault();
        if(sukiaiIndex < sukiaiTotal-1){
            sukiaiIndex = sukiaiIndex+1;
        } else {
            sukiaiIndex = 0;
        }
        ShowSukis();
    });

    sukiaiTotal = $("#contSukis h6 span").size();

    ShowSukis();


    $("#atliktiDarbai a.prev").click(function(event) {
        event.preventDefault();
        if(adIndex > 0){
            adIndex = adIndex-1;
        } else {
            adIndex = adTotal-1;
        }
        ShowAtliktiDarbai();
    });

    $("#atliktiDarbai a.next").click(function(event) {
        event.preventDefault();
        if(adIndex < adTotal-1){
            adIndex = adIndex+1;
        } else {
            adIndex = 0;
        }
        ShowAtliktiDarbai();
    });

    adTotal = $("#atliktiDarbai .imageHolder").size();

    ShowAtliktiDarbai();

    $(".button").hover(
        function () {
            $(this).find('del').addClass("btnActive");
        //alert($(this).attr('class'));
        },
        function () {
            $(this).find('del').removeClass("btnActive");
        }
        );
    onPageLoadExecuted = true;
}

function ResetMainImageSelector(){
    $(".selection a").removeClass('active');
}

var mainImgHolder = $("#contTitleImage, #contTitleImage2");
function ShowMainImage(){
    ResetMainImageSelector();
    var curr_link = $($(".selection a").get(mainImageIndex));
    if(curr_link){
        curr_link.addClass('active');

        mainImgHolder.animate({
            opacity: 0.05
        }, 'slow', function(){
            mainImgHolder.css('background-image', 'url('+curr_link.attr('href')+')');
            mainImgHolder.animate({
                opacity: 1
            }, 'slow');
        });


    }
}

function ShowSukis(){
    //alert(sukiaiTotal);
    ResetSukis();
    var curr_link = $($("#contSukis h6 span").get(sukiaiIndex));
    if(curr_link){
        curr_link.addClass('active');
    }
}

function ResetSukis(){
    $("#contSukis h6 span").removeClass('active');
}

function ShowAtliktiDarbai(){
    ResetAtliktiDarbai();
    var curr_link = $($("#atliktiDarbai .imageHolder").get(adIndex));
    if(curr_link){
        curr_link.addClass('active');
    }
}

function ResetAtliktiDarbai(){
    $("#atliktiDarbai .imageHolder").removeClass('active');
}

window._gmap = {};

window._gmap.lat = 55.709673;
window._gmap.lon = 21.144615;
window._gmap.map_id = 'gmap';
window._gmap.offices = [['Kad nebūtų šalta', 55.709673,21.144615 , 1]];

function showMap() {
    var myOptions = {
        zoom: 15,
        center: new google.maps.LatLng(window._gmap.lat, window._gmap.lon),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    }
    var map = new google.maps.Map(document.getElementById(window._gmap.map_id), myOptions);
    setMarkers(map, window._gmap.offices);
}

function setMarkers(map, locations) {
    var image = new google.maps.MarkerImage('images/icon_gmap.png',
        // This marker is 20 pixels wide by 32 pixels tall.
        new google.maps.Size(82, 78),
        // The origin for this image is 0,0.
        new google.maps.Point(0,0),
        // The anchor for this image is the base of the flagpole at 0,32.
        new google.maps.Point(38, 77));

    for (var i = 0; i < locations.length; i++) {
        var office = locations[i];
        var myLatLng = new google.maps.LatLng(office[1], office[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: image,
            title: office[0],
            zIndex: office[3]
        });
    }
}

if($('#gmap').length > 0){
    showMap();
}

if($('#vln_gmap').length > 0){
	window._gmap.lat = 54.6803092;
	window._gmap.lon = 25.294220399999972;
	window._gmap.map_id = 'vln_gmap';
	window._gmap.offices = [['Kad nebūtų šalta', 54.6803092, 25.294220399999972, 1]];
    showMap();
}

var introPicturesCount;
var introPicturesIndex = 0;
var introImagesSwitchTimer = null;

function IntroStart(){
    introPicturesCount = $('#intro img').size();
    $('#intro').css('height', $(window).height());
    $('#intro').css('display', 'block');
    $("#intro").click(function(event) {
        IntroStop();
    });
    introImagesSwitchTimer = setInterval('ChangeIntroPicture()', 1750);
}

function ChangeIntroPicture(){
    if(introPicturesIndex < introPicturesCount-1){
        ResetIntroImage();
        introPicturesIndex++;
        var curr_link = $($("#intro img").get(introPicturesIndex));
        if(curr_link){
            curr_link.addClass('active');
        /*
        if(introPicturesIndex < introPicturesCount){
                introImagesSwitchTimer = setInterval('ChangeIntroPicture()', 2000);
        }
        */
        }
    } else {
        IntroStop();
    }
}

function IntroStop(){
    stopIntroImagesSwitch();
    $('#intro').css('display', 'none');
    $('.withIntro').css('display', 'block');
    if(!onPageLoadExecuted){
        DoOnPageLoad();
    }
}

function ResetIntroImage(){
    $("#intro img").removeClass('active');
}

function stopIntroImagesSwitch(){
    if (introImagesSwitchTimer === null) {
        return;
    }
    clearInterval(introImagesSwitchTimer);
}

/* mindaugas */
$(function(){
	
    var fancy = $('.fancybox');
    if(fancy.length && typeof($.fancybox) == 'function'){
        fancy.fancybox();
    }

    var form = $('.form_holder .contactsForm');
    var mssg_holder = form.find('.mssg_holder');
    var inputs = form.find('input,textarea');

    if(form.length){
        form.submit(function(){

            var form_data = inputs.serialize();
            $('div.error').removeClass('error');
            mssg_holder.html('');

            $.ajax({
                type: "POST",
                url: window.location.href,
                data: {
                    'form': form_data,
                    'action': 'inquiry-form'
                },
                dataType: 'json',
                success: function(response){
                    if(typeof(response) == 'object'){
                        var status = parseInt(response.status);
                        if(status == 1){
                            form.find('.input').val('');
                            mssg_holder.html(response.mssg);
                            if(typeof(_gaq) == 'object' && typeof(_gaq.push) == 'function'){
								_gaq.push(['_trackPageview', '/forma-issiusta']);
							}
                            setTimeout(function(){
                                $.fancybox.close();
                            }, 2000);
                        }else if(status == 3){
                            var err_l = response.errors.length;
                            mssg_holder.html(response.mssg)
                            if(err_l > 0){
                                for(var i = 0; i < err_l; i++){
                                    var error_input = $('[name="'+ response.errors[i] +'"]', form);
                                    error_input.parents('.line').addClass('error');
                                }
                            }
                        }
                    }
                }
            });
            return false;
        });
    }

    /* show first gallery image in fancybox */
    var hash = window.location.hash.replace('#', '');
    if(hash.length){
        var f = $('.contItems .line a.fancybox:eq(' + String(hash - 1) + ')');
        if(f.length){
            f.trigger('click');
        }
    }

});


function ExtendActions(){
	
   }


function PageExtender(used_height){
    var elemet_id = "page_extender";
	var elem = null;
	if(elem = document.getElementById(elemet_id)){
	  //alert('Your resolution is '+screen.width+'x'+screen.height); 
	  
	  	var w, h;
		if (self.innerHeight) // all except Explorer
		{
			w = self.innerWidth;
			h = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	  //alert("W: "+ w + " H: " + h);
	  var diff = h - used_height;
	  //alert(diff);
	  if(diff < 0) diff = 0;
	  elem.style.height = diff +"px";
	  ExtendActions();
	}
   }
























