/* Plugin to make variable height divs equal heights */
$.fn.sameHeights = function() {

$(this).each(function(){
var tallest = 0;

$(this).children().each(function(i){
if (tallest < $(this).height()) { tallest = $(this).height(); }
});
$(this).children().css({'height': tallest});
});
return this;
};
  
function closeFancy() {
  $.fancybox.close();
}
  
$(document).ready(function() {
  $('.homepage-flood').empty();
  // Resets the form on page refresh
  $("#toy-giveaway-form").resetForm();

  //Sliders
  $('#slide-content').slides({
    generateNextPrev: false,
    effect:'fade',
    fadeSpeed:500,
    play: 10000
  });
  
  $('#top-toy-slide').slides({
    generateNextPrev: false,
    effect:'fade',
    fadeSpeed:500,
    generatePagination: false,
    container: 'top-toys',
    play: 10000
  });
  
  $('#character-widget').slides({
    generateNextPrev: false,
    paginationClass: 'character-navigation',
    container: 'characters',
    generatePagination: false
  });
  
  $('#videos').slides({
    generateNextPrev: false,
    paginationClass: 'video-links',
    container: 'video-slides',
    generatePagination: false
  });
  
  $('#get-help').slides({
    generateNextPrev: false,
    paginationClass: 'help-navigation',
    container: 'help-container',
    generatePagination: false,
    effect:'fade'
  });

  $("ul.game-list li a.hover").each(function() {
    $(this).simpletip({
    position: [0,-190],
    content: '<div class="game-hover"><img src="images/game-hovers/' + $(this).attr('title') + '.jpg" /></div>'
    });
    $(this).find('.game-hover').css('background-position', $(this).find('img').width() /2 + 'px bottom');
  });
  $('ul.game-list li a').hover(
  function () {
    $(this).find('.game-hover').show();
  },
  function () {
    $(this).find('.game-hover').hide();
  }
);

  $('#navigation ul li a img').hover(function() {
    $(this).addClass("hover").stop()
    $(this).animate({
      right: '0'
    }, 100);

    } , function() {
      $(this).removeClass("hover").stop()
      $(this).animate({
        right: '-12'
      }, 100);
  });
  
  //Video Functionality
  $("ul.rippisodes").ytplaylist({addThumbs:true, autoPlay: false, holderId: 'video-player-rippisodes', playerHeight:'238', playerWidth: '380'});
  
  $("ul.commercials").ytplaylist({addThumbs:true, autoPlay: false, holderId: 'video-player-commercials', playerHeight:'238', playerWidth: '380'});
  
  //Fancybox
  $("a.sign-up-now").fancybox({
    'width'                     : 610,
    'padding'                   : 0,
    'autoScale'                 : false,
    'type'                      : 'inline',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'autoDimensions'            : true,
    'onStart'                   : function () {
      $('.flood-tags').empty();
      var axel = Math.random() + ""; 
      var a = axel * 10000000000000; 
      var sweepCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=landi667;cat=mmisw351;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
      $('.flood-tags').append(sweepCode);
    },
    'onClosed'                  : function() {
      //$("#toy-giveaway-form").resetForm();
      $('#error-message').remove();
    }
  });
  
  $("a.last-weeks-winner").fancybox({
    'width'                     : 420,
    'padding'                   : 10,
    'autoScale'                 : false,
    'type'                      : 'inline',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'autoDimensions'            : true,
    'width'                     : 600
  });
  
  $("a.official-rules").fancybox({
    'width'                     : 420,
    'padding'                   : 10,
    'autoScale'                 : false,
    'type'                      : 'inline',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'autoDimensions'            : true
  });
  
  $("a.help-link").fancybox({
    'width'                     : 420,
    'autoScale'                 : false,
    'type'                      : 'inline',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'autoDimensions'            : true
  });
  
  $("a.login-panel").fancybox({
    'autoScale'                 : false,
    'type'                      : 'swf',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'padding'                   : 0,
    'swf'                       : {wmode: 'transparent'},
    'autoDimensions'            : true,
    'modal'                     : true,
    'onStart'                   : function () {
      $('.flood-tags').empty();
      var axel = Math.random() + ""; 
      var a = axel * 10000000000000; 
      var playNowCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=gener667;cat=playn274;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
      $('.flood-tags').append(playNowCode);
    }
  });
  
  $("a.testdrive-panel").fancybox({
    'autoScale'                 : false,
    'type'                      : 'swf',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'padding'                   : 0,
    'swf'                       : {wmode: 'transparent'},
    'autoDimensions'            : true,
    'modal'                     : true,
    'height'                    : 450,
    'width'                     : 700,
    'onStart'                   : function () {
      $('.flood-tags').empty();
      var axel = Math.random() + ""; 
      var a = axel * 10000000000000; 
      var testDriveCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=gener667;cat=testd119;ord=1;num' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
      $('.flood-tags').append(testDriveCode);
    }
  });
  
  $("a.registration-panel").fancybox({
    'autoScale'                 : false,
    'type'                      : 'swf',
    'overlayOpacity'            : 0.6,
    'overlayColor'              : '#000',
    'padding'                   : 0,
    'swf'                       : {wmode: 'transparent'},
    'autoDimensions'            : true,
    'modal'                     : true,
    'height'                    : 650,
    'width'                     : 850,
    'onStart'                   : function () {
      $('.flood-tags').empty();
      var axel = Math.random() + ""; 
      var a = axel * 10000000000000; 
      var registerCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=landi667;cat=mmisi671;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
      $('.flood-tags').append(registerCode);
    }
  });

  $('.equal-height').sameHeights();
  
  $('input.text').click(function() {
    $(this).focus();
    $(this).select();
  });
  
  $('#previous-winners').dataTable( {
    "sPaginationType": "full_numbers",
    "sDom": '<"top"i>rt<"bottom"p><"clear">'
  });
  
$.validator.addMethod("noq", 

function(value, element) {

return !/\?/.test(value);

}, 

"Do not use question marks"
);
  
  $.validator.addMethod("fname", function(value, element) {
    if(value == 'First name') {
      return false;
    }
    else {
      return true;
    }
    }, "Must not be the default value."
  );
  
  $.validator.addMethod("lname", function(value, element) {
    if(value == 'Last name') {
      return false;
    }
    else {
      return true;
    }
    }, "Must not be the default value."
  );
  
  $.validator.addMethod("cname", function(value, element) {
    if(value == "Child's Name") {
      return false;
    }
    else {
      return true;
    }
    }, "Must not be the default value."
  );
  
  $.validator.addMethod("add1", function(value, element) {
    if(value == "Street Address") {
      return false;
    }
    else {
      return true;
    }
    }, "Must not be the default value."
  );
  
  $.validator.addMethod("add2", function(value, element) {
    if(value == "City") {
      return false;
    }
    else {
      return true;
    }
    }, "Must not be the default value."
  );
  
  $.validator.addMethod("phone", function(value, element) {
    if(value == "Phone #") {
      return false;
    }
    else {
      return true;
    }
    }, "Must not be the default value."
  );
  
  $('#toy-giveaway-form').validate({
    rules: {
      first_name: {
        required: true,
        fname: true,
        noq: true
      },
      last_name: {
        required: true,
        lname: true,
        noq: true
      },
      childs_name: {
        required: true,
        cname: true,
        noq: true
      },
      address_1: {
        required: true,
        add1: true,
        noq: true
      },
      address_2: {
        required: true,
        add2: true,
        noq: true
      },
      phone: {
        phone: true,
        noq: true
      }
    },
    errorClass: "error",
    validClass: "success",
    submitHandler: function(form) {
      var dataString = $(form).formSerialize();
      $.ajax({  
        type: "POST",  
        url: "includes/submit.php",  
        data: dataString,
        dataType: "json",
        success: function(j) {
          if (j.data == 'invalid') {
            $('#error-message').remove();
            $('#toy-giveaway-form').prepend("<div id='error-message'></div>");  
            $('#error-message').html('<h3>Validation Error</h3><p>Your Application has not been submitted</p>')  
            .append("<p>An application with this email or address has already been entered this week. Sorry.</p>")  
            .hide()  
            .fadeIn(1500, function() {  
            });
            $('#email, #address_1, #address_2, #city, #state, #zipcode').removeClass('success').addClass('error');
          }
          else if(j.data == 'insert') {
            $('.flood-tags').empty();
            var axel = Math.random() + ""; 
            var a = axel * 10000000000000; 
            var submitCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=landi667;cat=mmisw527;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
            $('.flood-tags').append(submitCode);
            $('#toy-giveaway-form').html("<div id='thanks'></div>");  
            $('#thanks').html('<img src="images/thanks.jpg"/>')
            .append('<div class="thanks-return">RETURN</div>')
            .hide()  
            .fadeIn(1500, function() {  
            });
            $('.thanks-return').click(function() {
              closeFancy();
            });
          }
          else if(j.data == 'update') {
            $('.flood-tags').empty();
            var axel = Math.random() + ""; 
            var a = axel * 10000000000000; 
            var submitCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=landi667;cat=mmisw527;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
            $('.flood-tags').append(submitCode);
            $('#toy-giveaway-form').html("<div id='thanks'></div>");  
            $('#thanks').html('<img src="images/thanks.jpg"/>')
            .append('<div class="thanks-return">RETURN</div>')
            .hide()  
            .fadeIn(1500, function() {  
            });
            $('.thanks-return').click(function() {
              closeFancy();
            });
          }
        }  
      });
    }
  });
    
  $('.find-my-toy').click(function() {
    $('.flood-tags').empty();
    var axel = Math.random() + ""; 
    var a = axel * 10000000000000; 
    var findCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=landi667;cat=mmito163;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
    $('.flood-tags').append(findCode);
  });
  
  $('.top-toy-img').click(function() {
    $('.flood-tags').empty();
    var axel = Math.random() + ""; 
    var a = axel * 10000000000000; 
    var topCode = '<iframe src="http://fls.doubleclick.net/activityi;src=2384615;type=landi667;cat=toyst486;ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
    $('.flood-tags').append(topCode);
  });
});

