var sSelectMaxHeight = '170px';
var funcboxServicesSelect = null;

$(function () {
    //hlavne menu
    $('#pageContentMenu > ul.menu div.item').hover(function () {
        $(this).addClass('item-hover');
    },function () {
        $(this).removeClass('item-hover');
    }).click(function () {
        $(this).addClass('item-active');
    });
    
    funcboxServicesSelect = $('#funcboxServicesSelect').clone();
    
    // STYLOVANE SELECTBOXY
    $('#funcboxOpenHours').sSelect({ ddMaxHeight: sSelectMaxHeight });
    $('#funcboxServicesSelect').sSelect({ ddMaxHeight: sSelectMaxHeight });
    $('#tyreSearchForm select').sSelect({ ddMaxHeight: sSelectMaxHeight });
    
    // TABULATORY v bloku SLUZBY
    $('ul#tabs-services li').click(function () {
        $('ul#tabs-services li').removeClass('active');
        $(this).addClass('active');
        if ($(this).is('.serviceSetType-1')) {
            $('#serviceTyp').val(1);
        } else if ($(this).is('.serviceSetType-2')) {
            $('#serviceTyp').val(2);
        }
        funcboxServicesUpdate($('#serviceTyp').val());
    });
    funcboxServicesUpdate($('#serviceTyp').val());
    
    // TABULATORY v bloku PNEU
    $('ul#tabs-tyres li').click(
        function () {
            $('ul#tabs-tyres li').removeClass();
            $(this).addClass('active');
        }
    );
    
    //VYHLADAVANIE PODLA GEOLOKALITY
    //vyhladavaci string
    pnbxSearchGeoShowHint();
    $('#pnbxSearchGeo').focus(pnbxSearchGeoHideHint);
    $('#pnbxSearchGeo').blur(pnbxSearchGeoShowHint);
    //vymazanie query stringu
    $('#pnbxSearchForm').submit(function () {
        pnbxSearchGeoHideHint();
        return true;
    });
    
    // ikonky sluzieb
    $('#funcboxServicesIcons div.serviceIcon').hover(function () {
        var self = $(this);
        self.addClass('serviceIcon-active');
        var bublina = $('#serviceBuble');
        if (!bublina.length) {
            bublina = $('<div></div>');
            bublina.attr('id','serviceBuble');
            var bbt = $('<div class="serviceBuble-top"></div>');
            var bbb = $('<div class="serviceBuble-bottom"></div>');
            var bbc = $('<div class="serviceBuble-content"></div>');
            bbt.append(bbb);
            bbb.append(bbc);
            bublina.append(bbt);
            $('body').append(bublina);
            bublina.hide();
        }
        //text bubliny
        $('.serviceBuble-content',bublina).text($('a span',self).text());
        var offset = self.offset();
        bublina.show();
        bublina.css('top',(offset.top - bublina.height()) + 'px');
        bublina.css('left',offset.left + 'px');
    },function () {
        var self = $(this);
        self.removeClass('serviceIcon-active');
        $('#serviceBuble').hide();
    }).click(function () {
        var self = $(this);
        var sid = $('a',self).attr('rel');
        if (self.is('.serviceIcon-selected')) {
            self.removeClass('serviceIcon-selected');
            $('#funcboxServs_'+sid).remove();
        } else {
            self.addClass('serviceIcon-selected');
            $('#funcboxServs_'+sid).remove();
            if (!$('#funcboxServs_'+sid).length) {
                $('#funcboxServicesIcons').append('<input type="hidden" name="servs[]" value="'+sid+'" id="funcboxServs_'+sid+'" />');
            }
        }
        return false;
    });
});

function pnbxSearchGeoShowHint() {
    var spfq = $('#pnbxSearchGeo');
    if (spfq.val() == spfq.attr('title') || spfq.val() == '') {
        spfq.addClass('hint').val(spfq.attr('title'));
    }
}
function pnbxSearchGeoHideHint() {
    var spfq = $('#pnbxSearchGeo');
    spfq.removeClass('hint');
    if (spfq.val() == spfq.attr('title')) {
        spfq.val('');
    }
}

function funcboxServicesUpdate(pneuboxType) {
    pneuboxType = parseInt(pneuboxType);
    
    $('#funcboxServicesIcons > div.serviceIcon').each(function () {
        var a = $('a',this);
        var servicePneuboxType = parseInt(a.attr('rev'));
        if (pneuboxType == (servicePneuboxType&pneuboxType)) {
            $(this).show();
        } else {
            $(this).hide().removeClass('serviceIcon-selected');
            $('#funcboxServs_' + a.attr('rel')).remove();
        }
    });
    
    $('#funcboxServicesSelect > option').remove();
    $('option',funcboxServicesSelect).each(function () {
        var self = $(this);
        if(self.attr('rel') == 'pt' + pneuboxType || self.attr('rel') == undefined){
            var option = $('<option value="' + self.val() + '">' + self.text() + '</option>');
            if(self.is(':selected')){
                option.attr('selected','selected');
            }
            $('#funcboxServicesSelect').append(option);
        }
    });
    $('#funcboxServicesSelect').resetSS({ ddMaxHeight: sSelectMaxHeight });
}

function TyreChangeType(typ){
    $.get(
       LANGROOT + "/tyres/change-type/"
       ,{
           typ: typ
       }
       ,function(result){
           $('#tyreSearchForm').html(result);
           $('#tyreSearchForm select').sSelect({ ddMaxHeight: sSelectMaxHeight });
       }
   );
   TyreSearchLoading();
}

function TyreChangeOption(){
    $.get(
       LANGROOT + "/tyres/change-option/",
       $('#pnbxTyreSearch').serialize(),
       function(result){
           $('#tyreSearchForm').html(result);
           $('#tyreSearchForm select').sSelect({ ddMaxHeight: sSelectMaxHeight });
       }
    );
    TyreSearchLoading();
}

function TyreSearchLoading(){
    $('#tyreSearchForm').html('<div class="tyreLoading"></div>');
}
