//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 = {};
	this.allCurrencyHash = {};
	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 = "";
			
	for (code in currencyConv.currencyHash) {
		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();">Customize Currency</span>';
	$(elementName).html(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).css("display") == 'none'){	
 			updatePreferredCurrencyValues("#currency_"+id,dollarValue);
 			$(elementName).css("display", 'block');	
 	}
	else {
		$(elementName).hide();	
	}	  	
}//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).css("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).html('Please login to customize your currencies. ' +
			 '<span style="color: #f60; cursor: pointer; line-height: 1.5em; text-decoration: underline;" onclick="showCurrencyChangeDiv();"><strong>Close</strong></span>');
			 $(currencyChangeDivName).css("height", "50px");
			 $(currencyChangeDivName).css("width", "150px");
		 }
		 else
		 {
			 $(currencyChangeDivName).html('<strong>Select up to<br/>3 currencies</strong>'+
			 showCurrencySelection() +
			 '<span style="color: #f60; cursor: pointer; line-height: 1.5em; text-decoration: underline;" onclick="showCurrencyChangeDiv();"><strong>Close</strong></span>');
			 logToWindow("showCurrencyChangeDiv - user logged in- prosessed currency selection");
			 $(currencyChangeDivName).css("height", "470px");
			 $(currencyChangeDivName).css("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).css("position", 'absolute'); 
		 $(currencyChangeDivName).css("border", "solid 1px #c5effd");
		 $(currencyChangeBoxDivName).css("left", x + "px"); 
		 $(currencyChangeBoxDivName).css("top", y + "px");
		 $(currencyChangeDivName).css("backgroundColor", "#f5fcff");
		 
		 $(currencyChangeBoxDivName).css("display", 'block');	
		 //alert("showCurrencyChangeDiv - x:" +x + ", y:" +  y );

	 }
	 //if currencyChangeBox is already visible, hide the box when requested to showcurrencyChangeDiv.
	 else {
		 $(currencyChangeBoxDivName).hide();	
		 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">';
	//add check box and icon for all currencies
	var counter=0;
	for (code in currencyConv.allCurrencyHash) {
		//check if the currencycode is already user's selected currency.
		//if it is, check checkbox
		var userSavedCurrency = false;
		
		for (oldCode in currencyConv.currencyHash)
		{
			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>Save Changes</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').html('<span>Select at least<br />one currency.<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

