var dontClose = false;
var siClose = 0;
var isOpen = false;
var state = "closed";
var siSlide = 0;
var speedRate = 10;
var slideAmount = 10;
var closeDelay = 200;
var MAX_MENUS = 8;
var imagesFolder = "images/";
var currentPage = currentPage == undefined ? 1 : currentPage;

function init()
{
    for ( var i = 1; i < MAX_MENUS+1; i++ )
    {
        var e = GetElement("menu"+i);
        var s = e.src;
        s = s.substring(s.lastIndexOf("/")+1, s.length-4);
        s = s.split("-over").join("");
        
        e.oSrc = s;
        e.sid = i;
        
        
        e.onmouseover = function()
        {

            var newSrc = imagesFolder + this.oSrc;
            

            if ( newSrc.indexOf("-over") == -1 ) newSrc = newSrc + "-over.gif";
            else newSrc = newSrc + ".gif";
            
            if ( this.src != newSrc ) this.src = newSrc;
        };
        
        e.onmouseout = function()
        {
            if ( currentPage == this.sid ) return;
            var newSrc = imagesFolder + this.oSrc + ".gif";

            if ( this.src != newSrc ) this.src = newSrc;
        };
        
        if ( currentPage == i )
        {
            e.onmouseover();
        }
    }
}


function SetPage(i)
{
    if ( currentPage <= 0 ) currentPage = 1;

    currentPage = i + 1;
}

if ( String(window.location).indexOf("?") > -1 )
{
    setTimeout("init()", 500);
}

var browserName=navigator.appName;

if(browserName=="Microsoft Internet Explorer") 
{
    window.onload=init;
}
else
{
    if (document.addEventListener) 
    {
        document.addEventListener("DOMContentLoaded", init, false);
    }
} 


function OpenPopup(filename)
{
    var c = GetElement("cpopup");
    c.style.visibility = "visible";
    
    if ( c ) 
    {
        ClearClose();
        
        if ( state != "opened" && state != "opening" ) StartSlide("open");
        
        GetElement("pdata").innerHTML = getDocument(filename, false);
        var m = GetElement("cpopupmask");
        var s = GetElement("div-sidemenu");

        var obj = s.getBoundingClientRect();
        var l = obj.left;
        var t = obj.top;

        m.style.left = (l - s.offsetWidth) + "px";
        m.style.top = (t + 1) + "px";
        
        m.style.height = (s.offsetHeight) + "px";
        m.style.width = (s.offsetWidth + 2) + "px";
        
        m.style.clip="rect(0px,"+m.style.width+","+m.style.height+",0px)";

        c.onmouseover = function()
        {
            dontClose = true;
            ClearClose();
        };
        
        c.onmouseout = function()
        {
            dontClose = false;
            ClosePopup();
        };
    }
}

function ClearClose()
{
    if ( siClose != 0 )
    {
        clearTimeout(siClose);
        siClose = 0;
    }
}

function ClosePopup()
{
    ClearClose();
    siClose = setTimeout("_ClosePopup()", closeDelay);
}

function _ClosePopup()
{
   
    if  ( dontClose ) return;
    var c = GetElement("cpopup");
     ClearClose();
    dontClose = false;
    
    if ( c ) 
    {
        //c.style.display = "none";
        //c.style.left = "300px";
        //state = "closing";
        StartSlide("close");
    }
}
function ClearSlide()
{
    if ( siSlide != 0 )
    {
        clearInterval(siSlide);
        siSlide = 0;
    }
}

function StartSlide(direction)
{
    ClearSlide();
    
    var c = GetElement("cpopup");
    c.style.display = "block";

    if ( direction == "open" )
    {
        c.style.left = "300px";
        //Opening
        state = "opening";
    }
    else
    {
        c.style.left = "0px";
        //Closing
        state = "closing";
    }
    
    siSlide = setInterval("Slide()", speedRate);
}

//Returns the left and top values as integers and without the the "px" string
function GetCoords(obj)
{
    return new Array(parseInt(obj.style.left), parseInt(obj.style.top));
}

function Slide()
{
    var c = GetElement("cpopup");
    var gx = GetCoords(c)[0];

    //Move between 0 and object width
    if ( state == "opening" ) 
    {
        //The new position will equal where the object currently is, minus the rate (5 temp)
        if ( gx > 0 ) c.style.left = (gx - slideAmount) + "px";
        else EndSlide();
    }
    else if ( state == "closing" )
    {
        if ( gx < 310 ) 
        {
            c.style.left = (gx + slideAmount) + "px";
        }
        else EndSlide();
    }
}

function EndSlide()
{
    ClearSlide();
    
    if ( state == "opening" ) state = "opened";
    if ( state == "closing" ) 
    {
        state = "closed";
    }
}


