
var moveunit;
var options = {};
var autotimer;
var speed;
var speednormal = 3000;
var speedhigh = 500;
var delay = 150;
var directionleft = true;
var restart = false;

$(function(){
	speed = speednormal;
	moveunit = $('.boxin').children().first().outerWidth(true);
	bindevent();
	$(".yoxview").yoxview({
		lang: "ja",
		renderInfoPin:false,
		renderInfo:false,
		onClose:yoxclose,
		onOpen:yoxopen,
		onSelect:yoxselect
	});
	bindmouseevent();
	if(!directionleft || (directionleft && !restart)){
		directionleft = true;
		automove();
	}
});

function yoxclose(){
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		$('#overlayie6').hide();
	}
	bindmouseevent();
	automove();
}
function yoxopen(){
	stopautomove();
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		$('#overlayie6').unbind().click(function(){
			$.yoxview.close();
		});
		$('#yoxview_popupWrap').css('filter','alpha(opacity=0)');
	}
}
function yoxselect(){
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		if(!$('#overlayie6').length){
			$("body").append(
			$("<div/>").attr('id','overlayie6')
				.css("","")
				.css("z-index","90")
				.css("left","0px")
				.css("position","absolute")
				.css("top","0px")
				.css("background-color","#000")
				.css("display","none")
				.css("filter","alpha(opacity=80)")
			
			);
			
		}
		$('#overlayie6').width($(window).width());
		$('#overlayie6').height($(document).height()>$(window).height()?$(document).height():$(window).height());

		$('#overlayie6').show();
	}
	$('.box').unbind();
	stopautomove();
}
var yox;
function yoxsetup(){
	
	$(".yoxview").yoxview({
		lang: "ja",
		onClose:yoxclose,
		onOpen:yoxopen,
		onSelect:yoxselect
	});
}

	function bindmouseevent(){
		$('.box').mouseleave(function(){automove();});
		$('.box').mouseenter(function(){stopautomove();});
	}
	function bindevent(){
		$('#previous').unbind();
		$('#next').unbind();

		$('#previous').mouseenter(function() {
			$(".boxin").stop(true, true);
			speed = speedhigh;
			if(!directionleft || (directionleft && !restart)){
				directionleft = true;
				automove();
			}
		});
		$('#next').mouseenter(function() {
			$(".boxin").stop(true, true);
			speed = speedhigh;
			if(directionleft || (!directionleft && !restart)){
				directionleft = false;
				automove();
			}
		});

		$('#previous,#next').mouseleave(function() {
			$(".boxin").stop(true, true);
			speed = speednormal;
		});

//		$.yoxview.update();
	}
function moveprevious(){
	if(directionleft){
			$('.boxin').animate({
				"left": "-="+moveunit+"px"
			}, {duration: speed, easing: "linear", complete: function() {
				if(moveunit*2*-1 >= parseInt($('.boxin').css('left'),10)){
					$('.boxin').append($('.boxin').children().first());
					$('.boxin').animate({"left": "+="+moveunit+"px"},0);
					$(".yoxview").yoxview("update");
					
				}
				bindevent();
			    // Animation complete.
				if(restart && directionleft){
					clearTimeout(autotimer);
					autotimer = window.setTimeout(moveprevious,delay);
				}
			}});
	}
}
function movenext(){
	if(!directionleft){
			if(moveunit*0 == parseInt($('.boxin').css('left'),10)){
				$('.boxin').prepend($('.boxin').children().last());
				$('.boxin').animate({"left": "-="+moveunit+"px"},0);
				$(".yoxview").yoxview("update");
				
			}
			$('.boxin').animate({
				"left": "+="+moveunit+"px"
			}, {duration: speed, easing: "linear", complete: function() {
				if(moveunit*1 <= parseInt($('.boxin').css('left'),10)){
					$('.boxin').prepend($('.boxin').children().last());
					$('.boxin').animate({"left": "-="+moveunit+"px"},0);
					$(".yoxview").yoxview("update");
					

				}
				bindevent();
				if(restart && !directionleft){
					clearTimeout(autotimer);
					autotimer = window.setTimeout(movenext,delay);
				}
			}});
	}
}

function automove(){
		restart = true;
		clearTimeout(autotimer);
		if(directionleft){
			moveprevious();
		}else{
			movenext();
		}
}
function stopautomove(){
	$(".boxin").stop(true, true);
	clearTimeout(autotimer);
	restart = false;

}

