var EMPTY_HISTORY_MSG = "No Running Bid History";
var HISTORY_URL = "/bzJApp/BidHistory.action";
var CURRENCY_CONV_URL = "/bzJApp/ConvertCurrency.action";

//to display user proxy in product display
function displayUserProxy(userProxyVal, listingId){
	var elementName = '#userProxy_'+listingId;
	var valueElementName = 'userProxyValue_'+ listingId;
	nf.setNumber(userProxyVal);
	$(elementName).css("display", 'block');
	$(elementName).html("<strong>Your highest AutoBid is <div style='display: inline;' id ='"+valueElementName+"'>" + nf.toFormatted()+ " </div>USD<div class='small'>(Only you can see this information)</div></strong>");
}

//to disable displaying user proxy 
function disableUserProxy(listingId){	
	var elementName = '#userProxy_'+listingId;
	$(elementName).hide();	
}

//to display user max Bid in active auctions page
function displayUserMaxBid(userBid, listingId){
	var elementName = $('#userMaxBid_'+listingId);	
	nf.setNumber(userBid);
	if (elementName)
	{
		$(elementName).css("display", "block");
		$(elementName).html(nf.toFormatted());
	}
}

//to disable displaying user active auctions page 
function disableUserMaxBid(listingId){	
	var elementName = $('#userMaxBid_'+listingId);
	if (elementName)
	{
		$(elementName).hide();
	}
}

//function to add running history 
function addToRunningHistory(listingId, currentBidder, bidAmt, isOutBidSame){
	
	var HISTORY_MAX_SIZE = 6;
	
	var historyStrElementName = '#historyStr_'+ listingId;		
	
	// create top element with winner data		
	nf.setNumber(bidAmt);
	var	winnerElement = "<li style='white-space: nowrap; color:#0c3;'>"+ currentBidder + " " + nf.toFormatted() +"</li>";
	
	var runnerUpElement ="";
	var oldElements = "";
	
	var currentHistory= $(historyStrElementName).html();
	//alert( "current history : " +currentHistory);		
	
	// tag names are in uppercase in IE response
	currentHistory = currentHistory.replace(/LI/g, "li");	
	//alert( "current history : " +currentHistory);
	
	//get the runner up and remaining element from the current history before adding new winner data
	if (currentHistory != "" && currentHistory != EMPTY_HISTORY_MSG){
		var historyDoc = parseXMLString(currentHistory);		
		
		var liArray = historyDoc.getElementsByTagName("li");
	
		var noOfBids = liArray.length;	
		logToWindow( "no of bids in current history : " +noOfBids );			  
		
		// limit to specified size
	 	if (noOfBids >HISTORY_MAX_SIZE){
	 		noOfBids = HISTORY_MAX_SIZE;
	 	}	 	
	 
	 	//prepare runner up element which is available at the top in current history
	 	if(isOutBidSame){
	 		// change the colour for 2nd one if it is outbid by proxy winning bid
	 		runnerUpElement = "<li style='white-space: nowrap; color: #f60;'>"+liArray[0].firstChild.nodeValue+"</li>";	
	 	}
	 	else
	 		runnerUpElement = "<li style='white-space: nowrap;'>"+liArray[0].firstChild.nodeValue+"</li>";	
	 		
	 	//prepare old history to append it to winner and runner bids	
	 	for (var i=1; i<noOfBids; i++) {
		 	logToWindow( "bid at " +i+ " in the current history : " +liArray[i].firstChild.nodeValue );	
		  	oldElements = oldElements + "<li style='white-space: nowrap;'>" + liArray[i].firstChild.nodeValue + "</li>";	
	 	}
	 	
	} //end: current history check
	else {
		//make ajax call to get the bid history 
		
	}
	var newHistory ="<ul style='margin: 0; padding: 0; list-style: none;'>" +
						 winnerElement + runnerUpElement + oldElements +"</ul>";
					
	$(historyStrElementName).html(newHistory);
}//END:addToRunningHistory

//to display running history
function showRunningHistory(listingId){
	
	//toggle
	var elementName = '#runningHistory_'+listingId;
	if ($(elementName).css("display") == 'none'){		
		var historyStrElementName = '#historyStr_'+ listingId;	
		var currentHistory= $(historyStrElementName).html();
		if (currentHistory ==""){			
			//get the history from database
			getHistory(listingId);
		}

		$(elementName).show();	
	}
	else{
		$(elementName).hide();
	}
}//END:showRunningHistory

function hideRunningHistory(listingId){
	var elementName = '#runningHistory_'+listingId;
	$(elementName).hide();
}

function showShippingInfo(listingId, itemType){
	var elementName = '#shippingInfo_'+listingId;
	if ($(elementName).vcss("display") == 'none'){
		var shippingStrElementName = '#shippingStr_'+ listingId;
		
		//get item type info
		
		$(shippingStrElementName).html("itemType is: " + itemType);
		
		if(itemType == 'Regular'){
			$(shippingStrElementName).html('<strong>Jewelry Items</strong> are small high-value items such as earrings, bracelets, necklaces, rings, eyewear, accessories and some watches.');
		} else {
			if(itemType.length > 0 && itemType.substr(0,8) == 'Oversize')
			{
				$(shippingStrElementName).html('<strong>Oversized Items</strong> are items that may come individually packaged and are too large or bulky to be accommodated by our standard shipping rates. Specific charges may apply and will be available on the item description page. <strong>('+itemType+')</strong>');
			} else {
				if( itemType == 'Silver') {
					$(shippingStrElementName).html('<strong>Silver/Base Metal Items</strong> are items consisting mainly of silver or other alternative metals such as base metal, tungsten, or titanium.');
				} else
				{
					$(shippingStrElementName).html('Please review Shipping policies for different types of items in our Help Section.');
				}
			}
		}

		//display type info
		$(elementName).show();
	}
	else{
		$(elementName).hide();
	}
}

function hideShippingInfo(listingId){
	var elementName = '#shippingInfo_'+listingId;
	$(elementName).hide();
}

function showPriceInfo(listingId, priceType){
	var elementName = '#priceInfo_'+listingId;
	if ($(elementName).css("display") == 'none'){
		var priceStrElementName = '#priceStr_'+ listingId;
		
		//get item type info
		
		$(priceStrElementName).html("itemType is: " + priceType);
		
		if(priceType == 'compare'){
			$(priceStrElementName).html('The <strong>Compare</strong> price is based on what we believe to be the maximum price other jewelers advertise online for similar jewelry items. This price in no way reflects worth, suggested retail price or an actual advertised price.');
		} else {
			if(priceType == 'retail'){
				$(priceStrElementName).html('The <strong>Retail</strong> price displayed on our items represents either the retail price suggested by the manufacturer or an actual price intended to be representative of similar items sold in your community by department stores.');
			} else {
				$(shippingStrElementName).html('Please review Shipping policies for different types of items in our Help Section.');
			}
		}

		//display type info
		$(elementName).show();
	}
	else{
		$(elementName).hide();
	}
}

function hidePriceInfo(listingId){
	var elementName = '#priceInfo_'+listingId;
	$(elementName).hide();
}

function showReducedInfo(listingId){
	var elementName = '#reducedInfo_'+listingId;
	var reducedStrElementName = '#reducedStr_'+ listingId;
	$(reducedStrElementName).html("Price Reduced!");
	$(elementName).css("display", 'block');
}

function hideReducedInfo(listingId){
	var elementName = '#reducedInfo_'+listingId;
	$(elementName).hide();
}

function showVideoInfo(listingId){
	var elementName = '#videoInfo_'+listingId;
	var reducedStrElementName = '#videoStr_'+ listingId;
	$(reducedStrElementName).html("Video Available");
	$(elementName).css("display", 'block');
}

function hideVideoInfo(listingId){
	var elementName = '#videoInfo_'+listingId;
	$(elementName).hide();
}

function showWinbuyerLink(){
	var elementName = '#winbuyerlink';
	$(elementName).css("display", 'inline');
}

function showWinbuyer(){
	var elementName = '#winbuyer';
	$(elementName).css("display", 'block');
}

function hideWinbuyer(){
	var elementName = '#winbuyer';
	$(elementName).hide();
}

function showVideo(listingId, videoName){
	var flashvars = {video_url: "http://llfull.bidz.com/videos"+ videoName};
	var params = {bgcolor: "#ffffff", allowFullScreen: "false", wmode: "transparent"};
	var attributes = {
	id: "videoplayer",
	name: "videoplayer",
	allowFullScreen: "false"
	};
	swfobject.embedSWF("/bzJApp/views/flash/tm_player.swf", "videoPlayerHolder", "490", "393", "8", "/bzJApp/views/flash/expressInstall.swf", flashvars, params, attributes);
	
	var elementName = '#video_'+listingId;
	$(elementName).css("display", 'block');
	$('#videoButton').css("display", 'none');
}

function hideVideo(listingId){
	var elementName = '#video_'+listingId;
	$('#videoContainer').html('<div id="videoPlayerHolder"></div>');
	$(elementName).hide();
	$('#videoButton').css("display", 'block');
}

function getHistory(listingID){
	
	var HistoryURL = HISTORY_URL + "?" + "auctionId=" + listingID + "&cb=" + Math.random();
	
	logToWindow("History URL " + HistoryURL);
	
	$.ajax(
	{	url: HistoryURL,
		context: this,
		success: processHistoryResponse,
		error : processHistoryResponseError
	});
 
}

 function processHistoryResponse (data, textStatus, XMLHttpRequest) {
 	logToWindow("processHistoryResponse: " + transport.responseText);
	
	var resultDoc = parseXMLString(data);
	
	//get the root of the XML result
	var auctionIdNode = resultDoc.getElementsByTagName("auctionId")[0];
		logToWindow("auctionIdNode" + auctionIdNode);
		
	var auctionId =auctionIdNode.firstChild.nodeValue;
		logToWindow("auctionId" + auctionId);
	
	var historyElementName = 'historyStr_'+auctionId;	
	var currentHistory = $(historyElementName).innerHTML;
		logToWindow("elementName: " + historyElementName + ", currentHistory" + currentHistory);
	if(currentHistory ==""){
		var bidLength = 0;
		var noBidsNode = resultDoc.getElementsByTagName("bids")[0];
		var noBids = noBidsNode.firstChild.nodeValue;
		var bidNodes = resultDoc.getElementsByTagName("bid");
		bidLength = bidNodes.length;
		
		logToWindow("bidLength" + bidLength);
		
		if (noBids=="NO_BIDS_FOUND" || bidLength < 1 ){			
			$(historyElementName).innerHTML = EMPTY_HISTORY_MSG;
		}
		else {					
			for (var i= bidLength-1 ; i >= 0; i--){
			
				var bidNode=bidNodes[i];	
				var bidderName = bidNode.getElementsByTagName("bidderName")[0].firstChild.nodeValue;				
				
				var bidAmt = bidNode.getElementsByTagName("bidAmt")[0].firstChild.nodeValue;
				
				logToWindow("bidderName: " + bidderName  +" bidAmt: " + bidAmt);			
				
				addToRunningHistory(auctionId,bidderName,bidAmt,false);					
			} //end of loop
		} //end bidLength <>0
	}//if(currentHistory ==""){
	return;
	
} //END: processHistoryResponse 
 

function processHistoryResponseError (XMLHttpRequest, textStatus, errorThrown) {
	 logToWindow("processHistoryResponseError: " + textStatus);
	 if (errorThrown != null)
	 {
		 processHistoryException (XMLHttpRequest, errorThrown);
	 }
} //END: processHistoryResponseError
  
function processHistoryException (Req, Ex1) {
	logToWindow("processHistoryException: " + Ex1);
} //END: processHistoryException
 
function searchBrandURL(brandname){
	var convertedBrand = brandname.toLowerCase();
	convertedBrand = escape(convertedBrand.gsub(' ', '_'));
	window.open('/brand/' + convertedBrand);
}

function followURL(url){
	window.location = url;
}

function isSecure(){
   return window.location.protocol == 'https:';
}

