$(document).ready(function(){
  $(".sendform").click(function(e){
    e.preventDefault();
    $("#contact_form").submit();
  })
  $(".readarchiverelation").parents('li')
  .hover(function(){
    $(this).find('a').addClass('hover');
  },function(){
    $(this).find('a').removeClass('hover');
  })
  .click(function(){
    window.location.href = $(this).find('a').attr('href');
  });
  $(".button").hover(function(){
    $(this).addClass('hover');
  },function(){
    $(this).removeClass('hover');
  })
  $(".logo").click(function(){
    window.location.href="/";
  })

  if ($(".setworkshop").length > 0){
    var selectedWorkshop = $(':selected', this).text().split(' - ');
    var city = selectedWorkshop[0];
    var workshopDates = selectedWorkshop[1];
    if(city == "wybierz warsztat...") {
      initialize();
    } else {
      initialize(city, workshopDates);
    }
  }
  $("form").jqTransform({
    selectChange:function(select){
      if (select.children('option[value="'+select.val()+'"]').hasClass('reserve')){
        $(".noplace").stop(true,true).show('fast');
      }else{
        $(".noplace").stop(true,true).hide('fast');
      }
      if (select.val() != 0){
        var varses = $('option[value="'+select.val()+'"]').text().split(' - ');
        initialize(varses[0], varses[1]);
      }
      if($(".program_"+select.val()).length > 0){
        $(".program").children('a').hide();
        $('.program').show();
        $(".program").children('.program_'+select.val()).show();
      }else{
        $(".program").hide();
      };
      $("#formname").focus();
    }
  });
  jQuery('#partnership').jcarousel({
    wrap: 'circular',
    scroll: 3,
    auto: 5
  });
  $(".register").click(function(e){
    e.preventDefault();
    $(this).parent().parent().parent().submit();
  })
  $(".postcode").children('.first').find('.jqtranformdone').keyup(function(){
    if ($(this).val().length==2){
      $(".postcode").children('.second').find('.jqtranformdone').focus();
    }
  })
    $("a[rel^='prettyPhoto']").prettyPhoto({
      allow_resize: false,
      overlay_gallery: false
    });
    $("#toggleallworkshops").click(function(e){
      e.preventDefault();
      if ($(this).hasClass('showed')){
        $("#workshopslist").stop(true,false).animate({width:"298px"},500);
        $(this).removeClass('showed');
        $(this).find('a').text('Pokaż wszystkie');
        $("#toggleallworkshops_top").fadeOut('fast');
      }else{
        $("#workshopslist").stop(true,false).animate({width:"900px"},500);
        $(this).addClass('showed');
        $(this).find('a').text('Schowaj listę');
        $("#toggleallworkshops_top").fadeIn('fast');
        $.scrollTo($("#formreg"),1000);
      }
    })
    $("#toggleallworkshops_top").click(function(e){
      e.preventDefault();
      $("#workshopslist").stop(true,false).animate({width:"298px"},500);
      $(this).removeClass('showed');
      $("#toggleallworkshops").find('a').text('Pokaż wszystkie');
      $(this).fadeOut('fast');
      $("#toggleallworkshops").removeClass('showed');
    })
    if ($(".setworkshop").length > 0){
      $(".setworkshop").parent().parent().click(function(e){
        e.preventDefault();
        $.scrollTo($("#formreg"),1000);
        $("#toggleallworkshops_top").fadeOut('fast');
        var vars = $(this).find(".setworkshop").attr('rel').split(':');
        $("#workshop").val(vars[0]);
        $(".jqTransformSelectWrapper").children('div').children('span').text(vars[1]);
        $("#workshopslist").stop(true,false).animate({width:"298px"},500);
        $("#toggleallworkshops").removeClass('showed');
        $("#toggleallworkshops").find('a').text('Pokaż wszystkie');
        
      if($(".program_"+vars[0]).length > 0){
        $(".program").children('a').hide();
        $('.program').show();
        $(".program").children('.program_'+vars[0]).show();
      }else{
        $(".program").hide();
      };
      
        if ($('option[value="'+vars[0]+'"]').hasClass('reserve')){
          $(".noplace").stop(true,true).show('fast');
        }else{
          $(".noplace").stop(true,true).hide('fast');
        }
        var cityt = vars[1].split(' - ');
        initialize(cityt[0],cityt[1]);
        $("#formname").focus();
      })
    }
    $(".archivelink").click(function(){
      window.location.href="/archiwum";
    }).hover(function(){
      $(this).addClass('hover');
    },function(){
      $(this).removeClass('hover');
    })
    $(".choose_workshop_list").children('li').hover(function(){
      $(this).addClass('hover');
      $(this).find(".setworkshop").addClass('hover');
    },function(){
      $(this).removeClass('hover');
      $(this).find(".setworkshop").removeClass('hover');
    })
    $(".popup").find('.close').click(function(){
      $(this).parents(".overflow").children('div').fadeOut('fast',function(){
        $(this).parent().fadeOut('fast');
      });
    })
    if ($.cookie("firstVisit") == null){
      setTimeout( function()
      {
        $(".popup").show('0',function(){
          $(this).children(".overflow").fadeIn('normal',function(){
            $(this).children("div").fadeIn('normal');
            $.cookie("firstVisit","no");
          })
        })
      }, 2000,{ expires: 30, path: '/', domain: 'echo.edu.pl', secure: true });
    }
    $("a[rel='selectworkshop']").click(function(){
      if ($(".setworkshop").length > 0){
        var opss = $(this).attr('href').split('#');
        var vars = opss[1].split(',');
        $("#workshop").val(vars[0]);
        $(".jqTransformSelectWrapper").children('div').children('span').html($('#workshop').children('option[value="'+vars[0]+'"]').text());
        $("#workshopslist").stop(true,false).animate({width:"298px"},500);
        $("#toggleallworkshops").removeClass('showed');
        $("#toggleallworkshops").find('a').text('Pokaż wszystkie');
        if($(".program_"+vars[0]).length > 0){
          $(".program").children('a').hide();
          $('.program').show();
          $(".program").children('.program_'+vars[0]).show();
        }else{
          $(".program").hide();
        };
        
        if ($('option[value="'+vars[0]+'"]').hasClass('reserve')){
          $(".noplace").stop(true,true).show('fast');
        }else{
          $(".noplace").stop(true,true).hide('fast');
        }
        $("#formname").focus();
      }
    })
    var kotwica = $(location).attr('href').split('#');
    if(kotwica.length>1){
        var vars = kotwica[1].split(',');
        $("#workshop").val(vars[0]);
        console.log(vars[0]);

        $(".jqTransformSelectWrapper").children('div').children('span').html($('#workshop').children('option[value="'+vars[0]+'"]').text());
        $("#workshopslist").stop(true,false).animate({width:"298px"},500);
        $("#toggleallworkshops").removeClass('showed');
        $("#toggleallworkshops").find('a').text('Pokaż wszystkie');
        
      if($(".program_"+vars[0]).length > 0){
        $(".program").children('a').hide();
        $('.program').show();
        $(".program").children('.program_'+vars[0]).show();
      }else{
        $(".program").hide();
      };
        if ($('option[value="'+vars[0]+'"]').hasClass('reserve')){
          $(".noplace").stop(true,true).show('fast');
        }else{
          $(".noplace").stop(true,true).hide('fast');
        }
        $("#formname").focus();
    }
})

