// photopage by Sean Mollitt - Jan 28 2009 - sean@quebec.net


randompics= new Array(
'photos/randompics/',
'pixel.gif',
'taurus.jpeg',
'myeye.jpeg',
'pixel.gif',
'pixel.gif',
'mehal.jpeg'
)


opac  = 0;
opacp = 0;
isplaying=0

ie5 = (document.all && document.getElementById); 
ns6 = (!document.all && document.getElementById); 

timeDelay = 10; // change delay time in seconds
timeDelay *= 5000;
startat = 0;
numsq=9
caption_to=''


function init(){
//	getsq();
//	startcyc();
	gostart()
}

baseref  = location.href.substring(0, location.href.lastIndexOf('/')+1)
photolist = 'photos/photos.js'
photopath = 'photos/'



function goalbum(){
	alb = prompt("Get album (name):", "")
	var num = albumlabels.indexOf(alb)
	if(num!=-1){getalbum(num)}
}

function getalbum(num){
	
	var pobj=document.getElementById('viewphoto')
	
	pobj.style.visibility="hidden"
	
//	if(ie5){pobj.filters.alpha.opacity = 0;}
//	else{pobj.style.opacity=0}

	currentalbum=num
	albuminfo(currentalbum)

//	var htm = '<table cellpadding=0 cellspacing=0 height=40 width=100% border=0><tr><td valign=top class="albuminfotxt">'+albuminf+'</td></tr></table>'
//	vobj = document.getElementById('albuminfo')
//	vobj.innerHTML = htm		

//document.getElementById('albumtitle').innerHTML = albumlab
	
	photosrc=new Array()
	photocap=new Array()
	totpics = photos.length-1

	for (var i = 1; i < photos.length; ++i){
		var sep=photos[i].indexOf(',')

		if(sep!=-1){
		photosrc[i]=photos[i].substring(0,sep)
		photocap[i]=photos[i].substring(sep+1,photos[i].length)
		}
		else{
		photosrc[i]=photos[i]
		photocap[i]=''
		}
	}
//	clearthumbs()
	init();
}


function makealbummenu(){
	j=0
	albumlabels = new Array() 
	for (var i = 0; i < photos.length; ++i){
		albuminfo(i)
		albumlabels[i] = albumlab
		if(albumhide){continue}
		document.amenu.menu.options[j]=new Option((j+1)+'. '+albumlab,i, false,false)
		j++
	}
	document.amenu.menu.options[currentalbum].selected=true	
}

function albuminfo(num){
	var sep=photos[0].indexOf(',')

	if(sep!=-1){
		albumloc = photos[0].substring(0,sep)
		var sep2 = photos[0].indexOf(',',sep+1)	
		albumlab = photos[0].substring(sep+1,sep2)
		albuminf = photos[0].substring(sep2+1,photos[0].length)
	}
	else{	albumloc = photos[0]
		albumlab =''
		albuminf =''
	}
	
	if(albumloc.indexOf('@')==0){albumloc=albumloc.substring(1,albumloc.length);albumhide=1}else{albumhide=0}
}


function cycolors(){
	
return	
	
	if(j==colorcycle.length){j=0}
	iobj.style.backgroundColor= logograd[j]
	j++
}

function randomnum(num1,num2){
	num1 = parseInt(num1);
	num2 = parseInt(num2);
	var generator = Math.random()*(num2-num1);
	generator = Math.round(num1+generator);
	return(generator)
}


function pictransition(){
	vobj.style.visibility="visible"
	swapfade(document.getElementById('test'), 'buttons/udm4-whitebutton88x31.gif', '2', 'Accessible Website Menu [white]');
}


function setopac(obj,opc){
//	var opc=100
	if(ie5){obj.filters.alpha.opacity = opc;}
//	else if(ns6){document.getElementById('picbox').style.MozOpacity = opacp/100;}
	else{obj.style.opacity=opc/100}
}


function mover(num){
	var pobj = eval('sqobj'+num)
	if(ie5){pobj.filters.alpha.opacity = 100;}
	else{pobj.style.opacity=1}
}

function mout(num){
	var pobj = eval('sqobj'+num)
	if(ie5){pobj.filters.alpha.opacity = thopac;}
	else{pobj.style.opacity=thopac/100}
}

function getcaption(num){
	if(captions[num]){
	return captions[num]
	}
	else{return ' '}
}

function go(num){

	if(isplaying){
		stopit()
		resumeplay = setTimeout('play()', playspeed);
	}
	
	
	var pobj = eval('sqobj'+num)
	picnum = pobj.name
	t =  pobj.style.backgroundImage

	if(t.indexOf(randompics[0])==-1 && t!=''){	
		loadpic(picnum)
	}
}


function getpicnum(picname){
	for (var i = 0; i < photos.length; ++i){
		if(photos[i].indexOf(picname)==0){return(i);break}	
	}
	return(-1)
}

function showpic(picname){
	var num = getpicnum(picname)
	if (num!=-1){
		pauseplay()
		location = "#top"
		picnum = num
		loadpic(num)
	}
}

function gopic(num){
//		clearTimeout(picfade)
		picnum = num
		loadpic(picnum)
}

function gostart(){
	var num=0
	
	if(getarg('showpic')){
		firstpic = getarg('showpic')	
	}
	
	if(firstpic){
		for (var i = 0; i < photos.length; ++i){
		if(photos[i].indexOf(firstpic)==0){num=i;break}	
		}
	}
		
		if(!num){
		var num = randomnum(1,photos.length-1)
		}
		picnum = num
		loadpic(picnum)
}

function preloadpic(num){

		if(num<totpics){num++;}
		else{num=1}
	
		tphoto = new Image()
		tphoto.src = albumloc+photosrc[num]
		var htm='<img src="'+tphoto.src+'"></div>'
		var tobj = document.getElementById('tmpic')
		tobj.innerHTML = htm
}


function loadpic(num){

//&&		stopcyc()
		
//		document.getElementById('photobg').className='loading'
		document.getElementById('viewphoto').style.backgroundImage="url(images/pixel.gif)";		
		
		photo = new Image()
		photo.src = albumloc+photosrc[num]
		thtm='<img onload="displaypic(this,'+num+')" src="'+photo.src+'"></div>'
		var tobj = document.getElementById('tmpic')
		tobj.innerHTML = thtm
}

function pinfo(dobj,num){
	if(num){dobj.innerHTML = currentpic.substring(currentpic.lastIndexOf('/')+1,currentpic.length)}
	else{dobj.innerHTML =''}
}

currentpic=''

function swapphoto(newsrc){
//	alert(newsrc+'\n'+vobj.src)
	vobj.style.visibility="visible"

//	swapfade(vobj,newsrc,'1','nothing');
//	crosswipe(vobj,newsrc, '4', 'che', '')
	crossfade(vobj, newsrc,'1','')


}

function getaffiche(){
	return(unescape(affiche))	
}


function displaypic(pobj,num){

//		document.getElementById('photobg').className='loaded'
		ph = photo.height
		pw = photo.width
		
		cobj = document.getElementById('caption')
		
		currentpic = pobj.src

		ph = 424
		pw = 300
		
		vobj = document.getElementById('viewphoto')
		
		/// transition function that shows the picture
		
		swapphoto(pobj.src)
		
		cobj.innerHTML=''
		cap = photocap[num]
		
		if(cap.indexOf('javascript:')==0){
		cap = eval(cap.substring(11,cap.length));
		}
		
		// cobj.innerHTML = cap
		caption_to = setTimeout('cobj.innerHTML=cap', 1000);

		preloadpic(num)
return



		vobj.style.backgroundImage="url('"+pobj.src+"')";
//&&		if(isplaying){playstatus='Stop'}else{playstatus='Play'}


//	albumtitle()
//		htm=  '<img onclick="next()" id="photoborder" class="photoborder" src="images/pixel.gif" width='+pw+' height='+(ph-1)+'>'

		htm=  '<img onclick="clicknext()" id="photoborder" class="photoborder" alt="next" src="images/photo_mask.png">'
		

//&&		vobj.innerHTML = htm

		if(controls){showcontrols(num)}


		pictransition()
//&&		fadeinp()
		preloadpic(num)
//&&		startcyc()
}


function controlshow(dobj){
	dobj.className='controlsON'
	
}

function controlhide(dobj){
	dobj.className='controlsOFF'
}

function showcontrols(num){
	
return	
	
	var dobj = document.getElementById('viewphoto')
	x = findPosX(dobj)
	y = findPosY(dobj)
	
	var dobj = document.getElementById('controls')
	ch = parseInt(dobj.style.height)
	cw = parseInt(dobj.style.width)
	
	dobj.style.top = (y + ph) -ch +1
	dobj.style.left = x+1
	dobj.style.width = pw-1
	
	var htm=''	
	htm+='<table class="controls" width="'+(pw-3)+'" cellpadding=0 cellspacing=0 border=0><tr>'
	htm+='<td valign=top><div id=prev onclick="prev()"></div></td><td valign=top><div id=play onclick="play()">'+playstatus+'</div></td><td valign=top><div id=next onclick="next()"></div></td>'
	htm+='<td width=98% valign=middle align=left> <div class="caption">'+num+'. '+photocap[num]+'</div></td>'
	htm+='</tr></table>'
	dobj.innerHTML = htm
}


function putoverlay(){

	var dobj = document.getElementById('viewphoto')
	x = findPosX(dobj)
	y = findPosY(dobj)
	var dobj = document.getElementById('photoframe')
	dobj.style.top = y-15
	dobj.style.left = x-86
	
	var pobj = document.getElementById('prv')
	pobj.style.top = y+360
	pobj.style.left = x-40
		
	var nobj = document.getElementById('nxt')
	nobj.style.top = y+360
	nobj.style.left = x+275
	
	
}

function albumtitle(){
	htm= '<div id="albumtitle">'+albumlab+'</div>'
}


function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function stopit(){
	if(isplaying){
	isplaying=0
//&&	document.getElementById('play').innerHTML = 'Play'
	clearTimeout(caption_to)
	clearInterval(playing)
//	clearInterval(ixf.clock)
	}
}

function play(){
	if(isplaying){stopit();return}
	isplaying=1
	next()
	playing = setInterval("next()", playspeed);
//&&	document.getElementById('play').innerHTML = 'Stop'
}

function clicknext(){
	if(isplaying){
		stopit()
		playing = setTimeout('play()', playspeed);
	}
	next()
}

function clickprev(){
	if(isplaying){
		stopit()
		playing = setTimeout('play()', playspeed);
	}
	prev()
}



function pauseplay(){
	if(isplaying){
		stopit()
		playing = setTimeout('play()', playspeed);
	}
}

function next(){
	if(picnum<totpics){picnum++;}
	else{picnum=1}
	gopic(picnum)
}

function prev(){
	if(picnum!=1){picnum--;}
	else{picnum=totpics-1}
	gopic(picnum)
}


function makephotolist(){
	
	albumlab = prompt('Album label:', '')
	albumdes = prompt('Album description:', '')
	
	plist='photos = new Array()\nphotos[0] = new Array(\n\''+photopath+','+albumlab+','+albumdes+'\',\n'
	tmparray = getfiles(photopath,3)
	for (var i = 0; i < tmparray.length; ++i){
	plist+= '\''+tmparray[i]+'\',\n'
	}
	plist = plist.substring(0,plist.length-2)+'\n)'

//	alert(plist)

	savedat(photolist,plist,0)
}


function fixdatapath(fname,esc){
	fname=baseref+fname
	fname=fname.substring(fname.indexOf('///')+3,fname.length)
	fname=fname.replace(/\//g, '\\')
	if(esc){fname=fname.replace(/\\/g, '\\\\')}
return(fname)	
}

function getfiles(folderspec,act){
	
// act = 1 , write to media (default)
// act = 2 , return number of files	
// act = 3 , return array of files
// act = 4 , initialize media and write

folderspec= fixdatapath(folderspec,0)

	if(!act){act=1}
	
	var fso,f,fc,s,mf,srt

	if(!folderspec){
	var pname = photos+'/'
	pname = fixdatapath(pname,0)
	folderspec = pname
	}

	fso = new ActiveXObject("Scripting.FileSystemObject");
	f = fso.GetFolder(folderspec);
// 	fc = new Enumerator(f.SubFolders);
	fc = new Enumerator(f.Files);   
	s = "";srt='n'
	mediafiles=new Array();i=0
	for (; !fc.atEnd(); fc.moveNext()){
		mf = fc.item();
		mf = mf.name
		if(mf.indexOf('01')==0){srt='a'}
		mediafiles[i] = mf
		i++
	}

	if(srt=='n'){mediafiles.sort(sortnum)}
	if(srt=='a'){mediafiles.sort(sortalpha)}	

	if(act==1){
		for (var i = 1 ; i <= mediafiles.length; i++){
			j=i-1
			media[sn][i][5]	= mediafiles[j]
		}
	}
	if(act==2){return(mediafiles.length)}
	if(act==3){return(mediafiles)}

	if(act==4){
		media[sn] = new Array()
		j=mediafiles.length;
		var tit,evc,sli,l=0
		for (var i = 1 ; i <= j; i++){
			k=i-1
			tit = 'Slide '+i;evc='next';
			if(i==j){evc='stop'}
			var tim = (i*20)*1000+':'+evc
			sli= mediafiles[k]		
			media[sn][i] = new Array(sn,sn,sn,tit,'',sli,'HW',tim)
		}
		alert('sn: '+sn+'\n'+media[sn].toString())
	}
}

function sortnum(a,b){
	var aa = parseInt(a)
	var bb = parseInt(b)	
	if(isNaN(aa)){aa=1000}
	if(isNaN(bb)){bb=1000}	
	if(aa > bb){return 1}
	return -1
}

function sortalpha(a,b){
	var aa = a
	var bb = b
	if(aa > bb){return 1}
	return -1
}

function savedat(fname,dat,chck){
	var fso,f;
	
	fname = fixdatapath(fname,0)
	
//	this.focus()
	if(chck){fname = prompt('Save confdata file as:\n(previous version will be saved with .bak extention)', fname)}

	if(fname){
  		fso = new ActiveXObject("Scripting.FileSystemObject");
		bfname = fname+'.bak'
		if(fso.FileExists(fname)){fso.CopyFile(fname,bfname,true)}
   		f = fso.CreateTextFile(fname, true);
   		f.WriteLine(dat);   
   		f.Close();
	}
	if(chck){alert('Saved')}
}

var now = new Date();
var month_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
//var month_array = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

show_date = new Date();
	
yr = show_date.getYear()
yrto = yr+2

function makecal(dobj){

cx = findPosX(dobj)
cy = findPosY(dobj)

	show_date = new Date();
	wx = 150
	wy = 165
	destobj = dobj

	cal=''
//	cal+=('<html><head><title>Calendar</title></head><body TEXT="FFFFFF" marginheight="0" marginwidth="0" marginleft="0" marginright="0" margintop="0" >')
	cal+=('<table cellpadding=0 cellspacing=0 border=0 bgcolor=#aaaaaa><form name=date_list><tr><td>');

	cal+=("<select style='font-family:Arial;font-size:11.5px;' name='month' onchange=change_month(this.options.selectedIndex)>");

	for(i=0;i<month_array.length;i++){
 		if (now.getMonth() != i){cal+=("<option value="+i+">"+month_array[i]);}
 		else{cal+=("<option value="+i+" selected>"+month_array[i]);}
	}

	cal+=("</select>");
	cal+=("</td><td style='font-family:Arial;font-size:11.5px;'>");
	cal+=("<select style='font-family:Arial;font-size:11.5px;' name=year onchange=change_year(this.options[this.options.selectedIndex])>");

	for(i=yr;i<yrto;i++){
 		if (now.getYear() != i){cal+=("<option value="+i+">"+i)}
 		else{cal+=("<option value="+i+" selected>"+i)}
	}

	cal+=("</select></td></tr><tr><td colspan=2>");
	cal+=("<table bgcolor=#cccccc border=0 cellspacing=1 cellpading =0 width=100%><tr style='font-family:Arial;font-size:11.5px;font-color:silver' bgcolor='#aaaaaa' align=center>");
	cal+=("<td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td><td>S</td>");
	cal+=("</tr><tr>");

	for(j=0;j<6;j++){
	
 		for(i=0;i<7;i++){cal+=('<td style="font-family:Arial;font-size:11.5px;cursor:hand" align=center id=d'+i+'r'+j+' onclick="putdate(this.innerText,document.date_list.month.options[document.date_list.month.selectedIndex].text,document.date_list.year.options[document.date_list.year.selectedIndex].text)">&nbsp;</td>')}
 		cal+=("</tr>");

	}

	cal+=("</table>");
	cal+=("</td></form></tr></table>");

	calwin = document.getElementById('calwindow')

//	windowstr = '"toolbar=no,width='+wx+',height='+wy+',directories=no,status=yes,scrollbars=no,center=yes,resizable=no,menubar=no"'
//	calwin = window.open("","calwin",windowstr);
//	calwin.document.write(cal)
//	calwin.document.close()
//	calwin.focus()	

	calwin.innerHTML=cal

	calwin.style.top = cy -35
	calwin.style.left = cx +225


set_cal(show_date);

}

function hidecal(){
	calwin.style.top = -600
	calwin.style.left = -600	
}

function set_cal(show_date){
	begin_day = new Date (show_date.getYear(),show_date.getMonth(),1);
	begin_day_date = begin_day.getDay();
	end_day = new Date (show_date.getYear(),show_date.getMonth()+1,1);
	count_day = Math.round((end_day - begin_day)/1000/60/60/24);
	input_table(begin_day_date,count_day);
}
function input_table(begin,count){
	initcal();
	j=0;
	if (begin!=0){i=begin-1;}else{i=6}
	for (c=1;c<count+1;c++){
// 		colum_name = eval("calwin.document.all.d"+i+"r"+j);
 		 		
 		colum_name = eval('document.getElementById("d'+i+'r'+j+'")')
 		
 		if ((now.getDate() == c)&&(show_date.getMonth() == now.getMonth())&&(show_date.getYear() == now.getYear())) {
			colum_name.style.backgroundColor = "C0C0DD";
			colum_name.style.color = "000000";
 			};
 		colum_name.innerText =  c;
 		i++;
 		if (i==7){i=0;j++;}
	}
}

function initcal(){
	for(j=0;j<6;j++){
	 for(i=0;i<7;i++){
//		colum_name = eval("calwin.document.all.d"+i+"r"+j);
		colum_name = eval('document.getElementById("d'+i+'r'+j+'")')
		colum_name.innerText =  " ";
		colum_name.style.backgroundColor ="EFF3E7";
		colum_name.style.color ="000000";
 		}
	}
}

function change_month(sel_month){
show_date = new Date(show_date.getYear(),sel_month,1);
set_cal(show_date);
}

function change_year(sel_year){
sel_year = sel_year.value;
show_date = new Date(sel_year,show_date.getMonth(),1);
set_cal(show_date);
}

function putdate(dd,mm,yy){
//	if(mm==' '||yy==' '||dd==' '){calwin.focus();return}
//	ddd = dd+' '+mm.substring(0,3)+', '+yy
//	ddd = dd+'-'+mm.substring(0,3)+'-'+yy.substring(2,4)
	ddd = mm+' '+dd+', '+yy	
	destobj.value=ddd
	hidecal()
//	calwin.close()
}



colorcycle= new Array()

function RGB2Color(r,g,b){
    return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}

function byte2Hex(n){
    var nybHexString = "0123456789ABCDEF";
    return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
}

function makeColorGradient(frequency1, frequency2, frequency3,phase1, phase2, phase3,center, width, len, arrname){
     	eval(arrname+'=new Array()')

    	if (len == undefined)      len = 50;
    	if (center == undefined)   center = 128;
    	if (width == undefined)    width = 127;

    	for (var i = 0; i < len; ++i){
       	var red = Math.sin(frequency1*i + phase1) * width + center;
       	var grn = Math.sin(frequency2*i + phase2) * width + center;
       	var blu = Math.sin(frequency3*i + phase3) * width + center;
	eval(arrname+'[i]= RGB2Color(red,grn,blu)')
	}
}

 // center = 230, width = 25
makeColorGradient(.3,.3,.3,0,2,4,230,25,50,'logograd');
//logograd = colorcycle
makeColorGradient(.3,.3,.3,0,2,4,235,20,50,'sqgrad');
//sqgrad = colorcycle


function getsq(){
    for (var i = 1; i <= numsq; ++i){
    eval('sqobj'+i+'=document.getElementById("sq'+i+'")')
    }
}
 
thcyc=0
 
function startcyc(){
	if(thcyc){clearInterval(thcyc)}
	
	thcyc = setInterval("cyclethumbs()", thcyclespeed);
}

function stopcyc(){
	clearInterval(thcyc)
	thcyc = setInterval("cyclethumbs()", thcyclespeed*10);
}

function clearthumbs(){
	clearInterval(thcyc)
	opac=0
	getsq()
	for (var i = 1; i <= numsq; ++i){
		iobjid = 'sq'+i
		iobj = eval('sqobj'+i) 
		iobj.style.backgroundColor= sqgrad[randomnum(0,sqgrad.length)]
		iobj.style.backgroundImage="url(images/pixel.gif)";
//		iobj.name=picnum
	fadein()
	}
}


function cyclethumbs(){

return
	
	opac=0
	num = randomnum(1,numsq)
	iobjid = 'sq'+num
	iobj = eval('sqobj'+num) 
	iobj.style.backgroundColor= sqgrad[randomnum(0,sqgrad.length)]
	iobj.style.backgroundImage="url('"+randomimg()+"')";
	iobj.name=thpicnum
	fadein()
}


function randomimg(){
/*
	if(randomnum(1,numsq)==3){
		var rimg = new Image();
		rimg.src = randompics[0]+randompics[randomnum(1,randompics.length)]
		thpicnum = 0
		return rimg.src
	}
*/	
	var rimg = new Image();
	thpicnum = randomnum(1,photos.length)
	rimg.src = albumloc+'thumbs/'+photosrc[thpicnum]
	return rimg.src
}
