$(function() {
 
  $("body:not(.collection) #lCollection, body:not(.collection) #subCollection").hover(
    function() {
      $("#subCollection").show();
    },
    function() {
      $("#subCollection").hide();
    }
  );
  
  $("body:not(.styling) #stylingSubCollection").hide();
  $("body:not(.styling, .collection) #lStyling, body:not(.styling, .collection) #stylingSubCollection").hover(
    function() {
      $("#stylingSubCollection").show();
    },
    function() {
      $("#stylingSubCollection").hide();
    }
  );

  $("body:not(.company) #lCompany, body:not(.company) #subCompany").hover(
    function() {
      $("#subCompany").show();
    },
    function() {
      $("#subCompany").hide();
    }
  );
  
  $("#menPreview, #womenPreview").cycle({
     fx: "fade",
     speed: 1000,
     timeout: 4000,
     delay: -3000
  });
  
  var options = {
        text : { 
          cancel  : lng['cancel'],
          loading : lng['loading'],
          close   : '<strong>'+ lng['close'] + '</strong>',
          next    : lng['next'],
          prev    : lng['prev']
        },
        resizeLgImages:     true,
        displayNav:         true,
        handleUnsupported:  'remove',
        keysClose:          ['c', 27, 'x'], // c, x or esc
        autoplayMovies:     false,
        overlayColor:       "#fff",
        overlayOpacity:     1,
        initialHeight:      400,
        initialWidth:       160,
        resizeDuration:     0.25,
        fadeDuration:       0.0,
        loadingImage:       '/img/loading-light.gif',
        viewportPadding:    0,
        type:               'iframe',
        handleOversize:     'none',
        height:             590
    };

    Shadowbox.init(options);
    
    /* Background picture picker for front page. */
    
    $(".pick").bind("click", function() { 
        clearInterval(sleeper);
        $(this).trigger("update_background");
    });
    
    $(".pick").bind("update_background", function() {
        var target = "#" + $(this).attr("id").replace(/pick/i, "slide");
        
        $(".pick").removeClass("on");
        $(this).addClass("on");
        
        $(".slide").fadeOut("slow");
        $(target).fadeIn("slow");
        
        return false;
    });
    
    /* Randomly change background picture in frontpage every 8 seconds. */
    var sleeper = setInterval(function() {
        $(".pick:not(.on)").sort(function() { 
          return Math.round(Math.random())-0.5;
        }).slice(0,1).trigger("update_background");
    }, 8000);
    
    
    $("form[action*='join.html']").validate({
        rules: {
            fFirstName: {
                required: true
            },
            fLastName: {
                required: true
            },
            fBirthdate: {
                required: true
            },
            fPhone: {
                required: true
            },
            fEmail: {
                required: true,
                email: true
            },
            fAddress: {
                required: true
            },
            fExperience: {
                required: true
            },
            fLanguage1Skill: {
                required: true
            },
            fTimeToWork: {
                required: true
            },
            fWeekengsWork: {
                required: true
            },
            fWhereToWork: {
                required: true
            },
            fWhenToWork: {
                required: true
            },
            fWhatAttracts: {
                required: true
            }
        },
        errorElement: "div",
        errorPlacement: function(error, element) {
            if ( element.is("[name*=fLanguage1Skill]") ) {
                $("[for=fLanguage1Skill]").addClass("error");
            } else {
                element.parent().parent().find('label').addClass('error');
            }
            return;
        },
        success: function(label) {
           label.removeClass('error');
           var htmlFor = label.attr("htmlfor");
           console.log(label);
           $("label[for*="+htmlFor+"]").removeClass("error");
        }
    });

});

function sbopen(params) {
  eval('var options = ' + params);
  options.content + "&width="+options.width;
  Shadowbox.open(options);
}

function sbclose()
{
  parent.Shadowbox.close();
}
