var imgs = new Array();
var max = 0;
function loadComp(){
	var selector = "#itemImg li";
	imgs['item'] = new Array();
	imgs['w'] = new Array();
	$(selector).each(function(i){
		imgs['item'][i] = $(this);
		//var w = $("#itemImg li img").width();
		var w=740;
		$(this).css("position", "absolute");
		//EXIST VIDEO(iframe)
		if($("iframe").attr("width") > 0){
			var videoW = Number($("iframe").attr("width"));
			if(i==0){
				imgs['w'][0] = 0;
				max += videoW;
			}else if(i==1){
				imgs['w'][1] = videoW;
				max += w;
			}else{
				//video分をプラスする
				imgs['w'][i] = w*i+(videoW-740);
				max += w;
			}
		}else{
			imgs['w'][i] = w*i;
			max += w;
		}
		$(this).css("left", imgs['w'][i]+"px");
		
	});
	$("#itemImg").hide();
	$("#itemImg").fadeIn("slow");
	$('.loading').hide();
	
	//img exist over 1
	if(imgs['w'].length > 1){
		slideImg();
	}
	
	//KEY EVENT
	if($.browser.msie){
		// for IE
		window.document.onkeydown = function(e) {
			if(event.keyCode == 39){slideImgMenu('NEXT');return false;}
			if(event.keyCode == 37){slideImgMenu('PREV');return false;}
			if(event.keyCode == 38){zoom();return false;}
		}
	}else{
		$(window).keydown(function(e){
			if(event.keyCode == 39){slideImgMenu('NEXT');return false;}
			if(event.keyCode == 37){slideImgMenu('PREV');return false;}
			if(event.keyCode == 38){zoom();return false;}
		});
	}
	
}

var index = 0;
var selectors = "#itemImg li img";
var smax = 0;
var margin = 100;
function slideImg(){
	$(selectors).click(function(){
		index = $(selectors).index(this);
		slideAction();
	});
}
function slideImgMenu(page){
	if(smax == 0)return;
	if(page == "PREV"){
		if(0 <= index)index -=1;
	}else if(page == "NEXT"){
		if(smax > index)index +=1;
	}
		slideAction();
}
function slideAction(){
		var rest = max - $(window).width();
		if(index == 0){
			var pos =  0;
		}else if(index == smax || rest<=imgs['w'][index]){
			var pos = (-max) + $(window).width();
		}else{
			var pos = -imgs['w'][index]  + margin;
		}
		$("#itemImg").animate({left:  pos}, {easing:  "easeOutExpo",queue:false, duration:1000});
}



var zoom_flg = false;
var ww = $(window).width();
var wh = $(window).height();
var contH = $(document).height() + 300;
function zoom() {
	if(zoom_flg == false){
		$("#wrapper").before("<div class='blackScreen'></div><iframe scrolling='no' frameborder='0' id='shim'></iframe>");
		
		$("#shim").css("display", "block");
		if(ww <= 1220) {
			$(".blackScreen").width(1220);
			$("#shim").width(1220);
		}else{
			$(".blackScreen").css("width", "100%");
			$("#shim").css("width", "100%");
		}
		$("#shim").css("height", contH);
		
		posX = ww/2 - ($("#zoomItem").width())/2;
		posY = wh/2 - ($("#zoomItem").height())/2;
		if(posY <= 0) {
			posY = 10;
		}
		
		$("#zoomItem").fadeIn("slow");
		$("#zoomItem").css("left", posX);
		$("#zoomItem").css("top", posY);
		
		
		zoom_flg = true;
		return false;
	}else{
		zoomClose();
	}
}
function zoomClose() {
	if(zoom_flg == true){
		$(".blackScreen").fadeOut("slow", backRemove);
		$("#zoomItem").fadeOut("slow");
		zoom_flg= false;
		return;
	}
}
function backRemove() {
	$(".blackScreen").remove();
	$("#shim").remove();
}
function scrolls(){
	if(ww <= 1220) {
		$(".blackScreen").width(1220);
	}else{
		$(".blackScreen").css("width", "100%");
	}
	if(wh <= contH) {
		$(".blackScreen").height(contH);		
	}
}
function resizeW() {

	ww = $(window).width();
	wh = $(window).height();
	
	posX = ww/2 - ($("#zoomItem").width())/2;
	posY = wh/2 - ($("#zoomItem").height())/2;
	if(posY <= 0) {
		posY = 10;
	}
	$("#zoomItem").css("left", posX);
	$("#zoomItem").css("top", posY);
	
	if(ww <= 1220) {
		$(".blackScreen").width(1220);
	}else{
		$(".blackScreen").css("width", "100%");
	}
	if(wh <= contH) {
		$(".blackScreen").height(contH);		
	}
	
}
$(window).bind("resize", resizeW);
$(window).bind("scroll", scrolls);
