/*
 * jQuery UI Tabs @VERSION
 *
 * Copyright (c) 2007, 2008 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Tabs
 *
 * Depends:
 *	ui.core.js
 */
(function(j$) {

j$.widget("ui.tabs", {
	_init: function() {
		// create tabs
		this._tabify(true);
	},
	_setData: function(key, value) {
		if ((/^selected/).test(key))
			this.select(value);
		else {
			this.options[key] = value;
			this._tabify();
		}
	},
	length: function() {
		return this.j$tabs.length;
	},
	_tabId: function(a) {
		return a.title && a.title.replace(/\s/g, '_').replace(/[^A-Za-z0-9\-_:\.]/g, '')
			|| this.options.idPrefix + j$.data(a);
	},
	ui: function(tab, panel) {
		return {
			options: this.options,
			tab: tab,
			panel: panel,
			index: this.j$tabs.index(tab)
		};
	},
	_sanitizeSelector: function(hash) {
		return hash.replace(/:/g, '\\:'); // we need this because an id may contain a ":"
	},
	_cookie: function() {
		var cookie = this.cookie || (this.cookie = 'ui-tabs-' + j$.data(this.element[0]));
		return j$.cookie.apply(null, [cookie].concat(j$.makeArray(arguments)));
	},
	_tabify: function(init) {
		
		this.j$lis = j$('li:has(a[href])', this.element);
		this.j$tabs = this.j$lis.map(function() { return j$('a', this)[0]; });
		this.j$panels = j$([]);
		
		var self = this, o = this.options;
		
		this.j$tabs.each(function(i, a) {
			// inline tab
			if (a.hash && a.hash.replace('#', '')) // Safari 2 reports '#' for an empty hash
				self.j$panels = self.j$panels.add(self._sanitizeSelector(a.hash));
			// remote tab
			else if (j$(a).attr('href') != '#') { // prevent loading the page itself if href is just "#"
				j$.data(a, 'href.tabs', a.href); // required for restore on destroy
				j$.data(a, 'load.tabs', a.href); // mutable
				var id = self._tabId(a);
				a.href = '#' + id;
				var j$panel = j$('#' + id);
				if (!j$panel.length) {
					j$panel = j$(o.panelTemplate).attr('id', id).addClass(o.panelClass)
						.insertAfter(self.j$panels[i - 1] || self.element);
					j$panel.data('destroy.tabs', true);
				}
				self.j$panels = self.j$panels.add(j$panel);
			}
			// invalid tab href
			else
				o.disabled.push(i + 1);
		});
		
		// initialization from scratch
		if (init) {
			
			// attach necessary classes for styling if not present
			this.element.addClass(o.navClass);
			this.j$panels.addClass(o.panelClass);
			
			// Selected tab
			// use "selected" option or try to retrieve:
			// 1. from fragment identifier in url
			// 2. from cookie
			// 3. from selected class attribute on <li>
			if (o.selected === undefined) {
				if (location.hash) {
					this.j$tabs.each(function(i, a) {
						if (a.hash == location.hash) {
							o.selected = i;
							return false; // break
						}
					});
				}
				else if (o.cookie) {
					var index = parseInt(self._cookie(), 10);
					if (index && self.j$tabs[index]) o.selected = index;
				}
				else if (self.j$lis.filter('.' + o.selectedClass).length)
					o.selected = self.j$lis.index( self.j$lis.filter('.' + o.selectedClass)[0] );
			}
			o.selected = o.selected === null || o.selected !== undefined ? o.selected : 0; // first tab selected by default
			
			// Take disabling tabs via class attribute from HTML
			// into account and update option properly.
			// A selected tab cannot become disabled.
			o.disabled = j$.unique(o.disabled.concat(
				j$.map(this.j$lis.filter('.' + o.disabledClass),
					function(n, i) { return self.j$lis.index(n); } )
			)).sort();
			if (j$.inArray(o.selected, o.disabled) != -1)
				o.disabled.splice(j$.inArray(o.selected, o.disabled), 1);
			
			// highlight selected tab
			this.j$panels.addClass(o.hideClass);
			this.j$lis.removeClass(o.selectedClass);
			if (o.selected !== null) {
				this.j$panels.eq(o.selected).removeClass(o.hideClass);
				var classes = [o.selectedClass];
				if (o.deselectable) classes.push(o.deselectableClass);
				this.j$lis.eq(o.selected).addClass(classes.join(' '));
				
				// seems to be expected behavior that the show callback is fired
				var onShow = function() {
					self._trigger('show', null,
						self.ui(self.j$tabs[o.selected], self.j$panels[o.selected]));
				};
				
				// load if remote tab
				if (j$.data(this.j$tabs[o.selected], 'load.tabs'))
					this.load(o.selected, onShow);
				// just trigger show event
				else onShow();
			}
			
			// clean up to avoid memory leaks in certain versions of IE 6
			j$(window).bind('unload', function() {
				self.j$tabs.unbind('.tabs');
				self.j$lis = self.j$tabs = self.j$panels = null;
			});
			
		}
		// update selected after add/remove
		else
			o.selected = this.j$lis.index( this.j$lis.filter('.' + o.selectedClass)[0] );
		
		// set or update cookie after init and add/remove respectively
		if (o.cookie) this._cookie(o.selected, o.cookie);
		
		// disable tabs
		for (var i = 0, li; li = this.j$lis[i]; i++)
			j$(li)[j$.inArray(i, o.disabled) != -1 && !j$(li).hasClass(o.selectedClass) ? 'addClass' : 'removeClass'](o.disabledClass);
		
		// reset cache if switching from cached to not cached
		if (o.cache === false) this.j$tabs.removeData('cache.tabs');
		
		// set up animations
		var hideFx, showFx;
		if (o.fx) {
			if (o.fx.constructor == Array) {
				hideFx = o.fx[0];
				showFx = o.fx[1];
			}
			else hideFx = showFx = o.fx;
		}
		
		// Reset certain styles left over from animation
		// and prevent IE's ClearType bug...
		function resetStyle(j$el, fx) {
			j$el.css({ display: '' });
			if (j$.browser.msie && fx.opacity) j$el[0].style.removeAttribute('filter');
		}

		// Show a tab...
		var showTab = showFx ?
			function(/*mouseover*/click, j$show) {
				j$show.animate(showFx, showFx.duration || 'normal', function() {
					j$show.removeClass(o.hideClass);
					resetStyle(j$show, showFx);
					self._trigger('show', null, self.ui(/*mouseover*/click, j$show[0]));
				});
			} :
			function(/*mouseover*/click, j$show) {
				j$show.removeClass(o.hideClass);
				self._trigger('show', null, self.ui(/*mouseover*/click, j$show[0]));
			};
		
		// Hide a tab, j$show is optional...
		var hideTab = hideFx ? 
			function(/*mouseover*/click, j$hide, j$show) {
				j$hide.animate(hideFx, hideFx.duration || 'normal', function() {
					j$hide.addClass(o.hideClass);
					resetStyle(j$hide, hideFx);
					if (j$show) showTab(/*mouseover*/click, j$show, j$hide);
				});
			} :
			function(/*mouseover*/click, j$hide, j$show) {
				j$hide.addClass(o.hideClass);
				if (j$show) showTab(/*mouseover*/click, j$show);
			};
		
		// Switch a tab...
		function switchTab(/*mouseover*/click, j$li, j$hide, j$show) {
			var classes = [o.selectedClass];
			if (o.deselectable) classes.push(o.deselectableClass);
			j$li.addClass(classes.join(' ')).siblings().removeClass(classes.join(' '));
			hideTab(/*mouseover*/click, j$hide, j$show);
		}
		
		// attach tab event handler, unbind to avoid duplicates from former tabifying...
		this.j$tabs.unbind('.tabs').bind(o.event + '.tabs', function() {
			
			//var trueClick = e.clientX; // add to history only if true click occured, not a triggered click
			var j$li = j$(this).parents('li:eq(0)'),
				j$hide = self.j$panels.filter(':visible'),
				j$show = j$(self._sanitizeSelector(this.hash));
			
			// If tab is already selected and not deselectable or tab disabled or 
			// or is already loading or click callback returns false stop here.
			// Check if click handler returns false last so that it is not executed
			// for a disabled or loading tab!
			if ((j$li.hasClass(o.selectedClass) && !o.deselectable)
				|| j$li.hasClass(o.disabledClass)
				|| j$(this).hasClass(o.loadingClass)
				|| self._trigger('select', null, self.ui(this, j$show[0])) === false
				) {
				this.blur();
				return false;
			}
			
			o.selected = self.j$tabs.index(this);
			
			// if tab may be closed
			if (o.deselectable) {
				if (j$li.hasClass(o.selectedClass)) {
					self.options.selected = null;
					j$li.removeClass([o.selectedClass, o.deselectableClass].join(' '));
					self.j$panels.stop();
					hideTab(this, j$hide);
					this.blur();
					return false;
				} else if (!j$hide.length) {
					self.j$panels.stop();
					var a = this;
					self.load(self.j$tabs.index(this), function() {
						j$li.addClass([o.selectedClass, o.deselectableClass].join(' '));
						showTab(a, j$show);
					});
					this.blur();
					return false;
				}
			}
			
			if (o.cookie) self._cookie(o.selected, o.cookie);
			
			// stop possibly running animations
			self.j$panels.stop();
			
			// show new tab
			if (j$show.length) {
				var a = this;
				self.load(self.j$tabs.index(this), j$hide.length ? 
					function() {
						switchTab(a, j$li, j$hide, j$show);
					} :
					function() {
						j$li.addClass(o.selectedClass);
						showTab(a, j$show);
					}
				);
			} else
				throw 'jQuery UI Tabs: Mismatching fragment identifier.';
				
			// Prevent IE from keeping other link focussed when using the back button
			// and remove dotted border from mouseover link. This is controlled via CSS
			// in modern browsers; blur() removes focus from address bar in Firefox
			// which can become a usability and annoying problem with tabs('rotate').
			if (j$.browser.msie) this.blur();
			
			return false;
			
		});
		
		// disable click if event is configured to something else
		if (o.event != 'click') this.j$tabs.bind('click.tabs', function(){return false;});
		
	},
	add: function(url, label, index) {
		if (index == undefined)
			index = this.j$tabs.length; // append by default
		
		var o = this.options;
		var j$li = j$(o.tabTemplate.replace(/#\{href\}/g, url).replace(/#\{label\}/g, label));
		j$li.data('destroy.tabs', true);
		
		var id = url.indexOf('#') == 0 ? url.replace('#', '') : this._tabId( j$('a:first-child', j$li)[0] );
		
		// try to find an existing element before creating a new one
		var j$panel = j$('#' + id);
		if (!j$panel.length) {
			j$panel = j$(o.panelTemplate).attr('id', id)
				.addClass(o.hideClass)
				.data('destroy.tabs', true);
		}
		j$panel.addClass(o.panelClass);
		if (index >= this.j$lis.length) {
			j$li.appendTo(this.element);
			j$panel.appendTo(this.element[0].parentNode);
		} else {
			j$li.insertBefore(this.j$lis[index]);
			j$panel.insertBefore(this.j$panels[index]);
		}
		
		o.disabled = j$.map(o.disabled,
			function(n, i) { return n >= index ? ++n : n });
		
		this._tabify();
		
		if (this.j$tabs.length == 1) {
			j$li.addClass(o.selectedClass);
			j$panel.removeClass(o.hideClass);
			var href = j$.data(this.j$tabs[0], 'load.tabs');
			if (href)
				this.load(index, href);
		}
		
		// callback
		this._trigger('add', null, this.ui(this.j$tabs[index], this.j$panels[index]));
	},
	remove: function(index) {
		var o = this.options, j$li = this.j$lis.eq(index).remove(),
			j$panel = this.j$panels.eq(index).remove();
		
		// If selected tab was removed focus tab to the right or
		// in case the last tab was removed the tab to the left.
		if (j$li.hasClass(o.selectedClass) && this.j$tabs.length > 1)
			this.select(index + (index + 1 < this.j$tabs.length ? 1 : -1));
		
		o.disabled = j$.map(j$.grep(o.disabled, function(n, i) { return n != index; }),
			function(n, i) { return n >= index ? --n : n });
		
		this._tabify();
		
		// callback
		this._trigger('remove', null, this.ui(j$li.find('a')[0], j$panel[0]));
	},
	enable: function(index) {
		var o = this.options;
		if (j$.inArray(index, o.disabled) == -1)
			return;
		
		var j$li = this.j$lis.eq(index).removeClass(o.disabledClass);
		if (j$.browser.safari) { // fix disappearing tab (that used opacity indicating disabling) after enabling in Safari 2...
			j$li.css('display', 'inline-block');
			setTimeout(function() {
				j$li.css('display', 'block');
			}, 0);
		}
		
		o.disabled = j$.grep(o.disabled, function(n, i) { return n != index; });
		
		// callback
		this._trigger('enable', null, this.ui(this.j$tabs[index], this.j$panels[index]));
	},
	disable: function(index) {
		var self = this, o = this.options;
		if (index != o.selected) { // cannot disable already selected tab
			this.j$lis.eq(index).addClass(o.disabledClass);
			
			o.disabled.push(index);
			o.disabled.sort();
			
			// callback
			this._trigger('disable', null, this.ui(this.j$tabs[index], this.j$panels[index]));
		}
	},
	select: function(index) {
		// TODO make null as argument work
		if (typeof index == 'string')
			index = this.j$tabs.index( this.j$tabs.filter('[hrefj$=' + index + ']')[0] );
		this.j$tabs.eq(index).trigger(this.options.event + '.tabs');
	},
	load: function(index, callback) { // callback is for internal usage only
		
		var self = this, o = this.options, j$a = this.j$tabs.eq(index), a = j$a[0],
				bypassCache = callback == undefined || callback === false, url = j$a.data('load.tabs');
		
		callback = callback || function() {};
		
		// no remote or from cache - just finish with callback
		if (!url || !bypassCache && j$.data(a, 'cache.tabs')) {
			callback();
			return;
		}
		
		// load remote from here on
		
		var inner = function(parent) {
			var j$parent = j$(parent), j$inner = j$parent.find('*:last');
			return j$inner.length && j$inner.is(':not(img)') && j$inner || j$parent;
		};
		var cleanup = function() {
			self.j$tabs.filter('.' + o.loadingClass).removeClass(o.loadingClass)
					.each(function() {
						if (o.spinner)
							inner(this).parent().html(inner(this).data('label.tabs'));
					});
			self.xhr = null;
		};
		
		if (o.spinner) {
			var label = inner(a).html();
			inner(a).wrapInner('<em></em>')
				.find('em').data('label.tabs', label).html(o.spinner);
		}
		
		var ajaxOptions = j$.extend({}, o.ajaxOptions, {
			url: url,
			success: function(r, s) {
				j$(self._sanitizeSelector(a.hash)).html(r);
				cleanup();
				
				if (o.cache)
					j$.data(a, 'cache.tabs', true); // if loaded once do not load them again
				
				// callbacks
				self._trigger('load', null, self.ui(self.j$tabs[index], self.j$panels[index]));
				try {
					o.ajaxOptions.success(r, s);
				}
				catch (e) {}
				
				// This callback is required because the switch has to take
				// place after loading has completed. Call last in order to 
				// fire load before show callback...
				callback();
			}
		});
		if (this.xhr) {
			// terminate pending requests from other tabs and restore tab label
			this.xhr.abort();
			cleanup();
		}
		j$a.addClass(o.loadingClass);
		self.xhr = j$.ajax(ajaxOptions);
	},
	url: function(index, url) {
		this.j$tabs.eq(index).removeData('cache.tabs').data('load.tabs', url);
	},
	destroy: function() {
		var o = this.options;
		this.element.unbind('.tabs')
			.removeClass(o.navClass).removeData('tabs');
		this.j$tabs.each(function() {
			var href = j$.data(this, 'href.tabs');
			if (href)
				this.href = href;
			var j$this = j$(this).unbind('.tabs');
			j$.each(['href', 'load', 'cache'], function(i, prefix) {
				j$this.removeData(prefix + '.tabs');
			});
		});
		this.j$lis.add(this.j$panels).each(function() {
			if (j$.data(this, 'destroy.tabs'))
				j$(this).remove();
			else
				j$(this).removeClass([o.selectedClass, o.deselectableClass,
					o.disabledClass, o.panelClass, o.hideClass].join(' '));
		});
		if (o.cookie)
			this._cookie(null, o.cookie);
	}
});

j$.extend(j$.ui.tabs, {
	version: '@VERSION',
	getter: 'length',
	defaults: {
		// basic setup
		deselectable: false,
		event: 'click',
		//event: 'mouseover',
		disabled: [],
		cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true }
		// Ajax
		spinner: 'Loading&#8230;',
		cache: false,
		idPrefix: 'ui-tabs-',
		ajaxOptions: null,
		// animations
		fx: null, // e.g. { height: 'toggle', opacity: 'toggle', duration: 200 }
		// templates
		tabTemplate: '<li><a href="#{href}"><span>#{label}</span></a></li>',
		panelTemplate: '<div></div>',
		// CSS class names
		navClass: 'ui-tabs-nav',
		selectedClass: 'ui-tabs-selected',
		deselectableClass: 'ui-tabs-deselectable',
		disabledClass: 'ui-tabs-disabled',
		panelClass: 'ui-tabs-panel',
		hideClass: 'ui-tabs-hide',
		loadingClass: 'ui-tabs-loading'
	}
});

/*
 * Tabs Extensions
 */

/*
 * Rotate
 */
j$.extend(j$.ui.tabs.prototype, {
	rotation: null,
	rotate: function(ms, continuing) {
		
		continuing = continuing || false;
		
		var self = this, t = this.options.selected;
		
		function start() {
			self.rotation = setInterval(function() {
				t = ++t < self.j$tabs.length ? t : 0;
				self.select(t);
			}, ms);
		}
		
		function stop(e) {
			if (!e || e.clientX) { // only in case of a true click
				clearInterval(self.rotation);
			}
		}
		
		// start interval
		if (ms) {
			start();
			if (!continuing)
				this.j$tabs.bind(this.options.event + '.tabs', stop);
			else
				this.j$tabs.bind(this.options.event + '.tabs', function() {
					stop();
					t = self.options.selected;
					start();
				});
		}
		// stop interval
		else {
			stop();
			this.j$tabs.unbind(this.options.event + '.tabs', stop);
		}
	}
});

})(jQuery);

j$(function(){
	/* 選択中の写真のサムネイルに枠をつけたい */
	/* 商品詳細 */
    j$('#detailphotoblock .sub_list_img').bind('mouseover',function(e){
		var index = j$("#detailphotoblock .sub_list_img").index(this);
    	//全部の色を消す
		j$("#detailphotoblock .sub_list_img").removeClass("selected");
		
		j$target = j$("#detailphotoblock .sub_list_img");
		//ホバーしたサムネイルだけクラス追加
		j$(j$target[index]).addClass("selected");
    });
});


j$(function(){
   /*
	タブを開くリンクを作りたい
	商品一覧用
	*/


    
});


//コンシェルジュ用
j$(function(){
   /*
	タブを開くリンクを作りたい
	*/

	/* 価格比較タブを開く */
    j$('.spec_info a.comparison_link').bind('click',function(e){
    	//一番目のタブのクラスを除去
		j$("#fragment-1").removeClass("ui-tabs-hide");
		
		//二番目と三番目にクラスを追加
		j$("#fragment-2").addClass("ui-tabs-hide");
		j$("#fragment-3").addClass("ui-tabs-hide");
		j$("#fragment-4").addClass("ui-tabs-hide");
		j$("#ui-tab ul li:nth-child(2)").removeClass("ui-tabs-selected");
		j$("#ui-tab ul li:nth-child(3)").removeClass("ui-tabs-selected");
		j$("#ui-tab ul li:nth-child(4)").removeClass("ui-tabs-selected");
    });

	/* スペック情報タブを開く */
    j$('.spec_info a.spec_link').bind('click',function(e){
    	//二番目のタブのクラスを除去
		j$("#fragment-2").removeClass("ui-tabs-hide");
		j$("#ui-tab ul li:nth-child(2)").addClass("ui-tabs-selected");
		
		//一番目と三番目にクラスを追加
		j$("#fragment-1").addClass("ui-tabs-hide");
		j$("#fragment-3").addClass("ui-tabs-hide");
		j$("#fragment-4").addClass("ui-tabs-hide");
		j$("#ui-tab ul li:nth-child(1)").removeClass("ui-tabs-selected");
		j$("#ui-tab ul li:nth-child(3)").removeClass("ui-tabs-selected");
		j$("#ui-tab ul li:nth-child(4)").removeClass("ui-tabs-selected");
    });
    
});
