$(function() {

  $('#music_player').flash({
   src: '/images/xspf_player.swf',
   width: 258,
   height: 102,
   bgcolor: '#f3f3f3',
   flashvars: { playlist_url: '/songs/feed.xml' }
  });

  $('table.styled tr:even:not(.header)').addClass('alt');

  $('#navigation a').hover(function() {
    $(this).parent('li').addClass('hover');
  }, function() {
    $(this).parent('li').removeClass('hover');
  });
  
  $('.rss').pngFix();

  $('#comment-form form').validate({
    errorPlacement: function(error,element) {
      error.insertAfter(element);
    },
    submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
			  success: function(responseText, statusText) {
          $('.comment-form .thanks').show();
        }
			});
		}
  });
	
  $('#speedbump a').click(function() {
    tb_remove();
  });
  
  $('a:not(#video_wrapper,.video)').livequery(function() {
    $(this).linkDisclaimer({
      valid_domains: ['javascript', 'tinkerfcu.org', 'www.tinkerfcu.org'],
      showDisclaimer: function(link, settings) {
        $('#speedbump .accept').unbind('click').click(function() {
          window.location = link.attr('href');
          tb_remove();
        });
        tb_show('You are leaving the BucktheNorm Web site', '#TB_inline?height=190&width=350&inlineId=speedbump');
        return false;
      }
    });
  });

  $('a.video_player').livequery(function() {
    
    $(this).unbind('click').click(function(e) {

      e.preventDefault();

      var swf_path = $(this).attr('href');

  		$('#videoPlayer').empty().flash({
  			src: swf_path,
  			width: 425,
  			height: 344
  		});

  		tb_show($(this).attr('title') + " on YouTube", '#TB_inline?height=355&width=425&inlineId=videoPlayer');

    });

  });
    
  $('#vote-form .video-wrapper').livequery(function() {
    $(this).hover(function() {
      $(this).addClass('hover');
    }, function() {
      $(this).removeClass('hover');
    });
  });

  $('#vote-options').column_list();

  $('#vote-form .video').each(function(index, link) {
    
    link = $(link);

  	var video_id = jQuery.uri.setUri($(link).attr('href')).param("v");

    link.html('<img src="http://i2.ytimg.com/vi/' + video_id + '/default.jpg" />');
    
    var trigger = link.siblings('.trigger');
    
    var href = trigger.attr('href') + "?height=329&width=762";

    link.attr('href', href);
    trigger.attr('href', href);

    tb_init(link);
    tb_init(trigger);

  });

  $('.vote-form-wrapper input:radio').checkbox().click(function() {
    $('#cast-vote-wrapper').show();
  });
  
  $('.vote-form .description label').hover(function() {
    $(this).siblings('input').hover();
  });
  
  $('#comment-form form').validate({
    errorPlacement: function(error,element) {
      error.insertAfter(element);
    },
    submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
			  success: function(responseText, statusText) {
          $('.comment-form .thanks').show();
        }
			});
		}
  });
  
  $("#vote-form").validate({
    submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
			  target: '#cast-vote-wrapper',
			  success: function(responseText, statusText) {
          $('#vote-form .jquery-checkbox').hide();
          $('#vote-form').addClass('submitted');
        }
			});
		}    
  })

  $('#cast-vote').click(function(e) {
    e.preventDefault();
    if ($('#vote-form').valid()) {
      $('#vote-form').ajaxSubmit({ target: '#cast-vote-wrapper' });
      $('#vote-form .jquery-checkbox').hide();
      $('#vote-form').addClass('submitted');      
    }
  });
  
  var show_book_contest = function() {
  
    tb_show("Win your books!", '/book_contests?height=530&width=688');
  	$("#TB_window").addClass('contest-books');

  }

  $('a.win-books').click(function(e) {
    e.preventDefault();
    show_book_contest();
  });
  
  $('.poll_results li .result').livequery(function() {
    $(this).after('<div class="graph"><div class="result_bar" style="width:'+$(this).text()+';"></div></div>');
  });
  
});