(function($){
  //
  //  Cookie jar – methods for getting and setting cookies
  //
  var cookieJar = {
    createCookie: function(name,value,days,domain) {
      if(days)
      {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
      }
      else
      {
        var expires = "";
      }
      if (typeof(domain)!='undefined') {
         cdomain ="; domnain="+domain;
      } else {
         cdomain ="";
      }
      document.cookie = name + "=" + value + expires + "; path=/"+ cdomain;
    },

    readCookie: function(name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');

      for(var i=0; i < ca.length; i++)
      {
        var c = ca[i];

        while(c.charAt(0) == ' ')
        {
          c = c.substring(1, c.length);
        }

        if(c.indexOf(nameEQ) == 0)
        {
          return c.substring(nameEQ.length, c.length);
        }
      }

      return false;
    }
  }
  
  //
  // Favourite game – sets a cookie marking a favourite game to be redirected to from the homepage
  //
  var FavouriteGame = {
    initialize: function() {
      $('a.favourite-game').click(function() {
        var game = $(this).attr('rel');
        if(game)
        {
          cookieJar.createCookie('uwin-favourite-game', game, 365);
          console.log(cookieJar.readCookie('uwin-favourite-game'));
        }
      });
    }
  }
  
  //
  // Refer a friend form – configuring validation rules and behaviour
  //
  var ReferAFriendForm = {
    initialize: function() {
      // hide the form errors div
      $('.form-errors').hide();
     

      // define validation options
      var options = {
        validClass: 'valid',
        rules: {
          // your name
          'q1342:q1': 'required',
          // your email
          'q1342:q5': {
            required: true,
            email: true
          },
          // friend's name
          'q1342:q2': 'required',
          // friend's email
          'q1342:q6': {
            required: true,
            email: true
          },
          // message
          'q1342:q3': 'required'
        }
      };
      
      $('#form_email_1342').validate(options);
    }
  };

  $(document).ready(function() {
    // lightboxes
    $(".colorbox").colorbox({
      iframe: true,
      width: "660px",
      height: "685px"
    });
    $(".help_colorbox").colorbox({
      iframe: true,
      width: "886px",
      height: "685px"
    });
   $(".register_colorbox").colorbox({
      iframe: true,
      width: "578px",
      height: "685px"
    });
 $(".forgotten_details_colorbox").colorbox({
      iframe: true,
      width: "578px",
      height: "550px"
    });
    $(".payment_colorbox").colorbox({
      iframe: true,
      width: "870px",
      height: "640px"
    });
   $(".variation").colorbox({
      iframe: false,
      width: '420px',
      height: '230px',
      scrolling:false
    });
     $("input[value=fractions]").click(function(){ 
		//$("span.dec").css({'display':'none'});
		$("span.dec").hide();
		//$("span.frac").css({'display':'block !important'});
		$("span.frac").show();
        cookieJar.createCookie('format','fraction',365);
    });
   $("input[value=decimals]").click(function(){ 
		//$("span.frac").css({'display':'none'});
		$("span.frac").hide();
		//$("span.dec").css({'display':'block'});
		$("span.dec").show();
        cookieJar.createCookie('format','decimal',365);
    });


$().bind('cbox_complete', function(){ 
      $('a#go').click(function(e){
	    e.preventDefault();
            var url = $("input[name='variation']:checked").val()
	    $.fn.colorbox.close();
if(url){
	    window.location.replace(url)
}
	  });
});
    // load the RAF form validation
    ReferAFriendForm.initialize();
    
    // load the favourite game link functionality
    FavouriteGame.initialize();
  });

// Flags list

$('.sub-flags').hide();
$("#flags .active").mouseover(function(){
      $(this).children('ul').show();
    }).mouseout(function(){
      $(this).children('ul').hide();
    });



})(jQuery);
