
//.................................................................................................


function class_item(obiekt, function_true, function_false) {
    
    this.obiekt = obiekt;
    this.stan   = false;
    this.timer  = null;
    
    
    var self = this;
    
    
    
    this.obiekt.mouseover(function(){
        
        if (self.stan === false) {

            self.stan = true;

            
            if (self.timer !== null) {
                
                clearTimeout(self.timer);
                self.timer = null;
                }
            else {
                function_true();
                }
            }
        });

    
    this.obiekt.mouseleave(function(){
        
        if (self.stan === true) {
            
            self.stan = false;
            
            self.timer = setTimeout(function(){
                
                self.timer = null;
                function_false();
                }, 200);
            }
        });
    }

//.................................................................................................



function class_menu() {

    this.wyswietlona = null;
    
    
    this.aktywna = null;
    this.main    = false;
    this.sub     = false;

    
    var self = this;
    
        
    jQuery('.menu_pion').each(function(key, val) {
        
        var obiekt = jQuery(val);
        var id     = obiekt.attr('id').substr(10);
        
        new class_item(obiekt,
            function(){
                self.refresh_menu(true, id, true);
                },
            function(){
                self.refresh_menu(true, id, false);
                }            
            );
        });
    
    
    jQuery('.menu_pion_subitem').each(function(key, val) {
        
        var obiekt = jQuery(val);
        var id     = obiekt.attr('id').substr(18);
        
        new class_item(obiekt,
            function(){
                self.refresh_menu(false, id, true);
                },
            function(){
                self.refresh_menu(false, id, false);
                }            
            );
        });
    }

//.................................................................................................

/*
    main = true / false
    id   = identyfikator elementu od 1 do 3
    hover_in = true / false
*/

class_menu.prototype.refresh_menu = function(main, id, hover_in) {
        
    /*
    this.aktywna = null;
    this.main    = false;
    this.sub     = false;
    */
    
                                //ze stanu może go wybić tylko nowy hover na innym elemencie
    
    if (this.aktywna !== id && hover_in === true) {
        
        this.aktywna = id;
        this.main    = false;
        this.sub     = false;
        
        
        if (main === true) {
            this.main = hover_in;
            }
        
        else {
            this.sub = hover_in;
            }
        }
    
    else if (this.aktywna === id) {
        
        if (main === true) {
            this.main = hover_in;
            }
        
        else {
            this.sub = hover_in;
            }
        }
    
    else {
        return;
        }
    
   
    
    if (this.main === false && this.sub === false) {
        
        this.hide();
        }
    
    else {
        
        if (this.aktywna !== null) {
            
            this.show(this.aktywna);
            }
        
        else {
            
            }
        }
    }


//.................................................................................................
//pokazuje określony subelement


class_menu.prototype.show = function(id) {
    
    if (this.wyswietlona === id) {
        
        return;
        }
        
    
    jQuery('.menu_pion_subitem').each(function(key, val) {
        
        var obiekt = jQuery(val);
        
        var odczytane_id = obiekt.attr('id').substr(18);
        
        
        if (odczytane_id === id) {
            jQuery(val).show();
            }
        
        else {
            jQuery(val).hide();
            }
        });
    
    
    
    this.wyswietlona = id;
    }


//.................................................................................................
//ukrywaeszystkie subelementy


class_menu.prototype.hide = function(id) {
    
    if (this.wyswietlona === null) {
        
        //console.warn('ignoruję prośbę o ukrycie wszystkich');
        return;
        }
    
    jQuery('.menu_pion_subitem').each(function(key, val) {
        
        jQuery(val).hide();
        });
    
    
    //console.info('ukrywam wszystkie');
    
    this.wyswietlona = null;
    }


//.................................................................................................
//uruchamiamy kontrolki fsitowe które obsługują formularze


function kontrolki_fsite() {

    var params = {

          galleryClass      : 'gallery'
        , galleryDontScale  : true


        /*        
        , checkboxChecked   : '/img/kontrolki/checkbox_on.gif'
        , checkboxUnchecked : '/img/kontrolki/checkbox_off.gif'
        */
        
        , radioChecked : '/img/formularz_zgloszeniowy/radio_on.png'
        , radioUnchecked : '/img/formularz_zgloszeniowy/radio_off.png'
        
        , fakeParams: {
              imie_nazwisko       : 'Imię i Nazwisko'
            , ulica_nr            : 'Ulica, nr'
            , kod_pocztowy_miasto : 'Kod pocztowy, Miasto'
            , email               : 'E-mail'
            , telefon             : 'Telefon'
            , imie_psa            : 'Imię psa'
            , wiek_psa            : 'Wiek psa'
            , plec_psa            : 'Płeć psa'
            , rasa                : 'Rasa'
            , opis_problemow      : 'Uwagi' + "\n" + 'Jeśli u psa występują jakieś problemy z zachowaniem, opisz jakie.'
            , kod_obrazkowy       : 'Wpisz kod z obrazka'
            
            , newsletter_email    : 'Twój e-mail'
            
            , tresc               : 'Treść wiadomości'
            
            , email_klienta       : 'Twój e-mail'
            }
        };

//            , opis_problemow      : 'Jeśli u psa występują jakieś problemy z zachowaniem - opisz jakie ?'     

    FSite2.extendForm(document, params);
    }


//.................................................................................................

function galerie_obsluga_ikony() {
    
    jQuery('.box_galeria_lista').each(function(key, val) {
        
        var ob = jQuery(val);
        var ikona = ob.children('.ico');
        
        ob.mouseover(function(){
            ikona.addClass('active');            
            });
        
        
        ob.mouseleave(function(){
            ikona.removeClass('active');
            });
        
        });
    }


//.................................................................................................

function submit(ob) {
    

    var i = 1;
    
    while (ob.attr('method') !== 'post') {
        
        ob = ob.parent();
        
        if (i > 10) {
            break;
            }
        
        i++;
        }
    
    ob.submit();
    }
//.................................................................................................

function remlamy_glowna() {
    
    if (jQuery('#remlamy_glowna').size() === 1) {
        
        var ob = jQuery('#remlamy_glowna');
        
        var height1 = ob.children('#remlamy_glowna_1').height();
        var height2 = ob.children('#remlamy_glowna_2').height();
        
        
        if (height1 > 0 || height2 > 0) {
            
            jQuery('#remlamy_glowna hr').show();
            
            jQuery('#remlamy_glowna_1').css('visibility', 'visible');
            jQuery('#remlamy_glowna_2').css('visibility', 'visible');
            }
        };
    }


//.................................................................................................
//z dziwnych powodów występują różnice w implementacji białych znaków


function compare(str1, str2) {
    
    for (var i=0; i<str1.length; i++) {
        
        var chr1 = Convert.toHex(str1[i]);
        var chr2 = Convert.toHex(str2[i]);
        
        
        if (chr1 === '00')
            chr1 = '20';
        
        if (chr2 === '00')
            chr2 = '20';
        
        if (chr1 !== chr2) {
            return false;
            }
        
        }
    
    return true;
    }


//.................................................................................................


var Convert = {
     chars: " !\"#$%&amp;'()*+'-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
     hex: '0123456789ABCDEF', bin: ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111'],
     decToHex: function(d){
          return (this.hex.charAt((d - d % 16)/16) + this.hex.charAt(d % 16));
     },
     toBin: function(ch){
          var d = this.toDec(ch);
          var l = this.hex.charAt(d % 16);
          var h = this.hex.charAt((d - d % 16)/16);
          var hhex = "ABCDEF";
          var lown = l < 10 ? l : (10 + hhex.indexOf(l));
          var highn = h < 10 ? h : (10 + hhex.indexOf(h));
          return this.bin[highn] + ' ' + this.bin[lown];
     },
     toHex: function(ch){
          return this.decToHex(this.toDec(ch));
     },
     toDec: function(ch){
          var p = this.chars.indexOf(ch);
          return (p <= -1) ? 0 : (p + 32);
     }
};


//.................................................................................................

function formularz_zgloszeniowy_skacz(selectbox) {
    
    
    var caption = jQuery('td.text div', selectbox).text();

    var select  = jQuery('select option', selectbox);
    
    select.each(function(key, val){
        
        var ob = jQuery(val);
        
        if (compare(caption, ob.text()) === true) {
            
            var url = ob.val();
            
            document.location = url;
            return false;
            }
        });
    }


//.................................................................................................

function formularz_zgloszeniowy() {
    
        
    var selectbox = jQuery('#szkolenie_box');
                                                                //gdy dynamicznie zostanie stworzony nowy element wyboru dropduwn-a to wtedy należy
                                                                //go ożywić i zareagować na jego zdarzenie click

    jQuery('#szkolenie_box .dropdown').live('click', function(event) {

        if (event.type == 'click') {
            
            formularz_zgloszeniowy_skacz(selectbox);
            }
        });
    
    }

//.................................................................................................

function formularz_zgloszeniowy_komunikaty() {
    
    
    var komunikat = jQuery('#formularz_zgloszeniowy_komunikaty');
    
    if (komunikat.size() > 0) {
        
        //console.log(komunikat.children('p').size());
        
        if (komunikat.children('p').size() > 0) {
            
            komunikat.show();
            }
        }
    
    }

//.................................................................................................

function dump(obiekt, nazwa) {
    
    console.groupCollapsed(nazwa);
    
    var key_function = []; 
    var key_object   = [];
    var key_string   = [];
    var key_number   = [];
    var key_boolean  = [];
    
    
    for (key in obiekt) {
        
        if (typeof(obiekt[key]) === 'function') {
            
            key_function.push(key);
            }
        
        else if (typeof(obiekt[key]) === 'object') {
            
            key_object.push(key);
            }
        
        else if (typeof(obiekt[key]) === 'string') {
            key_string.push(key);
            }

        
        else if (typeof(obiekt[key]) === 'number') {
            key_number.push(key);
            }
        
        else if (typeof(obiekt[key]) === 'boolean') {
            key_boolean.push(key);
            }
        
        
        else {
            console.log('key: ' + key + ' typeof: ' + typeof(obiekt[key]));
            }
        }
    


    var i = 0;

    
    console.groupCollapsed('object');
    
    for (i = 0; i < key_object.length; i++) {
                
        var key = key_object[i];
        console.info(key);
        }
    
    console.groupEnd();
    
    
    console.groupCollapsed('number');
    
    for (i = 0; i < key_number.length; i++) {
                
        var key = key_number[i];
        console.info(key + ' = ' + obiekt[key]);
        }
    
    console.groupEnd();
    
    
    
    console.groupCollapsed('string');
    
    for (i = 0; i < key_string.length; i++) {
                
        var key = key_string[i];
        console.info(key + ' = ' + obiekt[key]);
        }
    
    console.groupEnd();
    
    
    console.groupCollapsed('boolean');
    
    for (i = 0; i < key_boolean.length; i++) {
                
        var key = key_boolean[i];
        console.info(key + ' = ' + obiekt[key]);
        }
    
    console.groupEnd();
    

                                    //wyświetlamy funkcje        
    console.groupCollapsed('function');
    
    for (i = 0; i < key_function.length; i++) {
                
        var key = key_function[i];
        console.info(key);
        }
    console.groupEnd();

    
    
    console.groupEnd();
    }


//.................................................................................................


function formularz_przewin() {
    
    
    var warunek1 = jQuery('a[name="formularz"]').size() > 0;
    var warunek2 = jQuery('body.post').size() > 0;
    var warunek3 = jQuery('body.szkolenie').size() > 0;
    
    if (warunek1 && (warunek2 || warunek3)) {
        
        window.location.hash = 'formularz';
        }
    }


//.................................................................................................

jQuery(document).ready(function(){
    
    formularz_przewin();
    
    jQuery('a.submit').click(function(){
        
        var ob = jQuery(this);
        
        submit(ob);
        return false;
        });
    
    jQuery('input[type="password"]').each(function(key, val) {
        
        var ob = jQuery(this);
        
        ob.keydown(function(e){
            
            if (e.keyCode === 13) {
                submit(ob);
                return false;
                }
            
            });
        
        }); 
    
    
                                //uruchomienie klasy odpowiedzialnej za działanie menu
    new class_menu();
    
    
    //kontrolki_fsite();
    
    galerie_obsluga_ikony();
    
    remlamy_glowna();
    
    
    formularz_zgloszeniowy();
    
    formularz_zgloszeniowy_komunikaty();
    });


//.................................................................................................


