/**
 * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
 * @requires jQuery v1.2 or above
 *
 * http://gmarwaha.com/jquery/jcarousellite/
 *
 * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 1.0.1
 * Note: Requires jquery 1.2 or above from version 1.0.1
 */
(function($) { // Compliant with jquery.noConflict()
	$.fn.jCarouselLite = function(o) {
		o = $.extend({
			root : null,
			btnPrev : null,
			btnNext : null,
			auto : 10000,
			speed : 400,
			fade : 400,
			current : 'current',
			easing : 'swing',
			vertical : false,
			visible : 4,
			start : 0,
			scroll : 1,
			mv : true,
			ex : false
		}, o || {});
		this.each(function() { // Returns the element collection. Chainable.
			var running = false,
				pause = false,
				animCss = o.vertical ? 'top' : 'left',
				sizeCss = o.vertical ? 'height' : 'width',
				div = $(this),
				ul = $('ul', div),
				tLi = $('li', ul),
				tl = tLi.size(),
				v = o.visible;
				itemLen = tLi.length,
				$banner = o.root.find('.banner'),
				$ui = o.root.find('.ui');
			if (o.mv) {
				$banner.css({
					height : $banner.find('img').height(),
					overflow : 'hidden'
				});
				$banner.find('p').css('position', 'absolute');
				$ui.show().find('a').attr('tabindex', '-1');
			} else {
				if (o.ex) {
					div.css('margin-left', parseInt(div.css('margin-left'), 10) + 2);
					div.css('margin-right', parseInt(div.css('margin-right'), 10) + 2);
				}
			}
			ul.prepend(tLi.slice(tl - v - 1 + 1).clone()).append(tLi.slice(0, v).clone());
			o.start += v;
			var li = $('li', ul),
				itemLength = li.size(),
				curr = o.start;
			li.css({
				float : o.vertical ? 'none' : 'left'
			});
			if (o.mv) {
				li.eq(v).addClass(o.current);
			}
			ul.css({
				position : 'relative',
				zIndex : 1
			});
			div.css({
				overflow : 'hidden',
				zIndex : 2
			});
			var liSize = o.vertical ? li.eq(0).outerHeight({
				margin : true
			}) : li.eq(0).outerWidth({
				margin : true
			}); // Full li size(incl margin)-Used for animation
			var ulSize = liSize * itemLength; // size of full ul(total length, not just for the visible items)
			var divSize = liSize * v; // size of entire div(total length for just the visible items)
			li.css({
				width : li.width(),
				height : li.height()
			});
			ul.css(sizeCss, ulSize +'px').css(animCss, -(curr * liSize));
			div.css(sizeCss, divSize - parseInt(li.css('margin-'+ (o.vertical ? 'bottom' : 'right')), 10) - (o.ex ? 3 : 0) +'px'); // Width of the DIV. length of visible images
			if (!o.mv) {
				li.not(vis()).find('a').attr('tabindex', '-1');
				li.not(vis()).find('>*').hide();
				$(o.btnPrev +','+ o.btnNext).show().attr('tabindex', 0);
			}
			setInterval(function() {
				if (!pause) {
					go(curr + o.scroll);
				}
			}, o.auto + o.speed);
			$(o.btnPrev).bind('click', function() {
				return go(curr - o.scroll);
			}).bind('keypress', function(e) {
				if (e.keyCode == 13) {
					return go(curr - o.scroll);
				}
			});
			$(o.btnNext).bind('click', function() {
				return go(curr + o.scroll);
			}).bind('keypress', function(e) {
				if (e.keyCode == 13) {
					return go(curr + o.scroll);
				}
			});
			o.root.hover(function() {
				pause = true;
			}, function() {
				pause = false;
			}).find('a, [tabindex]').bind('focus focusin', function() {
				pause = true;
			}).bind('blur focusout', function() {
				pause = false;
			});
			if (o.mv) {
				for (var i = 0, l = $banner.find('p').length; l > i; i++) {
					var $bnr = $($banner.find('p')[i]);
					if (i < 1) {
						$bnr.css('z-index', 3).addClass(o.current)
					} else {
						$bnr.css('opacity', 0);
					}
					if (!jQuery.support.opacity && $bnr.find('img').attr('src').indexOf('.png') > -1) {
						$bnr.find('img').css({
							filter : 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+ $bnr.find('img').attr('src') +'");'
						});
					}
				}
				var currLi = ul.find('.'+ o.current),
					currLiOffset = currLi.offset(),
					rootOffset = o.root.offset(),
					currFrame = $('<span/>').attr({
						'class' : 'current-frame'
					}).css({
						top : currLiOffset.top - rootOffset.top,
						left : currLiOffset.left - rootOffset.left
					}).append($('<span/>').attr({
						'data-href' : $banner.find('.'+ o.current).find('a').attr('href'),
						'data-target' : $banner.find('.'+ o.current).find('a').attr('target') ? $banner.find('.'+ o.current).find('a').attr('target') : 'false'
					}).text(currLi.find('img').attr('alt')));
				o.root.append(currFrame);
				currFrame.find('span').bind('click', function() {
					var $this = $(this),
						$href = $this.attr('data-href');
					if ($this.attr('data-target') == '_blank') {
						window.open($href);
					} else {
						location.href = $href;
					}
					$href = $href.match(/http/i) ? $href : "http://www.uqwimax.jp"+$href;
					matcTracker._trackEvent('top_slide',$href);
					return false;
				});
				li.find('a').bind('mouseover', function() {
					if (!running) {
						mv($(this));
					}
				}).bind('click', false);
				$banner.find('p').find('a').bind('focus', function() {
					var $bnr = $(this).parent('[id]');
					if (!$bnr.hasClass(o.current)) {
						$banner.find('.'+ o.current).css({
							zIndex : 1,
							opacity : 0
						}).removeClass(o.current);
						$bnr.css({
							zIndex : 4,
							opacity : 1
						}).addClass(o.current);
					}
					currFrame.hide();
				}).bind('blur', function() {
					currFrame.show();
				});
			}
			function vis() {
				return li.slice(curr).slice(0, v);
			};
			function go(to) {
				if (!running) {
					if (!o.mv) {
						li.find('a').attr('tabindex', '-1');
						li.find('>*').show();
					}
					var dir = to - curr > 0,
						joint = false;
					if (to <= o.start - v - 1) { // If first, then goto last
						joint = true;
						ul.css(animCss, -((itemLength - (v * 2)) * liSize) +'px');
						// If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
						curr = to == o.start - v - 1 ? itemLength - (v * 2) - 1 : itemLength - (v * 2) - o.scroll;
					} else if (to >= itemLength - v + 1) { // If last, then goto first
						joint = true;
						ul.css(animCss, -(v * liSize) +'px');
						// If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
						curr = to == itemLength - v + 1 ? v + 1 : v + o.scroll;
					} else {
						curr = to;
					}
					running = true;
					ul.stop(false, true).animate(animCss == 'left' ? {
						left : -(curr * liSize)
					} : {
						top : -(curr * liSize)
					}, o.speed, o.easing, function() {
						if (o.mv) {
							var $currLi = ul.find('.'+ o.current);
							if (joint) {
								for (var i = 0; itemLen - 1 > i; i++) {
									if (dir) {
										$currLi = $currLi.prev();
									} else {
										$currLi = $currLi.next();
									}
								}
								mv($currLi.find('a'));
							} else {
								if (dir) {
									mv($currLi.next().find('a'));
								} else {
									mv($currLi.prev().find('a'));
								}
							}
						} else {
							li.find('>*').hide();
							vis().find('>*').show();
							vis().find('a').removeAttr('tabindex', '-1');
						}
						running = false;
					});
				}
				return false;
			}
			if (o.mv) {
				function mv(obj) {
					var id = obj.attr('href'),
						$id = $(id),
						pos = o.vertical ? obj.offset().top - o.root.offset().top : obj.offset().left - o.root.offset().left;
					ul.find('.'+ o.current).removeClass(o.current);
					obj.parent('li').addClass(o.current);
					currFrame.css((o.vertical ? 'top' : 'left'), pos).find('span').attr({
						'data-href' : $id.find('a').attr('href'),
						'data-target' : $id.find('a').attr('target') ? $id.find('a').attr('target') : 'false'
					}).text(obj.find('img').attr('alt'));
					var currBnr = $banner.find('.'+ o.current);
					if (id.indexOf(currBnr.attr('id')) < 0) {
						currBnr.stop(true, true).css('z-index', 2);
						$id.css('z-index', 3).stop(true, true).animate({
							opacity : 1
						}, o.fade, o.easing, function() {
							currBnr.css({
								zIndex : 1,
								opacity : 0
							}).removeClass(o.current);
							$id.addClass(o.current);
						});
						function antiskid() {
							var opacity = 0,
								banner = $banner.find('>p');
							for (var i = 0; banner.length > i; i++) {
								opacity += parseInt(banner.eq(i).css('opacity'), 10);
							}
							if (opacity < 1) {
								$($ui.find('.'+ o.current).find('a').attr('href')).stop(false, true).animate({
									opacity : 1
								}, o.fade, o.easing);
							}
						}
						setTimeout(antiskid, o.fade + 10);
					}
				}
			}
			return this;
		});
	};
})(jQuery);
(function($) {
	$.fn.mainVisual = function(options) {
		o = $.extend({
			carousel : $(this).selector +' .ui',
			btnNext : $(this).selector +' .down img',
			btnPrev : $(this).selector +' .up img'
		}, options);
		return this.each(function() {
			var root = $(this);
			$(o.carousel).find('div').jCarouselLite({
				root : root,
				btnNext : o.btnNext,
				btnPrev : o.btnPrev,
				vertical : true
			});
		});
	};
})(jQuery);
(function($) {
	$.fn.carousel = function(options) {
		o = $.extend({
			carousel : $(this).selector +' .carousel',
			btnNext : $(this).selector +' .next img',
			btnPrev : $(this).selector +' .back img'
		}, options);
		return this.each(function() {
			var root = $(this);
			$(o.carousel).jCarouselLite({
				root : root,
				btnNext : o.btnNext,
				btnPrev : o.btnPrev,
				speed : 1000,
				visible : 3,
				mv : false,
				ex : true
			});
		});
	};
})(jQuery);

/**
 * jQuery AnotherPictRollOver plugin
 * マウスオーバーした画像と別の画像をロールする
 */
(function($){
	var name_space = 'AnotherPictRollOver';
	$.fn[name_space] = function(options){
		var elements = $(this);
		var settings = $.extend({
			setter : false,
			target : false
		}, options);
		function get(){
			return function(i){
				var setter = $("."+settings.setter, this), target = $("."+settings.target, this);
				if(target.length<=0){return}
				var overRegexp = new RegExp("(\.[a-z]+)$");
				var outRegexp = new RegExp("(_o)(\.[a-z]+)$");
				var roll = new Image;
				roll.src = target.attr("src").replace(overRegexp, "_o$1");
				setter.hover(function(){
					target.attr("src", target.attr("src").replace(overRegexp, "_o$1") );
				},function(){
					target.attr("src", target.attr("src").replace(outRegexp, "$2") );
				});
			}
		}
		elements.each(get());
		return this;
	};
})(jQuery);

/**
 * jQuery setProductImageRandom plugin
 * トップ商品画像のランダム切り替え
 */
(function($){
	var name_space = 'setTopProductImageRandom';
	$.fn[name_space] = function(options){

		var settings = $.extend({
			maxnum: false
		}, options);
		
		var maxnum = settings.maxnum,
			random = 1 + Math.floor(Math.random() * maxnum);
			src = $(this).children('.unroll').attr('src').replace(/_0[0-9]\./,"_0"+random+".");
		$(this).children('.unroll').attr('src',src);
	};
})(jQuery);

$(function(){
	$('#recommend').carousel();
	$('#theme').mainVisual();
	$('#random li').AnotherPictRollOver({ setter:'unroll', target:'target' });
	$('#random li:nth-child(1) a').setTopProductImageRandom({ maxnum:6 });
	$('#random li:nth-child(2) a').setTopProductImageRandom({ maxnum:8 });
	$('#random li:nth-child(3) a').setTopProductImageRandom({ maxnum:2 });
});
