$(document).ready(function() {
	
  /* remove all noscript classes */
  $('div').removeClass('noScript');
  if(ageVerificated() == true) {
	$('#exposeMask-ask').css('display', 'none');
  }
  
  /* enable javascript scrollbar */
  $('.scroll-pane').jScrollPane({
    scrollbarWidth: 14 
  });
  
  /* fix png in ie6 */
  $(document).pngFix(); 
  
  /* i like */
  /*
  $('#facebook-like').css('display', 'block');
  $('iframe').css('display', 'none');
  $('#facebook-like').click(function() {
    $('iframe').css('display', 'block');
    $('iframe').expose({
      maskId: 'exposeMask-iframe',
      onClose: function() {
    	$('iframe').css('display', 'none');
      }
    });
  });
  */
  
  $('#modal-ilike').css('display', 'block');
  //$('iframe').css('display', 'none');
  $('#iframe-fb-like').css('display', 'none');

  var iframe_fb = $('#iframe-fb-like').overlay({
  //var iframe_fb = $('iframe').overlay({
      closeOnClick:true,
      closeOnEsc:  true,
      mask: {
        maskId: 'exposeMask-iframe',
        opacity:0.7,
        color: '#000',
        loadSpeed: 200
      },
      top: 420,
      left: '50%',
      onClose: function() {
          $('iframe').css('display', 'none');
          $('#iframe-fb-like .close').css('display', 'none');
      },
    });

  $('#modal-ilike a').click(function(e) {
    $('iframe').css('display', 'block');
    $('#iframe-fb-like .close').css('display', 'block');
    iframe_fb.load();
    return e.preventDefault();
  });

  /* ech age verification */
  if(ageVerificated() == false) {
    var ask = $('#dialog-ask').overlay({
      closeOnClick: false,
      closeOnEsc: false,
      load: true,
      mask: {
    	maskId: 'exposeMask-ask',
        color: '#000',
        loadSpeed: 200,
        opacity: 0.7
      },
      top: 260,
      
      onClose: function() {
    	if(ageVerificated() != true) {
    	  var locked = $('#dialog-lock').overlay({
            closeOnClick: false,
            closeOnEsc: false,
            load: true,
            mask: {
    		  maskId: 'exposeMask-lock',
    		  color: '#000',
    		  loadSpeed: 200,
    		  opacity: 0.7
    		},
    		top: 260,
	      
    		onLoad: function() {
    	      $('#dialog-lock .close').css('display', 'none');
	    	  return e.preventDefault();
	        }
    	  });
    	}
      }
    });
  }

  /* age verification form */
  $('#dialog-ask form').submit(function(e) {
    var day   = $("input[name='day']", this).val();
    var month = $("input[name='month']", this).val();
    var year  = $("input[name='year']", this).val();
    var now   = new Date();
    
    /* test plausability */
    if(!isNaN(day) && !isNaN(month) && !isNaN(year) && day <= 31 && month <= 12 && year <= now.getFullYear()) {
      var today     = new Date();
      var birthdate = new Date(year, month-1, day);
      var age       =  Math.floor(((today - birthdate)/1000/60/60/24/365));

      /* is old enough */
      if(age >= 18) {
    	setCookie('age', 'true', 1);
        ask.eq(0).overlay().close();
        
        return e.preventDefault();
      }
    }
    
    var locked = $('#dialog-lock').overlay({
	  closeOnClick: false,
      closeOnEsc: false,
      load: true,
      mask: {
	    maskId: 'exposeMask-lock',
        color: '#000',
        loadSpeed: 200,
        opacity: 0.7
      },
      top: 260,
      
      onLoad: function() {
    	$('#dialog-lock .close').css('display', 'none');
    	return e.preventDefault();
      }
  	});

    /* do no submit */
    return e.preventDefault();
  });

  var buttons = $("#dialog-ask .button-wrapper button", this).click(function(e) {
    var cancel = buttons.index(this) === 0;
    
    if(cancel) {
      var locked = $('#dialog-lock').overlay({
  	    closeOnClick: false,
        closeOnEsc: false,
        load: true,
        mask: {
          maskId: 'exposeMask-lock',
          color: '#000',
          loadSpeed: 200,
          opacity: 0.7
        },
        top: 260,
    
        onLoad: function() {
  	      $('#dialog-lock .close').css('display', 'none');
  	      return e.preventDefault();
        }
	  });
    }
  });

  /* form fields */
  $('#day').focus(function() {
	if($(this).attr('value') == 'tt') $(this).attr('value', '');
  });
  $('#day').blur(function() {
    if($(this).attr('value') == '') $(this).attr('value', 'tt');
  });
  
  $('#month').focus(function() {
	if($(this).attr('value') == 'mm') $(this).attr('value', '');
  });
  $('#month').blur(function() {
    if($(this).attr('value') == '') $(this).attr('value', 'mm');
  });
  
  $('#year').focus(function() {
    if($(this).attr('value') == 'jjjj') $(this).attr('value', '');
  });
  $('#year').blur(function() {
	if($(this).attr('value') == '') $(this).attr('value', 'jjjj');
  });

});

/* Helper */
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return false;
}

function ageVerificated() {
  age=getCookie('age');
  if (age!=null && age!="" && age=="true") {
    return true;
  }
  
  return false;
}
