/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
 (function($) {
	jQuery.cookie = function (key, value, options) {
	
	    // key and at least value given, set cookie...
	    if (arguments.length > 1 && String(value) !== "[object Object]") {
	        options = jQuery.extend({}, options);
	
	        if (value === null || value === undefined) {
	            options.expires = -1;
	        }
	
	        if (typeof options.expires === 'number') {
	            var days = options.expires, t = options.expires = new Date();
	            t.setDate(t.getDate() + days);
	        }
	
	        value = String(value);
	
	        return (document.cookie = [
	            encodeURIComponent(key), '=',
	            options.raw ? value : encodeURIComponent(value),
	            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
	            options.path ? '; path=' + options.path : '',
	            options.domain ? '; domain=' + options.domain : '',
	            options.secure ? '; secure' : ''
	        ].join(''));
	    }
	
	    // key and possibly options given, get cookie...
	    options = value || {};
	    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
	};
})(jQuery);


(function($) {
	if (!Array.prototype.indexOf)
	{
	  Array.prototype.indexOf = function(elt /*, from*/)
	  {
	    var len = this.length;
	
	    var from = Number(arguments[1]) || 0;
	    from = (from < 0)
	         ? Math.ceil(from)
	         : Math.floor(from);
	    if (from < 0)
	      from += len;
	
	    for (; from < len; from++)
	    {
	      if (from in this &&
	          this[from] === elt)
	        return from;
	    }
	    return -1;
	  };
	}

	var name_space = 'basePlugin';
	//$.plugInClass = function(){	this.construct(); };

	$[name_space] = function( elements, options ) {
		//console.log(options);
		var settings = this.options = $.extend({
			'elements': $(elements),
			'navigation': '#pageNavigation',
			'indicator': {
				'selector':	'indicator',
				'code' :	'<p class="indicator"></p>',
				'width':	'65px',
				'height':	'3px'
				},
			'current':	'',
			'position': {
				'news':		'62',
				'about':	'219',
				'works':	'377',
				'essay':	'525',
				'contact':	'678'
				}
		}, options);

		this.init();
		return this; //method chain
	};

	$[name_space].prototype = {
		options : null,
		animation : function ( key, link ) {
			var s = this.options;
			var $indicator = $(s.navigation +' .'+ s.indicator.selector);
			var currentPosition = $indicator.position();
			currentPosition = currentPosition.left;
			var nextPosition = s.position[key] + 'px';
			$indicator.animate(
				{'left':nextPosition},
				{
					duration: 200,
					complete: function(){ location.href = link;}
				}
			);
		},

		init : function () {
			var self = this;
			var s = this.options;
			var leftPosition = '0px';
			var sp = $('body').attr('class').split(' ');
			$('html').addClass('javascript');
			if(s.navigation === '#pageNavigation'){
				if(sp.indexOf('news') !== -1) s.current = 'news';
				if(sp.indexOf('about') !== -1) s.current = 'about';
				if(sp.indexOf('works') !== -1) s.current = 'works';
				if(sp.indexOf('essay') !== -1) s.current = 'essay';
				if(sp.indexOf('contact') !== -1) s.current = 'contact';
				//console.log(s.current);
				if(s.current === undefined) s.indicator.code = null;
			} else if(s.navigation === '#localNavigation'){
				s.current = $('#pageTop').attr('class');
				if(s.current === undefined) s.indicator.code = null;
				//console.log(s.current);
			} else if(s.navigation === '#archiveNavigation'){
				s.current = $('#pageType').attr('class');
				if(sp.indexOf('date') === -1) {
					s.indicator.code = null;
					s.current = undefined;
				}
				//if(s.current === undefined || s.current === '') s.indicator.code = null;
				//console.log(s.current);
			}
			leftPosition = s.position[s.current] + 'px';
			$(s.navigation).prepend(s.indicator.code); // indicatorを生成
			$(s.navigation +' .'+ s.indicator.selector).css({'left': leftPosition});

			if(s.current !== undefined && s.current !== ''){
				s.elements.bind({
					click: function (event){
						event.preventDefault();
						var key = $(this).parent().attr('class');
						var link = $(this).attr('href'); 
						self.animation(key, link);
						return false;
					}
				});
			}
		}
	};

	$(function(){
		if($('body.home').length === 0 && $('body.error404').length === 0){
			console.log($('body.error404').length);
			new $[name_space](
				'#pageNavigation dd a',
				{
				'navigation': '#pageNavigation',
				'indicator': {
					'selector':	'indicator',
					'code':	'<p class="indicator"></p>'
					},
				'position': {
					'news':		'62',
					'about':	'219',
					'works':	'377',
					'essay':	'525',
					'contact':	'678'
					}
				}
			);
			new $[name_space](
				'#archiveNavigation dd a',
				{
				'navigation': '#archiveNavigation',
				'indicator': {
					'selector':	'indicator',
					'code':	'<p class="indicator"></p>'
					},
				'position': {
					'jan':		'22',
					'feb':		'85',
					'mar':		'148',
					'apr':		'211',
					'may':		'274',
					'jun':		'337',
					'jul':		'400',
					'aug':		'463',
					'sep':		'526',
					'oct':		'589',
					'nov':		'652',
					'dec':		'715'
					}
				}
			);

		}
		if($('#pageTop.index').length === 0){
			new $[name_space](
				'body.works #localNavigation dd a',
				{
				'navigation': '#localNavigation',
				'indicator': {
					'selector':	'indicator',
					'code' :	'<p class="indicator"></p>'
					},
				'position': {
					'graphics':		'14',
					'advertising':	'115',
					'branding':		'216',
					'exhibition':	'314',
					'campaign':		'414',
					'book':			'502',
					'logo':			'599',
					'package':		'703'
					}
				}
			)
		}
	});
})(jQuery);

(function($) {

/*	markup
	<div id="opening">
		<div class="animation-content">
			<div class="stage">
				<div class="compo_logo">
					<p id="v-lines" class="cast v-lines"><img src="images/home/logo-opening.png" alt="" /></p>
					<p id="h-g1" class="cast h-lines">&nbsp;</p>
					<p id="h-g2" class="cast h-lines">&nbsp;</p>
					<p id="h-g3" class="cast h-lines">&nbsp;</p>
					<p id="h-n1" class="cast h-lines">&nbsp;</p>
					<p id="h-c1" class="cast h-lines">&nbsp;</p>
					<p id="h-c2" class="cast h-lines">&nbsp;</p>
				</div><!-- .compo_logo -->
			</div><!-- .stage -->
		</div><!-- .animation-content -->
	</div><!-- #opening -->
*/	

	var mode = 'jquery'

	$(function () {
		if(document.referrer === 'http://www.nginc.jp/') $.cookie('ngHome', 0, { path: '/' });
		if( $('body.home').length && ($.cookie('ngHome') === null || $.cookie('ngHome') === '0') ) {
			$('body').append('<div id="opening"><div class="animation-content"><div class="stage"><div class="compo_logo"><p id="v-lines" class="cast v-lines"><img src="images/home/logo-opening.png" alt="" /></p><p id="h-g1" class="cast h-lines">&nbsp;</p><p id="h-g2" class="cast h-lines">&nbsp;</p><p id="h-g3" class="cast h-lines">&nbsp;</p><p id="h-n1" class="cast h-lines">&nbsp;</p><p id="h-c1" class="cast h-lines">&nbsp;</p><p id="h-c2" class="cast h-lines">&nbsp;</p></div></div></div></div>');
			
			setTimeout(function( ){
				// s1
				$('#v-lines').animate({'opacity': '0', leaveTransforms:true }, 750, function() {
					// s2
					$('#h-g1').animate({'left': '0', 'width': '215px', leaveTransforms:true }, 750);
					$('#h-g2').animate({'left': '215px', 'width': '107px', leaveTransforms:true }, 750);
					$('#h-g3').animate({'left': '322px', 'width': '215px', leaveTransforms:true }, 750);
					$('#h-n1').animate({'left': '537px', 'width': '58px', leaveTransforms:true }, 750);
					$('#h-c1').animate({'left': '595px', 'width': '120px', leaveTransforms:true }, 750);
					$('#h-c2').animate({'left': '715px', 'width': '120px', leaveTransforms:true }, 750, function() {
						// s3
						$('#h-g1').delay(200).animate({'top': '0', leaveTransforms:true }, 750);
						$('#h-g2').delay(200).animate({'top': '0', leaveTransforms:true }, 750);
						$('#h-g3').delay(200).animate({'top': '0', leaveTransforms:true }, 750);
						$('#h-n1').delay(200).animate({'top': '0', leaveTransforms:true }, 750);
						$('#h-c1').delay(200).animate({'top': '0', leaveTransforms:true }, 750);
						$('#h-c2').delay(200).animate({'top': '0', leaveTransforms:true }, 750, function() {
							// s4
							$('#opening').animate({'opacity': '0', leaveTransforms:true }, 750, function() {
								// end
								$('#opening').remove();
								$.cookie('ngHome', 0, { path: '/' });
							});
						});
					});
				});
			}, 1500); //3000
		}
		if( !$('body.home').length ) {
			$.cookie('ngHome', 1, { path: '/' });
		}
		if( $('body.about').length ){
			var h = $('#philosophy').height();
			h = h + 50;
			$('#slider').css({'height': h+'px'});
			$('#aboutus p.btn-philosophy a').bind('click', function(){
				$('#slider-contents').animate({'left': '-960px'}, 500);
				return false;
			});
			$('#philosophy p.btn-aboutus a').bind('click', function(){
				$('#slider-contents').animate({'left': '0'}, 500);
				return false;
			});
		}
	});
})(jQuery);

