$(document).ready(function(){
    transferSelect();
    $("#saveSubmitBtn").hide();
    showHide('p1');
    showHide('p2');
    showHide('p3');
    showHide('p4');
    showHide('p5');

    if ($("#komunikatWyplac").length)
        showHide('komunikatWyplac');
    if ($("#transfer_errorMessage").length)
        showHide('transfer_errorMessage');
    var transferError = $("#transfer_errorMessage");
    if (transferError.length && transferError.html() != '') {
        errorWindow("Komunikat:", "transfer_errorMessage");
    }
    oferta_indywidualna();

    konfigurator_dedyk_init();

    administracja_backup();
    dedykiParametryLacza();

    if ($(".tableHL").length)
        dedykiTabeleHL();
    
    
    if($(".tip-tip").length)
       $(".tip-tip").tipTip({
           defaultPosition:'right'
       });
   
   
   $(".anchor-slide").anchorAnimate();
});

function dedykiTabeleHL() {
     // test
    var table = $('.tableHL');

    table.find('td').mouseenter(function() {
        var $this = $(this);
        var index = $this.parent().children().index($this) + 1;

        if(index == 1) return;

        table.find('td')
                .not('td.nohl')
                .not('td.dtLogo')
                .removeClass('highlight');
        
        table.find('tr > *:nth-child(' + index + ')')
                .not('td.nohl')
                .not('td.dtLogo')
                .not('th')
                .addClass('highlight');
    })
    .mouseleave(function() {
        table.find('.highlight').removeClass('highlight');
    })
}

//zapytaj-doradce

function fixedBox(id) {
    var object = $("#"+id);
    var rightValue = object.css('right');

    if (rightValue == '0px') {
        object.animate(
            {right: '-100'}
            ,"normal"
            ,function() {
                object.removeClass("greenBorder");
                object.addClass("grayBorder");
            }
        );
    } else {
        object.animate(
            {right: 0}
            ,"normal"
            ,function() {
                object.addClass("greenBorder");
                object.removeClass("grayBorder");
            }
        );
    }
}

function zapytajDoradceIN() {
    $("#zapytajDoradce_text").toggle();
    $("#zapytajDoradce_nr").toggle();
    return true;
}

function oferta_indywidualna() {
    if ($("#temat_wiadomosci").length) {
        indywidualna_selectVal();
        $("#temat_wiadomosci").change(function(){
            indywidualna_selectVal()
        });
    }
}

function indywidualna_selectVal() {
    if ($("#temat_wiadomosci :selected").val() == 'Oferta indywidualna') {
        $("#indywidualna_extend").show();
    } else {
        $("#indywidualna_extend").hide();
    }
}

function administracja_backup() {
    administracja_backup_poziom();
    $("#p_admin").change(function(){
        administracja_backup_poziom();
    });
}

function administracja_backup_poziom() {
    var a_admin =  $("#p_admin :selected").val();
    switch (a_admin) {
        case 'Wsparcie Administracyjne':
        case 'Pe\u0142na Administracja':
           $("#p_backup1").hide();
           $("#p_backup2").show();
        break;
        case 'Pakiet Podstawowy':
           $("#p_backup1").show();
           $("#p_backup2").hide();
        break;
    }
}

function dedykiParametryLacza() {
    dedykiParametryLacza_poziom();
    $("#transfer_ext").change(function(){
        dedykiParametryLacza_poziom();
    });
}

function dedykiParametryLacza_poziom() {
    var transfer_ext =  $("#transfer_ext :selected").val();
    switch (transfer_ext) {
        case 'GB':
            $("#GB_Text").show();
            $("#Mbps_Text").hide();
        break;
        case 'Mbps':
            $("#GB_Text").hide();
            $("#Mbps_Text").show();
        break;
    }
}

function skonfigurujPakiet(id) {
    $("#wybrany_pakiet").val(id);
    $("#wybrany_pakiet_s2").html(id);
    konfigurator_dedyk_cena();
}

function powrot_listaPakietow() {
    $("#konfiguracja_serwera_s1").show();
    $("#konfiguracja_serwera_s2").hide();
}

function konfigurator_dedyk_init() {
    if ($("#konfigurator_dedyk").length) {
        konfigurator_dedyk_cena();
        $("input[name='pakiet']").change(function(){
            konfigurator_dedyk_cena();
        });
        $("#system").change(function(){
            konfigurator_dedyk_cena();
        });
        $("#p_admin").change(function(){
            konfigurator_dedyk_cena();
        });
        $("#aktywacja").change(function(){
            konfigurator_dedyk_cena();
        });
    }
}
function roundit(val) {
    return Math.round(val*100)/100;
}

function konfigurator_dedyk_cena() {
    var pakiet          = $("#wybrany_pakiet").val();
    var system          = $("#system :selected").val();
    var p_admin         = $("#p_admin :selected").val();
    var aktywacja       = $("#aktywacja :selected").val();

    var cenaPakiet      = $("input[name='cena[pakiet"+pakiet+"]']").val();
    var cenaSystem      = $("input[name='cena["+system+"]']").val();
    var cenaAdmin       = $("input[name='cena["+p_admin+"]']").val();
    var cenaAktywacja   = $("input[name='cena["+aktywacja+"]']").val();
    var cenaLaczna      = 0;

    cenaLaczna          = roundit(cenaPakiet)
                        + roundit(cenaSystem)
                        + roundit(cenaAdmin);
                
    $("#aktywacja_cena").val(cenaAktywacja);
    $("#abonament_mc").html(cenaLaczna);
    $("#abonament_mc_input").val(cenaLaczna);
}

function kolokacjaLacze() {
    $("#lacze_jednostka").html($("#kolokacja_lacze :selected").val());
    $("#kolokacja_lacze").change(function(){
        $("#lacze_jednostka").html($("#kolokacja_lacze :selected").val());
    });
}

function kolokacjaZasilanie() {
    kolokacjaZasilenieInit();
    $("#zasilanie_niewiem").change(function(){
        kolokacjaZasilenieInit();
    });
}

function kolokacjaZasilenieInit() {
   var niewiem2 = $("#zasilanie_niewiem").attr("checked");
   if (niewiem2) {
       $("#zasilanie").attr('disabled', 'disabled');
       $("#zasilanie").addClass('vno');

   } else {
       $("#zasilanie").removeAttr('disabled');
       $("#zasilanie").removeClass('vno');
   }
}

function wersjaPMA() {
    var selected = $("#mysql_server :selected").val();
    
    if (selected == 'mysql.iq.pl') {
        $("#pma2").attr('checked', 'checked');
        $("#pma3").hide();
        $("#pma3_text").hide();
    } else {
        $("#pma3").show();
        $("#pma3_text").show();
        $("#pma3").attr('checked', 'checked');
    }
}

function zmianaPakietu(model, pakiet) {
    window.location = '/oferta/'+model+'/?pakiet='+pakiet;
}

function shClass(id) {
    $('.'+id).toggle();
}

function sharedTable() {
    if ($(".ch").length) 
        $(".ch").hide();
}

function ad_gallery_int(gWidth, gHeight) {
    var ad_Width    = (gWidth != undefined) ? gWidth : 840;
    var ad_Height   = (gHeight != undefined) ? gHeight : 550;
    
    $('.ad-gallery').adGallery({
        width: ad_Width,
        height: ad_Height,
        animate_first_image: true, // Should first image just be displayed, or animated in?
        animation_speed: 700, // Which ever effect is used to switch images, how long should it take?
        display_next_and_prev: true, // Can you navigate by clicking on the left/right on the image?
        display_back_and_forward: true, // Are you allowed to scroll the thumb list?
        scroll_jump: 0, // If 0, it jumps the width of the container
        slideshow: {
            enable: true,
            autostart: true,
            speed: 5700,
            start_label: 'Start',
            stop_label: 'Stop',
            stop_on_scroll: true, // Should the slideshow stop if the user scrolls the thumb list?
            countdown_prefix: '(', // Wrap around the countdown
            countdown_sufix: ')',
            onStart: function() {
              // Do something wild when the slideshow starts
            },
            onStop: function() {
              // Do something wild when the slideshow stops
            }
        },
        effect: 'fade', // or 'slide-vert', 'resize', 'fade', 'none' or false
        enable_keyboard_move: true, // Move to next/previous image with keyboard arrows?
        cycle: true // If set to false, you can't go from the last image to the first, and vice versa
        // All callbacks has the AdGallery objects as 'this' reference
    });
}
