/*
 * $Id: components.js 4539 2010-06-30 13:12:41Z ba\toshk $
 */

if (!window.fujifilm){ var fujifilm = {}; }
fujifilm.ps01 = {};
(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.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;
	}

})(fujifilm, jQuery);

jQuery(function($){
	var ff = fujifilm;
	var $contentsBody = ( ff.contentsBody ) ? ff.contentsBody : $('#contentsBody');
	var $contentPanel01;
	if ( !ff.panelTrayInners ) {
		$contentPanel01 = $contentsBody.find('.ps01-contentPanel01');
	}
	else {
		$contentPanel01 = ff.panelTrayInners.find('div.ps01-contentPanel01');
	}
	$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', 'hidden');
		});

		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 = 330 - $label.find('> a').width();
			var h = $label.find('> a').height();
			$closeArea.height(h).width(w);
			$label.append($closeArea);
		}
		
		
		//
		var $linkListA01List = $child.find('ul.linkListA01 li');
		if ( $linkListA01List.filter('.stay').length == 0 && location.href.match(/\/features\/(.*?\.html)?$/)) {
			// detect stay item
			$linkListA01List.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('disable').html($(this).html()).find('span.thumb').css('visibility','hidden');
				}
			});
		}
		
		
		// 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){
			$child.css('visibility','visible');
			$linkListA01List.eq(fujifilm.ps01.navFeaturesActiveIndex).find('span.thumb').css('visibility','visible');
		}, function(evt){
			$child.css('visibility','hidden');
			$linkListA01List.eq(fujifilm.ps01.navFeaturesActiveIndex).find('span.thumb').css('visibility','hidden');
			fujifilm.ps01.navFeaturesActiveIndex = 0;
		});

		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;
	}
});

