/*
//  GlassDuck Photo Album Software
//  By Jehiah Czebotar http://jehiah.com/projects/glassduck
//  Change as you wish as long as you give credit where credit is due
*/

/* debug function : remove the 'return false' to enable*/
function $d(txt){return false;$("debug").innerHTML += txt + "<br>";}

var version = "1.0";

var pw=null; // parent window pointer
        
// prefetch the 'loading' images
loading1 = new Image();
loading1.src = "loading4030.gif";
loading2 = new Image();
loading2.src = "loading400300.gif";

// Phone home: this lets Jehiah know how much his software is used, and what versions
// feel free to comment this line out
phoneHome= new Image();
phoneHome.src="http://jehiah.com/projects/glassduck/phoneHome.php?version="+version;
document.lastThumb=null;

function getMonthString(month)
{
switch(month){
	case '01':
	return "Jan";
	case '02':
	return "Feb";
	case '03':
	return "Mar";
	case '04':
	return "Apr";
	case '05':
	return "May";
	case '06':
	return "Jun";
	case '07':
	return "Jul";
	case '08':
	return "Aug";
	case '09':
	return "Sep";
	case '10':
	return "Oct";
	case "11":
	return "Nov";
	case "12":
	return "Dec";
	default:
	return "default";
	}
	return month;
}

var cur_year ='';
var cur_month = '';
var cur_day = '';
function loadpage()
{
	// sort array - before sort is is in order by filesys order
	i_array.sort();

	h = document.location.hash.replace("#","").split("/");
	//if (h){$d("hash is " + h);}
	//$d("h.length="+h.length);
	// h=mm/dd/[yy]yy

	cur_year = loadyears();
	if (queryString('year')!='false')
		cur_year = queryString('year');
	if (h.length >=3 && h[2]!=''){cur_year = h[2];if (cur_year.length >= 2){cur_year = cur_year.substr(2,4);}}
	mark("year","y_"+cur_year);

	cur_month = loadmonths(cur_year);
	if (queryString('month')!='false')
		cur_month = queryString('month');
	// mark the current month as active
	if (h.length >=2){cur_month= h[0];}
	mark("month","m_"+cur_month);	

	// load days
	cur_day =  loaddays(cur_month,cur_year);
	if (queryString('day') != 'false')
		cur_day = queryString('day');
	// mark the current day as active
	if (h.length >=2){cur_day = h[1];}
	mark("day","d_"+cur_day);

	// load thumbs
	loadthumbs(cur_month+"-"+cur_day+"-"+cur_year);
}


function loadyears()
{
	var yearlist = new Array();
	var flag = false;
	var x = cleardiv("year");

	var i=0;
	// loop through all the images
	for (i=0;i<count;i++)
	{
		// split the filename into parts for each date
		var this_file = i_array[i].split(/[-_]/);
		//$0 = month
		//$1 = day
		//$2 = year

			flag = false;
			// loop through the current years in our list and see if it is there
			for (var z=0;z < yearlist.length && flag != true;z++)
			{	if ( yearlist[z] == this_file[2])
					flag=true;}
			// if it isn't there add it [ie not a dupe]
			if (flag == false){
				yearlist[yearlist.length] = this_file[2];		
			}
	}	

	if (x == null)
	alert ("x is null");
	
	// sort the yearlist
	yearlist.sort();

	// create the links from yearlist
	// create links for years 1950-1999
	var return_year = "04";
	for(var j=0;j<yearlist.length;j++)
	{
		if (yearlist[j] > 50)
		{
		a = x.appendChild(document.createElement("a"));
		a.href="javascript:var a_n = loadmonths(\""+yearlist[j]+"\");mark(\"year\",\"y_"+yearlist[j]+"\");";
		a.id="y_"+yearlist[j];
		a.innerHTML ="19"+yearlist[j];

		}
	}
	// create links for years 2000 - 2050
	for(var j=0;j<yearlist.length;j++)
	{
		if (yearlist[j] < 50)
		{
		a = x.appendChild(document.createElement("a"));
		a.href="javascript:var a_n = loadmonths(\""+yearlist[j]+"\");mark(\"year\",\"y_"+yearlist[j]+"\");";
		a.id="y_"+yearlist[j];
		a.innerHTML ="20"+yearlist[j];
		return_year = yearlist[j];
		}
	}

	return return_year;

}


function loadmonths(year)
{
	cur_year = year;
	var monthlist = new Array();
	var flag = false;
	var x = cleardiv("month");

	var i=0;
	// loop through all the images
	for (i=0;i<count;i++)
	{
		// split the filename into parts for each date
		var this_file = i_array[i].split(/[-_]/);
		//$0 = month
		//$1 = day
		//$2 = rest

		// if this date is even in the proper year
		if (this_file[2] == year)
		{
			flag = false;
			// loop through the current months in our list and see if it is there
			for (var z=0;z < monthlist.length && flag != true;z++)
			{	if ( monthlist[z] == this_file[0])
					flag=true;}
			// if it isn't there add it [ie not a dupe]
			if (flag == false){
				monthlist[monthlist.length] = this_file[0];		
			}
		}		
	}	

	if (x == null)
	alert ("x is null");

	// create the links from monthlist
	for(var j=0;j<monthlist.length;j++)
	{
		a = x.appendChild(document.createElement("a"));
		a.href="javascript:var a_n = loaddays(\""+monthlist[j]+"\",\""+year+"\");mark(\"month\",\"m_"+monthlist[j]+"\");";
		a.id="m_"+monthlist[j];
		a.innerHTML =getMonthString(monthlist[j]);
	}

	return monthlist[monthlist.length-1];
}


// function to mark links as active/visited
function mark(block,id)
{
	var x = document.getElementById(block);
	// change previous sel to arc 
	for (var i=0;i<x.childNodes.length;i++)
	{
		if (x.childNodes[i].className == "sel")
			x.childNodes[i].className = "arc";
	}	
	// set current to sel
	var y = document.getElementById(id);
	if (y){y.className = "sel";}
}

function loaddays(month,year)
{
	cur_month = month;
	var daylist = new Array();
	var flag = false;
	var x = cleardiv("day");

	var daycount = new Array();
	for (z=0;z<31;z++)
		daycount[z]=0;

	var i=0;
	for (i=0;i<count;i++)
	{
		var this_file = i_array[i].split(/[-_]/);
		//$0 = month
		//$1 = day
		//$2 = rest

		if (this_file[0] == month && this_file[2] == year)
		{
			flag = false;
			for (var z=0;z < daylist.length && flag != true;z++)
			{	if ( daylist[z] == this_file[1])
					flag=true;}
			if (flag == false){
				daylist[daylist.length] = this_file[1];		
			}
			daycount[parseInt(this_file[1])]++;
		}		
	}	

	if (x == null)
	alert ("x is null");

	// create the links from daylist
	for(var j=0;j<daylist.length;j++)
	{
		a = x.appendChild(document.createElement("a"));
		a.href="javascript:loadthumbs(\""+month+"-"+daylist[j]+"-"+year+"\");mark(\"day\",\"d_"+daylist[j]+"\");";
		a.id="d_"+daylist[j];
		a.innerHTML =+daylist[j];
		a.title=daycount[parseInt(daylist[j])] + " images";
	}
	// loadthumbs for the last day
	// clearthumbs
	//	loadthumbs(month+"-"+daylist[daylist.length-1]+"-"+year);
	return daylist[daylist.length-1];
}

function cleardiv(div_id)
{
	var x = document.getElementById(div_id);
	if (x != null)
	while (x.childNodes.length >= 1)
		x.removeChild(x.childNodes[x.childNodes.length-1]);
	return x;
}

// loadthumbs("MM-DD-YY")
function loadthumbs(day)
{
	cur_day = day.split("-")[1];
	selected_date=day.split("-");
	
	document.location.hash="#"+selected_date[0]+'/'+selected_date[1]+'/'+(parseInt(selected_date[2])>50 ? "19" + selected_date[2]:"20"+selected_date[2]);
	var i=0; // all images
	var j=0; // thumbs used
	var x = cleardiv("thumb"); // x = thumb block
	var thumb_height = 35;
	var max_thumbs = Math.round(document.body.clientWidth / (40+4+4)) ;  // 40 width + 4 margin + 4 border

	a = x.appendChild(document.createElement('a'));
	a.href="slideshow.html#"+cur_month+"/"+cur_day+"/"+cur_year;
	a.innerHTML = "view day as slideshow &raquo;";
	br = x.appendChild(document.createElement('br'));

	// loop through all the images
	for (i=0;i < count;i++)
	{
		// if is part of this day
		if (i_array[i].indexOf(day) != -1)
		{
			j++;
			// add to thumb block
			a = x.appendChild(document.createElement('a'));
			a.href="javascript:selectthumb("+i+");mark(\"thumb\",\"t_"+j+"\");";
			a.id = "t_"+j;
			a.title=getDate(i_array[i]);
			img = a.appendChild(document.createElement('img'));
			img.src="loading4030.gif";
			img.src="images40/"+i_array[i];

			// alert(j % max_thumbs );
			// make thumb block double row if needed
			if ( j % max_thumbs == 0 && j >= max_thumbs)
			{
			//	a.style.clear="left";
			//	alert("clearing " + max_thumbs);
			}
		}

	}

	// reset the thumb height if needed
	// alert( Math.ceil(j/max_thumbs));
	// x.style.height=((thumb_height * Math.ceil(j/max_thumbs))+3)+"px";
	br = x.appendChild(document.createElement('br'));
	br.style.clear="left";
}

function selectthumb(i)
{
document.lastThumb=i;

var x = document.getElementById("preview");
var y = document.getElementById("history");

// create preview image
// prefix to preview block
// prefix thumb to history

// clear out more than 4 previews
if (x.childNodes.length >= 4)
	x.removeChild(x.childNodes[x.childNodes.length-1]);

// clear out more than 
if (y.childNodes.length >= 30)
	y.removeChild(y.childNodes[y.childNodes.length-1]);


a = document.createElement('a');
//a.style.display="none";
a.id = "p-" + i_array[i];
x.insertBefore(a,x.childNodes.length >= 1 ? x.childNodes[0] : null);
a.href="javascript:enlarge("+i+");";
img = a.appendChild(document.createElement('img'));
img.src="loading400300.gif";
img.src="images400/"+i_array[i];
img.alt=i_array[i].split(".")[0];//cheap trick to take the beginning of the filename
img.title=getDate(i_array[i]);
//Effect.Grow("p-" + i_array[i]);
//alert("effect p-"+i_array[i]);
a = document.createElement('a');
y.insertBefore(a,y.childNodes.length >= 1 ? y.childNodes[0] : null);
a.href="javascript:selectthumb("+i+");";
img = a.appendChild(document.createElement('img'));
img.src="loading4030.gif";
img.src="images40/"+i_array[i];
}

function getDate(dtstring)
{
    //dtstring == MM-DD-YY_HH
    var dtarray = dtstring.split(/[-_.]/);
    var date= new Date((dtarray[2] < 90 ? "20"+dtarray[2]:"19"+dtarray[2]),dtarray[0],dtarray[1],dtarray[3].substr(0,2),dtarray[3].substr(2,2));//year,month,day
    var aMonth= new Array("January","January","February","March","April","May","June","July","August","September","October","November","December")
    return aMonth[date.getMonth()] + " " + date.getDate() + " " + date.getFullYear() + " " + (date.getHours() > 12 ? date.getHours()-12:date.getHours()+ ":" + date.getMinutes()) + (date.getHours() > 12 ? " pm" : " am");
}


/* BELOW THIS LINE IS CRUFT : still used but shouldn't be */

 function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key)
{
   var page = new PageQuery(window.location.search);
   return unescape(page.getValue(key));
}

// function to pop up a window with the 800x600 version
function enlarge( uri_id ){if(pw!=null&&!pw.closed){pw.location.href="./image.html?"+i_array[uri_id];}else{pw = window.open( "./image.html?"+i_array[uri_id] , "picWin" , "width=825,height=685,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,noresize" );void(0);}pw.focus();}

/* ABOVE THIS LINE IS CRUFT */

function onKeyPress(e){
	// we should still go to the right day... if we switched
	// filename = i_array[document.lastThumb+/-1]
	if (!document.lastThumb){return false;}
	if (e.keyCode == Event.KEY_LEFT){
		if (document.lastThumb >=1)
		selectthumb(document.lastThumb-1);
		return false;
	}
	if (e.keyCode == Event.KEY_RIGHT){
		if (document.lastThumb < i_array.length)
		selectthumb(document.lastThumb+1);
		return false;
	}
}
Event.observe(document,'keypress',onKeyPress);
