if (!window.fujifilm){ var fujifilm = {}; }

(function(fujifilm){
	var $ = jQuery;
	fujifilm.TabContent = FFTabContentA01;

	function FFTabContentA01 ( area ){
		this.area = area;
		this._init();
	}

	FFTabContentA01.prototype = {
		_isInitialized : false,
		_isSetUp : false,
		_hasSwitch: false,
		_isSelected: false,

		_init : function () {
			var self = this;

			var contents = this._contents = $('div.tabContent', this.area);
			if ( $.browser.msie ) {
				contents.css('visibility', 'visible')
					.find('p').css('visibility', 'visible').end()
					.find('div.switchB01').css('visibility', 'visible').end()
					.find('div.parallelUnitB01L, div.parallelUnitB01R, p').css('visibility', 'visible');
			}
			
			var panels = this.area.find('div.tabPanelA01');
			this.panelUls = panels.find('div.tabA01 ul');
			this.panelLis = this.panelUls.find('li');
			this.tabs = this.panelLis.find('a');
			if ( $.browser.msie && $.browser.version < 7 ) {
				this.panelLis.filter(":first-child").addClass("first-child")
			}
			if ( !contents.length || !panels.length || !this.panelUls.length || !this.panelLis.length || !this.tabs.length ) return;
			
			/*contents.each(function(){
				$(this).attr('base_id', $(this).attr('id'));
				$(this).attr('id', $(this).attr('id') + '_tab' );
			});*/

			this.tabs.click(function (evt) {
				evt.preventDefault();
				evt.stopPropagation();

				var targetId = '#' + this.hash.replace(/^#/, "");
				contents.hide();
				var $toShow = contents.filter(targetId);
				$toShow.attr('id', $toShow.attr('id')+'_active');
				$toShow.show();
				
				if ( self._isSetUp && location.hash != this.hash ) {
					location.replace(location.href.replace(/\#.*?$/,'') + targetId);
				}
				self.panelLis.removeClass('stay');
				$(this).parent().addClass('stay');
				$(this).blur();
				$toShow.attr('id', $toShow.attr('id').replace(/_active/,''));
		    });
		
			this._isInitialized = true;
		},


		setUp : function () {
			if ( !this._isInitialized ) return;

			var isSelected = false;
			var $selectedButton = null;
			if ( location.hash != '' ){
				$selectedButton = this.panelLis.find( "a[href^='" + location.hash + "']" );
				if ( $selectedButton.length ) {
					this._isSelected = isSelected = true;
				}
			}

			var hasSelectedInnerContent = false;
			if ( location.hash != '' && this.area.find('div.switchArea').length ) {
				var self = this;
				this._contents.each(function(){
					//var contentId = $(this).attr('base_id');
					var contentId = $(this).attr('id');
					$(this).find('div.switchArea').each(function(){
						if( $(this).find("div.switchB01,div.switchPanelB01,div.switchPanelA01").find("ul li a[href^='" + location.hash + "']").length ) {
							hasSelectedInnerContent = true;
							self.panelLis.find("a[href^='#" + contentId + "']").click();
						}
					});
				});
			}

			if ( !hasSelectedInnerContent ) {
				if ( isSelected && $selectedButton ) {
					$selectedButton.click();
				}
				else {
					this.panelUls.find('li:first a').click();
				}
			}
			this._isSetUp = true;
		},
		
		isSelected : function () {
			return this._isSelected;
		}
	};

	fujifilm.SwitchContent = FFSwitchContent;
	/*
	 * 
	 */
	function FFSwitchContent ( area ) {
		this.area = area;
		this._init();
	}

	FFSwitchContent.prototype = {
		_isInitialized : false,
		_isSetUp : false,
		_isA01 : false,
		_ALLLISTSELECTA: '.allListPreview',
		_ALLREGIONSSELECTA: '.allRegions',
		_isSelected: false,


		_init : function () {
			var self = this;
			var contents = this.area.find('div.switchContent');
			
			if ( $.browser.msie ) {
				contents.css('visibility', 'visible')
					.find('div.parallelUnitB01L, div.parallelUnitB01R, p').css('visibility', 'visible');
			}
			
			var panel = this.area.find('div.switchB01,div.switchPanelB01,div.switchPanelA01');
			
			if ( this.area.find('div.switchPanelA01').length ) {
				this._isA01 = true;
			}
			
			var panelUl = this._panelUl = panel.find('ul')
			var panelLi = this._panelLi = panelUl.find('li').filter(":not("+this._ALLLISTSELECTA+")");
			var panelNormalLi = this._panelNormalLi = panelUl.find('li').filter(":not("+this._ALLLISTSELECTA+","+this._ALLREGIONSSELECTA+")");
			
			if ( !contents.length || !panel.length || !panelUl.length || !panelLi.length || !panelNormalLi.length ) return;

			/*contents.each(function(){
				$(this).attr('base_id', $(this).attr('id'));
				$(this).attr('id', $(this).attr('id') + '_tab' );
			});*/

			panelNormalLi.find('a').click(function (evt) {
				evt.preventDefault();
				evt.stopPropagation();
				//var targetId = '#' + this.hash.replace(/^#/, "") +'_tab';
				var targetId = '#' + this.hash.replace(/^#/, "");
				contents.hide();
				var $toShow = contents.filter(targetId);
				$toShow.attr('id', $toShow.attr('id')+'_active');
				$toShow.show();
				if ( self._isA01 ) {
					$toShow.css("margin-top","16px");
				}

				if ( self._isSetUp && location.hash != this.hash ) {
					//location.replace(location.href.replace(/\#.*?$/,'') + '#' + $toShow.attr('base_id'));
					location.replace(location.href.replace(/\#.*?$/,'') + targetId);
				}

				panelLi.find('a.stay').removeClass('stay');
				$(this).addClass('stay');
				$(this).blur();
				$toShow.attr('id', $toShow.attr('id').replace(/_active/,''));
		    });

			panelUl.find('li'+this._ALLLISTSELECTA ).show()
			.find("a").click(function () {
		        contents.show();
				if ( self._isA01 ) {
					$('div.switchContent + div.switchArea', self.area).css("margin-top","25px");
				}
				if ( self._isSetUp && location.hash != '' ) {
					var h = this.hash || '#See_All';
					location.replace(location.href.replace(/\#.*?$/,'') + h);
				}
		        $('a.stay', panelLi).removeClass('stay');
		        return false;
		    });

		    $('li'+this._ALLREGIONSSELECTA+' a' , panelUl).click(function () {
		        contents.show();
				if ( self._isSetUp && location.hash != '' ) {
					var h = this.hash || '#All_Regions';
					location.replace(location.href.replace(/\#.*?$/,'') + h);
				}
		        $('a.stay', panelLi).removeClass('stay');
				$(this).addClass('stay');
		        return false;
		    });

			this._isInitialized = true;
		},


		setUp : function () {
			if ( !this._isInitialized ) return;

			var $selectedButton = null;
			if ( location.hash != '' ){
				$selectedButton = this._panelUl.find( "li a[href^='" + location.hash + "']" );
				if ( $selectedButton.length ) {
					this._panelUl.find( "li a[href^='" + location.hash + "']" ).click();
					this._isSelected = true;
				}
			}

			if ( !this._isSelected ) {
				this._panelUl.find('li:first a').click();
			}

			this._isSetUp = true;
			
		},
		
		isSelected : function () {
			return this._isSelected;
		}
	};

})(fujifilm);


function FFsetTabContensSetContentVisiblity ( targetSelector, val ) {
	fujifilm.setVisibilityStyle(targetSelector, val);
}

FFsetTabContensSetContentVisiblity('div.tabArea', false);
FFsetTabContensSetContentVisiblity('div.switchArea', false);

jQuery(function ($) {
	var $selectedTarget = null;
	var $contentsBody = $('#contentsBody');

	var tabAreas = $contentsBody.find('div.tabArea');
	if (tabAreas.length) {
		tabAreas.each(function(){
			var ffTabContent = new fujifilm.TabContent($(this));
			if ( ffTabContent ) {
				ffTabContent.setUp();
				if ( ffTabContent.isSelected() ) {
					$selectedTarget = $(this);
				}
			}
		});
	}
	
	var switchAreas = $contentsBody.find('div.switchArea');
	if (switchAreas.length) {
		switchAreas.each(function(){
			var ffSwitchContent = new fujifilm.SwitchContent($(this));
			if ( ffSwitchContent ) {
				ffSwitchContent.setUp();
				if ( ffSwitchContent.isSelected() ) {
					$selectedTarget = $(this);
				}
			}
			
		});
	}

	//div.switchPanelA01 adjust style
	var parentObj = $("#contents div.panelTrayA01:first-child");
	var switchPanelA01 = parentObj.find("div.switchPanelA01");
	if ( switchPanelA01.length ) {
		var parentPos = parentObj.position();
		var switchPos = switchPanelA01.position();
		if (switchPos.top - parentPos.top < 285) {
			switchPanelA01.addClass("headPosition");
		}
	}

	if ( $selectedTarget && $selectedTarget.length ) {
		scrollTo(0, $selectedTarget.offset().top - 50);
	}

	FFsetTabContensSetContentVisiblity('div.tabArea', true);
	FFsetTabContensSetContentVisiblity('div.switchArea', true);
});

