

// default position
var g_defaultX = "581px";
var g_defaultY = "80px";

// preserve state accross pages
var g_InitialPosition = retrieveListPostition();
var g_expanded = retrieveExpandedState();
var g_visible = retrieveVisibleState();
var g_initialOpenState = encodeStyleOpenStateString(g_expanded);
var g_InitialVisibleState = encodeStyleVisibleStateString(g_visible);

// track our active list
var g_currentWishListId = 1;

// note these vars are declared here, but typically updated on page load by jsp include.
var g_wishListCounter = 1;	
var g_strNewListPrefix = "My Wish List #";
var g_arrListType = new Array();	
var g_pathPlusButtonOlive = "";
var g_pathMinusButtonOlive = ""; 
var g_pathPlusButtonGrey = "";
var g_pathMinusButtonGrey = ""; 
var g_sectionType = 1;	// 1 Menswear, 2 Tuxedo, 3 About Us

// note these vars sync with wishList.java Constants
var NO_TYPE = 0;  // undifferentiated
var MENSWEAR_LIST = 1; // shopping list
var RENTAL_LIST = 2; // rental only
var g_itemsPerPage = 5;


//
// Drag and Drop event handlers
//

function onStartDragWishList()
{

}

function onEndDragWishList()
{
	storeListPosition();
}

function onDragWishList()
{

}


//
// list placement methods
//

function getListPosition() {
	
	var list=document.getElementById("listContainer");
	var posX = list.style.left;
	var posY = list.style.top;
	return encodeStylePositionString( posX, posY ); 
}

function setListPosition(X,Y) 
{
	var list = document.getElementById("listContainer");
	list.style.left = X;
	list.style.top = Y;
}

function storeListPosition() 
{
	Set_Cookie( 'mwListPosition', getListPosition(), 1 );
}

function retrieveListPostition()
{
	var cVal = Get_Cookie( 'mwListPosition' );
	var strPos = cVal != null ? cVal : encodeStylePositionString(g_defaultX,g_defaultY);
	return strPos;
	
}

function storeListOpenState() 
{
	Set_Cookie( 'mwListOpenState', g_expanded ? 1 : 0, 1 );
}

function retrieveExpandedState()
{
	var cVal = Get_Cookie( 'mwListOpenState' );
	return parseInt(cVal) == 1;
}

function retrieveVisibleState()
{
	var cVal = Get_Cookie( 'mwListVisibleState' );
	return parseInt(cVal) == 1;
}

function storeListVisibleState() 
{
	Set_Cookie( 'mwListVisibleState', g_visible ? 1 : 0, 1 );
}


function expandList() 
{
		setLayerVis("expandoList", 3);
}

function collapseList() 
{
	setLayerVis("expandoList", 4);
}

function hideList() 
{
	collapseList();
	setLayerVis("listContainer", 2);
}

function resetList() 
{
	setLayerVis("listContainer", 1);
	setListPosition (g_defaultX ,g_defaultY);
	
	g_expanded = false;
	g_visible = true;
	storeListVisibleState();
	storeListOpenState();
	storeListPosition();
	var img = document.getElementById("expandButtton");
	if ( img != null )
	{
		var path = img.src;
		path = path.replace(/minus/g, "plus");	
		img.src = path;	     
	}
}


function toggleList() 
{
	if (g_expanded) 
	{ 	
		collapseList();
		g_expanded = false;
	}
	else 
	{ 
		expandList();
		g_expanded = true;
	}
		storeListOpenState();
}




//
// list-management methods and event handlers
//


function onClickWishListSave()
{
	setLayerVis("saveMe", 3);
	document.getElementById("changeName").value = getSelectedWishListOption().text;	
	wishListRequest( 3, 0, 0 );
}



function onSelectWishList()
{
	g_currentWishListId = getSelectedWishListOption().value;
	wishListRequest( 5, g_currentWishListId, 0 );
}

function onClickNewWishList()
{
	wishListRequest( 4, 0, 0 );
	newWishListInDropDown();
}

function newWishListInDropDown()
{
	// update dropdown, sync by dead reckoning.
	// if this proves problematic, can add a cleanup function to async that handles tasks
	// on return from server.
	g_wishListCounter++;
	addNewWishListName(g_strNewListPrefix + g_wishListCounter, g_wishListCounter);	
	g_currentWishListId = g_wishListCounter;
	g_arrListType[g_currentWishListId] = NO_TYPE;
}

function onClickDeleteWishList()
{
	g_currentWishListId = getSelectedWishListOption().value;
	wishListRequest( 6, g_currentWishListId, 0 );	// delete from server
	
	// delete from drop down
	var sel = document.getElementById("sel_wishlistname");
	
	for ( var i = 0; i < sel.options.length; i++ )
	{	
		if (sel.options[i].value == g_currentWishListId)
		{	
			sel.remove(i);
			break;
		}
	}
	
	
	if ( sel.options.length <= 0)
		newWishListInDropDown();
	
	g_currentWishListId = sel.options[0].value;   
	wishListRequest( 5, g_currentWishListId, 0 );
}

function AddWishList2ShopCart(form)
{
		var rental = (g_arrListType[g_currentWishListId] == 2);
		if(rental){
			bringItIn();
		}else{
	       g_currentWishListId = getSelectedWishListOption().value;
	       form.listId.value = g_currentWishListId;
	       form.submit();
		}
}

function addToCurrentList(strProduct, bMenswear)
{
	
	if ( !g_visible )
	{
		setListPosition(g_defaultX ,g_defaultY);
		setLayerVis("listContainer", 1);
		g_visible = true;
	}
	
	expandList();
	g_expanded = true;
	
	var btn = document.getElementById("expandWishListButton");
	if ( btn != null )
		btn.src = getShadeButtonPath();	
	
	
	storeListVisibleState();
	storeListOpenState();
	//storeListPosition();
	
	
	var listId = getCurrentWishListId();
	var listType = bMenswear ? MENSWEAR_LIST : RENTAL_LIST;
	
	if ( g_arrListType[listId] == NO_TYPE )
		g_arrListType[listId] = listType;
	

	if ( listType == g_arrListType[listId] )
	{
		wishListRequest( 1, listId, strProduct );	// add product
	}
	else
	{

		var wid = getWishlistIdByType(listType);
		
		if ( wid < 0 )
		{
		
			onClickNewWishList();	// new wish List  
			wid = getCurrentWishListId();
			g_arrListType[wid] = listType;	
		}
		else
		{
			
			g_currentWishListId = wid;
			wishListRequest( 5, wid, 0 );	// switch focus
			
			selectWishListDropDown(g_currentWishListId);
			
			
		}
				
		wishListRequest( 1, wid, strProduct );	// add to wish List
	}
	
}


function selectWishListDropDown(id)
{
	var sel = document.getElementById("sel_wishlistname");
	selectDropDownById(sel, id);
}

function selectDropDownById(sel, id)
{
	
	for ( var i = 0; i < sel.options.length; i++ )
	{
		if (sel.options[i].value == id)
		{
			sel.options[i].selected = true;
			break;
		}
	}

}


function onClickWishListSaveOk()
{	
	var strNewName = document.getElementById("changeName").value;
	
	if(strNewName != "")
	{	
		var id = getSelectedWishListOption().value;
		//if(id != "1")
		//{	
			wishListRequest(3,id,strNewName);
			getSelectedWishListOption().text = strNewName;
			setLayerVis("saveMe", 4);
		//}
	}
}



function getWishlistIdByType(type)
{
	var sel = document.getElementById("sel_wishlistname");
	var val = -1;
	for ( var i = 0; i < sel.options.length; i++ )
	{
		if (g_arrListType[sel.options[i].value] == type)
		{
			val = sel.options[i].value;
			break;
		}
	}
	
	return val;
}

function getCurrentWishListId(){ return g_currentWishListId; /*return getSelectedWishListOption().value*/ };

function getSelectedWishListOption()
{
	var sel = document.getElementById("sel_wishlistname");
	return getSelectedOption(sel);
}

function getSelectedOption(sel)
{
	var defIndex = sel.options.length - 1;
	if ( defIndex < 0 ) defIndex = 0;	
	var opt = sel.options[defIndex];
	for ( var i = 0; i < sel.options.length; i++ )
	{
		if (sel.options[i].selected)
		{
			opt = sel.options[i];
			break;
		}
	}
	return opt;	
}


function addNewWishListName(strName, value)
{
	var sel = document.getElementById("sel_wishlistname");
	
	var n = sel.options.length;
	
	var opt = document.createElement("OPTION");
	opt.text = strName;
	opt.value = value;
	
	try 
	{
    	sel.add(opt, null); // standards compliant; doesn't work in IE
  	}
  	catch( ex ) 
	{
    	sel.add(opt); // IE only
  	}

	sel.options[n].selected = true;
}


//
// list appearance utilities
//


// 1 = Menswear
// 2 = tux
function setWishListSkin(type)
{

	// tux is gray, with different text and rent button instead of add to cart	
	var bar = document.getElementById("handle");
	
	bar.className = type == 2 ? "handle_grey" : "handle_olive";
	
	var btn = document.getElementById("expandWishListButton");
	if ( btn != null )
		btn.src = getShadeButtonPath();	
	
	btn = document.getElementById("closeWishListButton");
	path = btn.src;
	if ( type == 2 )
		path = close_wishlist_grey_btn;	
	else
		path = close_wishlist_olive_btn;	
	btn.src = path;	
	
	btn = document.getElementById("saveWishListButton");
	path = btn.src;
	if ( type == 2 )
		path = save_wish_list_grey_btn;	
	else
		path = save_wish_list_olive_btn;	
	btn.src = path;	     
	
	var elem = document.getElementById("borderright");
	
	/*if (type == 2)
		elem.innerHTML = '<font color="#db0c21">We\'re here to help. Call 1-800-776-SUIT(7848) to speak to a store.</font>';
	else
		elem.innerHTML = '<font color="#db0c21">We\'re here to help. Call 1-800-776-SUIT(7848) to speak to a store.</font>';	 
	*/
	
	btn = document.getElementById("addtocart");
	path = btn.src;
	if ( type == 2 )
		path = add_to_cart_grey_btn;	
	else
		path = add_to_cart_olive_btn;	
	btn.src = path;	
	
}

function getCurrentColorSuffix()
{
	return getPallete(getCurrentWishListId()) == 2 ? "grey" : "olive";
}

function getShadeButtonPath()
{
	if ( getPallete(getCurrentWishListId()) == 2 )
		return retrieveExpandedState() ? g_pathMinusButtonGrey : g_pathPlusButtonGrey;
	else
		return retrieveExpandedState() ? g_pathMinusButtonOlive : g_pathPlusButtonOlive;
}


function onOverWishListImage(id, prefix)
{
	swapWishListImage(id, prefix, true);	
}

function onOutWishListImage(id, prefix)
{
	swapWishListImage(id, prefix, false);	
}

/*function swapWishListImage(id, prefix, bOver)
{
	var img = document.getElementById(id);
	var strBase = prefix + "_" + getCurrentColorSuffix() + "_";
	var oldV = bOver ? "default" : "over";
	var newV =  bOver ? "over" : "default";
	var rg = new RegExp(strBase + oldV, "i" );
	img.src = img.src.replace(rg, strBase + newV );
}*/

function swapWishListImage(id, prefix, bOver)
{
	var img = document.getElementById(id);
	var type = getPallete(getCurrentWishListId());
	if (type==2) {
		if (prefix == "save_wish_list") {
			if (bOver) {
				img.src = save_wish_list_on_grey_btn;
			} else {
				img.src = save_wish_list_grey_btn;
			}
		} else {
			if (bOver) {
				img.src = add_to_cart_grey_on_btn;
			} else {
				img.src = add_to_cart_grey_btn;
			}
			
		}
	} else {
		if (prefix == "save_wish_list") {
			if (bOver) {
				img.src = save_wish_list_on_olive_btn;
			} else {
				img.src = save_wish_list_olive_btn;
			}
		} else {
			if (bOver) {
				img.src = add_to_cart_on_olive_btn;
			} else {
				img.src = add_to_cart_olive_btn;
			}
		}
	}
	/*var strBase = prefix + "_" + getCurrentColorSuffix() + "_";
	var oldV = bOver ? "default" : "over";
	var newV =  bOver ? "over" : "default";
	var rg = new RegExp(strBase + oldV, "i" );*/
	//img.src = img.src.replace(rg, strBase + newV );
}

function onClickExpandButton()
{
	toggleList(); 

	var btn = document.getElementById("expandWishListButton");
	if ( btn != null )
		btn.src = getShadeButtonPath();	
}

function onClickCloseButton()
{
	hideList();
	g_visible = false;
	storeListVisibleState();
}


function syncWishListAppearance(id)
{	
	if ( id == null ) id = g_currentWishListId;
		
	setWishListSkin(getPallete(id));	
}

function getPallete(id)
{
	var type = g_arrListType[id];
	
	// if undif, looks like section.  if said section is about us, looks like menswear
	if ( type == NO_TYPE && g_sectionType == 2) type = 2;
	
	return type;
}


// util method for setting visibility of an element
//valid parameter values for state |Show =1 | hide =2 | block =3 | none =4 |
function setLayerVis (whichLayer,state) 
{
	var expandoList;
	
	if (document.getElementById)
	{
		// this is the way the standards work
		expandoList = document.getElementById(whichLayer).style;
	 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		expandoList = document.all[whichLayer].style;
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		expandoList = document.layers[whichLayer].style;
	}
	
	if (state<3)
	{
		expandoList.visibility = (state==1)?"visible":"hidden";
		expandoList.display="block";
		
	}
	else 
	{
		expandoList.display = ( state==3)? "block":"none";
		expandoList.visibility="visible";		
	}	
}


function encodeStylePositionString(x,y)
{
	var str = "left:" + x + "; top:" + y + ";";
	return str;
}

function encodeStyleOpenStateString(bOpen)
{
	var str = bOpen ? "display:block;" : "display:none;";
	return str;
}

function encodeStyleVisibleStateString(bVis)
{
	var str = bVis ? "display:block;" : "display:none;";
	return str;
}
function addWishList(param1,param2,param3){
	//alert("Testomg...");
	initAjax();
	ajaxEngine.sendRequest('WishListCmd',			
				'cmd=1',
				'p1='+param1,
				'p2='+param2,
				'p3='+param3
				);

}