var imageCntList  = null;
var imageDayCnt = 0;
var imageAllCnt = 0;

var imageDirectory =  "./image/";

var imageTitle = "";
var startDate = "";
var captureInterval = 0;
var keepPeriod = 0;

var thumbWidth = 0;
var thumbHeight = 0;
var imageWidth = 0;
var imageHeight = 0;

var debugFlag = false;

var divImageStr = "";
var divTimeStr = "";

var horizontalSlider;

var preDate = 0;
var prePosition = -1;



function initialize(debug)
{
	debugFlag = debug;
	
	new Ajax.Request("./image/setting_webcam.txt", { method: 'get', 
						onSuccess: parseSetting});
}

function parseSetting(httpObj){
	var text = httpObj.responseText;
	text = text.replace(/\r/,"");
	var lines = text.split("\n");
	
	for(var i=0;i<lines.length;i++){
		var args = lines[i].split("\t");
		if(args.length < 2)
			continue;
			
		switch(args[0]){
			case "Title":
				imageTitle = args[1];
				break;
			case "ImageSize":
				imageWidth = args[1] * 1;
				imageHeight = args[2] * 1;
				break;
			case "ThumbSize":
				thumbWidth = args[1] * 1;
				thumbHeight = args[2] * 1;
				break;
			case "CaptureInterval":
				captureInterval = args[1] * 1;
				break;
			case "KeepPeriod":
				keepPeriod = args[1] * 1;
				break;
			case "StartDate":
				startDate = args[1];
			
		}
	}
	
	if(keepPeriod == 0){
		keepPeriod = 365;	
	}
	
	if(debugFlag){
		PrintSettingInfo();
	}
	
	imageCntList = new Array();
	imageDayCnt = 0;
	
	ShowLoadingState(-1, -1);
	
	new Ajax.Request("./image/filelist.php", { method: 'get', 
						onSuccess: parseFileList});
	
}

function parseFileList(httpObj){
	var text =  httpObj.responseText;
	text = text.replace(/\r/,"");
	var lines = text.split("\n");
		
	//alert(text);
	for(var i=0;i<lines.length;i++){
		var args = lines[i].split("\t");
		
		if(args.length < 2)
			continue;
		
		var fileName = imageDirectory + args[0];
		var time = args[1];
		//alert(fileName + " " + time);
		
		var date = time.split(" ")[0].split("\/")[2];
		if(date != preDate){
			imageCntList[imageDayCnt] = i;
			imageDayCnt++;
			//alert(date + " " + imageDayCnt + " " + i);
		}
		preDate = date;
		
		//alert (date);
		var thumbFile = fileName;
		var imageFile = thumbFile.replace(/-mini/, "");
		var imageStr =  '<div id="image' + i + '" style="display:none">'  + 
					'<div style="float:left" onmouseover="zoom_on(event,' + 
					thumbWidth + "," + thumbHeight + ",'" +  
					thumbFile + "','" + imageFile + "');" + '" ' + 
					'onmousemove="zoom_move(event);" onmouseout="zoom_off();">' +
					'<img src="' + thumbFile + '" alt="' + imageTitle + 
					'" width="' + thumbWidth +'" height="' + thumbHeight +
					'" style="padding:0;margin:0;border:0" /></div>' + 
					' <div style="clear:both;"></div>' + 
					' </div>';
		
		var timeStr = '<div id="time' + i + '" style="display:none"' + 
						'class="textmini" align="right">' + time +
						'</div>';
						
		divImageStr += imageStr;
		divTimeStr += timeStr;
		//ShowLoadingState(imageDayCnt,i);
		imageAllCnt++;		
	}
	

	imageCntList[imageDayCnt] = imageAllCnt;
	imageDayCnt++;

	$("result").innerHTML = divImageStr;
	$("imageTime").innerHTML = divTimeStr;
	$("image"+(imageAllCnt-1)).style.display = "block";	
	$("time"+(imageAllCnt-1)).style.display = "block";	
	
	initSlider(0, imageAllCnt-1);
}


//------YUI Slider̐----------//
function initSlider(max, min) {

	horizontalSlider = YAHOO.widget.Slider.getHorizSlider("horizBGDiv", 
                           "horizHandleDiv", max, min, 1);


	horizontalSlider.onChange = function(offsetFromStart) {
		 if(document.getElementById("horizVal").value != offsetFromStart){
			document.getElementById("horizVal").value = offsetFromStart;
			updateImage(offsetFromStart);
		}				
	};

	horizontalSlider.onSlideStart = function() {
		// alert("slidestart");
    }

	horizontalSlider.onSlideEnd = function() {
		// alert("slideend");
    }

	horizontalSlider.setValue(imageAllCnt-1);	
}


function onSliderKey(slider, ev) {
	
	var valueCurrent = horizontalSlider.getValue();
	var valueMin = horizontalSlider.thumb.leftConstraint;
	var valueMax = horizontalSlider.thumb.rightConstraint;

	var key = ev.keyCode;
	var shiftFlag = ev.shiftKey;
	switch(key){
		case YAHOO.util.Key.DOM_VK_LEFT:
			
			if(valueCurrent != valueMin){
				if(shiftFlag){
					valueCurrent -=10;
					
					if(valueCurrent < valueMin)
						valueCurrent = valueMin;		
				}
				else{
					valueCurrent--;
				}
				horizontalSlider.setValue(valueCurrent);	
			}
			break;
		case YAHOO.util.Key.DOM_VK_RIGHT:
			
			if(valueCurrent != valueMax){
				if(shiftFlag){
					valueCurrent +=10;
					
					if(valueCurrent > valueMax)
						valueCurrent = valueMax;		
				}
				else{
					valueCurrent++;
				}
				horizontalSlider.setValue(valueCurrent);	
			}
			break;
		default:
			break;
	}

    YAHOO.util.Event.stopEvent(ev);
	return;
}
	


function updateImage(cnt){
	var dayCnt = 0;
	var subCnt = 0;
	
	for(var i=0;i<imageCntList.length;i++){
		if(cnt < imageCntList[i]){
			dayCnt = i;
			if(i==0)
				subCnt = cnt;
			else
				subCnt = cnt - imageCntList[i-1];
			break;	
		}
	}
	
	document.getElementById("horizValDay").value = (imageDayCnt-1) - dayCnt;
	document.getElementById("horizValSub").value = subCnt;
	
	
	//var imageObject = $("image"+cnt);
	$("image"+cnt).style.display = "block";
	$("time"+cnt).style.display = "block";
	if(prePosition != -1){
		$("time"+prePosition).style.display = "none";
		$("image"+prePosition).style.display = "none";
	}

	prePosition = cnt;
				
	if(debugFlag){
		alert(imageStr);
	}

}

////////////////////////////////////////////////////////////////////

function PrintSettingInfo(){
	var result = "";
	
	result += "Title: " + imageTitle + "\r\n";
	result += "StartDate: " + startDate + "\r\n";
	result += "CaptureInterval: " + captureInterval + "\r\n";
	result += "KeepPeriod: " + keepPeriod + "\r\n";
	result += "thumbSize: " + thumbWidth + "," + thumbHeight + "\r\n";
	result += "imageSize: " + imageWidth + "," + imageHeight + "\r\n";

	alert(result);

}

function updateHoriz() {
	var fld = document.forms["formH"]["horizVal"];
	var v = parseFloat(fld.value, 10);
	
	if ( isNaN(v) ) v = 0;
	
	horizontalSlider.setValue(Math.round(v));
    
	var newVal = horizontalSlider.getValue();
	if (v != newVal) {
        fld.value = newVal;
		updateImage(newVal);		
    }
}

function ShowLoadingState(cnt, cnt2){
	var str = "Now Loading";
	if(cnt > 0 || cnt2 > 0){
		for(var i =0; i <cnt;i++){
				str += ".";
		}
	
		str += " " + cnt + ":" + cnt2;
	}
	str = '<table border ="0" width = "' + thumbWidth + '" height = "' +
			thumbHeight + '"><tr><td width="100%" align = "left">' + 
			str +
			'</td></tr></table>'
	
	$("result").innerHTML = str;	

}

