var _realkana = {
	'lookup': {
		'hiragana': 'h',
		'hiragana-double': 'hd',
		'katakana': 'k',
		'katakana-double': 'kd',
		'katakana-extended': 'ke',
		'options': 'o'
		},
	'plural': {
		'de': false,
		'en': false,
		'fr': true,
		'pl': false,
		'pt': true,
		'sv': false
		},
	'right': 0,
	'wrong': 0
	};

function __check( _check ) {
	$( 'table input' ).attr( 'checked', _check );
	__set();
	}

function __guess() {
	if ( ! $( '#guess' )[ 0 ].disabled ) {
		if ( $( '#guess' ).val() ) {
			if ( _realkana.answer.indexOf( '_' + $( '#guess' ).val().toLowerCase() + '_' ) >= 0 ) {
				$.getJSON( '/practice/json/', { 'i': _realkana.id }, __next );
				pageTracker._trackPageview( '/practice/json/' );
				$( 'p' ).css( 'visibility', 'hidden' );
				$( 'input' ).blur();
				$( 'input:eq(1)' ).attr( 'disabled', 'disabled' );
				$( '#wrong' ).css( 'visibility', 'hidden' );
				$( '#bottom-left span' ).html( ++_realkana.right );
				if ( ( _realkana.right == 2 ) && _realkana.plural[ $( '#all' ).attr( 'class' ) ] ) {
					$( '#bottom-left' ).append( 's' );
					}
				}
			else {
				$( '#guess' ).focus();
				$( '#wrong' ).css( 'visibility', 'visible' );
				$( '#bottom-right span' ).html( ++_realkana.wrong );
				if ( ( _realkana.wrong == 2 ) && _realkana.plural[ $( '#all' ).attr( 'class' ) ] ) {
					$( '#bottom-right' ).append( 's' );
					}
				}

			$( '#guess' ).val( '' );
			}
		else {
			$.getJSON( '/practice/json/', { 'i': _realkana.id }, __next );
			pageTracker._trackPageview( '/practice/json/' );
			}
		}

	$( '#bottom-left' ).css( 'visibility', 'hidden' );
	$( '#bottom-center' ).css( 'visibility', 'hidden' );
	$( '#bottom-right' ).css( 'visibility', 'hidden' );

	return false;
	}

function __href( _href ) {
	location.href = _href;
	}

function __init() {
	var _page, _src;

	if ( document.cookie.search( /c=(h\d+hd\d+k\d+kd\d+ke\d+o\d+)/ ) != -1 ) {
		_realkana.cookie = RegExp.$1;
		}
	else {
		_realkana.cookie = 'h1hd0k1kd0ke0o1';
		}

	if ( $( '#nav' ).height() == 43 ) {
		$( '#adsense' ).show();

		$( 'a' ).each(
			function () {
				var _href = $( this ).attr( 'href' ),
					_id = $( this ).attr( 'id' );

				$( this )
					.removeAttr( 'href' )
					.click(
						function () {
							if ( _id.match( /^id-\d{9}$/ ) && _href.match( /^http/ ) ) {
								_id = _id.replace( /id-/, '' );
								pageTracker._trackPageview( '/goal/click/' + _id + '/' );
								$.get( '/log/', { 'id': _id }, function () { __href( _href ) } );
								}
							else {
								location.href = _href;
								}
							}
						);
				}
			);
		}

	_page = $( 'body' ).attr( 'id' );

	if ( _page == 'practice' ) {
		$( '#wrong' ).css( 'visibility', 'hidden' );
		$( '#bottom-left' ).css( 'visibility', 'hidden' );
		$( '#bottom-center' ).css( 'visibility', 'hidden' );
		$( '#bottom-right' ).css( 'visibility', 'hidden' );

		$( '#question' ).hover(
			function () {
				$( '#bottom-left' ).css( 'visibility', 'visible' );
				$( '#bottom-center' ).css( 'visibility', 'visible' );
				$( '#bottom-right' ).css( 'visibility', 'visible' );
				}
			);

		$( 'form' ).submit( __guess );

		$.getJSON( '/practice/json/', __next );
		}
	else if ( ( _page == 'options' ) || ( $( 'body' ).attr( 'class' ) == 'kana' ) ) {
		$( '#bottom-left input:eq(0)' ).click( function () { __check( true ) } );
		$( '#bottom-left input:eq(1)' ).click( function () { __check( false ) } );
		$( 'table input' ).click( __set );
		}
	else if ( _page == 'home' ) {
		$( '<img>' ).attr( 'src', '/img/kana_options_bg.png' )
		$( '<img>' ).attr( 'src', '/img/1_bg.png' )
		}
	else if ( _page == 'shop' ) {
		$( '#carousel' ).jCarouselLite( {
			btnNext: '.more',
			btnPrev: '.back',
			auto: 5000,
			speed: 500,
			visible: 1
			} );
		}
	else if ( _page == 'iphone' ) {
		$( '#review' ).jCarouselLite( {
			auto: 5000,
			speed: 1500,
			vertical: true,
			visible: 3
			} );
		}
	else if ( _page == 'photo' ) {
		$( '#kana' ).before( '<div id="show">Kana</div>' );
		_src = $( '#right img' ).attr( 'src' );
		_src = _src.match( /\d+/ );
		$( '#kana' ).css( 'background-image', 'url( /img/photos/' + _src + '_bg.png )' );
		$( '#show' ).click( __kana );
		if ( GBrowserIsCompatible() ) {
			_map = new GMap2( document.getElementById( 'map' ) );
			_map.setCenter( new GLatLng( _realkana.lat, _realkana.lon ), 15 );
			_ui = _map.getDefaultUI();
			_ui.controls.menumaptypecontrol = false;
			_map.setUI( _ui );
			_marker = new GMarker( new GLatLng( _realkana.lat, _realkana.lon ) );
			_map.addOverlay( _marker );
			}
		}
	}

function __kana() {
	$( '#show' ).hide();
	$( '#kana' ).show();
	}

function __map( _lat, _lon ) {
	var _latA, _lonA, _map, _marker, _re, _ui;

	_re = /[ENSW] +([0-9]+)d +([0-9]+(?:\.[0-9]+)?)m +([0-9]+(?:\.[0-9]+)?)s/;

	_latA = _lat.match( _re );
	_lonA = _lon.match( _re );

	_realkana.lat = ( _latA[ 1 ] - 0 + _latA[ 2 ] / 60 + _latA[ 3 ] / 3600 );
	_realkana.lon = ( _lonA[ 1 ] - 0 + _lonA[ 2 ] / 60 + _lonA[ 3 ] / 3600 );
	}

function __next( _d, _s ) {
	$( '#question' ).attr( 'class', 'k-' + _d.i + ' t-' + _d.t + ( ( _d.i > 142 ) ? ' w-64' : '' ) );
	$( '#question span' ).html( _d.k );
	$( '#bottom-center' ).html( '"' + _d.r + '"' );

	_realkana.answer = '_' + _d.a + '_';
	_realkana.id = _d.i;

	$( 'input:eq(1)' )
		.removeAttr( 'disabled' );

	$( '#guess' )
		.attr( 'name', 'g-' + Math.floor( Math.random() * 1000000000 ) )
		.val( '' )
		.focus();
	}

function __set() {
	var _cookie, _i, _regexp, _value;

	_i = _value = 0;

	$( 'table input' ).each(
		function () {
			if ( $( this ).attr( 'checked' ) )
				_value += Math.pow( 2, _i );
			_i++;
			}
		);

	if ( $( 'body' ).attr( 'id' ) == 'options' ) {
		if ( _value > 1 ) {
			$( '<img>' ).attr( 'src', '/img/2_bg.png' );
			}
		if ( _value > 3 ) {
			$( '<img>' ).attr( 'src', '/img/3_bg.png' );
			}
		if ( _value > 7 ) {
			$( '<img>' ).attr( 'src', '/img/4_bg.png' );
			}
		if ( _value > 15 ) {
			$( '<img>' ).attr( 'src', '/img/5_bg.png' );
			}
		if ( _value > 31 ) {
			$( '<img>' ).attr( 'src', '/img/6_bg.png' );
			}
		if ( _value > 63 ) {
			$( '<img>' ).attr( 'src', '/img/7_bg.png' );
			}
		}

	_regexp = new RegExp( '(' + _realkana.lookup[ $( 'body' ).attr( 'id' ) ] + '\\d+)' );
	_realkana.cookie = _realkana.cookie.replace( _regexp, _realkana.lookup[ $( 'body' ).attr( 'id' ) ] + _value );
	document.cookie = ( 'c=' + _realkana.cookie + '; path=/; domain=realkana.com' );
	}

/* jCarouselLite 1.0.1 */

(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);

$( __init );
