/**
 * @author Tomohiro Tsutsumi @team-chan3.com
 */


/*----------------------------------------------------
	GNavi
----------------------------------------------------*/

function GNavi(){
	this._INTERVAL_TIME = 120;
	this._isOpen = false;
	
	var owner = this;
	this._gnavi = $('#gnavi');
	if(!this._gnavi) return;
	
	this._nav = this._gnavi.find('nav');
	this._top = this._gnavi.css('top');
	this._left = this._gnavi.css('left');
	this._top = Number(this._top.replace('px', ''));
	this._left = Number(this._left.replace('px', ''));
	
	
	this._parent = this._gnavi.parent();
	
	this._open = this._gnavi.find('.open');
	this._openBtn = this._open.children('span');
	this._close = this._gnavi.find('.close');
	this._closeBtn = this._close.children('span');
	
	this._navs = this._gnavi.find('.navchild');
	
	utils.setAlphaBtn(this._navs.find('a'));
	utils.setAlphaBtn(this._openBtn, 0.7);
	utils.setAlphaBtn(this._closeBtn, 0.7);
	
	this._navList = [];
	this._showList = [];
	
	this._intervalId;
	this._black;
	
	this._gnW = this._gnavi.width();
	this._gnH = this._gnavi.height();
	this._gnHW = this._open.width()+20;
	this._gnHH = this._open.height()+20;
	
	//
	this._gnavi.css({
		top : owner._top,
		width : this._gnHW, height : this._gnHH
	});
	
	if(!$.support.style){
		$('body').append(this._gnavi);
	}
	
	this._navs.each(function(i){
		var tmpMenu = new GNaviMenu('nav'+(i+1));
		tmpMenu.init();
		owner._navList.push(tmpMenu);
	});
}

GNavi.prototype.init = function(){
	if(!this._gnavi) return;
	var owner = this;
	this._open.bind({
		click : function(){
			owner._menuOpen();
		}
	});
	this._closeBtn.bind({
		click : function(){
			owner._menuClose();
		}
	});
	
	$(window).bind('resize', { owner:this }, this._onResize);
	$(window).bind('scroll', { owner:this }, this._onScroll);
	if(!$.support.style) this._onGNaviResize(this);
}

GNavi.prototype._stopInterval = function(){
	if(!this._intervalId) return;
	clearInterval(this._intervalId);
}

GNavi.prototype._menuOpen = function(){
	if(this._isOpen) return;
	this._isOpen = true;
	
	this._gnavi.css({
		width : this._gnW, height : this._gnH
	});
	this._nav.css({
		visibility : 'visible'
	});
	
	
	this._showBlack();
	this._hideCloseOrOpen(this._open);
	
	this._stopInterval();
	var owner = this;
	this._intervalId = setInterval(function(){
		owner._showMenu();
	}, this._INTERVAL_TIME);
}

GNavi.prototype._menuClose = function(){
	if(!this._isOpen) return;
	this._isOpen = false;
	
	this._hideBlack();
	this._hideCloseOrOpen(this._close);
	
	var owner = this;
	this._stopInterval();
	this._intervalId = setInterval(function(){
		owner._hideMenu();
	}, this._INTERVAL_TIME);
}


GNavi.prototype._showMenu = function(){
	var tmpMenu = this._navList[this._showList.length];
	this._showList.push(tmpMenu);
	tmpMenu.show();
	if(this._showList.length >= this._navList.length){
		this._stopInterval();
		this._showCloseOrOpen(this._close);
	}
}

GNavi.prototype._hideMenu = function(){
	var tmpMenu = this._showList.pop();
	tmpMenu.hide();
	//
	if(!this._showList.length){
		$(tmpMenu).bind('onHide', { owner:this }, this._onHideMenu);
		this._stopInterval();
		this._showCloseOrOpen(this._open);
	}
}

GNavi.prototype._onHideMenu = function(arg){
	var owner = arg.data.owner;
	owner._gnavi.css({
		width : owner._gnHW, height : owner._gnHH
	});
	owner._nav.css({
		visibility : 'hidden'
	});
}


GNavi.prototype._showCloseOrOpen = function(target){
	target.stop().css({
		visibility : 'visible',
		left : -target.width() - 10
	}).animate({
		left : 0
	}, 800, 'easeInOutQuart');
}

GNavi.prototype._hideCloseOrOpen = function(target){
	target.stop().animate({
		left : -target.width() - 10
	}, 400, 'easeInQuart', function(){
		target.css({
			visibility : 'hidden'
		});
	});
}

GNavi.prototype._showBlack = function(){
	var owner = this;
	this._black = utils.createFixBox('#000000', 1000);
	$('body').prepend(this._black);
	
	this._black.resize();
	
	
	this._black.stop().animate({
		opacity : 0.6
	}, 300);
}

GNavi.prototype._hideBlack = function(){
	var owner = this;
	this._black.stop().animate({
		opacity : 0
	}, 300, function(){
		$(this).remove();
		owner._black = null;
	});
}

GNavi.prototype._onGNaviResize = function(owner){
	owner._gnavi.stop().css({
		opacity : 0,
		left : owner._parent.offset().left + owner._left,
		top : owner._parent.offset().top + owner._top
	}).animate({
		opacity : 1
	}, 150);
}

GNavi.prototype._onResize = function(arg){
	var owner = arg.data.owner;
	if(owner._black) owner._black.resize();
	
	if(!$.support.style) owner._onGNaviResize(owner);
}

GNavi.prototype._onScroll = function(arg){
	var owner = arg.data.owner;
	if(owner._black) owner._black.resize();
}


/*----------------------------------------------------
	GNaviMenu
----------------------------------------------------*/

function GNaviMenu(id){
	this._id = id;
	this._menu = $('.'+this._id);
	this._hideLeft = -this._menu.width() - 10;
	this._initPos = {
		top : this._menu.position().top,
		left : this._menu.position().left
	};
	if(!$.support.style) this._menu.fixPng();
}

GNaviMenu.prototype.init = function(){
	var owner = this;
	this._menu.css({
		left : owner._hideLeft
	});
}

GNaviMenu.prototype.initialize = function(){
	var owner = this;
	this._menu.stop().css({
		visibility : 'hidden',
		left : owner._hideLeft
	});
}

GNaviMenu.prototype.show = function(){
	var owner = this;
	this._menu.stop().css({
		left : owner._hideLeft,
		visibility : 'visible'
	}).animate({
		left : 0
	}, 800, 'easeInOutQuart', function(){
		owner._onShow();
	});
}

GNaviMenu.prototype.hide = function(){
	var owner = this;
	this._menu.stop().animate({
		left : owner._hideLeft
	}, 400, 'easeInOutQuart', function(){
		owner._onHide();
	});
}

GNaviMenu.prototype._onShow = function(){
	$(this).trigger('onShow');
}
GNaviMenu.prototype._onHide = function(){
	$(this).trigger('onHide');
}


$(function(){
	if(!$('#gnavi').length) return;
	gnavi = new GNavi();
	gnavi.init();
});

var gnavi;
