var image_margin = 2;
var def_w = 130;
var def_h = 110;

var img_len;
var typeA = new Array();	//394*334の画像番号
var typeB = new Array();	//262*334の画像番号
var typeC = new Array();	//262*222の画像番号
var typeD = new Array();	//その他

var nowA;
var nowB;
var nowC;
var back_mcs = new Array();

var mTime = 7000;
var timerID;


$(function() {
	setMain();
	timerID = setInterval("phShow()", 7000);
});

function setMain(){
	$('#mainImage p').each( function(i){
		if($(this).width() == 394 && $(this).height() == 334) {
			typeA.push($(this));
		}else if($(this).width() == 262 && $(this).height() == 334) {
			typeB.push($(this));
		}else if($(this).width() == 262 && $(this).height() == 222) {
			typeC.push($(this));
		}else {
			typeD.push($(this));
		}
	});

	img_len = typeD.length;
	phShow();
}

function phShow(){

	var poss = new Array();
	var now_img = new Array();
	var cnt = 0;

	var Aimg = Math.floor( Math.random() * typeA.length );
//	if(nowA != Aimg) {
		var Adimg = typeA[Aimg].clone();
		$("#mainImage").append(Adimg);
		var myx = Math.floor( Math.random() * 6 );
		var myy = Math.floor( Math.random() * 2 );
		Adimg.css({left: myx * (def_w + image_margin), top: myy * (def_h + image_margin)});
		for(var i = 0; i < 3; i ++) {
			poss[(myy+0)*8 + myx + i] = true;
			poss[(myy+1)*8 + myx + i] = true;
			poss[(myy+2)*8 + myx + i] = true;
		}
		cnt += 9;
		now_img[myy * 8 + myx] = Adimg;
		nowA = Aimg;
//	}

	var Bimg = Math.floor( Math.random() * typeB.length );
//	if(nowB != Bimg) {
		var myx = Math.floor( Math.random() * 7 );
		var myy = Math.floor( Math.random() * 2 );
		var my_flg = false;
		for(var i = 0; i < 2; i ++) {
			if(poss[myx + i] == true || poss[(myy+1)*8 + myx + i] == true || poss[(myy+2)*8 + myx + i] == true) {
				my_flg = true;
				break;
			}
		}
		if(!my_flg) {
			var Bdimg = typeB[Bimg].clone();
			$("#mainImage").append(Bdimg);
			Bdimg.css({left: myx * (def_w + image_margin), top: myy * (def_h + image_margin)});
			for(var i = 0; i < 2; i ++) {
				poss[(myy+0)*8 + myx + i] = true;
				poss[(myy+1)*8 + myx + i] = true;
				poss[(myy+2)*8 + myx + i] = true;
			}
			cnt += 6;
			now_img[myy * 8 + myx] = Bdimg;
			nowB = Bimg;
		}
//	}

	var Cimg = Math.floor( Math.random() * typeC.length );
//	if(nowC != Cimg) {
		var myx = Math.floor( Math.random() * 7 );
		var myy = Math.floor( Math.random() * 3 );
		var my_flg = false;
		for(var i = 0; i < 2; i ++) {
			if(poss[myx + i] == true || poss[(myy+1)*8 + myx + i] == true || poss[(myy+2)*8 + myx + i] == true) {
				my_flg = true;
				break;
			}
		}
		if(!my_flg) {
			var Cdimg = typeC[Cimg].clone();
			$("#mainImage").append(Cdimg);
			Cdimg.css({left: myx * (def_w + image_margin), top: myy * (def_h + image_margin)});
			for(var i = 0; i < 2; i ++) {
				poss[(myy+0)*8 + myx + i] = true;
				poss[(myy+1)*8 + myx + i] = true;
			}
			cnt += 4;
			now_img[myy * 8 + myx] = Cdimg;
			nowC = Cimg;
		}
//	}

	var arr_a = new Array();
	var arr_b = new Array();
	for(var i=0; i < 32; i ++) {
		if(poss[i] != true) arr_a.push(i);
	}
	for(var i=0; i < img_len; i ++) {
		arr_b.push(i);
	}
	arr_a.shuffle();
	arr_b.shuffle();

	for(var i=0; i < arr_a.length; i ++) {
		if(cnt > 30 || i > img_len - 1) break;
		var my_pos = arr_a[i];
		if(poss[my_pos] == true) {
			continue;
		}
//		poss[my_pos] = true;
		var my_img = typeD[arr_b[i]].clone();
		$("#mainImage").append(my_img);
		my_img.css({left: (my_pos%8) * (def_w + image_margin), top: Math.floor(my_pos/8) * (def_h + image_margin)});
		now_img[my_pos] = my_img;
		cnt ++;
	}


	var arr_c = new Array();
	for(var i=0; i < 32; i ++) {
		arr_c.push(i);
	}
	arr_c.shuffle();
	for(var i=0; i < 32; i ++) {
		change(back_mcs[arr_c[i]], now_img[arr_c[i]], i)
	}

	back_mcs = new Array();
	back_mcs = now_img;
}
function change(back_img, now_ims, i) {
	if(back_img) back_img.delay(i*0.01 * 1000).fadeOut(600, "linear", function(){back_img.remove()});
	if(now_ims) now_ims.delay(i*0.01 * 1000).fadeIn(600, "linear");
}



Array.prototype.shuffle = function() {
    var i = this.length;
    while(i){
        var j = Math.floor(Math.random()*i);
        var t = this[--i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
}

