


// Updated January 16, 2011 - added isExpanded to resetCellSize function - required for other features ie. hover captions 
// Updated January 8, 2011 - [mds.com]
// Last updated September 11, 2009 6:30pm - AB 
//
/* ------------		Row Constructor  (See below for other GALLERY SCRIPTS) 	-------------*/


var gr = new Array();

addEvent(window,'load',loadgallery);

function loadgallery(e) {
	window.name = 'mdsgallery';
}


function MakeGalleryThumbRows(numRows)
{
//	alert("make constructor"+numRows);
	for (var i = 0; i < numRows; i++) {
		gr[i] = new rowConstructor(i);
	}
}



function rowConstructor(id)
{
	/* define properties */
	this.ID            = 'thumbrow'+id; // the Current row ID
	this.timerID       = -1; // the Current Menu's Delay timer
	this.isExpanded = false; // the Current Menu's Display Status
	this.numCells	= 0;
	this.name		= name;
	
	this.ii	= new Array(); //ids for each table cell within
	this.w		= new Array(); //std width for each table cell within
	this.h		= new Array(); //std ht for each table cell within
	this.path		= new Array();
	this.tbpath	= new Array();
	this.bigw		= new Array();
	this.bigh		= new Array();
	
	/* define methods */
	this.updaterowcells = update_rowcells;
	this.test		= testalertii;
	this.insertcell	= insert_cell;
	this.testreturnall	= returnall;

	return this;
}

function returnall() {

	var xx = 'ID ' + this.ID + ' \n timerID: ' + this.timerID + ' \n isExpanded: ' + this.isExpanded + ' \n numCells: ' + this.numCells + ' \n name:' +
		this.name + ' \n ii: (' + this.ii.join(',') + ') \n w: (' +
		this.w.join(',') + ') \n h: (' +
		this.h.join(',') + ') \n bigw: (' +
		this.bigw.join(',') + ') \n bigh: (' +
		this.bigh.join(',') + ') \n path: (' +
		this.path.join(',') + ') \n tbpath: (' +
		this.tbpath.join(',') + ' \n';
	alert(xx);
}

function update_rowcells(ids,w,h,wbig,hbig,truew,trueh,paths,tbpaths,maxrowht) 
{
	//ARRAYS
	this.ii 	= ids; 
	this.w			= w; //std width for each table cell within
	this.h			= h; //std ht for each table cell within
	this.bigw		= wbig;
	this.bigh		= hbig;
	this.path		= paths;
	this.tbpath		= tbpaths;
	this.numCells	= ids.length;
	
	this.truew		= truew;
	this.trueh		= trueh;

	//SINGLE VARIABLES
	this.maxrowht	= maxrowht;
}

function insert_cell (row,ids,w,h,wbig,hbig,path,tbpath) {
	this.ii.push(ids);
	this.w.push(w);
	this.h.push(h);
	this.bigw.push(wbig);
	this.bigh.push(hbig);
	this.path.push(path);
	this.tbpath.push(tbpath);
	
	this.numCells++;
}

function testalertii() {
	var strng = '';
	for (i=0; i < this.numCells; i++) {
		strng = strng + i + '. (' + this.ii[i] + ')    \n';
	}
	alert(strng);
}


/* //--------------------------------------------------------------------------------------// JavaScript Document */

/*

											if (gr[r].ii[i] > 0) {
												var thumbht = ((gr[r].h[i]/gr[r].w[i])*(gr[r].w[i]-smallwdiff));
												var tcellname = \"t_cell\"+i+gr[r].ii[i];
												$(tcellname).setAttribute(\"style\",\"height: ".($rowht)."px; width: "
													.($cellw-$small_w_diff)."px; left: \"+leftpos+\"px; ".$tcell_style."\");
													
												var imageframename = \"image_frame\"+gr[r].ii[i];
												
												$(imageframename).setAttribute(\"style\",\"width: \"+gr[r].w[i]+framemargin-smallwdiff+\"px; height: \"+(thumbht+framemargin)+\"px; ".$imageframesstyle."  \");
												
												var thumbname = \"thumb\"+gr[r].ii[i];
												$(thumbname).setAttribute(\"style\",\"height: \"+thumbht+\"px; width: \"+(gr[r].w[i]-smallwdiff)+\"px; ".$imagestyle."\");	
											} /* END make sure gr[r].ii[i] is greater than zero. We inserted a blank to make an array > 1 
*/	


function setCellSize(r , i , smallwdiff , framemargin , leftpos , cellw , maxrowht , tcellstyle , imageframestyle , imagestyle) {
	if (gr[r].ii[i] > 0) {
		var thumbht = ((gr[r].h[i]/gr[r].w[i])*(gr[r].w[i]-smallwdiff));
		var tcellname = "t_cell"+i+gr[r].ii[i];
		
		
//		$(tcellname).setAttribute("style","height: "+maxrowht+"px; width: "+(cellw-smallwdiff)+"px; left: "+leftpos+"px; " + tcellstyle + " ");
//		var currTh = $(tcellname).style.height;
//		var currTw = $(tcellname).style.width;
		setAstyle($(tcellname),"height: "+maxrowht+"px; width: "+(cellw-smallwdiff)+"px; left: "+leftpos+"px; " + tcellstyle + " ");
//		setAstyle($(tcellname),"left: "+leftpos+"px; " + tcellstyle + " ");		
//		fadeSize(tcellname,1,10,(cellw-smallwdiff),maxrowht,parseInt(currTw),parseInt(currTh));
				
		var imageframename = "image_frame"+gr[r].ii[i];
		
/* 		$(imageframename).setAttribute("style","width: "+gr[r].w[i]+framemargin-smallwdiff+"px; height: "+(thumbht+framemargin)+"px; "+ imageframestyle +"  "); */

		setAstyle($(imageframename),"width: "+gr[r].w[i]+framemargin-smallwdiff+"px; height: "+(thumbht+framemargin)+"px; "+ imageframestyle +"  ");
		
		var thumbname = "thumb"+gr[r].ii[i];
		setAstyle($(thumbname),"height: "+thumbht+"px; width: "+(gr[r].w[i]-smallwdiff)+"px; "+ imagestyle +" ");	
/*		$(thumbname).setAttribute('style',"height: "+thumbht+"px; width: "+(gr[r].w[i]-smallwdiff)+"px; "+ imagestyle +" ");			 */
	}
	
	/* END make sure gr[r].ii[i] is greater than zero. We inserted a blank to make an array > 1 */	
}

/*function resetCellSize(str) {
	alert(str);*/
function resetCellSize(r , smallwdiff , framemargin , leftpos , cellw , maxrowht , tcellstyle , imageframestyle , imagestyle) {
	for (i=0; i < gr[r].numCells; i++) {
		setCellSize(r , i , smallwdiff , framemargin , (leftpos*i) , cellw , maxrowht , tcellstyle , imageframestyle , imagestyle);
	}
	gr[r].isExpanded = false;
}


var imageframestyle,imagestyle,tcellstyle;

function ss(r,smallwdiff,framemargin,partleftpos,cellw,mrowht) {
//	gr[r].isExpanded = false;
	if (document.getElementsByName('shift'+r)) {
		multipleSetByName('shift'+r,'class','show');
	}
	resetCellSize(r , smallwdiff , framemargin , partleftpos , cellw , mrowht , tcellstyle , imageframestyle , imagestyle);
	
//	fadeOneSize ("thumbrow"+r,1,10,(mrowht+2),parseInt($('thumbrow'+r).style.height),"height");
	
	$('thumbrow'+r).style.height = 	(mrowht+2)+'px';
	rem_el("toolbox");
	//gr[r].isExpanded = false;
}

function getStyleObjectz(objectId) {
	if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		return getObjNN4(document,objectId);
	} else {
		return false;
	}
} 

/* ------------		GALLERY ZOOM FADE SIZE 	--------------------- */

function setStyleFromArrayB(objt,attr_arr,prop_arr) {
	
	var el = $(objt);
	for (var css_counter = 0; css_counter < attr_arr.length; css_counter++) {
//	alert("el.style."+attr_arr[css_counter] +" = " + prop_arr[css_counter]+ "   ");
		el.style.attr_arr[css_counter] = prop_arr[css_counter];
	
	}
}

function setStyleFromArray(objt,attr_arr,prop_arr) {
	
	var el = $(objt);
	var css_counter;
	for (css_counter=0; css_counter<attr_arr.length; css_counter++) {
		var attr_type;
		attr_type = attr_arr[css_counter];
		el.attr_type = prop_arr[css_counter];
//		alert(style.attr_arr[i] +"  " + prop_arr[i]);
	}
}

function fadeSize(el,waittime,increm,endW,endH,orW,orH) {
	//desiredPw/h are desired % change for each
	var desiredP = 1.1;

	var elOb = $(el);
	

//	var endW = Math.round(orW*desiredP);
//	var endH = Math.round(orH*desiredP);
	
	var ww = parseInt(elOb.style.width);
	var hh = parseInt(elOb.style.height);
	
	var plusOrMinus = ((endW-orW) > 1) ? 1 : -1;
	
	var SizeIncrW = plusOrMinus * Math.ceil((orW/orH)*increm);
	var SizeIncrH = plusOrMinus * Math.ceil(increm); //h/w = HH/WW
	
	
	
	if ((ww == endW) || (hh == endH)) {
		//if we have reached the right size, end the loop.
		return true;
	}
	
	var newW = ww + SizeIncrW;
	var newH = hh + SizeIncrH;
	
	//make sure we don't exceed our target before we set the target 
	if (desiredP > 1) {
		//INCREASING IN SIZE 
		if ((newW >= endW) || (newH >= endH)) {
			if ((newW >= endW) && ((endH-newH) > SizeIncrH)) {
				newW = endW;
				elOb.style.width = newW+"px";
				fadeOneSize(el,waittime,increm,endH,hh,"height");
				return true;
			} else if ((newH >= endH) && ((endW-newW) > SizeIncrW)) {
				newH = endH;
				elOb.style.height = newH+"px";
				fadeOneSize(el,waittime,increm,endW,ww,"width");
				return true;
			} else {
				newW = endW;
				newH = endH;
				elOb.style.width = newW+"px";
				elOb.style.height = newH+"px";				
				setTimeout("fadeSize('"+el+"',"+waittime+","+increm+","+endW+","+endH+","+orW+","+orH+")");
			}
		} else {
			//set element size 
			elOb.style.width = newW+"px";
			elOb.style.height = newH+"px";
			
			//call loop
			setTimeout("fadeSize('"+el+"',"+waittime+","+increm+","+endW+","+endH+","+orW+","+orH+")");
		
		}
	} else {
		if ((newW <= endW) || (newH <= endH)) {
			newW = endW;
			newH = endH;
		} else {
			//set element size 
			elOb.style.width = newW+"px";
			elOb.style.height = newH+"px";
			
			//call loop
			setTimeout("fadeSize('"+el+"',"+waittime+","+increm+","+endW+","+endH+","+orW+","+orH+")");
		
		}
	}
	
}

function fadeOneSize (el,waittime,increm,endSz,orSz,HorW) {
	var elOb = $(el);
	
	var desiredP = 1.2;

	//var endSz = Math.round(orSz*desiredP);
	
	if (HorW == "height") {
		var currSz = parseInt(elOb.style.height);
	} else {
		var currSz = parseInt(elOb.style.width);	
	}
	
	var plusOrMinus = ((endSz-orSz) > 1) ? 1 : -1;
	
	var SizeIncr = plusOrMinus * Math.ceil(increm);

	if (currSz == endSz) {
		//if we have reached the right size, end the loop.
		return true;
	}
	
	var newSz = currSz + SizeIncr;
	
	//make sure we don't exceed our target before we set the target 
	if (desiredP > 1) {
		if (newSz >= endSz) {
			newSz = endSz;			
		}
	} else {
		if (newSz <= endSz) {
			newSz = endSz;
		}
	}
	
	//set element size 
	if (HorW == "height") {
		elOb.style.height = newSz+"px";
	} else {
		elOb.style.width = newSz+"px";
	}
	
	//call loop
	setTimeout("fadeOneSize('"+el+"',"+waittime+","+increm+","+endSz+","+orSz+",'"+HorW+"')");
	
}


/* ------------		GALLERY BROWSER 	--------------------- */


function end_browser() {
	rem_el("blankbg"); 
	rem_el("browser_underlay");
	return;
}

function end_browser_and_scroll(scrolltoID) {
	rem_el("blankbg"); 
	rem_el("browser_underlay");
	if ($(scrolltoID)) {
		ScrollToElement(scrolltoID);
	}
	return;
}


function new_upload_form(formnum,targetdiv) {
	//create new upload form to replace form on submit 
	//make_el_dom("div","show","",targetdiv,"upformdiv"+formnum);
	
	var upformd = document.createElement("div");
	$(targetdiv).appendChild(upformd);
	upformd.id = "upformdiv"+formnum+'';
	upformd.style.border ='1px solid green';
	
	make_el_dom("form","","","upformdiv"+formnum,"upform"+formnum);

	formEl = $("upform"+formnum);
	formEl.style.marginLeft = "1em";
	formEl.style.marginRight = "1em";
	formEl.name='uploadimages'+formnum;
	formEl.enctype = 'multipart/form-data';

	make_el_dom("p","para_medtxt"," ","upform"+formnum,"formp"+formnum);

	make_el_dom("div","para_medtxt graywriting","","upform"+formnum,"photo_browse"+formnum);
	
	
	var photo_browse = $('photo_browse'+formnum);
	photo_browse.style.border = "1px solid red";
	photo_browse.appendChild(document.createTextNode('Photo: '));
	
	var fileinp = document.createElement("input");
	fileinp.id = "file_browser"+formnum;
	fileinp.name = " photo"+formnum+"_filename[]";
	fileinp.type = "file";
	fileinp.onchange = function () {
						var formnum = $('formnumber').value;
						var whichcat = get_radio_value('which_catj');
						var ind = $('selcid').selectedIndex;
						var selectedcid = $('selcid').options[ind].value;
						var newformnum = (parseInt(formnum) + 1);
						new_upload_form(newformnum,'singleuploadf');
	fileUpload(this.form,'../galleryfiles/upload_image.php?formnumber='+formnum+'&which_cat='+whichcat+'&selectedcid='+selectedcid,'upload'+formnum);
						$('formnumber').value = newformnum;
						rem_el('photo_browse'+formnum);
	}
	$('photo_browse'+formnum).appendChild(fileinp);
	
	make_el_dom("div","para_medtxt"," ","upform"+formnum,"upload"+formnum);
	$('upload'+formnum).style.margin = "1em";

}

