$(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 
	}
	
	/* 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() {
        // 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() {
          $(this).parents('form').submit();
          return false;
        });

        $(this).replaceWith( new_jq );
    });
    
    /* Auto focus
    -----------------------------------------------------------*/
    $( function() {
        $('form .focus').focus();
    });
    
    /* Toggle English / Japanese for any tag which has en="" attribute
    -----------------------------------------------------------*/
    if ( $('body.translatable') ) {
      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() {
        // 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') ).hide().fadeIn();
          });
          current_lang = 'en';
        }
        else {
          // Translate back to Japanese
          translatable_tags.each( function() {
            $(this).html( $(this).attr('ja') ).hide().fadeIn();
          });
          current_lang = 'ja';
        }
        return false;
        //console.log(current_lang);
      });
    }
    

});
