//a variable to use the same object everywhere
var currencyConv = new  preferredCurrencies();
var numFormat = new NumberFormat("0");

var currentSelect = new currentSelections();
function currentSelections()
{
	this.firstPos= -1;
	this.secondPos =-1;
	this.thirdPos=-1;
	
	this.firstVal="";
	this.secondVal="";
	this.thirdVal="";
}

function preferredCurrencies(){
	this.currencyHash = new Hash();
	this.allCurrencyHash = new Hash();
	this.loadAllCurrencyHash = loadAllCurrencyHash;
	this.loadCurrencyHash = loadCurrencyHash;
}//END : preferredCurrencies object

function loadCurrencyHash(curCode, curValue ){
	logToWindow("Adding new currency to hash." + "curCode:"+curCode + " curValue:" +curValue  );
	this.currencyHash[""+curCode] =curValue; 
	logToWindow("currency value in the hash :"+this.currencyHash[""+curCode]);	
}//END: loadCurrencyHash
 
function loadAllCurrencyHash(curCode, curValue ){
	logToWindow("Adding all currencies to hash." + "curCode:"+curCode + " curValue:" +curValue  );
	this.allCurrencyHash[""+curCode] =curValue; 
	logToWindow("currency value in the hash :"+this.allCurrencyHash[""+curCode]);	
}//END: loadAllCurrencyHash

function updatePreferredCurrencyValues(elementName,dollarValue){
	var finalHTML = "";
			
	var currencyCodes = currencyConv.currencyHash.keys();
	logToWindow("updatePreferredCurrencyValues - currencyCodes : " +currencyCodes);
	for (var i=0; i<currencyCodes.length; i++) {
		var code = currencyCodes[i];
		var value = currencyConv.currencyHash[code];
		var convertedValue = convertCurrency(value,dollarValue);
		logToWindow("updatePreferredCurrencyValues - converted value " + convertedValue);
		//append to div
		finalHTML = finalHTML + convertedValue + " " + code + " <img src='http://llpromo.bidz.com/img/flags/"+code +".gif' />" + "<br />";
		
	} //loop through the currencyCodes
	
	//only show currency link when user is logged in
	//var customercookie = getCookie('customer');
	//alert(customercookie);
	/*
	if (customercookie == null || customercookie == '')
		logToWindow("updatePreferredCurrencyValues - homepage or user not logged or search pages. not showing customize currency menu.");
	else
	*/
		finalHTML = finalHTML + '<span style="color: #f60; cursor: pointer; line-height: 1.5em; text-decoration: underline;" onclick="showCurrencyChangeDiv();">Especificar moneda</span>';
	$(elementName).innerHTML = finalHTML;
}//END: updatePreferredCurrencyValues

function convertCurrency(exchangeValue,dollarValue){
 	logToWindow("convertCurrency -  dollarValue: " + dollarValue);
 	logToWindow("convertCurrency -  exchange rate: " + exchangeValue);
 	var result = exchangeValue*dollarValue;
 	logToWindow("convertCurrency - converted value " + result);
 	numFormat.setNumber(result); 	 	
 	return numFormat.toCurrency();	
 	
 }//END: convertCurrency
 
 function showConvertedCurrency(id,dollarValue) {
 	var elementName = "currencyBox_"+id;
 	
 	if ($(elementName).style.display == 'none'){	
 			updatePreferredCurrencyValues("currency_"+id,dollarValue);
 			$(elementName).style.display = 'block';	
 	}
	else {
		$(elementName).style.display = 'none';	
	}	  	
}//END: showConvertedCurrency
 
 /*
  * Function to show currency conversion customize module in div 
  */
 function showCurrencyChangeMenu()
 {
	 logToWindow("showCurrencyChangeMenu - started");
	 logToWindow("showCurrencyChangeMenu - ended");
 }
 
 function showCurrencyChangeDiv()
 {
	 logToWindow("showCurrencyChangeDiv - started.");
	 
	 var currencyChangeBoxDivName="currencyChangeBox";
	 var currencyChangeDivName="currencyChangeDiv";
	 
	 //if currencyChangeBox is not visible, fill up required info and show the div
	 
	 if ($(currencyChangeBoxDivName).style.display == 'none'){	
		 logToWindow("showCurrencyChangeDiv - currencyChangeBox invisible. turning visible.");
		 var customercookie = getCookie('customer');
		 //if user is logged in, allow user to edit currency. else, direct user to login for customization.
		 if (customercookie == null || customercookie == '')
		 {
			 logToWindow("showCurrencyChangeDiv - user not loggedin.");
			 //user not logged in. display 'please login' message
			 $(currencyChangeDivName).innerHTML='Ingresa para especificar moneda. ' +
			 '<span style="color: #f60; cursor: pointer; line-height: 1.5em; text-decoration: underline;" onclick="showCurrencyChangeDiv();"><strong>Cerrar</strong></span>';
			 $(currencyChangeDivName).style.height="50px";
			 $(currencyChangeDivName).style.width="150px";
		 }
		 else
		 {
			 $(currencyChangeDivName).innerHTML='<strong>Selecciona hasta<br/>3 monedas</strong>'+
			 showCurrencySelection() +
			 '<span style="color: #f60; cursor: pointer; line-height: 1.5em; text-decoration: underline;" onclick="showCurrencyChangeDiv();"><strong>Cerrar</strong></span>';
			 logToWindow("showCurrencyChangeDiv - user logged in- prosessed currency selection");
			 $(currencyChangeDivName).style.height="470px";
			 $(currencyChangeDivName).style.width="100px";
		 }//END: if (currentUser == null || currentUser == '')
		 //get mouse position
		 var x = tempX; // - 250;
		 var y = tempY;// - 600;
		 
		 var totalWidth = (document.body.clientWidth );
		 var totalHeight = (document.body.clientHeight );
		 
		 $(currencyChangeDivName).style.position='absolute'; 
		 $(currencyChangeDivName).style.border="solid 1px #c5effd";
		 $(currencyChangeBoxDivName).style.left= x + "px"; 
		 $(currencyChangeBoxDivName).style.top= y + "px";
		 $(currencyChangeDivName).style.backgroundColor="#f5fcff";
		 
		 $(currencyChangeBoxDivName).style.display = 'block';	
		 //alert("showCurrencyChangeDiv - x:" +x + ", y:" +  y );

	 }
	 //if currencyChangeBox is already visible, hide the box when requested to showcurrencyChangeDiv.
	 else {
		 $(currencyChangeBoxDivName).style.display = 'none';	
		 logToWindow("showCurrencyChangeDiv - currencyChangeBox visible. turning invisible.");
	 }//END: if ($(currencyChangeBoxDivName).style.display == 'none')
	 
	 logToWindow("showCurrencyChangeDiv - ended.");
 }//END: showCurrencyChangeDiv
 
 function showCurrencySelection()
 {
	var finalHTML = '<br /><br /><div id="currencySelectionDiv">';
	var currencyCodes = currencyConv.allCurrencyHash.keys();
	logToWindow("showCurrencySelection - currencyCodes : " +currencyCodes);
	var oldCurrencyCodes = currencyConv.currencyHash.keys();
	//add check box and icon for all currencies
	var counter=0;
	for (var i=0; i<currencyCodes.length; i++) {
		var code = currencyCodes[i];
		//check if the currencycode is already user's selected currency.
		//if it is, check checkbox
		var userSavedCurrency = false;
		
		for (var j =0; j<oldCurrencyCodes.length;j++)
		{
			var oldCode = oldCurrencyCodes[j];
			if (oldCode==code)
			{
				userSavedCurrency = true;
			}
		}//END: for (var j =0; j<oldCurrencyCodes.length;j++)
		
		if (userSavedCurrency==true)		{
			
			finalHTML = finalHTML + '<form name="currencySelectForm"><div id="singleCurrency_' + code + '">' + 
			'<input style="margin: -3px 1px;" type="checkbox" checked="checked" onclick="checkSelectedCurrencies(this);" name="currencySelectBox" value="' + code + '">' +
			"<img style='margin: 1px 3px 0 3px;' src='http://llpromo.bidz.com/img/flags/" + code + ".gif' />" + code + "</div>";
			logToWindow("showCurrencySelection - loading selectedcurren code["+i+"]:"+code);
		}
		else
		{
			finalHTML = finalHTML + '<form name="currencySelectForm"><div id="singleCurrency_' + code + '">' + 
			'<input style="margin: -3px 1px;" type="checkbox" onclick="checkSelectedCurrencies(this);" name="currencySelectBox" value="' + code + '">' +
			"<img style='margin: 1px 3px 0 3px;' src='http://llpromo.bidz.com/img/flags/" + code + ".gif' />" + code + "</div>";
			logToWindow("showCurrencySelection - loading unselected curren code["+i+"]:"+code);
		}//END: if (userSavedCurrency==true)
		
	} //loop through the currencyCodes
	 
	finalHTML = finalHTML + '<span style="color: #f00; cursor: pointer; text-decoration: underline; line-height: 2em;" onclick="saveChangedCurrencies();"><strong>Guardar Cambios</strong></span><div id="currencySaveStatDiv" style="color: #008000;margin=1px;"></div>' +"</div></form>";
	return finalHTML;
 }//END: showCurrencySelection()
 
 function saveChangedCurrencies()
 {
	 
	 var currencyStr = getSelectedCurrencies();
	 if (currencyStr=='')
	 {
		 logToWindow("saveChangedCurrencies - nothing selected to save. not saving.");
		 $('currencySaveStatDiv').innerHTML = '<span>Selecciona al menos<br />una moneda.<br /></span>'; 
		 return false;
	 }
	 logToWindow("saveChangedCurrencies - started: saving: " + currencyStr);
	 var savePrefCurrenc =  new PrefCurrencies();
	 savePrefCurrenc.savePrefCurrencies(currencyStr);
 }
 
 function checkSelectedCurrencies(checkedBox)
 {
 	var checkboxGroup=document.currencySelectForm.currencySelectBox;
 	
	 var numSelectedCurrencies = 0;

	 //currentSelect.firstPos= -1;
	 //currentSelect.secondPos = -1;
	 //currentSelect.thirdPos = -1;

	 //go through all currency checkboxes and see what was selected
	 for (i = 0;i<checkboxGroup.length;i++)
	 {
		 if (checkboxGroup[i].checked == true)
		 {
			 logToWindow("checkSelectedCurrencies - checkbox ele [" + i + "] is checked - "+ checkboxGroup[i].value);
			 numSelectedCurrencies++;
			 
			 if (checkboxGroup[i].value != checkedBox.value)
			 {
				 if (currentSelect.firstPos == -1)
				 {
					 currentSelect.firstPos=i;
					 currentSelect.firstVal=checkboxGroup[i].value;
				 }
				 else if (currentSelect.firstPos!=-1 && currentSelect.secondPos==-1)
				 {
					 currentSelect.secondPos=i;
					 currentSelect.secondVal=checkboxGroup[i].value;
				 }
				 else if (currentSelect.firstPos!=-1 && currentSelect.secondPos!= -1 
						 && currentSelect.thirdPos == -1)
				 {
					 currentSelect.thirdPos=i;
					 currentSelect.thirdVal=checkboxGroup[i].value;
				 }
			 }

		 }
	 }//END: for (i = 0;i<checkboxGroup.length;i++)
	 //alert("checkSelectedCurrencies - first: " + currentSelect.firstVal + " second: " + currentSelect.secondVal + " third: " + currentSelect.thirdVal);
	 
	 if (numSelectedCurrencies>3)
	 {
		 //shift placements over
		 currentSelect.thirdVal = currentSelect.secondVal;
		 currentSelect.secondVal = currentSelect.firstVal;
		 currentSelect.firstVal = checkedBox.value;
		 
		 //uncheck all items and check the items that are right
		 for (i = 0;i<checkboxGroup.length;i++)
		 {
			 checkboxGroup[i].checked = false;
		 }
		 //recheck
		 for (i = 0;i<checkboxGroup.length;i++)
		 {
			 if (checkboxGroup[i].value==currentSelect.firstVal ||
					 checkboxGroup[i].value==currentSelect.secondVal ||
					 checkboxGroup[i].value==currentSelect.thirdVal)
				 checkboxGroup[i].checked = true;
		 }
		 //alert("checkSelectedCurrencies - first: " + currentSelect.firstVal + " second: " + currentSelect.secondVal + " third: " + currentSelect.thirdVal);
	 }
	 
 }//END:checkSelectedCurrencies
 
 function getSelectedCurrencies()
 {
	 logToWindow("getSelectedCurrencies - started! " );
	 var checkboxGroup = document.currencySelectForm.currencySelectBox;
	 //array to keep three currencies
	 var threeSelectedCurrencies = new Array(3);
	 //all user selected currencies
	 var allSelectedCurrencies = new Array();
	 //counter of number of selected currencies
	 var numSelectedCurrencies = 0;
	 var selectedCurrenciesStr ="";
	 //go through all currency checkboxes and see what was selected
	 for (i = 0;i<checkboxGroup.length;i++)
	 {
		 if (checkboxGroup[i].checked == true)
		 {
			 logToWindow("checkSelectedCurrencies - checkbox ele [" + i + "] is checked - "+ checkboxGroup[i].value);
			 numSelectedCurrencies++;
			 selectedCurrenciesStr = selectedCurrenciesStr + ":" + checkboxGroup[i].value;
		 }
	 }//END: for (i = 0;i<checkboxGroup.length;i++)

	 logToWindow("getSelectedCurrencies - # of selected currencies: " + numSelectedCurrencies + " final selected str: " + selectedCurrenciesStr);	 	 
	 logToWindow("getSelectedCurrencies - ended! " );
	 return selectedCurrenciesStr;
 }//END:checkSelectedCurrencies
