$(document).ready(function(){
    
  // jQuery Uniform
  if($('#questionnaire').length){
    $("select").uniform();
    $(":radio").uniform();
    $(":checkbox").uniform();
    $(":radio").focus(function() { $(this).blur(); });
    $(":checkbox").focus(function() { $(this).blur(); });
    
    // Checkboxes list
	$('.checkbox-list .noneoftheabove').toggle(function(){
		$(this).siblings().css("color","#808080");
		$(this).siblings().find(".checked").removeClass("checked").addClass("unchecked");
		$(this).siblings().find("input").attr('checked', false)
		$(this).siblings().find("input").each(function(index,elm){
			$(elm).removeAttr('checked');
		});
		$(this).find("span").addClass("checked");
		$(this).find("input").attr('checked', true);
		return true;
	}
	,function(){
		$(this).siblings().css("color","#000");
		$(this).siblings().find(".unchecked").addClass("checked").removeClass("unchecked");
		$(this).find("span").removeClass("checked");
		$(this).find("input").removeAttr('checked');
		return true;
	});
    $(".checkbox-list li:not(.noneoftheabove)").click(function(){
      $(".checkbox-list .noneoftheabove").find("span").removeClass("checked");
	  $(".checkbox-list .noneoftheabove").find("input").removeAttr('checked');
      $(this).css("color","#000");
      $(this).siblings().css("color","#000");
      return true;
    });
    
    $(".checkbox-list li:not(.noneoftheabove)").click(function(){
      $(".noneoftheabove-textarea").fadeOut();
      $(".noneoftheabove-textarea textarea").val("");
      $(this).css("color","#000");
      $(this).siblings().css("color","#000");
      return true;
    });
    $(".checkbox-list .noneoftheabove").click(function(){
      $(this).siblings().css("color","#808080");
      $(this).find("span").addClass("checked");
      $(".noneoftheabove-textarea").fadeIn();
      $(".noneoftheabove-textarea textarea").focus();
      return true;
    });  
    
    // Radio list    
    $(".noneoftheabove-textarea").hide();
    
    $(".radio-list li:not(.noneoftheabove)").click(function(){
      $(".noneoftheabove-textarea").fadeOut();
      $(".noneoftheabove-textarea textarea").val("");
      $(this).css("color","#000");
      $(this).siblings().css("color","#000");
      return true;
    });
    $(".radio-list .noneoftheabove").click(function(){
      $(this).siblings().css("color","#808080");
      $(this).find("span").addClass("checked");
      $(".noneoftheabove-textarea").fadeIn();
      $(".noneoftheabove-textarea textarea").focus();
      return true;
    });  
    
    $(".other-textarea").hide();
    
    $(".radio-list li:not(.other)").click(function(){
      $(".other-textarea").fadeOut();
      $(".other-textarea textarea").val("");
      $(this).css("color","#000");
      $(this).siblings().css("color","#000");
      return true;
    });
    $(".radio-list .other").click(function(){
      $(this).siblings().css("color","#808080");
      $(this).find("span").addClass("checked");
      $(".other-textarea").fadeIn();
      $(".other-textarea textarea").focus();
      return true;
    });
    
    
    // Section 6 - Monitoring / Enter Company Code
    if($("#id_entercompanycode").length){
        
        if($('#yesno_yes2:checked').length > 0){
            $(".entercompanycode-textarea").show();
        }else{
            $(".entercompanycode-textarea").hide();
        }
      
      $(".enter-the-code").click(function(){
        $(this).siblings().css("color","#808080");
        $(".entercompanycode-textarea").fadeIn();
        $(".entercompanycode-textarea textarea").focus();
        return true;
      });
      
      $("div.enter-companycode .answer-no").click(function(){
        $(".entercompanycode-textarea").fadeOut();
        $(".entercompanycode-textarea textarea").val("");
        $(this).css("color","#000");
        $(this).siblings().css("color","#000");
        return true;
      });
    }
    
    if($("#id_enteremail").length){
        
        if($('#yesno_yes3:checked').length > 0){
            $(".enteremail-textarea").show();
        }else{
            $(".enteremail-textarea").hide();
        }
      
      $("li.enter-email").click(function(){
        $(this).siblings().css("color","#808080");
        $(".enteremail-textarea").fadeIn();
        $(".enteremail-textarea textarea").focus();
        return true;
      });
      
      $("div.enter-email .answer-no").click(function(){
        $(".enteremail-textarea").fadeOut();
        $(".enteremail-textarea textarea").val("");
        $(this).css("color","#000");
        $(this).siblings().css("color","#000");
        return true;
      });
    }
    
    // Force-launch Facebox
    $("li.answer-no input").click(function(){
      $("#facebox-launch").click();
    });
    
    //Form simple validation
	$("#questionnaire-form").validate({
		invalidHandler: function(form) {
			$('#form-errors').show();
			var errors = $("#form-errors");
            $('html, body').animate({scrollTop: errors.offset().top}, 1000, 'swing');
		}
	});
	
	$("#questionnaire-form #id_age").setMask({ mask: '999', autoTab: false });
  }
  
  // Facebox
	if($('a[rel*=facebox]').length >0){  
  	    $('a[rel*=facebox]').facebox({
  		    loading_image : 'loading.gif',
  		    close_image   : 'closelabel.gif'
  	    });                
  }     
	
  // Button fix for IE
	$("a.button").click(function(){
    $(this).blur();
  });

	
	// Cufon
  Cufon.replace('.title h2');
  Cufon.replace('.title h3');
  Cufon.replace('a.btn_expand');
  Cufon.replace('h3.title');
  Cufon.replace('p.title');
});
