////////////////////////////////////////////////////////////
// FUNCTIONS FOR EXTENDED SCRIPTING
// Use these for your own extensions,
// or to call functions defined elsewhere



/* my_PickFunc IS AUTOMATICALLY CALLED WHEN AN ITEM STARTS TO BE DRAGGED.
The following objects/properties are accessible from here:

- dd.e: current mouse event
- dd.e.property: access to a property of the current mouse event.
  Mostly requested properties:
  - dd.e.x: document-related x co-ordinate
  - dd.e.y: document-related y co-ord
  - dd.e.src: target of mouse event (not identical with the drag drop object itself).
  - dd.e.button: currently pressed mouse button. Left button: dd.e.button <= 1

- dd.obj: reference to currently dragged item.
- dd.obj.property: access to any property of that item.
- dd.obj.method(): for example dd.obj.resizeTo() or dd.obj.swapImage() .
  Mostly requested properties:
	- dd.obj.name: image name or layer ID passed to SET_DHTML();
	- dd.obj.x and dd.obj.y: co-ordinates;
	- dd.obj.w and dd.obj.h: size;
	- dd.obj.is_dragged: 1 while item is dragged, else 0;
	- dd.obj.is_resized: 1 while item is resized, i.e. if <ctrl> or <shift> is pressed, else 0

For more properties and details, visit the API documentation
at http://www.walterzorn.com/dragdrop/api_e.htm (english) or
http://www.walterzorn.de/dragdrop/api.htm (german)    */
function my_PickFunc()
{
	// brian 052606
	if (dd.obj.name=="slider") {
		return;
	}
	if (dd.obj.name=="greybar") {
		if (dd.e.x > dd.elements.slider.x) {
			moveIndex('plus');
			return;
		}
		if (dd.e.x < dd.elements.slider.x) {
			moveIndex('minus');
			return;
		}
	} // end
//	if (dd.obj.name=="PictureBox"){
//		return
//	}
//	alert(dd.obj.src);
//	alert(dd.elements.tile1.src);
//	dd.elements.tile1.swapImage(dd.obj.src);
//	dd.elements.tile1.swapImage(dd.obj.src);

// brian 090706
	swapAndAlign(dd.obj.src);
	hilited=indexGi;
// change cell color for selected	
	// reset all squares
	document.getElementById('s1').bgColor="gray";
	document.getElementById('s2').bgColor="gray";
	document.getElementById('s3').bgColor="gray";
	document.getElementById('s4').bgColor="gray";
	document.getElementById('s5').bgColor="gray";
	document.getElementById('s6').bgColor="gray";


	var tempo=dd.obj.name;
	tempo=tempo.replace(/name/,"");
	tempo="s"+tempo;
	document.getElementById(tempo).bgColor="#66cc00";
// end brian 090706
	
	fixItUp();
}

/* my_DragFunc IS CALLED WHILE AN ITEM IS DRAGGED
See the description of my_PickFunc above for what's accessible from here. */
function my_DragFunc()
{
	if (dd.obj.name.match("name")) {
		moveIndex('initialize');
	}

	// brian 052606 this was empty before this.
	if (dd.obj.name=="slider") {
		curxpos=dd.obj.x-dd.elements.greybar.defx;
		curindex=Math.floor(curxpos/slicesize);
		if (curxpos<slicesize) { // hm have to automate this ...
			curindex=0;
		}
		if (curindex>Thumb.length-viewPer) {
			curindex=Thumb.length-viewPer;
		}
		moveIndex(curindex);
// remove from production code.
// DEBUG		window.status = 'dd.elements.' + dd.obj.name + '.x  = ' + dd.obj.x + '     dd.elements.' + dd.obj.name + '.y = ' + dd.obj.y + " ";
	}
}

/* my_ResizeFunc IS CALLED WHILE AN ITEM IS RESIZED
See the description of my_PickFunc above for what's accessible from here. */
function my_ResizeFunc()
{
//	window.status = 'dd.elements.' + dd.obj.name + '.w  = ' + dd.obj.w + '     dd.elements.' + dd.obj.name + '.h = ' + dd.obj.h;
}

var buffer="";
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		
		if (keyCode == 8) { 
			buffer=buffer.substr(0,buffer.length-1);
			return true;
		}
		if (keyCode == 46) {
			return true;
		}
		if (keyCode == 13) { 
			text[textfield]=buffer;
			document.getElementById(obname).innerHTML=text[textfield];
			return false;
		} 
		else if (keyCode >= 32 && keyCode <= 126) {
			alert(keyCode);
			buffer=buffer+String.fromCharCode(keyCode);
			return true;
		}
}

function handleLeaveTextBox(field, event) {
	document.getElementById(obname).innerHTML=text[textfield];
	return false;
}

/* THIS ONE IS CALLED ONCE AN ITEM IS DROPPED
See the description of my_PickFunc for what's accessible from here.
Here may be investigated, for example, what's the name (dd.obj.name)
of the dropped item, and where (dd.obj.x, dd.obj.y) it has been dropped... */
var obname;
var textfield;

function my_DropFunc() 
{
return;
	// added by brian 052606 slider contrl related
	if (dd.obj.name=="slider") {
		return;
	}
	if (dd.obj.name=="greybar") {
		return;
	} // end
 //dd.obj.hide();
//	document.images.PictureBox.src=dd.obj.src;
//swapAndAlign("PictureBox");
}



// cleans up image selector area 
function fixItUp()
{
	/* should equal number of images displayed in selector */
	fixItHelper("name1",0);
	fixItHelper("name2",1);
	fixItHelper("name3",2);
	fixItHelper("name4",3);
	fixItHelper("name5",4);
	fixItHelper("name6",5);

}

function fixItHelper(foobar,os) {
	if ((index+os) >= Thumb.length) 
	{ 
		dd.elements[foobar].setDraggable(false);
		return; 
	} 
	else
	{
    		dd.elements[foobar].swapImage(Thumb[(index+os)]);
	}
	
//	alert(Thumb[(index+os)]);
//	get_w_and_h_and_i((index+os),Thumb);
//    dd.elements[foobar].resizeTo(indexGw,indexGh);
//	offsetamt=get_hwratio(indexGw,indexGh);
//	alert("Thumb["+(index+os)+"]:"+Thumb[index+os]);

//	if ( indexGh > indexGw ) {	
//		dd.elements[foobar].moveTo(dd.elements[foobar].defx,dd.elements[foobar].defy+offsetamt);
//	} else {
//		dd.elements[foobar].moveTo(dd.elements[foobar].defx+offsetamt,dd.elements[foobar].defy);
//	}
}

function swapAndAlign()
{
    curX=dd.e.x;
    curY=dd.e.y;
   var oursize=450;

    if (dd.obj.w > dd.obj.h)
    {
        hwratio=(dd.obj.h/dd.obj.w);
        ourwidth=oursize;
        ourheight=ourwidth*hwratio;
        offsetamt=(ourwidth-ourheight)/2;
    }
    else
    {
        hwratio=(dd.obj.w/dd.obj.h);
        ourheight=oursize;
        ourwidth=ourheight*hwratio;
        offsetamt=(ourheight-ourwidth)/2;
    }
//alert(dd.obj.name);
    thumburl=dd.obj.src;
    get_i(thumburl);
  //  get_w_and_h_and_i(indexGi,Picture);

    document.images.PictureBox.src=Picture[indexGi];
    document.getElementById("NameBox").innerHTML=Name[indexGi]; 
    gah=parseInt(indexGi)+1;
    document.getElementById("IndexBox").innerHTML=gah+" of "+Picture.length;
    jss=indexGi;
//    document.images.PictureBox.width=indexGw;
 //   document.images.PictureBox.height=indexGh;
//alert(dd.obj.name+" "+indexGh+" "+indexGw+" "+indexGi);
}



function get_hwratio(t_width,t_height) 
{
	if ( t_width > t_height )
        {
                hwratio=(t_width/t_height)*100;
        }
        else
        {
                hwratio=(t_height/t_width)*100;
        }
         return ((100-hwratio)/2);
}

function get_i(path)
{
        len=path.length;
        while ( path.charAt(len) != '=' && len > 0) {
                len--;
        }
	if ( path.charAt(len-1) == 'a' ) {
		indexGi=path.substring(len+1,999);
	}
        while ( path.charAt(len) != '&' && len > 0) {
                len--;
        }
        andind=len;
        while ( path.charAt(len) != '=' && len > 0) {
                len--;
        }
	if ( path.charAt(len-1) == 'a' ) {
		indexGi=path.substring(len+1,andind);
	}
        while ( path.charAt(len) != '&' && len > 0) {
                len--;
        }
        andind=len;
	while ( path.charAt(len) != '=' && len > 0) {
                len--;
        }
	if ( path.charAt(len-1) == 'a' ) {
		indexGi=path.substring(len+1,andind);
	}
}

function get_w_and_h_and_i(ourindex,ourarray)
{
        len=ourarray[ourindex].length;
        while ( ourarray[ourindex].charAt(len) != '=' && len > 0) {
                len--;
        }
        if ( ourarray[ourindex].charAt(len-1) == 'w' ) {
                indexGw=ourarray[ourindex].substr(len+1,999);
        }
        if ( ourarray[ourindex].charAt(len-1) == 'h' ) {
                indexGh=ourarray[ourindex].substr(len+1,999);
        }
    	if ( ourarray[ourindex].charAt(len-1) == 'a' ) {
	        	indexGi=ourarray[ourindex].substring(len+1,999);
	    }
        while ( ourarray[ourindex].charAt(len) != '&' && len > 0) {
                len--;
        }
        andind=len;
        while ( ourarray[ourindex].charAt(len) != '=' && len > 0) {
                len--;
        }
        if ( ourarray[ourindex].charAt(len-1) == 'w' ) {
                indexGw=ourarray[ourindex].substring(len+1,andind);
        }
        if ( ourarray[ourindex].charAt(len-1) == 'h' ) {
                indexGh=ourarray[ourindex].substring(len+1,andind);
        }
	    if ( ourarray[ourindex].charAt(len-1) == 'a' ) {
		        indexGi=ourarray[ourindex].substring(len+1,andind);
	    }
        while ( ourarray[ourindex].charAt(len) != '&' && len > 0) {
                len--;
        }
        andind=len;
	    while ( ourarray[ourindex].charAt(len) != '=' && len > 0) {
                len--;
        }
        if ( ourarray[ourindex].charAt(len-1) == 'w' ) {
                indexGw=ourarray[ourindex].substring(len+1,andind);
        }
        if ( ourarray[ourindex].charAt(len-1) == 'h' ) {
                indexGh=ourarray[ourindex].substring(len+1,andind);
        }
	    if ( ourarray[ourindex].charAt(len-1) == 'a' ) {
		        indexGi=ourarray[ourindex].substring(len+1,andind);
	    }
}

function moveIndex(xxx)
{

// modified and added to 052606 by brian changed to switch statement made more
// generic to allow on the fly index switching instead of incremental only.
	switch(xxx) {
		case 'plus' : 
		if ( index < Thumb.length-viewPer)
		{
			index++; 
	// adjust position of slider based on index (if it's moved with "buttons"
			dd.elements.slider.moveTo( ((slicesize*index)+dd.elements.greybar.defx),dd.elements.greybar.defy);
		} 
		break;
		case 'minus' :
		if (index > 0)
		{
			index--;
	// adjust position of slider based on index (if it's moved with "buttons"
			dd.elements.slider.moveTo( ((slicesize*index)+dd.elements.greybar.defx),dd.elements.greybar.defy);
		}
		break;
		case 'initialize' :
		break;
		default :
		index=xxx;
		break;
	}
	
        // arrow changing code
        if ( index <= 0 ) {
                document['leftarrow'].src="images/noleftarrowa.gif";
        } else {
                document['leftarrow'].src="images/leftarrowa.gif";
        }
  // length of image arry -1 (0 based bath) - offset of index that is shown
        if ( index >= Thumb.length-viewPer ) {
                document['rightarrow'].src="images/norightarrowa.gif";
        } else {
                document['rightarrow'].src="images/rightarrowa.gif";
        }

// reset pick tile color brian 090706
    document.getElementById('s1').bgColor="gray";
    document.getElementById('s2').bgColor="gray";
    document.getElementById('s3').bgColor="gray";
    document.getElementById('s4').bgColor="gray";
    document.getElementById('s5').bgColor="gray";
    document.getElementById('s6').bgColor="gray";
// end brian 090706

/* this should equal the number of displayed images in file selector */
	moveIndexHelper("name1",0);
	moveIndexHelper("name2",1);
	moveIndexHelper("name3",2);
	moveIndexHelper("name4",3);
	moveIndexHelper("name5",4);
	moveIndexHelper("name6",5);

} // end of function moveIndex

function moveIndexHelper(foobar,os) 
{
	if ((index+os) >= Thumb.length) { dd.elements[foobar].setDraggable(false);return; }
	get_w_and_h_and_i((index+os),Thumb);
	offsetamt=get_hwratio(indexGw,indexGh);
	dd.elements[foobar].swapImage(Thumb[(index+os)],true);
	dd.elements[foobar].resizeTo(indexGw,indexGh);
	dd.elements[foobar].setDraggable(true);

	if (indexGh > indexGw) {
		dd.elements[foobar].moveTo(dd.elements[foobar].defx,dd.elements[foobar].defy+offsetamt);
	} else {
		dd.elements[foobar].moveTo(dd.elements[foobar].defx+offsetamt,dd.elements[foobar].defy);
	}

// see if this tile is "hi lighted" currently and set to color if so ...
	if (indexGi==hilited) {
    	var tempo=foobar;
    	tempo=tempo.replace(/name/,"");
    	tempo="s"+tempo;
    	document.getElementById(tempo).bgColor="#66cc00";
	}
}

function get_w_and_hInit(ourindex)
{
        len=(tiles[ourindex]).length;
        while ( tiles[ourindex].charAt(len) != '=' && len > 0) {
                len--;
        }
        if ( tiles[ourindex].charAt(len-1) == 'w' ) {
                indexGw=tiles[ourindex].substr(len+1,999);
        }
        if ( tiles[ourindex].charAt(len-1) == 'h' ) {
                indexGh=tiles[ourindex].substr(len+1,999);
        }
        if ( tiles[ourindex].charAt(len-1) == 'a' ) {
                indexGi=tiles[ourindex].substr(len+1,999);
        }
        while ( tiles[ourindex].charAt(len) != '&' && len > 0) {
                len--;
        }
        andind=len;
        while ( tiles[ourindex].charAt(len) != '=' && len > 0) {
                len--;
        }
        if ( tiles[ourindex].charAt(len-1) == 'w' ) {
                indexGw=tiles[ourindex].substring(len+1,andind);
        }
        if ( tiles[ourindex].charAt(len-1) == 'h' ) {
                indexGh=tiles[ourindex].substring(len+1,andind);
        }
        if ( tiles[ourindex].charAt(len-1) == 'a' ) {
                indexGi=tiles[ourindex].substring(len+1,andind);
        }
        while ( tiles[ourindex].charAt(len) != '&' && len > 0) {
                len--;
        }
        andind=len;
        while ( tiles[ourindex].charAt(len) != '=' && len > 0) {
                len--;
        }
        if ( tiles[ourindex].charAt(len-1) == 'w' ) {
                indexGw=tiles[ourindex].substring(len+1,andind);
        }
        if ( tiles[ourindex].charAt(len-1) == 'h' ) {
                indexGh=tiles[ourindex].substring(len+1,andind);
        }
        if ( tiles[ourindex].charAt(len-1) == 'a' ) {
                indexGi=tiles[ourindex].substring(len+1,andind);
        }

        indexGh=parseInt(indexGh);
        indexGw=parseInt(indexGw);
}


function initPage()
{
//if (document.images.PictureBox) swapAndAlignInit("PictureBox");
}




