var _oSiteIdHiddenField = null;
var _oInventoryIdHiddenField = null;
var _oAddThisButton = null;
var _aAddThisButtons = null;
var _iAddThisButtonPopupContainerFound = null;
var _iAddThisButtonLinksEventAttached = null;
var _oWaiter = null;
var _iWaiterIterationCount = null;
var _vehicleInteractionTypes = {'MakeAddThisRequest':3, 'PrintInventoryDetail':5};
var buttonType = "";


function InitializeAddThisLogging()
{
	_aAddThisButtons = $$('.inventoryDetailSocialNetworkingPlugin_container');
	_oAddThisButton = (_aAddThisButtons.length > 0) ? _aAddThisButtons[0] : null;
	_iAddThisButtonPopupContainerFound = false;
	_iAddThisButtonLinksEventAttached = false;
	_oWaiter = null;
	_iWaiterIterationCount = 0;

   if (_oAddThisButton)
	{
		$(_oAddThisButton).observe('mouseover', _oAddThisButton_Mouseover);
	}
}
function _oAddThisButton_Mouseover(e)
{
	if (!_iAddThisButtonLinksEventAttached)
	{
		var oAddThisButtonPopupContainer = $('at20mc');			
		if (oAddThisButtonPopupContainer)
		{
			_iAddThisButtonPopupContainerFound = true;
			AttachClickEventHandlerToAddThisButtonLinks();
		}
		else
		{
			_iWaiterIterationCount = 0;
			_oWaiter = new PeriodicalExecuter(FindAddThisButtonPopupContainer, 0.25);
		}
	}
}
function FindAddThisButtonPopupContainer()
{
	var oAddThisButtonPopupContainer = $('at20mc');			
	if (!_iAddThisButtonPopupContainerFound && oAddThisButtonPopupContainer)
	{
		_iAddThisButtonPopupContainerFound = true;
		_oWaiter.stop();
		_oWaiter = null;			
		AttachClickEventHandlerToAddThisButtonLinks();
	}
	else if (!_iAddThisButtonPopupContainerFound)
	{
		_iWaiterIterationCount++;
	}
	if (_iWaiterIterationCount > 20)
	{
		_oWaiter.stop();
		_oWaiter = null;			
	}
}
function AttachClickEventHandlerToAddThisButtonLinks()
{
	var oAddThisButtonLinksContainer = $('at20mc').select('#at_hover')[0];
	
	if (oAddThisButtonLinksContainer)
	{
		var aAddThisButtonLinkDivs = $(oAddThisButtonLinksContainer).select('.at_item');
		if (aAddThisButtonLinkDivs.length > 0)
		{
			$A(aAddThisButtonLinkDivs).each(function(linkDiv){
				linkDiv.observe('click', AddThisButtonLink_Click);
			});
			_iAddThisButtonLinksEventAttached = true;
		}
	}
	
	var oAddThisButtonPopupLinksContainer = $('at16ps'); 
	
	if (oAddThisButtonPopupLinksContainer)
	{
		var aAddThisButtonPopupLinkDivs = $(oAddThisButtonPopupLinksContainer).select('.at_item');
		if (aAddThisButtonPopupLinkDivs.length > 0)
		{
			$A(aAddThisButtonPopupLinkDivs).each(function(linkDiv){
				linkDiv.observe('click', AddThisButtonLink_Click);
			});
			_iAddThisButtonLinksEventAttached = true;
		}
	}		 				
}
function AddThisButtonLink_Click(e)
{
	var oButtonSpan = e.element();
	var selectedButton = $(oButtonSpan).innerHTML; 
	buttonType = selectedButton;
	LogAddThisRequest(selectedButton);
}	


function LogAddThisRequest(selectedButton)
{
	var logUrl = GetLogUrl();
	if (logUrl)
	{
		logUrl += "&VehicleInteractionTypeId=" + _vehicleInteractionTypes.MakeAddThisRequest; 
		logUrl += "&SelectedAddThisButton=" + selectedButton;
		logUrl += "&dt=" + (new Date()).getTime().toString();
		
		SubmitLogRequest(logUrl);
	}
}
function LogInventoryDetailPrint()
{
	var logUrl = GetLogUrl();
	if (logUrl)
	{
		logUrl += "&VehicleInteractionTypeId=" + _vehicleInteractionTypes.PrintInventoryDetail; 
		logUrl += "&dt=" + (new Date()).getTime().toString();
		
		SubmitLogRequest(logUrl);
	}
}


function GetLogUrl()
{
	var logUrl = null;
	if (!_oSiteIdHiddenField || !_oInventoryIdHiddenField)
	{
		_oSiteIdHiddenField = $('siteIdHiddenField');		
		_oInventoryIdHiddenField = $('inventoryIdHiddenField');
	}
	if (_oSiteIdHiddenField && _oInventoryIdHiddenField)
	{
		siteId = _oSiteIdHiddenField.value;
		inventoryId = _oInventoryIdHiddenField.value;
		
		logUrl = "/VehicleInteractionLogHandler.ashx?SiteId=" + siteId + "&InventoryId=" + inventoryId;
	}  
	return logUrl;
}
function SubmitLogRequest(logUrl)
{
	_xmlHttp = GetXmlHttpRequestObject();
	if (_xmlHttp)
	{
	   _xmlHttp.onreadystatechange = StateChanged;
		_xmlHttp.open("GET", logUrl, true);
		_xmlHttp.send(null);
	}
}
function StateChanged()
{
	if (_xmlHttp.readyState == 4)
	{
		if (_xmlHttp.status == 200)
		{
			if(typeof createEvent == 'function' && buttonType != '')
			{
				createEvent('Add This',  buttonType + ' Clicked', _xmlHttp.responseText);
				waPost();
			}
		}
	}
}
function GetXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
