////////////////////////////////////////////////////////////
// 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 changeBorder(word) {
    document.getElementById(word).style.border='thin solid #FF0000'; 
}

function changeBorderback(word) {
    document.getElementById(word).style.border='thin none'; 
}

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.original)
		{dd.obj.original.copy();}
	else
		{dd.obj.copy();}
	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()
{
	// 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 + " ";
	}
var mainpicx=YAHOO.util.Dom.getX('mainpic');
var mainpicy=YAHOO.util.Dom.getY('mainpic');
var thisw=YAHOO.util.Dom.getStyle('mainpic','width');
thisw=parseInt(thisw.replace('px',''));
var thish=YAHOO.util.Dom.getStyle('mainpic','height');
thish=parseInt(thish.replace('px',''));

// 1 == skip test
if ( 1 || dd.e.x > mainpicx && dd.e.x < (mainpicx+thisw) && dd.e.y > mainpicy && dd.e.y < (mainpicy+thish) ) {
// only enter here if over the card image
for (i=0;i<=dz.length-1;i++) { 
// main pic pos
	var mainpicx=parseInt(YAHOO.util.Dom.getX('mainpic'));
	var mainpicy=parseInt(YAHOO.util.Dom.getY('mainpic'));
// drop zones pos
	var test="dropzone"+i;
	var thisx=YAHOO.util.Dom.getX(test);
	var thisy=YAHOO.util.Dom.getY(test);
	var thisw=YAHOO.util.Dom.getStyle(test,'width');
	thisw=parseInt(thisw.replace('px',''));
	var thish=YAHOO.util.Dom.getStyle(test,'height');
	thish=parseInt(thish.replace('px',''));

	if ( dd.e.x > thisx && dd.e.x < (thisx+thisw) && dd.e.y > thisy && dd.e.y < (thisy+thish) ) {
		changeBorder(test);
	} else {
		changeBorderback(test);
	}
}
} // end of seeing if over card picture in first place

}

/* 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() 
{
	// added by brian 052606 slider contrl related
	if (dd.obj.name=="slider") {
		return;
	}
	if (dd.obj.name=="greybar") {
		return;
	} // end
 dd.obj.hide();

for (i=0;i<=dz.length-1;i++) { 
// main pic pos
	var mainpicx=YAHOO.util.Dom.getX('mainpic');
	var mainpicy=YAHOO.util.Dom.getY('mainpic');
// drop zones pos
	var test="dropzone"+i;
	var thisx=YAHOO.util.Dom.getX(test);
	var thisy=YAHOO.util.Dom.getY(test);
	var thisw=YAHOO.util.Dom.getStyle(test,'width');
	thisw=parseInt(thisw.replace('px',''));
	var thish=YAHOO.util.Dom.getStyle(test,'height');
	thish=parseInt(thish.replace('px',''));

	if ( dd.e.x > thisx && dd.e.x < (thisx+thisw) && dd.e.y > thisy && dd.e.y < (thisy+thish) ) {
//		alert("drop point: "+(dd.e.x-mainpicx)+"  Yahoo: "+thisx+"-"+(thisx+thisw) );
   	 	thumburl=dd.obj.src;
		get_i(thumburl);
		get_w_and_h_and_i(indexGi,thumb);	
	// remove this when ajax works
//		document.getElementById("mainpic").style.background="url('"+thumb[indexGi]+"')";
		makePicRequest(thumb[indexGi],i);
	}
}
// if (dd.elements.tile1) swapAndAlign("tile1");
// if (dd.elements.tile2) swapAndAlign("tile2");
// if (dd.elements.tile3) swapAndAlign("tile3");
// if (dd.elements.tile4) swapAndAlign("tile4");
// if (dd.elements.tile5) swapAndAlign("tile5");
// if (dd.elements.tile6) swapAndAlign("tile6");
// if (dd.elements.tile7) swapAndAlign("tile7");
// if (dd.elements.tile8) swapAndAlign("tile8");
// if (dd.elements.tile9) swapAndAlign("tile9");
// if (dd.elements.tile10) swapAndAlign("tile10");
// if (dd.elements.tile11) swapAndAlign("tile11");
// if (dd.elements.tile12) swapAndAlign("tile12");
}



// cleans up image selector area 
function fixItUp()
{
	var ind=0;
	/* 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) {
    dd.elements[foobar].swapImage(thumb[index+os]);
	get_w_and_h_and_i(index+os,thumb);
    dd.elements[foobar].resizeTo(indexGw,indexGh);
	offsetamt=get_hwratio(indexGw,indexGh);

	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);
	}
        ind=0;
        while  (dd.elements[foobar].copies.length > ind)
        {
                dd.elements[foobar].copies[ind].swapImage(thumb[index+os]);
                dd.elements[foobar].copies[ind].resizeTo(indexGw,indexGh);
		if ( indexGh > indexGw ) {	
			dd.elements[foobar].copies[ind].moveTo(dd.elements[foobar].defx,dd.elements[foobar].defy+offsetamt);
		} else {
			dd.elements[foobar].copies[ind].moveTo(dd.elements[foobar].defx+offsetamt,dd.elements[foobar].defy);
		}
                ind++;
        }
}


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 swapAndAlign(tile)
{
    curX=dd.e.x;
    curY=dd.e.y;
   var oursize=dd.elements[tile].defw; 

    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;
    }
	
    if ( (curX >= dd.elements[tile].defx ) && (curX <= dd.elements[tile].defx+dd.elements[tile].defw) && (curY >= dd.elements[tile].defy) && (curY <= dd.elements[tile].defy+dd.elements[tile].defh))
    {
	currentx=dd.elements[tile].defx;
	currenty=dd.elements[tile].defy;

        thumburl=dd.obj.src;
	get_i(thumburl);
	get_w_and_h_and_i(indexGi,thumb);	

        dd.elements[tile].swapImage(thumb[indexGi],true);
//		alert(dd.elements[tile].src);
        dd.elements[tile].resizeTo(ourwidth,ourheight);
        if (dd.obj.w > dd.obj.h) {
        	dd.elements[tile].moveTo(currentx,currenty+offsetamt);
	} else {
        	dd.elements[tile].moveTo(currentx+offsetamt,currenty);
	}
        dd.elements[tile].setDraggable(false);
	tiles[tile]=dd.elements[tile].src;
    }
}


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)
{
		if (!ourarray[ourindex]) { return; }
        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/cardbuilder/scrollleft-off.gif";
        } else {
                document['leftarrow'].src="images/cardbuilder/scrollleft-on.gif";
        }
  // length of image arry -1 (0 based bath) - offset of index that is shown
        if ( index >= thumb.length-viewPer ) {
                document['rightarrow'].src="images/cardbuilder/scrollright-off.gif";
        } else {
                document['rightarrow'].src="images/cardbuilder/scrollright-on.gif";
        }

/* 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) {
	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);

	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);
	}

	ind=0;
        while  (dd.elements[foobar].copies.length > ind)
        {
                dd.elements[foobar].copies[ind].swapImage(thumb[index+os]);
                dd.elements[foobar].copies[ind].resizeTo(indexGw,indexGh);
                if (indexGh > indexGw) {
                        dd.elements[foobar].copies[ind].moveTo(dd.elements[foobar].defx,dd.elements[foobar].defy+offsetamt);
                } else {
                        dd.elements[foobar].copies[ind].moveTo(dd.elements[foobar].defx+offsetamt,dd.elements[foobar].defy);
                }
                ind++;
	}
}

function formSubmit()
{
        if (document.images.tile1) document.forms.tiles.tile1.value=tiles['tile1'];
        if (document.images.tile2) document.forms.tiles.tile2.value=tiles['tile2'];
        if (document.images.tile3) document.forms.tiles.tile3.value=tiles['tile3'];
        if (document.images.tile4) document.forms.tiles.tile4.value=tiles['tile4'];
        if (document.images.tile5) document.forms.tiles.tile5.value=tiles['tile5'];
        if (document.images.tile6) document.forms.tiles.tile6.value=tiles['tile6'];
        if (document.images.tile7) document.forms.tiles.tile7.value=tiles['tile7'];
        if (document.images.tile8) document.forms.tiles.tile8.value=tiles['tile8'];
        if (document.images.tile9) document.forms.tiles.tile9.value=tiles['tile9'];
        if (document.images.tile10) document.forms.tiles.tile10.value=tiles['tile10'];
        if (document.images.tile11) document.forms.tiles.tile11.value=tiles['tile11'];
        if (document.images.tile12) document.forms.tiles.tile12.value=tiles['tile12'];
        if (document.images.tile13) document.forms.tiles.tile13.value=tiles['tile13'];
//        if (document.forms.tiles.hiddentext1) document.forms.tiles.hiddentext1.value=text['text1field'];
//        if (document.forms.tiles.hiddentext2) document.forms.tiles.hiddentext2.value=text['text2field'];
//        if (document.forms.tiles.hiddentext3) document.forms.tiles.hiddentext3.value=text['text3field'];
//        if (document.forms.tiles.hiddentext4) document.forms.tiles.hiddentext4.value=text['text4field'];
//        if (document.forms.tiles.hiddentext5) document.forms.tiles.hiddentext5.value=text['text5field'];
//        if (document.forms.tiles.hiddentext6) document.forms.tiles.hiddentext6.value=text['text6field'];
//        if (document.forms.tiles.hiddentext7) document.forms.tiles.hiddentext7.value=text['text7field'];
//        if (document.forms.tiles.hiddentext8) document.forms.tiles.hiddentext8.value=text['text8field'];
//        if (document.forms.tiles.hiddentext9) document.forms.tiles.hiddentext9.value=text['text9field'];
//        if (document.forms.tiles.hiddentext10) document.forms.tiles.hiddentext10.value=text['text10field'];
//        if (document.forms.tiles.hiddentext11) document.forms.tiles.hiddentext11.value=text['text11field'];
//        if (document.forms.tiles.hiddentext12) document.forms.tiles.hiddentext12.value=text['text12field'];
        document.forms.tiles.submit();

}

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 swapAndAlignInit(month)
//{
//        currentx=dd.elements[month].defx;
//        currenty=dd.elements[month].defy;
//	var oursize=dd.elements[month].defw;
//
//        get_w_and_hInit(month);
//        if ( indexGw > indexGh )
//        {
//                hwratio=(indexGh/indexGw);
//                ourwidth=oursize;
//		ourheight=ourwidth*hwratio;
//		offsetamt=(ourwidth-ourheight)/2;
//        } else {
//                hwratio=(indexGw/indexGh);
//                ourheight=oursize;
//                ourwidth=oursize*hwratio;
//		offsetamt=(ourheight-ourwidth)/2;
//        }
//
//        dd.elements[month].swapImage(tiles[month],true);
//
//        dd.elements[month].resizeTo(ourwidth,ourheight);
//        if (dd.elements[month].w > dd.elements[month].h) {
//                dd.elements[month].moveTo(currentx,currenty+offsetamt);
//        } else {
//                dd.elements[month].moveTo(currentx+offsetamt,currenty);
//        }
//        dd.elements[month].setDraggable(false);
//}


function initPage()
{
// if (document.images.tile1) swapAndAlignInit("tile1");
// if (document.images.tile2) swapAndAlignInit("tile2");
// if (document.images.tile3) swapAndAlignInit("tile3");
// if (document.images.tile4) swapAndAlignInit("tile4");
// if (document.images.tile5) swapAndAlignInit("tile5");
// if (document.images.tile6) swapAndAlignInit("tile6");
// if (document.images.tile7) swapAndAlignInit("tile7");
// if (document.images.tile8) swapAndAlignInit("tile8");
// if (document.images.tile9) swapAndAlignInit("tile9");
// if (document.images.tile10) swapAndAlignInit("tile10");
// if (document.images.tile11) swapAndAlignInit("tile11");
// if (document.images.tile12) swapAndAlignInit("tile12"); 
}



