$(function() {

  /*$('#content').show();
  $('.block').hide();
  $('#content_the_course').show();*/

  // Rounded corners
  $('#fancybox-outer').corner();
  
  // Background image
  $.backstretch("images/background.jpg");
  
  // Thumbnail setup
  $('.block .thumb:eq(1)').addClass('middle');
  var thumb_class = '';
  for(i=0; i<20; i++) {
    for(n=0; n<3; n++) {
      thumb_class = 'thumb';
      if(i == 0) thumb_class += ' top';
      if(n == 1) thumb_class += ' middle';
      $('.thumb_grid').append('<a href="#" class="' + thumb_class + '"></a>');
    }
  }
  $('a.thumb').fancybox({overlayOpacity: 0.75});
  
  // Logo closes all menus and windows
  $('#logo').click(function(){
    $('#dynamic_menu').fadeOut();
    $('#content').fadeOut();
    $('.nav a, #golf_balls_menu a').removeClass('active');
    return false;
  });
  
  // Static and dynamic menu control
  $('.nav a').click(function(){
    if(!$(this).hasClass('active')) {
      var link = this;
      var menu = '#' + $(this).parents('.nav_menu').attr('id');
      if(menu == '#static_menu') {
        var reveals = '#dynamic_menu';
        var block = '.nav';
        $('#content').fadeOut(350);
        $('#dynamic_menu .nav a').removeClass('active');
      } else {
        var reveals = '#content';
        var block = '.block';
      }
      $(menu + ' .nav a').removeClass('active');
      $(this).addClass('active');
      $(reveals).fadeOut(350, function(){
        $(reveals+' '+block).hide();
        var active_block = $(reveals+'_'+$(link).text().toLowerCase().replace(/ /g, '_').replace(/-/g, ''));
        setContentBackground(active_block);
        loadThumbnails(active_block);
        active_block.show();
        $(reveals).fadeIn(350);
      });
      golfMenuOff();
    }
    return false;
  });
  $('#dynamic_menu .nav a').hover(
    function(){
      $('#extra p').text($(this).attr('rel'));
    },
    function(){
      $('#extra p').text('');
    }
  );
  
  // Golf balls menu control
  $('#golf_balls_menu a')
    .hover(
      function(){
        $('#label').text($(this).text());
      },
      function(){
        var active = $('#golf_balls_menu a.active');
        if(active.length) {
          $('#label').text(active.text());
        } else {
          $('#label').text('');
        }
      })
    .click(function(){
      $('.nav a, #golf_balls_menu a').removeClass('active');
      $(this).addClass('active');
      return false;
    });  
  
  // Display content window with non-menu link
  $('a[rel^="content_"]').click(function(){
    var block = $(this).attr('rel');
    if(block != $('#golf_balls_menu a.active').attr('rel')) golfMenuOff();
    $('#dynamic_menu').fadeOut(350);
    $('#content').fadeOut(350, function(){
      $('#content .block').hide();
      var active_block = $('#' + block);
      setContentBackground(active_block);
      loadThumbnails(active_block);
      active_block.show();
      $('#content').fadeIn(350);
      $('.nav a').removeClass('active');
    });
    return false;
  });  
  
  // Close buttons
  $('a.close').click(function(){
    var parent = $(this).parent();
    parent.fadeOut();
    if(parent.attr('id') == 'content') {
      $('#dynamic_menu .nav a').removeClass('active');
      golfMenuOff();
    } else {
      $('#static_menu .nav a').removeClass('active');
    }
    return false;
  });
  
  // Contact form
  $('a[href="#contact"]')
    .click(function(){
      var contact_id = ($(this).attr('rel')) ? $(this).attr('rel') : $(this).parents('.block').attr('id').substring(8);
      $.each(forms, function(form_name, form_value) {
        if(form_name == contact_id) {
          $('form #subject').attr({value: form_value.subject});
          $('form #to').attr({value: form_value.to});
          $('form .reset').val('');
          return false;
        }
      });
    })
    .fancybox({
      onStart: contactShow,
      onClosed: contactHide,
      overlayOpacity: 0.75
    });
  var forms = {
    static: {
      subject: '',
      to: 'erika'
    },
    onecity: {
      subject: '',
      to: 'erika'
    },
    '2010_proam': {
      subject: '',
      to: 'joe'
    },
    the_course: {
      subject: '',
      to: 'sarah'
    },
    create_your_foursome: {
      subject: "Foursome You're Interested In:",
      to: 'joe'
    },
    party_for_a_purpose: {
      subject: '',
      to: 'erika'
    },
    get_in_the_game_dinner: {
      subject: '',
      to: 'erika'
    },
    sponsorshop_opportunities: {
      subject: "Sponsorship Level You're Interested In:",
      to: 'joe'
    },
    volunteer_opportunities: {
      subject: "Volunteer Position You're Interested In:",
      to: 'tricia'
    }                                                                                                                  
  };
  $('form').submit(function(){
    $(this).ajaxSubmit({
      success: contactFormResponse
    });
    return false;
  });
  
  // Fix multiple alpha channel problem in IE7
  $.fn.extend({
    fixPNG: function(sizingMethod, forceBG) {
	    if (!($.browser.msie)) return this;
	    var emptyimg = "empty.gif"; //Path to empty 1x1px GIF goes here
	    sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
	    this.each(function() {
	      var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
	        imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
	        src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
	      this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
	      if (isImg) this.src = emptyimg;
	      else this.style.backgroundImage = "url(" + emptyimg + ")";
	    });
	    return this;
    }
  });  
  
});

// Fancybox callbacks
function contactShow() {
  $('#contact p').hide();
  $('#contact, form').show();
}
function contactHide() {
  $('#contact').hide();
}

function golfMenuOff() {
  $('#label').text('');
  $('#golf_balls_menu a').removeClass('active');
}

function setContentBackground(active_block){
  if(active_block.hasClass('no_rings') && active_block.hasClass('block')) {
    active_block.parent().css({
      backgroundImage: 'url(images/content_window_background.png)',
      backgroundRepeat: 'repeat'
    });
  } else if(active_block.hasClass('no_swoosh') && active_block.hasClass('block')) {
    active_block.parent().css({
      backgroundImage: 'url(images/content_window_background_rings_no_swoosh.png)',
      backgroundRepeat: 'no-repeat'
    });
  } else if(active_block.hasClass('block')) {
    active_block.parent().css({
      backgroundImage: 'url(images/content_window_background_rings.png)',
      backgroundRepeat: 'no-repeat'
    });
  }
}

function loadThumbnails(active_block) {
  var folder = active_block.attr('id').substring(8);
  var i = 0;
  $('a.thumb', active_block).each(function(){
    i++;
    $(this).css({backgroundImage: 'url(images/photos/' + folder + '/thumbnails/' + i + '.jpg)'});
    $(this).attr({
      href: 'images/photos/' + folder + '/large/' + i + '.jpg',
      rel: folder
    });
  });
}

function contactFormResponse(responseText, statusText, xhr, $for) {
  $('form').hide();
  $('#contact p')
    .show()
    .html(responseText);
}