function submit_once( ev ) {
  /* This handler prevents forms from being POSTed several times
     due to double-clicking of a submit button or multi-pressing Enter in an input

     To be used like this:
       $('form')
         .submit( validate )
         .submit( submit_once );
  */
  var form = jQuery(this);

  if (! form.data('is_submitting')) {
    form.data('is_submitting', true);
    form.find(':submit, input.submit')
      .attr('disabled','disabled')
      .val('Please wait...')
      .fadeTo('slow', 0.5);
  }
}

jQuery(document).ready( function($) {
	/* Sniff IE and add classes "ie" and "ieV" (V=6,7,8) to html tag.
	-----------------------------------------------------------*/
	if( $.browser == 'msie' ) {
	  $('body').addClass('ie');
	  $('body').addClass('ie' + parseInt($.browser.version)); // ex: ie8 
	}

  /* Accordion *
   * --------- */
  $('div.toggle-content').hide();
  $('div.toggle-header').click( function(){
    $(this).next('.toggle-content').toggle('fast');
  });
	
	/* Popup
	-----------------------------------------------------------*/
	$('.popup').click(function() 
	{
		var page = $(this).attr('href');
		window.open(page);
		return false;
	});
	
	$('.center-popup').click(function() 
	{
	    var height = 630;
	    if ( $(this).hasClass('mini') ) {
	        var height = 300;
	    }
	    var width = 500;
		var url = $(this).attr('href');
    	var top = (screen.height - height) / 2;
    	var left = (screen.width - width) / 2;
    	window.open(url, "_blank", 'location=0,status=0,scrollbars=0,top='+top+',left='+left+',width='+width+',height='+height);
    	return false;
	});
	
	
	/* Close window
	-----------------------------------------------------------*/
	$('a.close-window').click(function() {
	   window.close(); 
	});
	
	/* News Box
	-----------------------------------------------------------*/
	// There are 4 tabs: news, tutorials, campaign, hot-item
	current_content = '#news-content';
	
  $('#news-box-tabs a').click( function( ev ) {
      ev.stopPropagation();
      ev.preventDefault();

      // A tab was clicked. Which one?
      tab = $('#' + this.id);
      new_content = '#' + this.id.replace('-tab', '-content');
      // Set all tabs to off except this one
      $('#news-box-tabs a').removeClass('selected-news-tab');
      tab.addClass('selected-news-tab');
      // Now fade out the current content and fade in the new content
      $(current_content).fadeOut('fast', function() {
          $(new_content).fadeIn();
      });
      current_content = new_content;
      return false;
  });

  /* Replace submit buttons
	-----------------------------------------------------------*/
  // Replace all pretty-submit buttons with a link
  $('input.image-submit').each( function() {
      var new_tag = '<a id="' + $(this).attr('id') + '" href="submit" class="' + $(this).attr('class') + '"';
      
      // Special case for language toggle, keep the 'en' attribute if there was one
      var en_value = $(this).attr('en');
      if (en_value) {
        new_tag += ' en="' + en_value + '"';
      }
      new_tag += '>' + this.value + '</a>';
      
      // Now attach the submit behavior to these links
      var new_jq = $( new_tag ).click( function( ev ) {
        ev.stopPropagation();
        ev.preventDefault();

        $(this).parents('form').submit();
        return false;
      });

      $(this).replaceWith( new_jq );
  });
  
  /* Auto focus
  -----------------------------------------------------------*/
  $('form .focus:first').focus();
  
  /* Toggle English / Japanese for any tag which has en="" attribute
  -----------------------------------------------------------*/
  function toggle_language() {
    // Hotkey was pressed, switch language for all translatable_tags
    if (current_lang == 'ja') {
      // Translate to English
      translatable_tags.each( function() {
        $(this)
          .html( $(this).attr('en') )
          .filter(':visible').not(':animated')
            .hide()
            .fadeIn();
      });
      current_lang = 'en';
    }
    else {
      // Translate back to Japanese
      translatable_tags.each( function() {
        $(this)
          .html( $(this).attr('ja') )
          .filter(':visible').not(':animated')
            .hide()
            .fadeIn();
      });
      current_lang = 'ja';
    }
    return false;
    //console.log(current_lang);
  }

  if ( $('body.translatable').length ) {
    var current_lang = 'ja';
    var translatable_tags = $('[en]');
    // Set the ja attr to the content of the tag
    translatable_tags.each( function() {
      $(this).attr('ja', $(this).html());
    });
    
    $(document).bind('keydown', 'Shift+esc', function() {
      toggle_language();
      $('#toggle-language')
        .toggleClass('orangellow')
        .toggleClass('magenta');
    });
    $('#toggle-language').click( function() {
      toggle_language();
      $(this)
        .toggleClass('orangellow')
        .toggleClass('magenta');
    });
  }
});

