﻿var xmlStr
var hotspots = new Array()
var lastmoved
var hotspottimer
var hotspotDelay = 1000
var currentHotSpot=-1
var intooltip
var linktarget

function initHotspots(){
    var xmlDoc = document.getElementById('HotSpotXML')
    var xmlDoc = xmlDoc.childNodes[0]
    for(var n=0;n<xmlDoc.childNodes.length;n++){
        
        if (xmlDoc.childNodes[n].nodeName=='Table'){
            var hotspot = new Object()
            for(var i=0;i<xmlDoc.childNodes[n].childNodes.length;i++){
                if (xmlDoc.childNodes[n].childNodes[i].nodeName!='#text'){
                    if (xmlDoc.childNodes[n].childNodes[i].childNodes[0]) 
                        hotspot[xmlDoc.childNodes[n].childNodes[i].nodeName]=xmlDoc.childNodes[n].childNodes[i].childNodes[0].nodeValue
                    }
            }
            
            hotspots[hotspots.length]=hotspot
        } else if (xmlDoc.childNodes[n].nodeName!='#text') alert('Error parsing xml')
    }
    
    for (var i=0; i<hotspots.length;i++){
        hotspots[i].Coords = hotspots[i].Coords.split(' ')
        for (var n=0 ; n<hotspots[i].Coords.length;n++){
            hotspots[i].Coords[n]=hotspots[i].Coords[n].split(',')
        }
    }
    
    
    if (hotspots.length>0) hotspottimer = setInterval('checkMouseTimeout()',500)
    
    //if this window was opened by another window, then links will go to that window
    //if not, links will go to a new window
    //this is so that the catalogue can compliment an online shop
    
    linktarget = 'catalogueLinkWindow'
    try {
        if (window.opener && window.opener.name)
            linktarget=window.opener.name
    } catch (e) {/*Recover from this error*/}
}

function insertHTML(where, what){
    if (browser.browser=='IE') 
        where.innerHTML=what
    else {

	    var parser = new DOMParser()
	    var doc = parser.parseFromString('<div xmlns="http://www.w3.org/1999/xhtml">' + what + '<\/div>', 'application/xhtml+xml')
	    var root = doc.documentElement

        while (where.hasChildNodes())
            where.removeChild(where.childNodes[0])
        
	    for (var i=0; i < root.childNodes.length; ++i)
		    where.appendChild(document.importNode(root.childNodes[i], true))
    
    }
    //change all links in where to open in seperate window
    
    var links=where.getElementsByTagName('a')
    
    for (var i = 0; i < links.length; i++) { 
        links[i].setAttribute('target',linktarget)
    }

}

function checkMouseTimeout(){
    if (currentHotSpot != -1) {
        //There is a tooltip currently shown, so check to see if the mouse
        //is in the right area or whether it should be hidden
        
        var x1 = Math.round(zoomWidth*(currentMousePos[0]-elementPos(document.getElementById('container'))[0])/pageWidth)
        var x2 = Math.round(zoomWidth*(currentMousePos[0]-elementPos(document.getElementById('container'))[0]-pageWidth)/pageWidth)
        var y = Math.round(zoomHeight*(currentMousePos[1]-elementPos(document.getElementById('container'))[1] - topspace)/bookheight)

        if (intooltip) return
             
        if (hotspots[currentHotSpot].PageNo==currentpage) {
            if (ptInRegion(hotspots[currentHotSpot].Coords, [x1,y])) return
        } else if (hotspots[currentHotSpot].PageNo==(currentpage+1)){
            if (ptInRegion(hotspots[currentHotSpot].Coords, [x2,y])) return
        }

            
        currentHotSpot=-1
        document.getElementById('tooltip').style.display='none'


    }

    if (document.getElementById('ZoomPanel').style.display != 'none') return
    
    //if (document.getElementById('thumbnails').style.display != 'none') return
    
    if ((new Date()).getTime()-lastmoved>hotspotDelay) {
        lastmoved=null
        var x1 = Math.round(zoomWidth*(currentMousePos[0]-elementPos(document.getElementById('container'))[0])/pageWidth)
        var x2 = Math.round(zoomWidth*(currentMousePos[0]-elementPos(document.getElementById('container'))[0] - pageWidth)/pageWidth)
        var y = Math.round(zoomHeight*(currentMousePos[1]-elementPos(document.getElementById('container'))[1] - topspace)/bookheight)

        for (var i=0; i<hotspots.length;i++){
            if (hotspots[i].PageNo==currentpage) {
            
                if (ptInRegion(hotspots[i].Coords, [x1,y])) {
                    if (currentHotSpot != i) {
                        document.getElementById('tooltip').style.display='inline'
                        document.getElementById('tooltip').style.filter='alpha(opacity=0)'
                        document.getElementById('tooltip').style.opacity='0'
                        insertHTML(document.getElementById('productdesc'),hotspots[i].Description)
                        document.getElementById('producthead').innerHTML=hotspots[i].ProductName
                        document.getElementById('productamount').innerHTML='$'+hotspots[i].Amount
                        if (hotspots[i].ImageURL) 
                            document.getElementById('productimg').src=hotspots[i].ImageURL
                        else
                            document.getElementById('productimg').src='Images/noimage.png'
                        document.getElementById('tooltip').style.left=currentMousePos[0] +'px'
                        document.getElementById('tooltip').style.top=currentMousePos[1]-document.getElementById('tooltip').clientHeight/2 +'px'
                        animate('ToolTipFade',0,100,300,function(n) {document.getElementById('tooltip').style.opacity=n/100;document.getElementById('tooltip').style.filter='alpha(opacity='+n +')'}, function(){}, true)
                    }
                    currentHotSpot=i
                    return
                }
            } else if (hotspots[i].PageNo==currentpage+1) {
                if (ptInRegion(hotspots[i].Coords, [x2,y])) {
                
                    if (currentHotSpot != i) {
                        document.getElementById('tooltip').style.display='inline'
                        document.getElementById('tooltip').style.filter='alpha(opacity=0)'
                        document.getElementById('tooltip').style.opacity='0'
                        insertHTML(document.getElementById('productdesc'),hotspots[i].Description)
                        document.getElementById('producthead').innerHTML=hotspots[i].ProductName
                        document.getElementById('productamount').innerHTML='$'+hotspots[i].Amount
                        if (hotspots[i].ImageURL) 
                            document.getElementById('productimg').src=hotspots[i].ImageURL
                        else
                            document.getElementById('productimg').src='Images/noimage.png'
                        document.getElementById('tooltip').style.left=currentMousePos[0]-parseInt(document.getElementById('tooltip').clientWidth)+'px'
                        document.getElementById('tooltip').style.top=currentMousePos[1]-document.getElementById('tooltip').clientHeight/2+'px'
                        animate('ToolTipFade',0,100,300,function(n) {document.getElementById('tooltip').style.opacity=n/100;document.getElementById('tooltip').style.filter='alpha(opacity='+n +')'}, function(){}, true)
                    }
                    currentHotSpot=i
                    return
                }

            }
            
        }

    }
}


