// Tooltip
var tooltip=function(){
	var id = 'tt';
	var top = -73;
	var left = -36;
	var maxw = 232;
	var speed = 10;
	var timer = 20;
	var endalpha = 100;
	var alpha = 1;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity	= alpha * 0.01;
				tt.style.filter		= 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();

$(function(){
	
	/*---------------------------------------------
	// setting
	---------------------------------------------*/
	DEF_FADE_TIME	= 300;
	
	//browser IE6 flag
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		var DEF_FLG_IE6	= 1;
	}
	else {
		var DEF_FLG_IE6	= 0;
	}
	
	//browser IE7 flag
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 7){
		var DEF_FLG_IE7	= 1;
	}
	else {
		var DEF_FLG_IE7	= 0;
	}
	
	//mainVisual
	var DEF_DATA_MAIN	= new Array(
		'images/main1.jpg',
		'images/main2.jpg',
		'images/main3.jpg',
		'images/main4.jpg'
	);
	
	//関数実行
	indexHover();
	headInclude();
	footerInclude();
	mainVisual();
	newsRelease();
	
	//dropdown
	//srcに_onを含むgnaviのliにactiveクラスを追加
	$('#gNavi li img').each( function(){
		if( $(this).attr('src').match(/^(.+)_on(\.[a-z]+)$/) ){
			$(this).parents('li').addClass('current');
		}
	});
	
	$('#gNavi ul:first li').hover( function(event){
		
		$('#gNavi ul:first li').filter( function(){
			if( $(this).hasClass('current') == true ){
				return false;
			}
			else {
				return true;
			}
		}).each( function(){
			$(this).find('img').attr('src' , $(this).find('img').attr('src').replace(/^(.+)_on(\.[a-z]+)$/ , '$1$2') );
		});
		
		$(this).filter( function(){
			if( $(this).hasClass('current') == true ){
				return false;
			}
			else {
				return true;
			}
		}).find('img').attr('src' , $(this).find('img').attr('src').replace(/^(.+)(\.[a-z]+)$/ , '$1_on$2') );
		
		$.yuga.rollover();
		
		//何番目のgNaviにhoverしたか
		var intNum	= null;
		intNum		= $('#gNavi ul:first li').index(this);
		
		switch(intNum){
			case 0:
				$('#gNavi .dropdownBtm').fadeOut(DEF_FADE_TIME);
				break;
				
			case 1:
				$('#gNavi .dropdownBtm').fadeOut(DEF_FADE_TIME);
				$('#ddCorp').fadeIn(DEF_FADE_TIME);
				break;
				
			case 2:
				$('#gNavi .dropdownBtm').fadeOut(DEF_FADE_TIME);
				$('#ddBiz').fadeIn(DEF_FADE_TIME);
				break;
				
			case 3:
				$('#gNavi .dropdownBtm').fadeOut(DEF_FADE_TIME);
				$('#ddCsr').fadeIn(DEF_FADE_TIME);
				break;
				
			case 4:
				$('#gNavi .dropdownBtm').fadeOut(DEF_FADE_TIME);
				$('#ddJobs').fadeIn(DEF_FADE_TIME);
				break;
				
			default :
				$('#gNavi .dropdownBtm').hide();
				break;
		}
		
	},
	function(event){
		if( $(event.relatedTarget).parents('#gNavi').length ){
			return false;
		}
		else {
			$('#gNavi .dropdownBtm').fadeOut(DEF_FADE_TIME);
			
			$('#gNavi ul:first li').filter( function(){
				if( $(this).hasClass('current') == true ){
					return false;
				}
				else {
					return true;
				}
			}).each( function(){
				$(this).find('img').attr('src' , $(this).find('img').attr('src').replace(/^(.+)_on(\.[a-z]+)$/ , '$1$2') );
			});
		}
	});
	
	$('#gNavi .dropdownBtm').live( 'mouseout' , function(event){
		if( $(event.relatedTarget).parents('#gNavi').length ){
			return false;
		}
		else {
			$(this).fadeOut(DEF_FADE_TIME);
			
			$('#gNavi ul:first li').filter( function(){
				if( $(this).hasClass('current') == true ){
					return false;
				}
				else {
					return true;
				}
			}).each( function(){
				$(this).find('img').attr('src' , $(this).find('img').attr('src').replace(/^(.+)_on(\.[a-z]+)$/ , '$1$2') );
			});
		}
	});
	
	
	/*---------------------------------------------
	// footerInclude()
	---------------------------------------------*/
	function footerInclude(){
		var strHOST	= document.URL;
		
		//テスト環境でなければ、本番パスを読む
		var strJsPath	= null;
		if( strHOST.indexOf('www.densotechno.co.jp/demo/') != -1 ){
			strJsPath	= '/demo/js/inc/footer_data.html';
		}
		else {
			strJsPath	= '/js/inc/footer_data.html';
		}
		
		$('body').append('<div id="footer"></div>');
		$('#footer').load(strJsPath);
	}
	
	
	/*---------------------------------------------
	// headInclude()
	---------------------------------------------*/
	function headInclude(){
		var strHOST	= document.URL;
		
		//テスト環境でなければ、本番パスを読む
		var strJsPath	= null;
		if( strHOST.indexOf('www.densotechno.co.jp/demo/') != -1 ){
			strJsPath	= '/demo/js/inc/dropdown_data.html';
		}
		else {
			strJsPath	= '/js/inc/dropdown_data.html';
		}
		
		$('#gNavi ul').after('<div id="dropdownBox"></div>');
		$('#dropdownBox').load( strJsPath , function(){
			
			if( strHOST.indexOf('www.densotechno.co.jp/demo/') != -1 ){
				$('#dropdownBox img').each( function(){
					$(this).attr('src' , ( '/demo' + $(this).attr('src') ) );
				});
				
				$('#dropdownBox a').each( function(){
					$(this).attr('href' , ( '/demo' + $(this).attr('href') ) );
				});
			}
			
			//dropdownのz-indexを調整
			zindexHack();
		});
	}
	
	
	/*---------------------------------------------
	// zindexHack()
	---------------------------------------------*/
	function zindexHack(){
	//	if( DEF_FLG_IE6 == 1 || DEF_FLG_IE7 == 1 ){
			zIndexNumber	= 10000;
			$('div').each( function(){
				$(this).css('zIndex', zIndexNumber);
				zIndexNumber -= 10;
			});
	//	}
	}
	
	
	/*---------------------------------------------
	// indexHover()
	---------------------------------------------*/
	function indexHover(){
		//第2階層トップのhover処理
		if( $('#content').hasClass('index') ){
			$('#indexLink li').mouseenter( function(){
				$(this).addClass( 'active' );
				var strOriSrc	= $(this).find('img:last').attr( 'src' );
				var strNewSrc	= strOriSrc.replace( '.gif' , '_on.gif' );
				$(this).find('img:last').attr( 'src' , strNewSrc );
			});
			
			$('#indexLink li').mouseleave( function(){
				$(this).removeClass( 'active' );
				var strOriSrc	= $(this).find('img:last').attr( 'src' );
				var strNewSrc	= strOriSrc.replace( '_on.gif' , '.gif' );
				$(this).find('img:last').attr( 'src' , strNewSrc );
			});
			
			$('#indexLink li').click( function(){
				var strHref		= $(this).find('a:first').attr('href');
				if( $(this).find('a:first').attr('target') == '_blank' ){
					window.open(strHref);
					return false;
				}
				else {
					location.href	= strHref;
				}
			});
		}
	}
	
	
	/*---------------------------------------------
	// mainVisual()
	---------------------------------------------*/
	function mainVisual(){
		if( $('body').attr('id') == 'top' ){
			
			var randNum	= new Date();
			var intData	= DEF_DATA_MAIN.length;
			
			randNum		= randNum.getMilliseconds()%intData;
			
			var strUrl	= 'url("' + DEF_DATA_MAIN[randNum] + '")';
			
			$('#mainVisual').css( 'background-image' , strUrl );
		}
	}
	
	
	/*---------------------------------------------
	// newsRelease()
	---------------------------------------------*/
	function newsRelease(){
		if( $('body').attr('id') == 'top' ){
			
			$('#news p a, #news2 p a').click( function(){
				
				$('#news ul, #news2 ul').slideToggle();
				
				return false;
			});
		}
	}
});
