var MONTH_MAP = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.","Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."];
var home = {
	swfPass: '/swf/index/loader.swf',
	swfReqVer: '9.0.124',
	swfEnable: false,
	//rssPass: '/rss/newsfeed.xml',
	rssPass: '/swf/index/newsfeed.xml',
	jsonPass: '/json/index/index.json'
};


(function(home){
	var $ = jQuery;

	/*
	 *
	 */
	home.init = init;
	function init () {
		if ( swfobject.hasFlashPlayerVersion(home.swfReqVer) ) {
			/*
			 * SetStyle
			 */
			document.writeln(
				['<style type="text/css">',
				'#annotationA01{display:none;}',
				'#flashAlternate { display:none }',
				'#homeContentsSection1 div.featurePanelA01 div.bgImageUnit{ height: 363px }',
				'#homeContentsSection1 div.featurePanelA01 { position: relative }',
				'#homeContentsSection1 div.featureItemA01 { position: absolute; top: 0; left: 3px }',
				'body#home #baseBgImageTray.bgHome {background: none}',
				'</style>'].join('')
			);
			home.swfEnable = true;
		}


		/*
		 * SetStyle
		 */
		document.writeln('<style type="text/css">'
		+ 'div#contents div.tabArea div.tabContent h3.contentTitleB02 {display: none}'
		+ '#home #contents div.parallelBlockB01Half {margin-top:0}'
		+ '</style>');
	}

	/*
	 *
	 */
	home.setNewsPanel = setNewsPanel;

	function setNewsPanel ( data ) {

		var $table = $('<table />');
		if ( $(data).find("item").length ) {
			var $items = $(data).find("item");
			$.each($items, function(i, val){
				var date = new Date($(this).find('pubDate').text());
				var year = date.getFullYear();
				var mon = date.getMonth();
				var day = date.getDate();
			
				var $tr = $('<tr />').html(
				'<th>'
				+ MONTH_MAP[mon] + ' ' + day + ', ' + year
				+ '</th>'
				+'<td><ul class="linkListA01"><li>'
				+ '<a href="'
				+ $(this).find('link').text()
				+ '">'
				+ $(this).find('title').text()
				+ "</a></li></ul></td>");
				if ( i == 0 ) $tr.addClass('first-child');
				$table.append($tr);
			});
		}
		else if ( $(data).find("#bodyblock li.regularitem").length ) {
			var $items = $(data).find("#bodyblock li.regularitem");

			$.each($items, function(i, val){
				var $dateObj = $(this).find('.itemposttime');
				$dateObj.find('span').remove();
				var date = new Date($.trim($dateObj.text()));
				var year = date.getFullYear();
				var mon = date.getMonth();
				var day = date.getDate();
				
				var $titleAnc = $(this).find('.itemtitle a');
				var link = $titleAnc.attr('href');
				var title = $titleAnc.text();
			
				var $tr = $('<tr />').html(
				'<th>'
				+ MONTH_MAP[mon] + ' ' + day + ', ' + year
				+ '</th>'
				+'<td><ul class="linkListA01"><li>'
				+ '<a href="'
				+ link
				+ '">'
				+ title
				+ "</a></li></ul></td>");
				if ( i == 0 ) $tr.addClass('first-child');
				$table.append($tr);
			});
		}

		var $target = $('#news').addClass('enable');
		$('<div />').addClass('body').append($table).insertAfter( $target.find('div.head') );
	}

	/*
	 *
	 */
	home.BgImgController = new BgImgController();

	function BgImgController () {
		this.map = null;
		this.isEnable = false;
		this.isSet = false;
	}

	BgImgController.prototype.set = function ( json ) {
		this.map = {};
		for ( var i in json.data.featurelist ) {
			this.map[json.data.featurelist[i].id] = {
				color: json.data.featurelist[i].bgColor,
				img: json.data.featurelist[i].bgImage
			};
			//new Image().src = json.data.featurelist[i].bgImage;
		}
	};

	BgImgController.prototype.change = function ( id ) {
		if ( !this.map || this.isSet ) return;
		$('#baseBgImageTray').css({
			'background-image': 'url('+ this.map[id].img +')',
			'background-color': this.map[id].color
		});

		this.isSet = true;
	};

	/*
	 *
	 */
	home.adjastContentHeight = adjastContentHeight;

	function adjastContentHeight () {
		this.timer = null;
		this.maxHeight = 0;
		this.preHeight = 0;
		this.tabChanged = false;
		this.$homesection2 = $('#homeContentsSection2');
		this.$tabcontents = this.$homesection2.find('div.tabContent');
		this.$tabpanel = this.$homesection2.find('div.tabPanelA01');
		this.$wwpAnc = this.$homesection2.find('div.worldwidePanelA01 div.body a');
	}

	adjastContentHeight.prototype.set = function () {
		var h = this.$homesection2.height();
		if ( h == this.preHeight ) {
			this.setTimer();
			return;
		}

		if ( $.browser.msie ) {
			var tab = this.$tabpanel.find('div.tabA01');
			if ( tab.height() > 33 ) {
				tab.width(623);
				this.tabChanged = true;
			}
			else if ( this.tabChanged ) {
				this.tabChanged = false;
				tab.css('width','auto');
			}
		}


		this.preHeight = h;
		var newHeight = 0;
		this.$tabcontents.each(function(){
			var tmp = $(this).css('display');
			$(this).css('display', 'block');
			newHeight = Math.max(newHeight,$(this).find('div:first-child').height());
			$(this).css('display', tmp);
		});

		this.maxHeight = newHeight;
		this.$tabcontents.height(this.maxHeight);
		
		this.$wwpAnc.height( this.maxHeight + this.$tabpanel.height() + 22 - 26);

		this.preHeight = this.$homesection2.height()
		this.setTimer();
	};

	adjastContentHeight.prototype.setTimer = function () {
		if (this.timer) {
			clearTimeout(this.timer);
		}
		this.timer = setTimeout( home.delegate(this.set, this), 1000 );
	}

	/*
	 *
	 */
	home.delegate = delegate;
	function delegate(func, aThis) {
		var ret = function () {
			return func.apply(aThis, arguments);
		}
		return ret;
	}
	
	/*
	 * External Interface;
	 */
	window.fl_setBg = fl_setBg;
	function fl_setBg( id ) {
		if ( !id || !home.BgImgController ) return;
		home.BgImgController.change(id);
		if ( !fl_setBg.isFirst ) {
			fl_setBg.isFirst = true;
		}
	}

})(home);


/*
 * tasknav
 */
(function($){
	$.fn.setTaskNav = function(properties){
		var properties = $.extend({}, properties);
		
		function _set ( $self ){
			var objId = 0;
			$self.each(function () {
				var $panels = $(this).find('#features div.parallelBlockB01Half');
				if ( !$panels.length ) return;
				this.count = 0;
				this.countMax = $panels.length - 1;
				this.objId = objId;
				_setPanels( this, $panels);
				_setIndicator( this, $panels.length );
				_setButtons( this );
				this.isReady = true;
				objId++;
			});
			
			var pass = ['/img/shared/carouselpanel/icon_position_01_s.gif', '/img/shared/carouselpanel/icon_position_01_o.gif', '/img/index/btn_prev_01_o.gif','/img/index/btn_next_01_o.gif'];
			for ( var i =0; i < pass.length; i++) {
				new Image().src = pass[i];
			}
		}

		function _setPanels ( baseObj, $panels ) {
			var i = 0;
			$panels.each(function(){
				$(this).attr('id', __panelId(baseObj.objId,i));
				if ( baseObj.count != i ) $(this).hide();
				i++;
			});
			$panels.find('div.parallelUnitB01L').css({
				'width': '193px',
				'text-align': 'right'
			});
		}

		function _setIndicator ( baseObj, num ) {
			var $ind = $('<div />').addClass('posIndicator');
			var $icn = $('<span />').addClass('icn').html('<a href=""><img src="/img/shared/carouselpanel/icon_position_01.gif" alt="" width="12" height="12" /></a>');
			
			for ( var i = 0; i < num; i++ ) {
				var $tmp = $icn.clone();
				$tmp.attr('id', __indId(baseObj.objId, i) );
				$ind.append($tmp);
				$tmp.click(function(){
					var num = ($(this).attr('id').split(/_/))[1];
					_changePanel( baseObj, num );
					return false;
				});
				
				$tmp.hover(function(){
					var aImg = $(this).find('a img');
					if ( !aImg.length ) return;
					var src = aImg.attr('src').replace(/_01\.gif/, '_01_o.gif');
					aImg.attr('src',src);
				},function(){
					var aImg = $(this).find('a img');
					if ( !aImg.length ) return;
					var src = aImg.attr('src').replace(/_01_o\.gif/, '_01.gif');
					aImg.attr('src', src)
				});
			}
			$ind.insertBefore($('#features', baseObj));
			
			var stayInd = $('#' + __indId(baseObj.objId, 0) + ' img');
			var chSrc = stayInd.attr('src').replace(/_01(_o){0,1}\.gif/, '_01_s.gif');
			stayInd.attr('src', chSrc);
			stayInd.parent().replaceWith( stayInd.parent().html() );
		}

		function _setButtons ( baseObj ) {
			$(baseObj).find('div.cntCollL').css('background-image','none')
				.find('a').click(function(){
					_prevPanel(baseObj);
					return false;
				}).hide()
					.find('img').hover(function(){
						this.src = this.src.replace(/_01\.gif/, '_01_o.gif');
					},function(){
						this.src = this.src.replace(/_01_o\.gif/, '_01.gif');
					});
			

			$(baseObj).find('div.cntCollR a').click(function(){
				_nextPanel(baseObj);
				return false;
			})
				.find('img').hover(function(){
					this.src = this.src.replace(/_01\.gif/, '_01_o.gif');
				},function(){
					this.src = this.src.replace(/_01_o\.gif/, '_01.gif');
				});
		}

		function _prevPanel ( baseObj ) {
			if( baseObj.count == 0 || !baseObj.isReady) return;
			baseObj.countPrev = baseObj.count;
			baseObj.count--;
			_changePanel(baseObj);
		}
		
		function _nextPanel ( baseObj ) {
			if( baseObj.count == baseObj.countMax || !baseObj.isReady ) return;
			baseObj.countPrev = baseObj.count;
			baseObj.count++;
			_changePanel(baseObj);
		}

		function _changePanel ( baseObj, num ) {
			if ( !baseObj.isReady ) return;
			if ( num ) {
				if( num > baseObj.countMax || num < 0 || num == baseObj.count ) return;
				baseObj.countPrev = baseObj.count;
				baseObj.count = num;
			}
		
			baseObj.isReady = false;
			var $nextPanel = $('#' + __panelId( baseObj.objId, baseObj.count));
			
			var $prevPanel = $('#' + __panelId( baseObj.objId, baseObj.countPrev));
			if ( $.browser.msie ) {
				$prevPanel.find( 'dl.definitionListB01').hide();
			}

			$prevPanel.fadeOut(200,function(){
				if ( $.browser.msie ) {
					$nextPanel.find( 'dl.definitionListB01' ).hide();
				}
				$nextPanel.fadeIn(200, function(){
					if ( $.browser.msie ) {
						$(this).find( 'dl.definitionListB01' ).show();
					}
					baseObj.isReady = true;
				});
			});
			
			var preInd = $('#' + __indId(baseObj.objId, baseObj.countPrev) + ' img');
			if ( preInd.length ) {
				var preIndSrc = preInd.attr('src').replace(/_01_s\.gif/, '_01.gif');
				preInd.attr('src', preIndSrc);
				preInd.wrap('<a href=""></a>');
			}
			
			var stayInd = $('#' + __indId(baseObj.objId, baseObj.count) + ' img');
			var chSrc = stayInd.attr('src').replace(/_01(_o){0,1}\.gif/, '_01_s.gif');
			stayInd.attr('src', chSrc);
			stayInd.parent().replaceWith( stayInd.parent().html() );

			if( baseObj.count == baseObj.countMax ) {
				$(baseObj).find('div.cntCollR').css('background-image','none')
					.find('a').hide();
				$(baseObj).find('div.cntCollL').css('background-image', 'url(/img/index/tasknav_border_02.gif)')
					.find('a').show();
			}
			else if( baseObj.count == 0 ) {
				$(baseObj).find('div.cntCollL').css('background-image','none')
					.find('a').hide();
				$(baseObj).find('div.cntCollR').css('background-image', 'url(/img/index/tasknav_border_02.gif)')
					.find('a').show();
			}
			else {
				$(baseObj).find('div.cntCollL').css('background-image', 'url(/img/index/tasknav_border_02.gif)')
					.find('a').show();
				$(baseObj).find('div.cntCollR').css('background-image', 'url(/img/index/tasknav_border_02.gif)')
					.find('a').show();
			}
		}
		
		function __panelId ( objId, panelId ) {
			return 'taskNav' + objId + 'Panel_' + panelId;
		}
		
		function __indId ( objId, indId ) {
			return 'taskNav' + objId + 'Ind_' + indId;
		}

		_set(this);

	}
})(jQuery);


/*
 *
 */
home.init();

/*
 * onReady
 */
jQuery(function($){
	if ( $.browser.msie) {
		if ( $.browser.version <= 8 ) {
			$("div#homeContentsSection2 div.panelAreaR div.worldwidePanelA01 div.footer ul.linkListA01 li, div#annotationA01 ul.linkListA01 li").find("a[href^='http']").not(':has(img.iconExternal)').each(function(){
				if( $(this).attr("href").indexOf('www.fujifilm.com') == -1 ) {
					$(this).append('<img src="/img/shared/icn_window_02.gif" class="iconExternal" />');
				}
			});

			$("div#homeContentsSection1 div.body").find("a[href^='http']").not(':has(img.iconExternal)').each(function(){
				if($(this).attr("href").indexOf('www.fujifilm.com') == -1 ) {
					$(this).append('<img src="/img/shared/icn_window_03.gif" class="iconExternal" />');
				}
			});

			$("div#contents div.carouselPanelA01 div.carouselItemA01").find("a[href^='http']").not(':has(img.iconExternal)').each(function(){
				if($(this).attr("href").indexOf('www.fujifilm.com') == -1 ) {
					$(this).find("span.titleText").append('<img src="/img/shared/icn_window_02.gif" class="iconExternal" />');
				}
			});
		}
		
		if ( $.browser.version < 7 ) {
			var $lists = $('div#homeContentsSection2 div.tabArea div.tabA01 li');
			var $lastList = $('div#homeContentsSection2 div.tabArea div.tabA01 li').filter(':last');
			$lastList.css('margin-right','1px').filter('.stay').css('margin-right','0');
			$lists.find('a').click(function(){
				$lastList.css('margin-right','1px').filter('.stay').css('margin-right','0');
			});
		}
	}

	$('div#contents div.tabArea').addClass("conpact")
		.find('div.contentPanelA01').removeClass("contentPanelA01")
			.find('> div.contentUnitA01').removeClass("contentUnitA01")
				.find('> div.bgImageUnit').removeClass("bgImageUnit")
					.find("h3.contentTitleB02").hide();

	$('#panel1 div.taskNavContent').setTaskNav();

	if ( home.swfEnable ) {
		var res = $.get(home.jsonPass, function(data) {
			home.BgImgController.set(eval('('+data+')'));
		}, "json");

		var t = $('<ins />').attr('id','flash').insertBefore("#flashAlternate");
		swfobject.embedSWF(
			home.swfPass,
			"flash",
			"958",
			"358",
			home.swfReqVer,
			"",
			false,
			{
				bgcolor: "#FFFFFF",
				wmode: "transparent",
				allowscriptaccess: "samedomain",
				menu: 'false'
			},
			{
			  id: "mainFlash",
			  name: "mainFlash"
			}
		);
	}
	else {
		$("#flashAlternate").show();
	}

	$.ajax({
		type: "GET",
		url: home.rssPass,
		success: function(data){
			home.setNewsPanel( data );
		},
		dataType: 'xml'
		//dataType: (location.href.match(/http\:\/\/fujifilm\.com|http\:\/\/www\.fujifilm\.com/)) ? 'html' : 'xml'
	});
	
	var ajaster = new home.adjastContentHeight();
	ajaster.set();
});

