//////////////////////////////////////////////
// AdvancedSearch Object VERSION_0.8
//////////////////////////////////////////////
//
// Ali Russell 10.2006
// Joe Lambert 12.2006
//
//////////////////////////////////////////////

//Global variables
var XMLRequestBusy_HoveringImagePreview = false;

var ImageHoveringPreviewCue = Class.create();
ImageHoveringPreviewCue.prototype = {
//
// Member Variables
m_hoverTimeoutId: -1,
m_hoverSender: null,
m_isOpen: false,
m_xmlRequest: new XMLHttpRequest(),

//
// Constructor
initialize: function(objectOptions)
    {
        
        //Override the defatul object parameters, with those from the objectOptions.
        ParseOptions(objectOptions, this);
        
		
		mSpaceApplication.Subscribe("Item.MouseOver", this);
		mSpaceApplication.Subscribe("Item.MouseOut", this);
        
        
        
    },
    
    
Notify: function(eventName, eventParams)
    {
        switch(eventName)
        {
            case 'Item.MouseOver' : this.MouseOver(eventParams); break;
			case 'Item.MouseOut' : this.MouseOut(eventParams); break;
            default: break;
        }
    },
	
MouseOver: function(eventParams)
	{
		
		if (!eventParams.sourceItem)
			return;
			
		if (this.m_hoverTimeoutId!=-1)
		{
			clearTimeout(this.m_hoverTimeoutId);
			this.m_hoverTimeoutId = -1;
		}
			
		this.m_hoverSender = eventParams;
		
		var thisObj = this;
		this.m_hoverTimeoutId = setTimeout(function() { thisObj.OpenPreview(); }, 750);
	
		
	},
	
MouseOut: function(eventParams)
	{
		if (!eventParams.sourceItem)
			return;
					
		if ((eventParams.sourceItem==this.m_hoverSender.sourceItem) && (this.m_hoverTimeoutId!=-1))
		{
			clearTimeout(this.m_hoverTimeoutId);
			this.m_hoverTimeoutId = -1;
			if (this.m_isOpen)
			{
				this.Close();
			}			
		}
		

	},
	
OpenPreview: function()
	{
		if ((this.m_hoverSender==null) || (this.m_isOpen))
			return;
			
		this.m_isOpen = true;
			
		this.LoadPreviews();
	},
	
Close: function()
	{
		this.m_isOpen = false;
	},
	
LoadPreviews: function()
	{
	
		var previewUrl = "./inc/mspace.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=getpreviewcues&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel")) + "&itemuri=" + URLEncode(this.m_hoverSender.sourceItem.uri) + "&col=" + URLEncode(this.m_hoverSender.column.m_Uri);
		
		
			var thisObj = this;

			// BUG FIX IN FIREFOX FOR 'NS_ERROR_NOT_INITIALIZED' ERROR
			if(XMLRequestBusy_HoveringImagePreview && thisObj.m_xmlRequest.readyState != 4 && thisObj.m_xmlRequest.readyState != 0)
			{
				this.m_xmlRequest.onreadystatechange = function () {}; // Must clear the handler otherwise a status 4 is called which makes the script think it has made a successful call
				this.m_xmlRequest.abort();
				this.m_xmlRequest = new XMLHttpRequest();
			}
			
			
			// Use the onload function if the browser is anything other than ie <7.0.  Fixes problems on some firefox and mozilla
			// versions
			if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
			{
				this.m_xmlRequest.onreadystatechange = function() {
					try {
						if(thisObj.m_xmlRequest.readyState == 4 && (thisObj.m_xmlRequest.status == 200 || thisObj.m_xmlRequest.status == 304)) {
							//#################
							//RESULTS
							//#################
							XMLRequestBusy_HoveringImagePreview = false;
							alert(thisObj.m_xmlRequest.responseText);
						}
					}catch (e){
						thisObj.SetContents("Error Loading Contents: " + e);
					}				  
				};
			}else{
				this.m_xmlRequest.onload = function() {
					try {
						//#################
						//RESULTS
						//#################
						XMLRequestBusy_HoveringImagePreview = false;
						alert(thisObj.m_xmlRequest.responseText);
					}
					catch (e)
					{
						thisObj.SetContents("Error Loading Contents: " + e);
					}				  
				};			
			}
		
			try
			{
				if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
				{
					window.setTimeout( function()
												{
													thisObj.m_xmlRequest.open('GET', previewUrl);
													XMLRequestBusy_HoveringImagePreview = true;
													thisObj.m_xmlRequest.send(null);
												}
										,500);
				}
				else
				{
					this.m_xmlRequest.open('GET', previewUrl);
					XMLRequestBusy_HoveringImagePreview = true;
					this.m_xmlRequest.send(null);			
				}	
			}
			catch(e){}		
				
	}
    
}

