﻿

<!--
$(document).ready(function() {
    if($('body').hasClass('product-b')) {
        $('img[src *= "btn"]').each(function() {
            var newsrc = $(this).attr("src");
            newsrc = newsrc.replace(/btn/ig,'blue-btn');            
            $(this).attr("src",newsrc);
        });
    }
});
// Add the ability to switch between tabs
(function($){
    var ControlTabs = function(target, options){
        this.target = $(target);
        this.options = $.extend({}, this.defaults, options);
        this.init();
    }
    
    ControlTabs.prototype = {
        defaults: {},                        
        init: function() {            
            var _this = this;            
            this.target.children().each(function() {                
                if($(this).children().prop('rel') !== '')
                    $('#'+$(this).children().prop('rel')).hide().addClass('tabContent');
            });
            this.target.children(this.options.tabsDOM).bind('click.controlTab',function() {
                if($(this).children().prop('rel') != '') {                    
                    window.location.hash = $(this).children().prop('rel');
                    _this.target.children().removeClass('active');
                    $('.tabContent').hide();
                    $('#'+$(this).children().prop('rel')).show(0,function() {
                        $(this).scrollTo();
                    });
                    $(this).addClass('active');
                    return false;
                }                
            });
            
            if(window.location.hash != '') {                
                var locHash = window.location.hash + '';
                locHash = locHash.substr(1);
                this.target.find('li a[rel="'+locHash+'"]').parent().trigger('click.controlTab');
            }  
            
            if(this.target.children('.active').length == 0) {
                this.target.children().eq(0).trigger('click.controlTab');
            }
        }
    }
    
    $.fn.controlTabs = function(options){
        return this.each(function(){
            new ControlTabs(this, options);
        });
    };
    
})(jQuery);
// Mark a single tab active based on the tab name
function productSetTab(tabCon) {
    $('.tabs').find('li').filter(function() {
        return ($(this).find('a span').text().toLowerCase() == tabCon.toLowerCase()) ? true : false;
    }).addClass('active');
}
// Make tables pretty 0 with green header when atleast one cell in the 1st row is not empty
$.fn.extend({
    makeTablePretty: function() {
        return this.each(function() {
            $(this).attr('border','0');
            var empty = true;
            $(this).find('tr:first td').each(function() {                            
                if($.trim($(this).text()) != '') empty = false;
            });
            if(!empty)
                $(this).find('tr:first').addClass('table-head');
            else
                $(this).find('tr:first').remove();
            $(this).find('tr:odd').addClass('odd');
        });        
    }
});
// Add AJAX support / validation to contact form
var cf = {
    // init the basic form supprt
    init: function() {
        this.form = $('#contact-form');        
        this.form.find('.btn-submit-2').click(function() {            
            cf.form.submit();
            return false;
        });
        this.form.find('input[name="first_name"]').setDefaultValue('Imię');
        this.form.find('input[name="last_name"]').setDefaultValue('Nazwisko');
        this.form.find('input[name="phone"]').setDefaultValue('Telefon, format np. 111222333');
        this.form.find('input[name="email"]').setDefaultValue('Adres e-mail');
        this.step1();
        this.step2();
    },
    // gather data form form fields
    getFormData: function(step) {
        cf.formData = {};
        cf.formData['step'] = step;
        cf.form.find('input[type="text"],input[type="hidden"]').each(function() {
            var name = $(this).attr('name');
            var val = (!$(this).hasClass('def')) ? $(this).val() : '';            
            cf.formData[name] = val;
        });            
    },
    // add AJAX support to step 1
    step1: function() {
        $('#contact-form .btn-submit').click(function() {            
            cf.getFormData(1);            
            $.ajax({
                url: '/wyslij-kontakt.html',
                type: 'post',
                data: cf.formData,
                dataType: 'json',
                beforeSend: function() {
                    if(cf.form.find('input.err').length > 0) return false;                    
                },
                success: function(re) {
                    if(re.result == 0 && re.errors.length > 0) {                    
                        cf.error(re);
                    } else {
                        cf.form.find('.error-info').hide();
                        cf.form.append('<div id="st2-remove" style="display: none"><a href="#cf-step2">KLIK</a></div>');                        
                        cf.form.find('#st2-remove a').fancybox({
                            'type' : 'inline',
                            'href' : '#cf-step2',
                            'hideOnOverlayClick': false,
                            'onStart': function() {
                                cf.form.find('#st2-remove').remove();
                            }
                        }).trigger('click');                                                
                    }
                }
            });        
            return false;
        });
    },
    // add AJAX support to step 2
    step2: function() {
        $('#contact-form').submit(function() {     
            cf.getFormData(2);
            $.ajax({
                url: '/wyslij-kontakt.html',
                type: 'post',
                data: cf.formData,
                dataType: 'json',                
                success: function(re) {
                    if(re.result == 0 && re.errors.length > 0) {                    
                        cf.error(re);
                    } else {
                        cf.form.find('input[type="text"]').each(function() {
                            $(this).val('').trigger('blur');
                        });
                        $('#cf-step2 input[type="checkbox"]').attr('checked','');                        
                        $.fancybox('<p style="text-align: center;"><br /><strong>Dziękujemy za wysłanie formularza.</strong><br /><br />Nasz Konsultant skontaktuje się z Państwem w ciągu 48h.<br /><br /></p>');
                    }
                }
            });
            return false;
        });
    },
    // show errors
    error: function(re) {
        $.each(re.errors,function() {
            var locV = ($('input[name="'+this+'"]').hasClass('def')) ? '' : $('input[name="'+this+'"]').val();
            var tmpVal = $('input[name="'+this+'"]').siblings('.error').text();                        
            $('input[name="'+this+'"]').addClass('err').val(tmpVal).bind('focus.cf',function() {     
                $(this).removeClass('err').val(locV).unbind('focus.cf');
                cf.moveCursorToEnd(this);
                if(cf.form.find('input.err').length == 0) cf.form.find('.error-info').hide();
            });            
        });
        cf.form.find('.error-info').show();
    },
    // move the cursor to teh end of teh input field - mostly for IE
    moveCursorToEnd: function(el) {
        var len = el.value.length || 0;
        if (len) {
            if ('setSelectionRange' in el) el.setSelectionRange(len, len);
            else if ('createTextRange' in el) {// for IE
                var range = el.createTextRange();
                range.moveStart('character', len);
                range.select();
            }
        }
    }
}
// Add basic AJAX supprt for news list
var newsList = {
    init: function() {
        if($('#news-ajax').length == 0) return false;        
        $.ajax({
            type: 'get',
            url: '/ajax/lista-informacji-produktowych/3.html',
            dataType: 'html',        
            success: function(re) {            
                $('#news-ajax').prepend(re);
                var maxH = 0;
                var $cols = $('#news-ajax').find('li');
                $cols.each(function() {
                    maxH = ($(this).height() > maxH) ? $(this).height() : maxH;
                });
                $cols.height(maxH);        
                $('#news-ajax').find('li:last').addClass('last');
            }
        });
    }
}
// Add javascript validation / default value for quick search input
var quickSearch = {
    init: function() {
        this.qs = $('#quick-search');
        this._setDefault();
        this._blockPost();     
    },
    _setDefault: function() {
        this.qs.find('.query').setDefaultValue('SZUKAJ');
    },
    _blockPost: function() {
        this.qs.submit(function() {
            if(quickSearch.qs.find('.query').hasClass('def')) return false;
            if(quickSearch.qs.find('.query').val().length <= 2) return false;
        });
    }
}
// Global functions - one big mess
$(document).ready(function() {
    
    // make breadcrubs shorter
    $('#breadcrumb').breadCutter({
        maxCharCount: 115,
        exceptionCrumbs: '0',
        dots: '<span>...</span>'
    });
    
    // activate teh tab controll
    $('.tabs').controlTabs();
    
    // Show news headline
    newsList.init();
    
    // make CTA items equal in width
    $('.cta-list').not('.cta-news').find('li:last').addClass('last').end().each(function() {
        var ch = $(this).children();
        switch(ch.length) {
            case 1:
                ch.width(909);
                break;;
            case 2:
                ch.width(454);
                break;;
            case 3:
                ch.width(302);
                break;;
        }
    });    
        
    $.when((function(){
        // make columns equal in height
        $('.cols:not(.not-equal)').each(function() {            
            var maxH = 0;
            var $cols = $(this).find('.col');
            $cols.each(function(i) {                
                maxH = ($(this).height() > maxH) ? $(this).height() : maxH;
            });            
            $cols.height(maxH);
            $cols.last().addClass('last');
        });
    })()).then(function(){
        // make content vertical align
        $('.content-middle').each(function() { /* only for content that has 1 child */        
            var newMiddle = ($(this).outerHeight() / 2) - ($(this).children().outerHeight() / 2);                    
            $(this).children().css('margin-top',newMiddle);
        });
    });
    
    
    // add link funcions to head shot lists
    $('.head-shot-list').find('li:last').addClass('last').end().each(function() {
      $(this).find('li').each(function() {
       if($(this).find('.info a.more').length > 0) {
        var myLink = $(this).find('.info a.more').attr('href');
        $(this).find('img').wrap('<a href="'+myLink+'" />');
        $(this).find('.info strong').wrap('<a href="'+myLink+'" />');
       }
      });
    });
    
    // add validation and def value to quick search
    quickSearch.init();    
    
    $('.jump-list').hover(function() {}, function() {
        $(this).hide();
    });
    $('#quick-jump .trigger').click(function() {
        $(this).siblings('.jump-list').show();
    });    
        
    $('#navi-top .submenu:not(.submenu-small)').each(function() {                        
        var left = $('<div class="submenu-col" />');        
        left.append($(this).find('.submenu-col-l'));
        var right = $('<div class="submenu-col" />');        
        right.append($(this).find('.submenu-col-r'));                
        $(this).find('.menu-content').prepend(right);
        $(this).find('.menu-content').prepend(left);        
    });
    $('#navi-top .submenu-small').each(function() {                        
        $(this).find('.submenu-col-null').children('a').remove();
        $(this).find('.submenu-col-null ul').unwrap();        
    });
    
    $('#navi-top li.lvl1').hover(function() {        
        $(this).addClass('hover');
        $(this).find('.submenu').show();        
    },function() {        
        $(this).removeClass('hover');
        $(this).find('.submenu').hide();        
    });
    $('#navi-top li.lvl1').bind('click',function() {
        $(this).trigger('hover');
    });
    $('.sm-lvl3').each(function() {                        
        var left = $('<ul class="sm-lvl3 submenu-col" />');        
        left.append($(this).find('.submenu-col-l'));
        var right = $('<ul class="sm-lvl3 submenu-col" />');        
        right.append($(this).find('.submenu-col-r'));                
        $(this).parent().prepend(right);
        $(this).parent().prepend(left);        
        $(this).remove();
    });
    
    $('body').delegate('#error-bar .close','click',function() {        
        $(this).parent().slideUp();
        return false;
    });        
    
    $('.content .section.visible,.content .section.hidden').each(function() {                        
        var dir = ($(this).hasClass('visible')) ? 'up' : 'down';                
        var dirText = (dir == 'up') ? 'Zwiń' : 'Rozwiń';        
        $(this).find('h2').css({'cursor':'pointer'}).append('<em class="'+dir+'">'+dirText+'</em>');
        if(dir == 'down') $(this).find('.section-content').hide();         
    });
    
    $('.content').delegate('h2','click',function() {
        var locTxt = ($(this).find('em').text() == 'Rozwiń') ? 'Zwiń' : 'Rozwiń';                    
        $(this).find('em').text(locTxt).toggleClass('up down').parents('.section').toggleClass('hidden visible').find('.section-content').slideToggle();            
    });
    
    // Make table pretty
    $('.pure-text table').makeTablePretty();
    
    
    if($('.move-outside').length > 0) {
        var tmpObj = $('.move-outside');
        $('.move-outside').parents('.section').after(tmpObj);
    }
    
    $('.content a[href *= ".doc"]').addClass('fileico doc').attr('target','_blank');
    $('.content a[href *= ".pdf"]').addClass('fileico pdf').attr('target','_blank');
    
    $('form .error').each(function() {
        $(this).wrapInner('<span class="txt" />');        
        var mySyb = $(this).siblings('input,textarea,select');
        if(mySyb.length > 0) $(this).offset({left: mySyb.offset().left + mySyb.width() + 10}).hide();
    });
    
    $('select').customDropdown({ optsScrollable: true});
    
    $('#job-offers-list .job-offer:odd').addClass('force-no-bg');    
    $('#job-offer-application').submit(function() {
        var form = this;
        $(this).ajaxSubmit({
            dataType: 'html',
            beforeSubmit: function() {
                $(form).find('.error').hide();
                return true;
            },
            success: function(re) {                
                re = re.replace(/(\<pre)[^>]*(>)/gi,'');
                re = re.replace(/\<\/pre>/gi,'');
                re = JSON.parse(re);
                if(re.result == 0 && re.errors.length > 0) {
                    $.each(re.errors,function() {
                        $(form).find('*[name="'+this+'"]').siblings('.error').show();
                    });
                } else {
                    window.location = '/info/oferty-pracy-thx.html';
                    return false;
                }            
            }
        });
        return false;
    });
    
    cf.init();
    
    $('body').delegate('.btn-more,.btn-szukaj','mouseover mouseout',function(e) {
        switch(this.tagName) {
            case 'INPUT' :
                var img = $(this);
                break;;
            case 'A' :
                var img = $(this).find('img')
                break;;
        }                
        switch(e.type) {
            case 'mouseover':        
                if(img.attr('src').match(/-active.png/ig) == null) {
                    img.attr('src',img.attr('src').replace('.png','-active.png'));                
                }
                break;;
            case 'mouseout' :
                img.attr('src',img.attr('src').replace('-active.png','.png'));
                break;;
        }        
    });
        
    $('.gfx-n-list ol').each(function() {        
        var blue = ($(this).parents('.product-b').length > 0) ? 'blue/' : '';        
        $(this).find('li').each(function(i) {
            i++;                     
            $(this).append('<img src="/img/gfx-ol-numbers/'+blue+pad(i,2)+'.png" alt="'+String(i)+'" class="gfx"/>');
        });
    });    
});
//-->

