var g_XmlHTTPProgID;
var g_Features = new Array(
        "categorisation",
        "catalogue browsing",
        "searching",
        "personalisation",
        "online marketing",
        "cross sell",
        "shopping cart",
        "checkout",
        "domain names",
		"security",
		"customer profile history",
		"smart client",
		"payment methods",
		"currency support",
		"taxation",
		"checkout rules",
		"freight",
		"catalogue management",
		"integration",
		"content management",
        "order tracking",
        "customer database",
        "email marketing",
        "reporting",
        "payment gateways",
        "data feeds",
        "data centre",
        "network",
        "hardware",
        "backup",
        "architecture");
        
var g_FeaturePopups = new Object();
var g_CloseTimer;
var g_CurrentPopup;
var g_RequestStack = new Object();
var g_DisplayIndex;
var g_ScrollOffset = 190;
var g_CloseTimeout = 1200;

function cancelEvent(e)
{
    if (!e) var e = window.event;
 
    if (e.returnValue)
        e.returnValue = false;
        
    if (e.cancelBubble)
        e.cancelBubble = true;
           
    if (e.stopPropagation)
        e.stopPropagation();
        
    if (e.preventDefault)
        e.preventDefault();        

    return false;
}

function currentSitePath()
{
    var pathname = window.location.pathname;
    
    // strip the trailing path
    pathname = pathname.replace(/\/[^\/]*$/, "");
    
    return window.location.protocol + "//" + window.location.hostname + pathname + "/";
}

function featureLoad(i)
{
    var xmlHttp = g_RequestStack[i].xmlHttp;
    var popup = g_RequestStack[i].popup;
    
    if (xmlHttp.readyState == 4)
    {
        popup.innerHTML = xmlHttp.responseText;
               
        if (g_DisplayIndex == i)
        {
            if (g_CloseTimer)
            {
                clearTimeout(g_CloseTimer);
                g_CloseTimer = null;
            }
            
            if (g_CurrentPopup)
            {
                g_CurrentPopup.style.display = "none";
            }        

            g_CurrentPopup = popup;

            if (g_CurrentPopup)    
                g_CurrentPopup.style.display = "block";                             
        }
    }
}

function featureHover(e)
{
    if (!e) var e = window.event;
    if (!e.srcElement) e.srcElement = this;

    g_DisplayIndex = null;
        
    var popup = g_FeaturePopups[e.srcElement];
    
    if (g_CurrentPopup && popup == g_CurrentPopup)
        return;
        
    if (g_CloseTimer)
    {
        clearTimeout(g_CloseTimer);
        g_CloseTimer = null;
    }
    
    if (g_CurrentPopup)
    {
        g_CurrentPopup.style.display = "none";
    }     
    
    if (!popup)
    {    
        var featureElement = document.getElementById("Features");
        
        if (featureElement)
        {
            var anchorsList = featureElement.getElementsByTagName("A");
                  
            for(var idx = 0; idx < anchorsList.length; idx++)
            {
                if (anchorsList.item(idx) == e.srcElement)
                {               
                    popup = document.createElement("DIV");
                    popup.className = "featurepopup";
                    popup.style.display = "none";
                    
                    if (!document.recalc)
                        popup.style.position = "fixed";
                    else
                        popup.style.pixelTop = document.documentElement.scrollTop + g_ScrollOffset;
                    
                    addEvent(popup, "mouseover", featureCancelClose);
                    addEvent(popup, "mouseout", featureClose);
                    
                    g_FeaturePopups[e.srcElement] = popup;
                    
                    e.srcElement.parentNode.insertBefore(popup, e.srcElement);

                    var xmlHttp = new XMLHttpRequest();
                    g_RequestStack[idx] = new Object();
                    g_RequestStack[idx].xmlHttp = xmlHttp;
                    g_RequestStack[idx].popup = popup;
                    g_RequestStack[idx].feature = g_Features[idx];
                    g_DisplayIndex = idx;
                 
                    xmlHttp.onreadystatechange = new Function("featureLoad(" + idx + ")");
                    xmlHttp.open("GET", currentSitePath() + "features/" + g_Features[idx] + ".xml", true);
                    xmlHttp.send("");
                    return;
                }
            }
        }
    }
        
    g_CurrentPopup = popup;
    if (g_CurrentPopup)    
        g_CurrentPopup.style.display = "block";
}

function featureCancelClose(e)
{
    if (g_CloseTimer)
    {
        clearTimeout(g_CloseTimer);
        g_CloseTimer = null;
    }
}

function featureHide()
{
    if (g_CurrentPopup)
        g_CurrentPopup.style.display = "none";
        
    g_CurrentPopup = null;
}

function featureClose(e)
{
    if (!e) var e = window.event;
    if (!e.srcElement) e.srcElement = this;
    if (!e.toElement && e.relatedTarget) e.toElement = e.relatedTarget;
    if (!e.toElement) return;
    
    if (e.srcElement.tagName != "LI")
        return;
        
    if (e.toElement && e.srcElement.contains(e.toElement))
        return;
        
    if (e.srcElement == e.toElement)
        return;        

    if (g_CloseTimer)
    {
        clearTimeout(g_CloseTimer);
        g_CloseTimer = null;
    }
    
    g_CloseTimer = setTimeout(featureHide, g_CloseTimeout);
}

function initFeatures()
{
    if ((g_XmlHTTPProgID || window.XMLHttpRequest) && document.documentElement.contains)
    {
        var featureElement = document.getElementById("Features");
        
        if (featureElement)
        {
            var anchorsList = featureElement.getElementsByTagName("A");
            
            for(var idx = 0; idx < anchorsList.length; idx++)
            {
                anchorsList.item(idx).style.cursor = "default";
                addEvent(anchorsList.item(idx), "click", cancelEvent);
                addEvent(anchorsList.item(idx), "mouseover", featureHover);
                addEvent(anchorsList.item(idx).parentNode, "mouseout", featureClose);
            }
        }
    }
}


function addEvent(obj, eventName, funcHandler)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(eventName, funcHandler, true);
        return true;
    }
    if (obj.attachEvent)
    {
        obj.attachEvent("on" + eventName, funcHandler);
        return true;
    }
    
    return false;
}

addEvent(window, "load", initFeatures);

if(!window.XMLHttpRequest)
{
    XMLHttpRequest = function() {
        if(!g_XmlHTTPProgID){
            var l_XmlHTTPProgID = new Array("Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");

             for(var idx = 0; idx < l_XmlHTTPProgID.length; idx++)
             {
                try{
                    var oDoc = new ActiveXObject(l_XmlHTTPProgID[idx]);
                    
                    g_XmlHTTPProgID = l_XmlHTTPProgID[idx];
                    
                    break;                    
                }
                catch (e)
                {
                }                    
            }
            
            if (idx == l_XmlHTTPProgID.length)
                return null;
        }
        
        return new ActiveXObject(g_XmlHTTPProgID);
    }
}

function featureScroll()
{
    for(el in g_FeaturePopups)
        g_FeaturePopups[el].style.pixelTop = document.documentElement.scrollTop + g_ScrollOffset;
}

if (document.recalc)
{
    window.attachEvent("onscroll", featureScroll);
}

function featureMovie(strMovie)
{
    var strMovieEncoded = strMovie.replace(/ /g, '_');

     var fo = new SWFObject( "videos/" + strMovieEncoded + "_controller.swf", "videos/" + strMovieEncoded + "_controller.swf", "400", "355", "7", "#FFFFFF", false, "best" );
     fo.addVariable( "csConfigFile", "videos/" + strMovieEncoded + "_config.xml"  ); 
     fo.addVariable( "csColor"     , "FFFFFF"           );
     fo.addVariable( "csPreloader" , "videos/" + strMovieEncoded + "_preload.swf" );
     if( args.movie )
     {
        fo.addVariable( "csFilesetBookmark", args.movie );
     }     
     fo.write("mov_" + strMovieEncoded);    
}

if (window.Node && window.Node.prototype && !window.Node.prototype.contains)
{
    if (document.documentElement.compareDocumentPosition)
    {
        window.Node.prototype.contains = function (el) {
            return (this.compareDocumentPosition(el) & 16) != 0;
        }
    }
    else
    {
        window.Node.prototype.contains = function (el) {
            while(el != document.documentElement)
            {
                el = el.parentNode;
                
                if (el == this)
                    return true;
            }
            
            return false;
        }
    }
}
