﻿/*
 -------------------------------
 The Eat creative 
 JavaScript actions document
 2011
 -------------------------------
*/

/*
 -------------------------------
 Globals
 -------------------------------
*/
var nice_browser = true;

var more_en = "More";
var more_ja = "More";

var less_en = "Close";
var less_ja = "Close";

/*
 -------------------------------
 Platform Detect
 -------------------------------
*/
if ( (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
	nice_browser = false;
}

/*
 -------------------------------
 Page Fade Module
 -------------------------------
*/

var darth_fader = function(){
	
	var speed = 200;
	var $cover;
	
	function init(){
		// --- Adjust the cover
		$cover = $('#trans');
		heightReset();
		
		// --- Change menu to fading links
		$(document).delegate('.menu a', 'click', function(e){
			if( $(this).is(":contains('Contact')") || $(this).is(":contains('問い合わせ')") ){
				if($('#map').is(":visible")){
					first_contact.hide();
				}else{
					first_contact.show();
				}
			}else{
				if( $(this).is(":contains('@')") ){
					window.location = $(this).attr('href');
				}else{
					darth_fader.fadeIn($(this).attr('href'));
				}
			}
			e.preventDefault();
		});
		
	}
	
	function heightReset(who){
		$cover.css('height', $(document).height());
	}
	
	function fadeIn(url){
		heightReset();
		$cover.fadeIn(speed, function(){
			window.location = url;
		});
		setTimeout(function(){window.location = url;},speed+10)
	}
	
	function fadeOut(){
		$cover.fadeOut(speed);
	}

	return { init:init, fadeIn:fadeIn, fadeOut:fadeOut }

}();

/*
 -------------------------------
 Panel Module
 -------------------------------
*/

var peter_panel = function(){
	
	function init(){
		// --- Find every highliter line
		$.each($('.highliter'), function(i, val) {
			if($(this).parent().attr('class')=='row'){
				$(this).html( highliter($(this), true));
			}else{
				$(this).html( highliter($(this), false));
			}
		});
		
	}
	
	function highliter(who, long){

		// --- Get path to secret
		var pathBack = who.parent();
		
		// --- Cut up the raw text
		var broken_line = who.text().split(' ');
		var rough = $('<div/>',{'class':'hilite_box'});
		
		$.each(broken_line, function(i, val){
			var hi_class = 'highlight';
			if(i==broken_line.length-1){
				hi_class = 'highlight';
			}
			
			$('<span/>',{'class':hi_class,html:val+' '}).appendTo(
				$('<span/>', {'class':'marker'}).appendTo(rough)
			);

		});
		
		if(pathBack.children('.bonus_box').length>0){
			var more_text = more_en;
			var less_text = less_en;
			if(lang=="ja"){ 
			    more_text = more_ja;
			    less_text = less_ja; 
			}
			
			var bonus_bar = $('<div/>',{'class':'bonus_long'});
			$('<div />', {'class':'more_text',html:more_text}).appendTo(bonus_bar);
			$('<div />', {'class':'less_text',html:less_text}).appendTo(bonus_bar);
			
			bonus_bar.appendTo(rough);
		}
		
		return rough;
		
	}
	
	return { init:init }

}();

/*
 -------------------------------
 Sorting Module
 -------------------------------
*/

var sort_of = function(){
	
	function init(){
		// --- Adjust link actions
		$(document).delegate('.work-menu a', 'click', function(e){
		
			// --- Set selection
			$('.work-menu a').removeClass("selected");
			$(this).addClass("selected");
			
			// --- Sort box
			var cat = $(this).attr("href");
			sort_of.sort(cat);
			
			e.preventDefault();
		});

		
	}
	
	function sort(cat){
		// --- Clear all
		$(".link").removeClass("selected");
		
		// --- Make special highlighted
		var chosenOnes = $("#work_nav ." + cat);
		$.each(chosenOnes, function(){
			$(this).addClass("selected");
		});
	}
	
	return { init:init, sort:sort }

}();

/*
 -------------------------------
 Corner logo animation
 -------------------------------
*/

var eat_rolls = function(){
	
	var speed  = 300;
	var toggle = true;
	
	function init(){
		
		$('#logo').delegate('h1', 'mouseenter mouseleave', function(){

			if(toggle){
				$('#logo').find('.highlight').css({ 'background':'red' });
			}else{
				$('#logo').find('.highlight').css({ 'background':'white' });
			}
			
			toggle = !toggle;
			
		});
		
	}
	
	return { init:init }
	
}();

/*
 -------------------------------
 Bonus window
 -------------------------------
*/

var top_secret = function(){

	var pic_show   = false;
	var chosen_one = "";
	var speed = 700;
	var is_open = false;

	function init(){
		
		// --- Open/close bonus information windows
		$(document).delegate('.bonus_long', 'click touchend', function(e){
			if(!is_open){
				is_open = true;
				chosen_one = "";
   				display( $(this).parent().parent().parent() );
   			}
   		});
   		
   		// --- Show/Hide thumbnails behind navigation area
   		$(document).delegate('.sample_pic', 'mouseenter mouseleave', function(){
   			var cap = $(this).attr("title");
   			if(lang=='ja'){
   				cap = $(this).attr("alt");
   			}
   			if($(window).width() <= 320){
   				$(".SamplePic").attr("src",$(this).attr("link"))
   			}else{
   				pickers(!pic_show, $(this).attr("link"), cap);
   			}
		});
		
		// --- OUCH!!!! This handle is a bit overkill but it works....
		$(document).bind('mousemove', function(){
			if(!pic_show){
				pic_show = true;
				pickers(false, "", "");
			}
		});
		
		// --- If Images arn't showing up
		$(document).delegate('.bonus_footer', 'click', function(){
			//display( $(this).parent().parent().parent().parent().parent().parent().parent() );
		});
		
		hiders($(document));
		
	}
	
	function display(who){
		
		if(chosen_one !== who.index()){
			
			chosen_one = who.index();
   			hiders(who);
   			
   			BB = who.find('.bonus_box');
   			BB_open = BB.is(":visible");
   			  			
   			if(BB_open){
   			
   				BB.slideUp( speed, function(){
					who.find('.less_text').stop(true,true).fadeOut( 'fast', function(){
   						who.find('.more_text').stop(true,true).fadeIn( 'fast', function(){
   							is_open = false;
   						});
   					});
   				});
   				
   			}else{
   			
   				BB.show();
   				BB_high = BB.height();
   				BB.css('height',0);
   				BB.hide();
   			
   				BB.show().animate( {height: BB_high}, speed, function(){
   					who.find('.more_text').stop(true,true).fadeOut( 'fast', function(){
   						who.find('.less_text').stop(true,true).fadeIn( 'fast', function(){
   							is_open = false;
   						});
   					});
   				});
   			}
   			
   		}
   		
   		
   		
	}
	
	function clear(){
		//$('.bonus_box').addClass( 'hidden' );
		
		$.each($('.bonus_box'), function(){
			//if(chosen_one !== $(this).parent().parent().parent().index()){
				$('.bonus_box').stop(true,false).slideUp( speed ,function(){
					$('.less_text').stop(true,true).fadeOut( 'fast', function(){
						$('.more_text').stop(true,true).fadeIn( 'fast' );
					});	
				});
			//}
		});
		
		//top_secret.scroll('TOP');
		chosen_one = "";
		
	}
	
	function close( who ){
		if(chosen_one !== who.index()){
			$('.bonus_box').slideUp( speed );
			$('.less_text').stop(true,true).fadeOut( 'fast', function(){
				$('.more_text').stop(true,true).fadeIn( 'fast' );
			});	
			chosen_one = "";
		}else{
			who.find('.bonus_box').slideUp( speed, function(){
				chosen_one = "";
				$('.more_text').stop(true,true).fadeOut( 'fast', function(){
					$('.less_text').stop(true,true).fadeIn( 'fast' );
				});	
			} );
		}
	}
	
	function scroll( who, anchor, speed ){
	
		if(who == 'TOP'){
			$('html,body').stop(true,true).delay(speed).animate({ 'scrollTop': 0 }, speed);
		}else{
			if(chosen_one !== ""){
				$('html,body').stop(true,true).delay(speed).animate({ 'scrollTop': anchor }, speed);
			}
		}
		
	}
	
	function pickers(cmd, img, text){
	
	
		if($(document).find('#work_nav').width() > 0){
			
			pic_show = !pic_show;
			var cat_pos  = $('#work_nav').offset();
			var side_bar = cat_pos.left;
			var more_top = -20;
			
			// --- Replace navigation header titles
			$.each($('.link'), function(){
				if(cmd){
					var pos = $(this).offset();
					var from_top = (pos.top+more_top)*-1
					var from_edge = pos.left*-1 + side_bar;
					$(this).attr('background',$(this).css('background'));
					$(this).addClass('hide');
					$(this).animate({'background': from_edge + 'px' + from_top + 'px'}, 50, function(){
						$(this).css({'background':'url(' + img + ') no-repeat ' + from_edge + 'px ' + from_top + 'px #fff'});
					});
				}else{
					$(this).removeClass('hide');
					$(this).css({'background':$(this).attr('background')});
				}
			});
			
			// --- Replace marker text
			$.each($('#content .marker span'), function(){
				if(cmd){
					var pos = $(this).offset();
					var from_top = (pos.top+more_top)*-1;
					var from_edge = pos.left*-1 + side_bar;
					$(this).attr('background',$(this).css('background'));
					$(this).addClass('hide');
					$(this).animate({'background': from_edge + 'px' + from_top + 'px'}, 50, function(){
						$(this).css({'background':'url(' + img + ') no-repeat ' + from_edge + 'px ' + from_top + 'px #fff'});
					});
				}else{
					$(this).removeClass('hide');
					$(this).css({'background':$(this).attr('background')});
				}
			});
			
			$.each($('.bonus_long'), function(){
				if(cmd){
					var pos = $(this).offset();
					var from_top = (pos.top+more_top)*-1;
					var from_edge = pos.left*-1 + side_bar;
					$(this).attr('background',$(this).css('background'));
					$(this).addClass('hide');
					$(this).animate({'background': from_edge + 'px' + from_top + 'px'}, 50, function(){
						$(this).css({'background':'url(' + img + ') no-repeat ' + from_edge + 'px ' + from_top + 'px #fff'});
					});
				}else{
					$(this).removeClass('hide');
					$(this).css({'background':$(this).attr('background')});
				}
			});
			
			// --- Add a caption box
			if(text!==""){
				$('.caption_box').remove();
				$('<span/>',{ 'class':'caption_box', 'text': text }).appendTo( $('#caption_area') );
				if(!pic_show){
					$('.caption_box').remove();
				}
			}
		
		}
		
	}
	
	function hiders(pathBack){
		pathBack.find('.bonus_footer').html( $('.thumbs').html() );
	}
	
	return { init:init, display:display, pickers:pickers, clear:clear, close:close, scroll:scroll }

}();

/*
 -------------------------------
 Contact window
 -------------------------------
*/

var first_contact = function(){
	
	var speed = 250;
	var Xtra;
	
	function init(){ Xtra = $('#map'); }
	function show(){ Xtra.slideDown(speed); }
	function hide(){ Xtra.slideUp(speed); }
	
	return { init:init, show:show, hide:hide }

}();

/*
 -------------------------------
 Special window
 -------------------------------
*/

var secret_garden = function(){
	
	var speed = 250;
	
	function init(){
		
		// --- Get the "special" word
		reword();
		
		// --- Only show it to rolloverers
		$(document).delegate('.logo_row', 'mouseenter', function(){
   			show();
		});
		
		$(document).delegate('.logo_row', 'mouseleave', function(){
   			hide();
		});
	
	}
	
	function reword(){
		
		// --- Find the word list
		var wl = $('#top_surprise').text().split(",");
		var n = Math.random()*(wl.length-1);
		var word = wl[~~(0.5 + n)];
		
		// --- Clear the old word
		$('.logo_rowlet').remove();
		
		// --- Append the new one
		/*
		$('<span/>',{ 'class':'highlight', 'text': word }).appendTo(
			$('<div/>',{'class':'logo_rowlet'}).appendTo(
				$('<h1/>').appendTo($('#logo a:first'))
			)
		);
		*/
		
	}
	
	function show(){
		reword();
		$('.logo_rowlet').stop(true, true).fadeIn(speed);
	}
	
	function hide(){
  		$('.logo_rowlet').stop(true, true).fadeOut(speed);
	}
	
	return { init:init, show:show, hide:hide, reword:reword }

}();

/*
 -------------------------------
 Internal Links
 -------------------------------
*/

var in_de_go = function(){
	
	var speed = 500;
	
	function init(){

		$.each( $('.in_link'), function(){
			$(this).attr( 'anchor', link_top( $(this).attr("href") ) );
		});

		$(document).delegate('.in_link', 'click', function(e){
			
			// --- Collect link information
			var link = $(this).attr("href");
			var anchor = $(this).attr("anchor");
			
			// --- Hide and tell
			top_secret.clear(  $(link).parent());
			top_secret.display( $(link).parent() );
			top_secret.scroll( $(link).parent(), anchor, speed );
			
			e.preventDefault();
			
		});
	}
	
	function link_top(who){
		var me = $(who).offset();
		var tops = me.top - 30;
		return tops;
	}
	
	return { init:init }

}();

/*
 -------------------------------
 Register when ready
 -------------------------------
*/
$(document).ready( function(){
	
	// Contact Window
	first_contact.init();
	
	// Panel Maker
	peter_panel.init();
	
	// Category Sort
	sort_of.init();
	
	// Corner Logo -- If an extra animation is needed
	//eat_rolls.init();
	
	// Bonus Window
	top_secret.init();
	
	// Special Window
	secret_garden.init();

	// Internal Links
	in_de_go.init();
	
	// Page Fade
	darth_fader.init();
	darth_fader.fadeOut();
	
	// Hide addressbar for mobile version
	window.scrollTo(0,1);

	// Mousewheel takeover
	$(window).bind('mousewheel', function(){
		$('html,body').stop(true,true);
	});
	
	$(window).bind('scroll', function(){
		if(nice_browser){
			$('#navigation').stop(true,false).animate({'opacity':'0'}, function(){
				$('#navigation').css({'margin-top':$(window).scrollTop()}).animate({'opacity':'1'}, 'slow');
			});
		}
	});
	
	if($(window).width() <= 320){
		var samplePic = $("<img/>").addClass("SamplePic")
   		$(".main_zone").append(samplePic);
   	}
	
});
