

$(document).ready(function(){
    $('#menu-upper-main').append('<div id="slider_up"></div>');
    var slider_actual_pos = -40;

    setTimeout(function(){
        if ( $('#menu-upper-main>.current-menu-item').size() > 0 ) {
            var add_slider_middle = $('#menu-upper-main>.current-menu-item').width() / 2;
            slider_actual_pos = $('#menu-upper-main>.current-menu-item').offset().left - $('#menu-upper-main').offset().left + add_slider_middle;
            $('#slider_up').css('left', slider_actual_pos);
        }
    }, 100);

    $('#slider_up').css('left', slider_actual_pos);

    $('#menu-upper-main').mousemove(function(event){
        var actual = event.clientX - $('#menu-upper-main').offset().left - 10 ;

        if ( actual < 1 || actual > 523)
            return;

        $('#slider_up').css('left', actual);
    });
    $('#menu-upper-main').mouseenter(function(event){
        $('#slider_up').stop();
    });
    $('#menu-upper-main').mouseleave(function(event){
        $('#slider_up').animate({
            left: slider_actual_pos
        }, 1000);
    });

    $('#form_submit').mousedown(function(){$(this).css('margin-top', '3px');});
    $('#form_submit').mouseup(function(){$(this).css('margin-top', '0px');});

    if ( $('#wgallery').size() > 0 ) {
        var gallery_pos = 0;
        var counter = $('#wgallery a').size();
        var images = [];
        var anchors = [];

        $('#wgallery a').each(function(){
            anchors.push( $(this).attr('href') );
            images.push( $(this).find('img').attr('src') );
        });

        $('#wgallery').html('');
        var box = $('<div id="gallery_slider_box"></div>');
        var box2 = $('<div id="gallery_slider_box_inside"></div>');
        var img_2;
        for(var i = 0; i<counter; i++) {
            box2.append('<a href="'+anchors[i]+'" rel="lightbox" class="gallery_over" style="background-image: url(http://tronik.gogoweb.pl/wp-content/themes/tronik/images/gallery_over.png);">\n'+
            '<img src="'+images[i]+'" />\n'+
            '</a>');
        }
        $(box2).css('width', counter*120);



        box.append(box2);

        if ( counter*118 > 850 ) {
            $('#wgallery').append('<a href="#" id="g_set_r"></a><a href="#" id="g_set_l"></a>');
        }

        $('#wgallery').append(box);
        $('#wgallery>#gallery_slider_box a').lightbox();

        $('#g_set_r').live('click', function(event){
            event.preventDefault();
            if ( gallery_pos <= -($('#gallery_slider_box_inside').width() - 831 - 108) ) {
                return;
            }
            gallery_pos = gallery_pos - 118;

            $('#gallery_slider_box_inside').animate({
                left: gallery_pos
            }, 1000);
        });
        $('#g_set_l').live('click', function(event){
            event.preventDefault();
            if ( gallery_pos >= 0 )
                return null;

            gallery_pos = gallery_pos + 118;
            $('#gallery_slider_box_inside').animate({
                left: gallery_pos
            }, 1000);
        });
    }


    var cnt = 0;
    $('.table_box_item').each(function(){
        cnt++;
        if ( cnt == 1 ) {
            $(this).css('padding-left', '0');
        }

        if($(this).hasClass('kontakt_')){
        if ( cnt == 4 ) {
            $(this).css('padding-right', '0');
            $(this).css('border-right', 'none');
            cnt = 0;
        }        
        }else{
        if ( cnt == 3 ) {
            $(this).css('padding-right', '0');
            $(this).css('border-right', 'none');
            cnt = 0;
        }
        }
    });
    /*
    var maxh = 0;
    var maxH = 0;
    var num = parseInt( 980 / $('.table_box').size()+1 );
    var marg = num - 11;
    var t = $('.table_box');

    $(t[0]).css('margin-left', '0px');
    $(t[ t.length - 1 ]).css('margin-right', '0px');
    */
    /*
    $('.table_box').css('width', marg);
    $('.table_box>img').css('width', marg);
    */
   /*
    $('.red_right_btn').html('&rarr;');
    */
    //$('.insert_box').css('height', $('.storycontent').height());

    /*
    $('.table_box').each(function(){
        if($(this).height() > maxh){
            maxh = $(this).height();
        }

        $(this).find('strong:first').each(function(){
            if($(this).height()>maxH){
                 maxH = $(this).height();
            }
        });



    });
    */
   /*
    $('.table_box strong').height(maxH);
  */
/*
    $('.table_box').height(maxh);
    $('.table_box').each(function(e){
        var loc=$(this).find('a').attr('href');

        if(typeof loc == 'undefined'){
            $(this).css('cursor', 'default');

        }





  });
*/
    $('.table_box').click(function(e){
        var loc=$(this).find('a').attr('href');

        if(typeof loc == 'undefined'){
          return false;
        }


        var target=$(this).find('a').attr('target');
        if(target == '_blank'){
            window.open(loc);
        }else{
            document.location = loc;
        }


        e.stopPropagation();
        return false
    });
    $('.table_box a').click(function(e){
        e.stopPropagation();
    });


    if($(document).height() > $(window).height()){
        $('body').append('<div id="up_fixed"><a onclick="javascript:$(window).scrollTop(0);" class="upp_div"></a></div>');

    };


    $('.storycontent li').each(function(){
        $(this).html('<span>'+$(this).html()+'</span>');

    });
    $('.special_table').each(function(){
    $(this).find('tr:first').addClass('firstrow');
    });


    $('.wpcf7-form').submit(function(){
      var error = false;
       if(typeof $('input[name=radio-420]:checked').val() == 'undefined'){
        error = true;
       }
       if(typeof $('input[name=radio-421]:checked').val() == 'undefined'){
        error = true;
       }
       if(typeof $('input[name=radio-422]:checked').val() == 'undefined'){
        error = true;
       }
       if(typeof $('input[name=radio-423]:checked').val() == 'undefined'){
        error = true;
       }
       if($('input[type=checkbox]:checked').size() == 0){
          error=true;
       }

         if(error){
          alert('Wypełnij wymagane pola');
       return false;
       }

    });


    $('.kolumna_oferta_item').each(function(){
      if($(this).hasClass('first')){
        return true;
      }
       var txt = $(this).text();
       $(this).html('<div>'+txt+'</div>');

       var div = $(this).find('div');


       var h = $(this).height();
       var h2 = div.height();
       var top = (h-h2)/2;
//           console.log(top);

       div.css('margin-top', parseInt(top)+'px');

  //     console.log(h);
  //     console.log(h2);
  //     console.log('-');



    });

    $('#form_submit textarea').html(' ').val(' ');
    $('#form_submit').click(sendForm);

});

function sendForm () {
    var data = $('#submit_form').serializeArray();

    for(var i in data) {
        switch(data[i].name) {
            case 'desc':
                var t = $.trim(data[i].value);
                if (t == '') {
                    alert('Wpisz treść wiadomości');
                    return;
                }
            break;        
            case 'name':
                var t = $.trim(data[i].value);
                if (t == '') {
                    alert('Wpisz imię i nazwisko');
                    return;
                }
            break;
            case 'email':
                var t = $.trim(data[i].value);
                if (t == '') {
                    alert('Wpisz adres email');
                    return;
                }
				
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(reg.test(t) == false) { 
					alert('Wpisz poprawny adres email');
					return false;
				}
				
            break;
        }
    }

    $.post('/', data, function(){
        alert('Wiadomość została wyslana.\n Dziękujemy.');
        document.forms[0].reset();
    });
}

function loadDataClient (this_) {
    var t = $(this_);
    var img = t.find('img');
    var img_src = img.attr('src');
    var h2 = t.find('h2').text();
    var desc = t.find('.desc_k_inline').html();
    var actual_img = $('.client_box_inline').find('img');
    var new_img = $('<img />').attr('src', img_src);

    var referencje = t.find('.referencje_klienci a');

    $(referencje).attr('rel', 'lightbox');
    $(referencje).lightbox();

    $('.referencje_img').html('');
    $('.referencje_img').append(referencje);
    
    console.log(referencje.length);
    if(referencje.length == 0){
                        $('.referencje_img').html();
                        $('.client_box_ref').hide();
    }else{
    //$('.client_box_ref').show();
    }

    $('.client_box_inline').append(new_img);


    //var new_img2 = $('<img src="'+img_src+'" />').appendTo('body').css('visibility','hidden').load(function

    if(new_img.width() > 0){
      new_img.css('left', 210 - (parseInt(new_img.width())/2) );
      new_img.css('top', 130 - (parseInt(new_img.height())/2) );
      new_img.css('display', 'none');
      new_img.css('position', 'absolute')
      new_img.css('marginTop', '0px');
    }                                                                                                         

    $('.client_box_title').html(h2);
     Cufon.replace('.client_box_title', {fontFamily: 'DIN' });
    

    $('.client_box_desc').html( desc );
    actual_img.fadeOut('fast', function(){
        actual_img.remove();
    });
    new_img.fadeIn('fast');
    $(window).scrollTop(0);
}
