var langDirectory = "tc";
/* prototypeとjqueryの競合回避
jQuery.noConflict();
jQuery(document).ready(function($){
	//expression jQuery...
	$('#id').blend();
});
//expression otherLibs.(prototipe etc..)
$('id')
*/

/*++++++++++++++++   onLoadEvent    ++++++++++++++++*/
$('document').ready(function(){
	

	/**
	*	SEXY Drop Down Menu! +R
	*	http://www.noupe.com/tutorial/drop-down-menu-jquery-css.html
	*/
	//$("ul.pulldown").parent().append('<span></span>'); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
	
	var hitarea = $('<span class="sexy_hitarea"></span>');
	var pulldownz = $('#global-navi ul.pulldown');
	var startzIndex = 1000;
	var zindexIncl = 1;
	
//	var pulldownOverlay = $('<div class="sexy_hitarea_overlay"></div>');
//	pulldownOverlay.css({
//			backgroundColor:'#ffffff'
//			,position:'absolute'
//			,top:0
//			,left:0
//			,width:$(document).width()
//			,height:$(document).height()
//			,opacity:0.2
//			,zIndex:3
//		});
//	$('body').prepend(pulldownOverlay);
	
	hitarea.css({
			display:"block"
			,position:"absolute"
			,zIndex:startzIndex
		});
	pulldownz.parent().prepend(hitarea);
	
	var hitareaz = $('#global-navi span.sexy_hitarea');
	hitareaz.map(function(i,elem){
		$(this).width( $(this).parent().find('a:first').outerWidth()); //('.pulldown.eq(3)')
		$(this).height( $(this).parent().find('a:first').outerHeight());
	})
	
	$('#global-navi span.sexy_hitarea').hover(function() { //When trigger is clicked...  
		
		var btn = $(this).parent();
		zindexIncl++;
		
		//alert("startzIndex + zindexIncl : "+(startzIndex + zindexIncl));
		$(this).css({display:'none'});
		
		btn.css({zIndex:startzIndex + zindexIncl});
		
		//Following events are applied to the subnav itself (moving subnav up and down)  
		btn.find("ul.pulldown").slideDown(255/*'fast'*/).show(); //Drop down the subnav on click  
	
		btn.hover(function(){}, function(){
			//$(this).parent().find("ul.pulldown").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
			$('#global-navi ul.pulldown').slideUp(459/*'slow'*/);
			hitareaz.css({display:'block'});
		});
		
		//Following events are applied to the trigger (Hover events for the trigger)  
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"  
		}, function(){  //On Hover Out  
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	});
	
	
	
	/**
	*	blendButton attach!
	*/
	pulldownz.css({display:'block'});
	$('#global-navi a').blend({speed:305});
	pulldownz.css({display:'none'});
	
	$('#local-navi a').blend({speed:305});
	
	
	/**
	*	colorBox
	*/
	$('.colorbox').colorbox();
	//append EnlargeImageBtn..
	$('.colorbox').map(function(){
		var cbImgOverlay = $('<span class="cboxImageOverlayBtn" />');
		var img = $(this).find('img:first');
		cbImgOverlay.css({width:img.outerWidth()-2,height:img.outerHeight()-2,opacity:.82}); //-border;;;;
		$(this).prepend(cbImgOverlay);
	});

	
	
	
	/**
	*	S.M.O.O.T.H scroll TOP
	*/
	$('.pagetop a').click(function(){
		$(this).blur();
		scrollTo(0);
	});
	
	
	/**
	*	S.M.O.O.T.H scroll (links "location.href" params To Anchor)
	*/
	//alert(outLink_anchor_id)
	if(outLink_anchor_id){
		scrollTo(outLink_anchor_id);
	}
	
	//var naviz = $A($('sub-navi').getElementsByTagName("a")).zip($('_global-navi').getElementsByTagName("a"));
	$('#global-navi a').each(aLokFnc);
	$('#local-navi a').each(aLokFnc);
	
	$('#global-navi a').each(aOutLokFnc);
	$('#local-navi a').each(aOutLokFnc);
});

/*++++++++++++++++   onLoadEvent    ++++++++++++++++*/


	/**
	*	FOR S.M.O.O.T.H scroll
	*/
	var isNumber = function(a){ return +a == a };
	var isString =function (obj){return ( typeof(obj) == "string" || obj instanceof String );};
	var scrollTo = function(position/*number of scrollTop || anchor element id*/){
		p = isString(position) && !isNumber(position) ? $('#'+position).position().top:position;
		//alert("position:"+position+" | scrollTop:"+$('#'+position).scrollTop()+" | position().Top:"+$('#'+position).position().top)
		$('html,body').animate({
			scrollTop : p
		}
		,1360
		,"easeOutQuint"
		);
	}
	// 外部のHTMLへ、ページ推移後に（アンカーまで）スクロールする動きを与える為、a.hrefに追加する値
	var out_anchor_str = '#/?anchor='
	var outLink_anchor_id;
	outLink_anchor_id =  String((location.href.lastIndexOf(out_anchor_str)!=-1)
	? location.href.substr(location.href.lastIndexOf(out_anchor_str)+10) : "");

	//現在見ているページ内のアンカーリンクを変更
	var location_no_anchor =	location.href.lastIndexOf('#')!=-1 ? 
								location.href.substr(0,location.href.lastIndexOf('#')) : location.href;
	var aLokFnc = function(i,a){
		if(a.href){
			if(a.href.indexOf(location_no_anchor)!=-1){
				var tmpt = a.href.substr(location_no_anchor.length);
				if (tmpt.lastIndexOf('#') == 0) {
					var aix = tmpt.lastIndexOf('#');
					var aid = tmpt.substr(aix + 1);
					a.href = 'javascript:void(0);';//a.href.substr(0,aix);
					//Event.observe(a, "click", _global.scrollTo.bindAsEventListener(this, $(aid)));
					$(a).click(function(){
						$(this).blur();
						scrollTo(aid);
					});
				}
			}
		}
	}
	//現在見ているページ外部のアンカーリンクを変更
	var aOutLokFnc = function(i,a){
		if(a.href){
			if (a.href.lastIndexOf('#') != -1) {
				var aix = a.href.lastIndexOf('#');
				var aid = a.href.substr(aix + 1);
				a.href = a.href.substr(0,aix) + out_anchor_str + aid
				//Event.observe(a, "click", _global.scrollTo.bindAsEventListener(this, $(aid)));
			}
		}
	}
	
	
	////Initialize Switching-Tab
	var initFigureTabs = function(){
		$('.figureTab a').each(function(i,me){
			$(this).click(function(){
				var fb
				fb = $(this).parent().next()
				if($(this).hasClass('tab_d3s')){
					fb.find('.d3sBg').css({/*zIndex:1*/display:"block"})
					fb.find('.d3xBg').css({/*zIndex:0*/display:"none"})
				}else{
					fb.find('.d3sBg').css({/*zIndex:0*/display:"none"})
					fb.find('.d3xBg').css({/*zIndex:1*/display:"block"})
				}
			});
		})
		
		//setting Tab Figure css
		$('.figureBlock').each(function(i,me){
				//$(this).css({position:"relative",display:"block"});
				$(this).find('.d3sBg').css({/*position:'absolute',zIndex:1*/display:"block"});
				$(this).find('.d3xBg').css({/*position:'absolute',zIndex:0*/display:"none"});
		});
	}
	

// Open "SPECIAL SITE" with flex sizing.
function getAvailWidth ( ) {
	if ( screen.availWidth ) return screen.availWidth; else return 0;
}
function getAvailHeight ( ) {
	if ( screen.availHeight ) return screen.availHeight; else return 0;
}
function openD3SSpecialSite( isNotTopPage ){
	if(!isNotTopPage){
		var src = "./special/tc/index.html"
	}else{
		var src = "special/tc/index.html"
	}
	openSpecialSite(src,"D3S_SpecialSite");
}
function openD3XSpecialSite(){
	var src = "http://chsvimg.nikon.com/products/imaging/lineup/d3/d3x-sp/"+ langDirectory +"/index.html"
	openSpecialSite(src,"D3X_SpecialSite",1024,768);
}
function openSpecialSite( src,winName,w,h ){
	var name = winName
	var options = "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no"
	var width,height
	var minWidth = 1004
	var minHeight = 712
	var foceWidth = 1260
	var foceHeight = 780
	if(getAvailWidth()>minWidth){
		if(getAvailWidth()>foceWidth){
			width = foceWidth
		}else{
			width = getAvailWidth()
		}
	}else{
	width = getAvailWidth()
	}
	if(getAvailHeight()>minHeight){
		if(getAvailHeight()>foceHeight){
			height = foceHeight
		}else{
			height = getAvailHeight()
		}
	}else{
		height = getAvailHeight()
	}
	width = w? w:width
	height = h? h:height
	options += ",width="+width+",height="+height
	window.open(src,name,options);
	
}



function stageSize(){
	
	var agent = navigator.userAgent;
	var isSafari = agent.indexOf("Safari",0);
	
	var width,height
	var minWidth = 1004
	var minHeight = 712
	var foceWidth = 1260
	var foceHeight = 780
	if(getAvailWidth()>minWidth){
		if(getAvailWidth()>foceWidth){
			width = foceWidth
		}else{
			width = getAvailWidth()
		}
	}else{
	width = getAvailWidth()
	}
	if(getAvailHeight()>minHeight){
		if(getAvailHeight()>foceHeight){
			height = foceHeight
		}else{
			height = getAvailHeight()
		}
	}else{
		height = getAvailHeight()
	}
	
	setStageSize(width,height,isSafari);
}

function setStageSize(w,h,sf){
	selectMovie("index").setStageFunc(langDirectory,w,h,sf);
}



