function log(mssg){
    if(typeof(console) == 'object'){
        console.log(mssg);
    }
    return true;
}

jQuery('document').ready(function($){

	$("a.disabled").click( function(){
		document.getElementById('main_style').href = 'styles/nv_style.css';
		$("a.disabled").css("display", "none");
		$("a.enabled").css("display", "inline");
		return false;
	});
	
	$("a.enabled").click( function(){
		document.getElementById('main_style').href = 'styles/blank.css';
		$("a.disabled").css("display", "inline");
		$("a.enabled").css("display", "none");
		return false;
	});

    /* faq - starts */

    var default_faq = $('.faq-one-question a', $('div.faq'));
    var structure_faq = $('.faq-one-question a', $('div.structure_faq'));

    if(structure_faq.length == 0){
        default_faq.hover(
            function () {
                $(this).parents('.faq-one-question').addClass('hover');
            },
            function () {
                $(this).parents('.faq-one-question').removeClass('hover');
            }
            );

        default_faq.click(function(e) {
            e.preventDefault();
            var a = $(this).parents('.faq-one:first').toggleClass('active');
            /* if there is an gmap in collapsed content load it ONLY after expanding the content */
            if(a.find('#bigmap') && window.__gmap.big_map_loaded == false){
                window.__gmap.big_map_loaded = true;
                window.__gmap.icon = 'images/small_icon2.png';
                window.__gmap.map_id = 'bigmap';
                window.__gmap.locations = places;
                window.__gmap.zoom = 12;
                showMap();
            }
        });
    }else{
        structure_faq.hover(
            function () {
                $(this).parents('.faq-one-question').addClass('hover');
            },
            function () {
                $(this).parents('.faq-one-question').removeClass('hover');
            }
            );
        structure_faq.click(function(e) {
            if($(this).parents('.faq-one:first').is('.active')){
                e.preventDefault();
            }
            $(this).parents('.faq-one').toggleClass('active');
        });

        $('#structure_links a.slink').click(function(){
            var href = $(this).attr('href').split('#');
            var faq_id = parseInt(href[1].replace('faq_', ''));
            if(faq_id != ''){
                $('#faq_' + faq_id).addClass('active');
            }
        });

        var faq_id = parseInt(window.location.hash.replace('#faq_', ''));
        if(faq_id != ''){
            $('#faq_' + faq_id).addClass('active');
        }
    }


    /* faq - ends */

    /* external link - starts */

    $('a[rel~="external"]').click(function(){
        $(this).attr({
            'target':'_blank'
        });
    });

    /* external link - ends */

    /* search input - starts */

    $('.header-bottom-input input').focus(function() {
        if (this.value == this.defaultValue){
            this.value = '';
        }
    });
    $('.header-bottom-input input').blur(function() {
        if (this.value == ''){
            this.value = this.defaultValue;
        }
    });

    /* search input - ends */

    /* banners - starts */
    
    

    activeBanner = 0;
    bannerCount = $('.carousel').length;
    if (bannerCount > 1) {
        bannerTimer = setInterval(rotateBannerCarousel, 5000);
    }

    $('.carousel:eq(' + activeBanner + ')').show();

    function rotateBannerCarousel(){
        $('.carousel:eq(' + activeBanner + ')').animate({
            opacity: 0.05
        }, 750, function(){

            $(this).hide();

            activeBanner++;
            if (activeBanner >= bannerCount) {
                activeBanner = 0;
            }

            $('.carousel:eq(' + activeBanner + ')')
            .css('opacity', 0.05)
            .show()
            .animate({
                opacity: 1.0
            }, 750);
        });
    }

    /* banners - ends */

    /* contact form - starts */

    $('form#contact-form').submit(function(e){
        e.preventDefault();
        $.ajax({
            cache: false,
            dataType: 'json',
            data: $(this).serialize(),
            type: 'POST',
            url: window.location.toString(),
            beforeSend: function(){
                $('form#contact-form .err').removeClass('err');
                $('form#contact-form input, form#contact-form textarea, form#contact-form select').attr('disabled', 'disabled');
                $('form#contact-form .form-line-scs').hide();
                $('form#contact-form .form-line-err').hide();
            },
            error: function(){
                $('form#contact-form input, form#contact-form textarea, form#contact-form select').removeAttr('disabled');
                alert('Error!');
            },
            success: function(response){
                if (response.valid) {
                    $('form#contact-form .form-line-scs').show();
                }
                else {
                    $('form#contact-form input, form#contact-form textarea, form#contact-form select').removeAttr('disabled');
                    $('form#contact-form .form-line-err').show();
                    $.each(response.errors, function(i, field){
                        $('form#contact-form [name='+field+']').parent().parent().addClass('err');
                    });
                }
            }
        });
    });

    /* contact form - ends */

    /* subscribe form - starts */

    $('form#subscribe-form').submit(function(e){
        e.preventDefault();
        $.ajax({
            cache: false,
            dataType: 'json',
            data: $(this).serialize(),
            type: 'POST',
            url: window.location.toString(),
            beforeSend: function(){
                $('form#subscribe-form .err').removeClass('err');
                $('form#subscribe-form input, form#subscribe-form textarea, form#subscribe-form select').attr('disabled', 'disabled');
                $('form#subscribe-form .form-line-scs').hide();
                $('form#subscribe-form .form-line-err').hide();
            },
            error: function(){
                $('form#subscribe-form input, form#subscribe-form textarea, form#subscribe-form select').removeAttr('disabled');
                alert('Error!');
            },
            success: function(response){
                if (response.valid) {
                    $('form#subscribe-form .form-line-scs').show();
                }
                else {
                    $('form#subscribe-form input, form#subscribe-form textarea, form#subscribe-form select').removeAttr('disabled');
                    $('form#subscribe-form .form-line-err').show();
                    $.each(response.errors, function(i, field){
                        $('form#subscribe-form [name='+field+']').parent().parent().addClass('err');
                    });
                }
            }
        });
    });

    /* contact form - ends */

    /* faq form - starts */

    $('form#faq-form').submit(function(e){
        e.preventDefault();
        $.ajax({
            cache: false,
            dataType: 'json',
            data: $(this).serialize(),
            type: 'POST',
            url: window.location.toString(),
            beforeSend: function(){
                $('form#faq-form .err').removeClass('err');
                $('form#faq-form input, form#faq-form textarea, form#faq-form select').attr('disabled', 'disabled');
                $('form#faq-form .form-line-scs').hide();
                $('form#faq-form .form-line-err').hide();
            },
            error: function(){
                $('form#faq-form input, form#faq-form textarea, form#faq-form select').removeAttr('disabled');
                alert('Error!');
            },
            success: function(response){
                if (response.valid) {
                    $('form#faq-form .form-line-scs').show();
                }
                else {
                    $('form#faq-form input, form#faq-form textarea, form#faq-form select').removeAttr('disabled');
                    $('form#faq-form .form-line-err').show();
                    $.each(response.errors, function(i, field){
                        $('form#faq-form [name='+field+']').parent().parent().addClass('err');
                    });
                }
            }
        });
    });

/* faq form - ends */

});

//----------------------------------------------------------------------------------------------------------------------------------
/* google maps - starts */

window.__gmap = {};
window.__gmap.icon = 'images/icon2.png';
window.__gmap.map_id = 'map';
window.__gmap.locations = null;
window.__gmap.zoom = 15;
window.__gmap.big_map_loaded = false;

var offices = [
['Klaipėdos Vanduo', 55.70069909999999, 21.154038799999967, 1]
]

var places = [
['Klaipėdos tub. ligoninė', 55.7312951, 21.12253439999995, 1],
['Klaipėdos ligoninė', 55.7542214, 21.126648400000022, 2],
['Spec. kūdikių namai', 55.7708133, 21.08731369999998, 3],
['AB "Klaipėdos mediena"', 55.7226899, 21.156657499999937, 4],
['UAB "Minonas"', 55.7572292, 21.150441, 5],
['J.Zauerveino 1', 55.7140343, 21.12484219999999, 6],
['VŠĮ  Klaipėdos vaikų ligoninė', 55.7148931, 21.136144999999942, 7],
['I-ma vand., siurblys', 55.716292, 21.145669999999996, 8],
['l/d "Bitutė"', 55.7211003, 21.1122292, 9],
['V. Didžiojo gimnazija', 55.716784, 21.133550199999945, 10],
['Vitės pagr. mokykla', 55.9104289, 21.063450200000034, 11],
['AB "Klaipėdos Nafta"', 55.729336, 21.096715300000028, 12],
['Verslo ir tech. kolegija', 55.7249202, 21.172159999999963, 13],
['Ligoninės g. 7, gyv. namas', 55.7143385, 21.131348600000024, 14],
['Gulbių g. 8', 55.7180128, 21.116779700000052, 15],
['L/d "Boružėlė"', 55.7123719, 21.138234399999988, 16],
['"žemynos" m-la', 55.7280528, 21.13296850000006, 17],
['L/d "Obelėlė"', 55.7257342, 21.13379359999999, 18],
['L/d "Alksniukas"', 55.6721827, 21.166359100000022, 19],
['L/d "žuvėdra"', 55.6855956, 21.17905589999998, 20],
['AB "Klaipėdos duona"', 55.6948796, 21.174719900000014, 21],
['UAB "Klaipėdos maistas"', 55.6794461, 21.198693100000014, 22],
['I-mas spec. internatas', 55.6681651, 21.18259839999996, 23],
['"Svetliaèok" m-la', 55.68824309999999, 21.173671900000045, 24],
['L/d "Linelis"', 55.6682649, 21.18698619999998, 25],
['L/d  "Ąžuoliukas"', 55.6651327, 21.200414499999965, 26],
['L/d "Pingvinukas"', 55.7033407, 21.14628229999994, 27],
['ŠermukŠnių g. 10', 55.69864889999999, 21.138348500000006, 28],
['Nemuno g. 40', 55.6646793, 21.150589399999944, 29],
['AB "Klaipėdos pienas"', 55.6935252, 21.17656880000004, 30],
['I-mas spec. internatas', 55.6658334, 21.16487200000006, 31],
['III vandenvietė, siurblys', 55.6434674, 21.169656199999963, 32],
['Rimkai', 55.664645, 21.228715700000066, 33],
];

function showMap() {
    
    if(typeof(google) == 'undefined'){
        return true;
    }

    var myOptions = {
        zoom: window.__gmap.zoom,
        center: new google.maps.LatLng(55.70069909999999, 21.154038799999967),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    }
    var map = new google.maps.Map(document.getElementById(window.__gmap.map_id), myOptions);
    setMarkers(map, window.__gmap.locations);
}

function setMarkers(map, locations) {
    var image = new google.maps.MarkerImage(window.__gmap.icon,
        // This marker is 20 pixels wide by 32 pixels tall.
        new google.maps.Size(86, 86),
        // 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(39, 85));

    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(jQuery('#map').length > 0){
    window.__gmap.locations = offices;
    showMap();
}
if(jQuery('#bigmap').length > 0){
/* this map is loaded when collapsed content is expanded, leave this code just form sample ;)
    window.__gmap.icon = 'images/small_icon2.png';
    window.__gmap.map_id = 'bigmap';
    window.__gmap.locations = places;
    window.__gmap.zoom = 12;
    showMap();
    */
}
/* google maps - ends */

jQuery('document').ready(function($){
    var forms = jQuery('form.cform');
    var inputs = jQuery('input[type="text"]', forms);
    inputs.focus(function(){
        jQuery('input[name="asd"]', forms).val('178');
    });
});

