var path=window.location.href; 
var myName=path.substring(path.lastIndexOf('/',path.length)+1,path.length); 
var getParam = window.location.search;

//alert(getParam);

imageNo = 1;

imagePath = "./picture/";
textPath = "./caption/";

lotEnd = 266;
firstLot = 1;

nowBranch = 1;

// 初回読み込み時
function firstNo()
{
	changeImage(getParam.substring(1));
}


// 前の画像
function getBack()
{
	imageNo--;

	if(imageNo<1) imageNo = lotEnd;
	
	changeImage(imageNo);

}

// 次の画像
function getNext()
{

	imageNo++;
	
	if(imageNo>lotEnd) imageNo = 1;
	
	changeImage(imageNo);


}


// image変更
function changeImage(no)
{

	var newImage = new Image();

	if(no == ""){
		no = firstLot;
	}


/*
    newImage.src = imagePath + no + ".jpg";
	document.images["image"].src = newImage.src;
*/

	changeImage_path(imagePath + no + ".jpg");

	loadText = textPath + no + ".jpg";
    document.images["text"].src = loadText;

	imageNo = no;

	// 枝番を検索(初期枝番は1)
	nowBranch = 1;
	checkBranch(nowBranch);
	
}

// 画像の切り替え
function changeImage_path(path){

	document.images["image"].src = path;


}

// 枝番の検索
function searchBranch(no, branch) {

	if(branch != 1){
		fileName = imagePath + no + "-" + branch + ".jpg";
	} else {
		fileName = imagePath + no + ".jpg";
	}

	return fileExist(fileName);

}

// 次の枝番を表示
function branchChange(move){

	var fileName;

	nowBranch += move;
	if(nowBranch != 1){
		fileName = imagePath + imageNo + "-" + nowBranch + ".jpg";
	} else {
		fileName = imagePath + imageNo + ".jpg";
	}

	changeImage_path(fileName);
	checkBranch(nowBranch);

}


function checkBranch(branchNo){

	// 手前を検索
	var branch_next = document.getElementById('branch_next');
	if(searchBranch(imageNo, nowBranch+1)){
		branch_next.style.display = 'inline';
	} else {
		branch_next.style.display = 'none';
	}	

	// 次を検索
	var branch_back = document.getElementById('branch_back');
	if(searchBranch(imageNo, nowBranch-1)){
		branch_back.style.display = 'inline';
	} else {
		branch_back.style.display = 'none';
	}	

}


// 接続の開始
function createHttpRequest(){

  var xmlHttpRequest = false;

	if (typeof XMLHttpRequest != 'undefined') {
		//firefox
		xmlHttpRequest = new XMLHttpRequest();
	} else {
		// ie
		try {
			xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
	  	} catch (e) {
	    	try {
	      		xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
	    	} catch (E) {
	      		xmlHttpRequest = false;
	    	}
	  	}
	}

  return xmlHttpRequest;

}

// ファイルの有無確認
function fileExist(uri){
	var objHTTP = createHttpRequest();

	try {
		objHTTP.open("GET", uri, false);
		objHTTP.send(null);
	} catch(e) {
		objHTTP.abort();
		return false;
	}



	// 読み込めた？
	for(i=0; i<10000; i++){
		if(objHTTP.status == 200){
			objHTTP.abort();
			return true;
		}
	}
	
	objHTTP.abort();
	return false;

}


function notes(eve){

// message = "Don not copy the image without any notice.\nCopyright@2008 SHINWA ART AUCTION Co., LTD.";
 message = "掲載写真の無断転載を禁じます。";

    if(document.all){
        if(event.button == 2){
            alert(message);
            return false;
        }
    }
    if(document.layers){
        if(eve.which == 3){
            alert(message);
            return false;
        }
    }
}

if(document.layers)document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=notes;
