

//
// Global parameters that must be defined by including page.
//

var strDragHandleImage = "";
var strOffHandleImage = "";
var g_strElemTop = -1;
var g_strElemHeight = -1;

var strItemDragHandleImage = "";
var strItemOffHandleImage = "";

var g_strElemItemTop = -1;
var g_strElemItemHeight = -1;

var g_nWishLists = -1;

var g_rentalOnlyPath = "";
var g_clearPath = "";

var g_currentWishListId = -1;
var g_strCurrentWishListName = "";
var g_currentWishListType = 1;

//
// Class Definitions
//


function DropSlot(objDrag, nId, anchorY, dimY)
{
	this.occupant = objDrag;
	this.rank = nId;
	this.dimY = dimY;
	this.anchorY = anchorY;
	// calculate absolute position of slot based on ID 
	// relative to given root coordinate fixed dimensions	
	this.y = this.anchorY  + (this.rank * this.dimY);
	 
	this.isEmpty = function() { return this.occupant == null };
	
	this.snapOccupant = function ( objDrag )	// put the given DragDrop object into this slot
	{
		var objAbs = (objDrag.nId * this.dimY) + this.anchorY;
		var slotAbs = this.y;
		var relMove = slotAbs - objAbs;
		
		objDrag.root.style.top = relMove + "px";
		
		this.occupant = objDrag;
			
	}
	
	this.hitTest = function(x,y)
	{
		var hit = ( y > this.y && y < (this.y + this.dimY) );
		return hit;
	}
	
	this.makeEmpty = function() { this.occupant = null; };
};

function slotList()
{
	this.aSlots = new Array();
	this.addSlot = function(objSlot){ 	this.aSlots[this.aSlots.length] = objSlot; }
		
	this.findSlot = function(objDrag)
	{
		
		for ( var i = 0; i < this.aSlots.length; i++ )
		{
			if ( !this.aSlots[i].isEmpty() && this.aSlots[i].occupant.nId == objDrag.nId )
			{
				return this.aSlots[i];
			}
		}
		
		return null;
	}
	
		
	this.findEmpty = function()
	{
		for ( var i = 0; i < this.aSlots.length; i++ )
		{
			if ( this.aSlots[i].isEmpty() )
				return this.aSlots[i];
		}
		
		return null;
	}
	
	// returns a hit drop slot in the list
	this.checkForHit = function( x, y )
	{
		for ( var i = 0; i < this.aSlots.length; i++ )
		{
			if ( !this.aSlots[i].isEmpty() && this.aSlots[i].hitTest(x, y) )
			{
				return this.aSlots[i];
			}
		}
		return null;
	}
	
	this.getSortOrder = function()
	{
		var str = "";
		for ( var i = 0; i < this.aSlots.length; i++ )
		{
			if ( i > 0 ) str += ",";
			str += this.aSlots[i].occupant.nId;
		}
		return str;
	}
	
};


//
// Globals
//

//
// WishList Drag Handlers
//
var bDragging = false;
var objLastDown = null;
function onOverWishListSorter(obj)
{
	if ( !bDragging )
		obj.className = "wlContainer_over";	
}

function onOutWishListSorter(obj)
{
	if ( obj != objLastDown )
		obj.className = "wlContainer_off";
}

function onClickWishListSorter(obj)
{
	if(g_nWishLists>0)
	{
		obj.className = "wlContainer_over";
		
		if ( objLastDown != null )
			objLastDown.className = "wlContainer_off";
			
		objLastDown = obj;
	}
}


function onStartDragWishListSorter(x,y)
{
	bDragging = true;
	theSlotList.findSlot(this).makeEmpty();
	setHandleImage(this.nId, true);	
}

function onEndDragWishListSorter(x,y)
{
	bDragging = false;
	theSlotList.findEmpty().snapOccupant(this);
	setHandleImage(this.nId, false);
}




var theSlotList = new slotList();
var theItemSlotList = new slotList();

function addDragList( id, ofTotal, ht, elemTop)
{
	var theHandle = document.getElementById("container_" + id);
	var theRoot   = document.getElementById("container_" + id);
	
	Drag.init(theHandle, theRoot, 0, 0, id * -1 * ht, (ofTotal-id)*ht );	
	theRoot.onDragStart = onStartDragWishListSorter;
	theRoot.onDragEnd = onEndDragWishListSorter;
	theRoot.onDrag = onDragWishListSorter;
	theRoot.nId	= id;

	var ds = new DropSlot(theRoot, id, elemTop, ht );
	theSlotList.addSlot( ds );	
}


function addDragListItem( listDivId, itemDivId, ofTotal, ht, elemTop)
{
	var bOk = false;
	
	var ob = document.getElementById("itemContainer_" + listDivId + "_" + itemDivId);
	if ( ob != null)
	{			
		Drag.init(ob, ob, 0, 0, itemDivId * -1 * ht, (ofTotal-itemDivId)*ht );	
		ob.onDragStart = onStartDragWishListSorterItem;
		ob.onDragEnd = onEndDragWishListSorterItem;
		ob.onDrag = onDragWishListSorterItem;
		ob.nId	= itemDivId;
	
		var ds = new DropSlot(ob, itemDivId, elemTop, ht );
		theItemSlotList.addSlot( ds );	
		
		bOk = true;
	}
	
	return bOk;
}

function setHandleImage(nId, bDrag)
{
	var hImg = document.getElementById("handle_" + nId + "_left"); 
	var strImg = bDrag ? strDragHandleImage : strOffHandleImage;
	hImg.src = strImg;
	hImg = document.getElementById("handle_" +  nId + "_right"); 
	hImg.src = strImg;
}

function onDragWishListSorter(x,y)
{
	// convert coordinates
	absY = (g_strElemTop + (this.nId * g_strElemHeight)) + y;
	var ds = theSlotList.checkForHit(x,absY);
		
	if ( ds != null )
	{
		
		var obj = ds.occupant;
		theSlotList.findEmpty().snapOccupant(obj);	// snap occupant will make empty dropSlot not Empty			
		ds.makeEmpty();// make old ds empty
	}	
}

function onClickSaveWishListSort(form)
{
	if(g_nWishLists>0)
	{
		var order = theSlotList.getSortOrder();
		
		document.getElementById("newSortOrder").value = order;
		
		form.submit();
	}else
	{
		alert("Nothing to save!");
	}
}

//
// WishList Item Drag Handlers
//

var bItemDragging = false;
function onOverWishListSorterItem(obj)
{
	if ( !bItemDragging )
		obj.className = "wlItemContainer_over";	
}

function onOutWishListSorterItem(obj)
{
	obj.className = "wlItemContainer_off";
}

function onStartDragWishListSorterItem(x,y)
{
	bItemDragging = true;
	theItemSlotList.findSlot(this).makeEmpty();
	setItemHandleImage(this.nId, true);
}

function onEndDragWishListSorterItem(x,y)
{
	bItemDragging = false;
	theItemSlotList.findEmpty().snapOccupant(this);
	setItemHandleImage(this.nId, false);
}

function onClickWishList(listId, divId, type)
{
	if(g_nWishLists>0)
	{
		g_currentWishListId = listId; 
		g_currentWishListType = type;
		
		var anc = document.getElementById( "anc_wl_name_" + divId );
		g_strCurrentWishListName = anc.innerHTML;
		
		var txt = document.getElementById("listName");
		txt.value = g_strCurrentWishListName;
		
		var ttl = document.getElementById ("ttl_wishlistname");
		ttl.innerHTML = g_strCurrentWishListName;
		
		var img = document.getElementById ("img_rentalonly");
		img.src = g_currentWishListType == 2 ? g_rentalOnlyPath : g_clearPath;
		
		showSelectedWishListItems(divId);
		
		document.getElementById("selectedList").value = divId;
		
		buildItemListDragBlock();
	}
}

function showSelectedWishListItems(listDivId)
{
	document.getElementById("interestItemLists_" + listDivId).style.display = 'block';
	
	var i=0;
	var ob=null;
	do
	{
		ob = document.getElementById("interestItemLists_" + i);
		if(ob != null){
			if(i != listDivId){
				ob.style.display = 'none';
			}
			i++;
		}
	}while (ob != null);
	
	i=0;
	ob=null;
	do
	{
		ob = document.getElementById("itemContainer_" + listDivId + "_" + i);
		if(ob != null){
			ob.style.display = 'block';
			i++;
		}
	}while (ob != null);
	
	i=0;
	ob=null;
	previousSelectedList = document.getElementById("selectedList").value;

	if(previousSelectedList != listDivId){
		do
		{
			ob = document.getElementById("itemContainer_" + previousSelectedList + "_" + i);
			if(ob != null){
				ob.style.display = 'none';
				i++;
			}
		}while (ob != null);
	}
}

function setItemHandleImage(nId, bDrag)
{
	var hImg = document.getElementById("item_handle_" + nId + "_left"); 
	var strImg = bDrag ? strItemDragHandleImage : strItemOffHandleImage;
	hImg.src = strImg;
	hImg = document.getElementById("item_handle_" +  nId + "_right"); 
	hImg.src = strImg;
}

function onDragWishListSorterItem(x,y)
{
	// convert coordinates
	absY = (g_strElemItemTop + (this.nId * g_strElemItemHeight)) + y;
	var ds = theItemSlotList.checkForHit(x,absY);
	if ( ds != null )
	{
		var obj = ds.occupant;
		theItemSlotList.findEmpty().snapOccupant(obj);	// snap occupant will make empty dropSlot not Empty			
		ds.makeEmpty();// make old ds empty
	}
}

function onClickSaveListItemSort(form)
{
	if(g_nWishLists>0)
	{
		var order = theItemSlotList.getSortOrder();	
		
		
		document.getElementById("newItemSortOrder").value = order;
		
		var txt = document.getElementById("listName");
		var selectedList = document.getElementById("selectedList").value;
		document.getElementById("listId").value = document.getElementById("listId_" + selectedList).value;
		document.WishListItemSortForm.listId.value = document.getElementById("listId_" + selectedList).value;
		form.submit();
	}else
	{
		alert("Nothing to save!");
	}
}

//
// Wish List Drag Block Methods
// 

// called on page load
function buildListDragBlock()
{	
	for ( var i = 0; i < g_nWishLists; i++ )
	{
		addDragList( i, g_nWishLists, g_strElemHeight, g_strElemTop);
	}
}


//
// Wish List Item Drag Block Methods
// 

// called by async callback handler
function buildItemListDragBlock()
{
	theItemSlotList = new slotList();
	var nLists = countLists();
	
	var selectedList = document.getElementById("selectedList").value;

	//for(var i=0; i<nLists; i++)
	//{
		var nItems = countListItems(selectedList);

		for(var k=0; k<nItems; k++)
		{	
			addDragListItem(selectedList, k, nItems, g_strElemItemHeight, g_strElemItemTop);
		}
	//}
}

function displayFirstItemList()
{
	if(g_nWishLists>0)
	{
		document.getElementById("interestItemLists_0").style.display = 'block';
		
		var nItems = countListItems('0');
		for(var i=0; i<nItems; i++)
		{
			document.getElementById("itemContainer_0_" + i).style.display = 'block';
		}
		document.getElementById("selectedList").value = '0';
	}
}

function countListItems(listDivId)
{
	var i = 0;
	var ob = null;
	do
	{
		ob = document.getElementById("itemContainer_" + listDivId + "_" + i);
		if  (ob != null) i++;
	}while (ob != null);
	
	return i;
}

function countLists()
{
	var i = 0;
	var ob = null;
	do
	{
		ob = document.getElementById("container_" + i);
		if(ob != null) i++;
	}while(ob != null);
	
	return i;
}