/*
 * $Rev: 5801 $
 */

if (!window.fujifilm){ var fujifilm = {}; }
fujifilm.ps01 = {};

if ( typeof __PS01_SIBLINGLINKS_AUTOSTAY__ === "undefined" ) {
	var __PS01_SIBLINGLINKS_AUTOSTAY__ = true;
}
if ( typeof __PS01_SIBLINGLINKS_CURRENTINDEX__ === "undefined" ) {
	var __PS01_SIBLINGLINKS_CURRENTINDEX__ = -1;
}

(function(pkg, $){
	pkg.ps01.navFeaturesActiveIndex = 0;

	/**
	 * Overview
	 */
	pkg.ps01.Overview = Overview;
	function Overview () {
		this.detector = null;
		this.ajustSpecItemHeightIsRan = false;
		this.ajustContentLinkBlocksHeightIsRan = false;
		this.detectorHeight = 0;
	};
	Overview.prototype.ajustComponents = function () {
		if ( !this.specItems || !this.specItems.length || !this.contentLinkBlockParents || this.contentLinkBlockParents.length );
		this.specItemB01 = this.specItems.filter('div.specItemB01');
		this.specItemB02 = this.specItems.filter('div.specItemB02');
		
		$('<span />').addClass('lb').appendTo(this.specItemB02);
		$('<span />').addClass('rb').appendTo(this.specItemB02);
		
		this.ajustSpecItemHeight();
		this.ajustContentLinkBlocksHeight();

		//font size detection
		this.detector = $('<div />').text('S').css({
			'visibility': 'hidden',
			'position': 'absolute',
			'top': '0'
		}).appendTo('body');
		this.detectorHeight = this.detector.height();
		setInterval(pkg.delegate(this, function(){
			if ( this.detectorHeight == this.detector.height() ) return;
			this.ajustSpecItemHeight();
			this.ajustContentLinkBlocksHeight();
			this.detectorHeight = this.detector.height();
		}), 1000);
	}
	Overview.prototype.ajustSpecItemHeight = function () {
		var sh = 0, h, $label;
		var l = this.specItems.length;
		for ( var i = 0; i < l; i++ ) {
			if ( this.ajustSpecItemHeightIsRan ) {
				$label = this.specItems.eq(i).find('dd > span.label, > span.label');
				h = $label.height();
				$label.css('height','auto');
			}
			sh = Math.max(this.specItems.eq(i).height(), sh);
			if ( this.ajustSpecItemHeightIsRan ) {
				$label.height(h);
			}
		}
		l = this.specItemB01.length;
		var $item, w;
		var isMsIE6 = false;
		if ($.browser.msie && $.browser.version < 7 ) isMsIE6 = true;
		for ( var j = 0; j < l; j++ ) {
			$item = this.specItemB01.eq(j);
			$label = $item.find('dd > span.label');
			w = $label.width();
			var setH = sh - $item.find('dt').height() - 10;
			$label.height( setH );

			if ( isMsIE6 ) $label.width(w);
			else if ( $.browser.msie ) {
				$label.css('width',"auto");
				$label.width($label.parent().width() - 16);
			}
		}
		l = this.specItemB02.length;
		for ( var k = 0; k < l; k++ ) {
			$item = this.specItemB02.eq(k);
			$label = $item.find('> span.label');
			w = $label.width();
			var setH = sh - 16;
			$label.height(setH);

			if ( isMsIE6 ) {
				$label.width(w);
				var io = $item.offset();
				var $bs = $item.find('.rb,.lb');
				$bs.css('bottom', 0);
				var bso = $bs.offset();
				if ( bso.top != io.top + $item.height() - $bs.height() ) {
					var b = bso.top - (io.top + $item.height() - $bs.height());
					$bs.css('bottom', b + 'px');
				}
				var $rb = $bs.filter('.rb');
				$rb.css('right',0);
				var ro = $rb.offset();
				if ( ro.left != io.left + $item.width() - $rb.width() ) {
					var r = ro.left - (io.left + $item.width() - $rb.width());
					$rb.css('right', r + 'px');
				}
			}
		}
		
		this.ajustSpecItemHeightIsRan = true;
	}
	Overview.prototype.ajustContentLinkBlocksHeight = function () {
		var c_parents = this.contentLinkBlockParents;
		var h, sh=0, $u, $c;
		var self = this;
		for ( var i= 0; i < c_parents.length; i++ ) {
			sh=0;
			$c = c_parents.eq(i).find('div.contentLinkBlockB01');
			$c.each(function(){
				if ( self.ajustContentLinkBlocksHeightIsRan ) {
					$u = $(this).find('> div.contentLinkUnitB01');
					h = $u.height();
					$u.css('height','auto');
				}
				sh = Math.max($(this).height(), sh);
				if ( this.ajustSpecItemHeightIsRan ) {
					$u.height(h);
				}
			});
			
			for ( var j= 0; j < $c.length; j++ ) {
				$u = $c.eq(j).find('> div.contentLinkUnitB01');
				h = $c.eq(j).height();
				$u.height($u.height() + sh - h);
			}
		}
		
		this.ajustContentLinkBlocksHeightIsRan = true;
	}

	/********** *********/
	pkg.ps01.SiblingLinks = SiblingLinks;
	function SiblingLinks ( $elm ) {
		if  (!$elm.length ) return;
		this._elm = $elm;
		
		this._hide_timer = null;
		this._current_target = null;
		
		this.initialize();
	}
	SiblingLinks.prototype.initialize = function () {
		this._linksUl     = this._elm.find(".links");
		this._linksList = this._linksUl.find("li");

		var $l,$rn,$ln;
		var lmax = this._linksList.length;
		var hasCurrentItem = false;
		if ( __PS01_SIBLINGLINKS_CURRENTINDEX__ >= 0 && __PS01_SIBLINGLINKS_CURRENTINDEX__ < lmax) {
			var currentIndex = __PS01_SIBLINGLINKS_CURRENTINDEX__;
			hasCurrentItem = true;
			var res = this.setCurrentContents(currentIndex);
			$ln = res[0];
			$rn = res[1];
		}
		else if ( __PS01_SIBLINGLINKS_AUTOSTAY__ ) {
			for ( var i = 0; i < lmax; i++ ) {
				$l = this._linksList.eq(i);
				var h = $l.find('a').attr('href').replace(/\/$/,'/index\.html');
				var locH = location.href.replace(/\/$/,'/index\.html');
				var re = new RegExp(h.replace(/\?/g,'\\?'));
				if ( locH.match(re) ) {
					hasCurrentItem = true;
					var res = this.setCurrentContents(i);
					$ln = res[0];
					$rn = res[1];
					break;
				}
			}
		}

		this._elm.addClass("siblingLinksCompact-activate").show();
		this._linksList.find("a")
			.bind("mouseenter.SiblingLinks", pkg.delegate(this,this.mouseEnterHandler))
			.bind("mouseleave.SiblingLinks", pkg.delegate(this,this.mouseLeaveHandler))
		;

		if ( hasCurrentItem ) {
			this._linksUl.addClass("hasCurrent");
			var left = ( this._elm.outerWidth() - this._linksUl.outerWidth() ) / 2;
			this._linksUl.css("left",left+"px");
			var d = 32;
			if ( $ln ) {
				$ln.width( left - d );
			}
			if ( $rn ) {
				$rn.width( left - d );
			}
		}

		this._labelPanel = new SiblingLinksLabel();
	};
	SiblingLinks.prototype.setCurrentContents = function ( index ) {
		var $l,$rn,$ln;
		$l = this._linksList.eq(index);
		$l.addClass('current').html($l.find('a').html());
		if ( index != 0 ) {
			$ln = this.createArrowUnit(this._linksList.eq(index-1).find("a"))
			if ( $ln ) {
				$l.parent().before($ln);
			}
		}
		if ( index != this._linksList.length -1 ) {
			$rn = this.createArrowUnit(this._linksList.eq(index+1).find("a"), 1)
			if ( $rn ) {
				$l.parent().after($rn);
			}
		}
		return [$ln, $rn];
	};
	SiblingLinks.prototype.createArrowUnit = function ($a, next) {
		if ( !$a.length ) return;
		href = $a.attr("href");
		number = $a.find("span.number").text();
		title  = $a.find("span.title").text();

		var $div = $("<div />").addClass("arrowUnit");
		$div.html('<a href="'+ href +'"><span class="title">'+ number +'. ' + title + '</span><span class="arrow"></span></a>');
		if ( next ) {
			$div.addClass("arrowUnit-right");
		}
		else {
			$div.addClass("arrowUnit-left");
		}
		return $div;
	};
	SiblingLinks.prototype.mouseEnterHandler = function ( evt ) {
		this._is_on = true;
		this.clearTimer();

		var $a = this.getEventTarget(evt, 'a');
		this._labelPanel.show();
		this._labelPanel.setText($a.find('span.number').text(),$a.find('span.title').text());
		var otd = Math.round(this._linksUl.offset().top) + $a.outerHeight() + 2;
		var c   = Math.round(this._elm.offset().left) + this._elm.outerWidth()/2;
		this._labelPanel.adjustPosition( c, otd );
		this._labelPanel.adjustBg( Math.round( $a.offset().left + $a.width()/2 ) );
	};
	SiblingLinks.prototype.mouseLeaveHandler = function ( evt ) {
		this._is_on = false;
		
		this.clearTimer();
		this._hide_timer = setTimeout( pkg.delegate(this, function(){
			if ( this.is_on ) {
				this.clearTimer();
				return;
			}
			this._labelPanel.hide();
		}), 100);
	}
	SiblingLinks.prototype.getEventTarget = function (evt, nodeTypeStr) {
		var $node = null;
		if ( $.browser.msie ) {
			if ( evt.target.nodeName != nodeTypeStr.toUpperCase() ) {
				$node = $(evt.target).parent(nodeTypeStr.toLowerCase());
			}
			else {
				$node = $(evt.target)
			}
		}
		else {
			$node = $(evt.currentTarget);
		}
		return $node;
	};
	SiblingLinks.prototype.clearTimer = function () {
		if( this._hide_timer ) {
			clearTimeout(this._hide_timer);
			this._hide_timer = null;
		}
	};

	/********** *********/
	function SiblingLinksLabel () {
		this.initialize();
	}
	SiblingLinksLabel.prototype.initialize = function () {
		this._elm = $('div.siblingLinksCompactLabel');
		if ( this._elm.length == 0 ) {
			this._elm = $('<div />').addClass('siblingLinksCompactLabel')
				.attr('id','siblingLinksCompactLabel')
				.appendTo($('body'))
			;
			this._elm.html(
				'<div class="bg-l bg"></div>'
				+ '<div class="bg-r bg"></div>'
				+ '<div class="bg-ml bg"></div>'
				+ '<div class="bg-mr bg"></div>'
				+ '<div class="bg-p bg"></div>'
				+ '<p><span class="number"></span> <span class="title"></span></p>'
			);
		}
		this._bgl  = this._elm.find('.bg-l');
		this._bgr  = this._elm.find('.bg-r');
		this._bgml = this._elm.find('.bg-ml');
		this._bgmr = this._elm.find('.bg-mr');
		this._bgp  = this._elm.find('.bg-p');
		this._elm.hide();
	};
	SiblingLinksLabel.prototype.setText = function ( number, title) {
		this._elm.find(".number").text(number + '.');
		this._elm.find(".title").text(title);
		if ( $.browser.msie && $.browser.version < 7) {
			this._elm.css('width','auto');
			if ( this._elm.width() < 166 ) this._elm.width(166);
		}
	};
	SiblingLinksLabel.prototype.adjustPosition = function ( center, top ) {
		this._top = top;
		this._left = center - Math.round(this._elm.outerWidth()/2);
		this._elm.css({
			'top': this._top + 'px',
			'left': this._left + 'px'
		});
	};
	SiblingLinksLabel.prototype.adjustBg = function ( pointCenter ) {
		var baseC = pointCenter - Math.round(this._elm.offset().left);
		var bgpLeft = Math.round(baseC - this._bgp.width()/2)
		this._bgp.css('left',bgpLeft+'px');

		var bgmlW = bgpLeft - this._bgl.width();
		this._bgml.width(bgmlW).css('left', this._bgl.width());

		var bgmrW = this._elm.outerWidth() - (bgpLeft + this._bgp.width() + this._bgr.width());
		this._bgmr.width(bgmrW).css('right', this._bgr.width());
	};
	SiblingLinksLabel.prototype.show = function () {
		if ( $.browser.msie ) {
			this._elm.show();
		}
		else {
			this._elm.fadeIn(150);
		}
	}
	SiblingLinksLabel.prototype.hide = function () {
		if ( $.browser.msie ) {
			this._elm.hide();
		}
		else {
			this._elm.fadeOut(150);
		}
	}

})(fujifilm, jQuery);

jQuery(function($){
	var ff = fujifilm;
	var $contentsBody = ( ff.contentsBody ) ? ff.contentsBody : $('#contentsBody');
	var $contentPanel01 = null;
	if ( !ff.panelTrayInners ) {
		$contentPanel01 = $contentsBody.find('.ps01-contentPanel01');
	}
	else {
		$contentPanel01 = ff.panelTrayInners.find('div.ps01-contentPanel01');
	}

	fujifilm.ps01.showChild = function ( $li ) {
		fujifilm.ps01.clearHideTimer();
		var $child = $li.find('div.child');
		$child.find('ul.linkListA01 li').eq(fujifilm.ps01.navFeaturesActiveIndex).find('span.thumb').css('visibility','visible');
		$child.css('visibility','visible');
		$child.show();
	}
	fujifilm.ps01.hideChildTimer = null;
	fujifilm.ps01.hideChild = function ( $li ) {
		var $child = $li.find('div.child');
		$child.find('ul.linkListA01 li').eq(fujifilm.ps01.navFeaturesActiveIndex).find('span.thumb').css('visibility','hidden');
		fujifilm.ps01.navFeaturesActiveIndex = 0;
		$child.hide();
		$li.mouseleave();
		fujifilm.ps01.clearHideTimer();
	}
	fujifilm.ps01.clearHideTimer = function () {
		if ( fujifilm.ps01.hideChildTimer ) clearTimeout(fujifilm.ps01.hideChildTimer);
		fujifilm.ps01.hideChildTimer = null;
	}
	$contentPanel01.find('div.navigation ul li.withChild').each(function(){
		var $child = $(this).find('div.child');
		if ( !$child.length ) return;

		//define close area
		var $closeArea = $('<div />').css({
			position: 'absolute',
			right: 0,
			top: 0
		}).bind('mouseover.nav',function() {
			$child.css('visibility','visible');
			fujifilm.ps01.hideChildTimer = setTimeout(ff.delegate(this,function(){
				fujifilm.ps01.hideChild($(this).parents('li.withChild'));
			}), 500);
			$child.find('div.body, div.headerLabel > a, div.footerLabel > a').bind('mouseenter.nav',function(){
				fujifilm.ps01.showChild($(this).parent("li.withChild"));
				$(this).unbind('mouseenter.nav');
			});
		});

		var $headerLabel = $(this).find('div.headerLabel');
		var $footerLabel = $(this).find('div.footerLabel');
		if ( $headerLabel.length || $footerLabel.length ) {
			var $label;
			if ( $headerLabel.length ) $label = $headerLabel;
			else $label = $footerLabel;
			var w = $child.outerWidth() - $label.find('> a').outerWidth() -20;
			var h = $label.find('> a').height();
			$closeArea.height(h).width(w);
			$label.append($closeArea);
		}

		// for older version
		var $linkListA01List = $child.find('ul.linkListA01 li');
		// mouseover action
		$linkListA01List.bind('mouseover.nav', function(){
			var index = $linkListA01List.index($(this));
			if ( fujifilm.ps01.navFeaturesActiveIndex != index ) {
				$linkListA01List.eq(fujifilm.ps01.navFeaturesActiveIndex).find('span.thumb').css('visibility','hidden');
			}

			$(this).find('span.thumb').css('visibility','visible');
			fujifilm.ps01.navFeaturesActiveIndex = index;
		});

		$(this).hover(function(evt){
			fujifilm.ps01.showChild($(this));
		}, function(evt){
			fujifilm.ps01.hideChildTimer = setTimeout(ff.delegate(this,function(){
				fujifilm.ps01.hideChild($(this));
			}), 500);
		});

		if ( $.browser.msie && $.browser.version < 7 ) {
			if ( $(this).attr('class').match(/stay/) ) $(this).addClass('withChildActive-stay');
			else $(this).addClass('withChildActive');
		}
	});

	if ( $.browser.msie && $.browser.version < 7 ) {
		$contentsBody.find("div.callToActionButtonC01:has('div.callToActionButtonChildPanel')").hover(function(){
			$(this).find('div.callToActionButtonChildPanel').css('visibility','visible');
		},
		function(){
			$(this).find('div.callToActionButtonChildPanel').css('visibility','hidden');
		}).filter(":has('div.rightAlign')").each(function(){
			var o = $(this).offset();
			var $child = $(this).find('div.callToActionButtonChildPanel');
			var l = o.left + $(this).width() - $child.width();
			var tol = $child.offset().left - l;
			$child.css('left', '-' + tol + 'px');
		});
	}

	if ( fujifilm.isInternalDomain ) {
		var externalImage02 = '<img src="/img/shared/icn_window_02.png" class="iconExternal" />';
		var externalImage03 = '<img src="/img/shared/icn_window_03.png" class="iconExternal" />';
		if ( $.browser.msie && $.browser.version < 7 ) {
			externalImage02 = '<img src="/img/shared/icn_window_02.gif" class="iconExternal" />';
		}
		var $callToActionButton = $contentsBody.find('div.callToActionButtonC01');
		var $callToActionButtonExLinks = $callToActionButton.find("> a[href^='http']");
		for ( var i = 0; i < $callToActionButtonExLinks.length; i++ ) {
			var $a = $callToActionButtonExLinks.eq(i);
			if( fujifilm.isInternalDomain($a.attr("href")) ) continue;
			var icon = ( $a.attr('class') == 'buttonType03' ) ? externalImage03 : externalImage02;
			if ( $a.find('span.label').find('img.iconExternal').length ) continue;
			$a.find('> img.iconExternal').remove();
			$a.find('span.label').append( icon );
		}
		var $callToActionChildLinks = $contentsBody.find("div.callToActionButtonChildPanel a[href^='http']").not(':has(img.iconExternal)');
		for ( var i = 0; i < $callToActionChildLinks.length; i++ ) {
			var $a = $callToActionChildLinks.eq(i);
			if( fujifilm.isInternalDomain($a.attr("href")) ) continue;
			$a.append(externalImage02);
		}
	}

	var $overviewContent = $contentPanel01.filter('div.ps01-contentPanel01-dc01');
	if ( $overviewContent.length ) {
		var overview = new ff.ps01.Overview();

		overview.specItems = $overviewContent.find('div.miniSpecBlockB01').find('div.specItemB01, div.specItemB02');
		overview.contentLinkBlockParents = $overviewContent.find('div.parallelBlockB01L2R1 div.parallelBlockB01Half, div.parallelBlockB013Clm');

		overview.ajustComponents();
		return;
	}

	if ( __PS01_SIBLINGLINKS_AUTOSTAY__ ) {
		var $siblingLinks = $contentPanel01.find('div.siblingLinks');
		if ( location.href.match(/\/features\/(.*?\.html)?$/) && $siblingLinks.length > 0 ) {
			if( $.browser.msie && $.browser.version < 7 ) {
				
			}
			if ( $siblingLinks.find('.current').length == 0 ) {
				// detect stay item
				$siblingLinks.find('a').each(function(){
					var h = $(this).attr('href').replace(/\/$/,'/index\.html');
					var locH = location.href.replace(/\/$/,'/index\.html');
					var re = new RegExp(h.replace(/\?/g,'\\?'));
					if ( locH.match(re) ) {
						$(this).parent().addClass('current').html($(this).html());
					}
				});
			}
		}
	}

	var $slc = $contentPanel01.find('div.siblingLinksCompact');
	if ( $slc.length == 1 && !$contentPanel01.find('div.sequentialNav02').length ) {
		$slcb = $slc.clone().addClass("siblingLinksCompact-bottom");
		$contentPanel01.find('div.bgImageUnit').append($slcb);
		new fujifilm.ps01.SiblingLinks($slcb);
	}
	new fujifilm.ps01.SiblingLinks($slc);
});
document.write('<style>.siblingLinksCompact{ display:none; }</style>');
