//var CatDesc = new Array("fp-INT", "fp-ACS", "fp-DCS", "fp-ACL", "fp-DCL", "fp-HV", "fp-RM", "fp-BC", "fp-INV", "fp-CON", "fp-GTI");
//var CatPics = new Array("feat-INT.jpg", "feat-ACS.jpg", "feat-DCS.jpg", "feat-ACL.jpg", "feat-DCL.jpg", "feat-HV.jpg", "feat-RM.jpg", "feat-BC.jpg", "feat-INV.jpg", "feat-CON.jpg", "feat-GTI.jpg");
//var CatHref = new Array("#self", "http://www.etps.co.uk/products/ac-power-sources/default.aspx", "http://www.etps.co.uk/products/dc-sources/default.aspx", "http://www.etps.co.uk/products/ac-electronic-loads/default.aspx", "http://www.etps.co.uk/products/dc-electronic-loads/default.aspx", "http://www.etps.co.uk/products/high-voltage-power-supply/default.aspx", "http://www.etps.co.uk/products/rectifiers-modular-psu/default.aspx", "http://www.etps.co.uk/products/battery-charger/default.aspx", "http://www.etps.co.uk/products/dc-ac-inverters/default.aspx", "http://www.etps.co.uk/products/dc-dc-converters/default.aspx", "http://www.etps.co.uk/products/general-test-instruments/default.aspx");

var CatDesc = new Array("fp-INT", "fp-ACS", "fp-DCS", "fp-DCP", "fp-ACL", "fp-DCL", "fp-HV", "fp-RM", "fp-BC", "fp-INV", "fp-CON", "fp-GTI");
var CatPics = new Array("feat-INT.jpg", "feat-ACS.jpg", "feat-DCS.jpg", "feat-DCP.jpg", "feat-ACL.jpg", "feat-DCL.jpg", "feat-HV.jpg", "feat-RM.jpg", "feat-BC.jpg", "feat-INV.jpg", "feat-CON.jpg", "feat-GTI.jpg");
var CatHref = new Array("#self", "http://www.etps.co.uk/products/ac-power-sources/default.aspx", "http://www.etps.co.uk/products/dc-sources/default.aspx", "http://www.etps.co.uk/products/high-power-dc/default.aspx", "http://www.etps.co.uk/products/ac-electronic-loads/default.aspx", "http://www.etps.co.uk/products/dc-electronic-loads/default.aspx", "http://www.etps.co.uk/products/high-voltage-power-supply/default.aspx", "http://www.etps.co.uk/products/rectifiers-modular-psu/default.aspx", "http://www.etps.co.uk/products/battery-charger/default.aspx", "http://www.etps.co.uk/products/dc-ac-inverters/default.aspx", "http://www.etps.co.uk/products/dc-dc-converters/default.aspx", "http://www.etps.co.uk/products/general-test-instruments/default.aspx");

var Delay = 3000;           //Sets the timer delay to 3s
var iPause;                 //Determines whether or not the 'Pause' status is active 1 = Paused, 0 = Unpaused (default)
var sPauseHidden            //ID of the DIV 'to be hidden' when clicking Pause
var sPauseShown             //ID of the DIV 'to be shown' when clicking Pause
var Cid;                    //Constant for the current position in the CatDesc array
var Cidx;                   //Constant for the last position of the CatDesc array
var Swap;                   //Determines the pages JS setTimeout property status
var gLocation = '#self';    //Constant for the current contents of the CatHref array

function gotoMyUrl() {
    location = gLocation;
}

function SetLink(url) {
    document.getElementById('fpanelhref').href = url            //Sets the URL specified by CatHref to the image's HREF argument
}

function Pause(pstate, callid) {
    sPauseHidden = document.getElementById('fpanelpause' + pstate);         //Make a note to hide the currently visible pause button.
    if (pstate == 'on') {
        iPause = 1;                                                             //Set pause status active
        if(Swap) window.clearTimeout(Swap);
        sPauseShown = document.getElementById('fpanelpauseoff');                //Make a note to show 'fpanelpauseoff' (Glowing, active pause button)
    }
    else {
        iPause = 0;                                                             //Set pause status inactive
        sPauseShown = document.getElementById('fpanelpauseon');                 //Make a note to show 'fpanelpauseon' (Regular pause button)
        NextCat('next', 'fpanelpic');                                           //Jump to the next category (refreshes the timer)
    }
    if (sPauseHidden) {                                                     //When there's a valid button to hide (prevents OOB/No-ID errors)
            sPauseHidden.style.visibility = "hidden";                           //Hide the button that's currently visible
            sPauseShown.style.visibility = "visible";                           //Show the button that's currently hidden 
    }
}

function TimedSwitch(floattextID) {                                             //This runs on Page_Load
    ShowFT(CatDesc[0]);
    SetLink(CatHref[0]);
    Cid = 0;
    iPause = 0;
    Swap = window.setTimeout("NextCat('next','" + floattextID + "')", Delay);       //Begin timer to move to next category
}

function SelectCat(value, floattextID) {                                        //This runs when a specific category is selected in the drop-down
    if (value != -1) {
        value = ((+value) + 1);                                                     
        window.location = CatHref[value];                                           //Redirect the page to that category
        //NextCat(value, floattextID); //(Depreciated code)
    }
}

function NextCat(catid, floattextID) {                                              //NextCat is both for moving to Next, Previous and Selected categories, depending on how the function arguments are used
    Delay = 7000;                                                                   //IMPORTANT: This alters the category preview timing to 7s after the first frame
    if (Swap) window.clearTimeout(Swap);                                            //Prepare timer for refresh
    if (catid == 'next') {
        if (Cid > CatDesc.length - 2) {                                             //Tests for end of range
            Cid = 1;                                                                    //If EOR, reset to 1 (0 is INTroduction, and only shown once)
            HideFT(CatDesc[CatDesc.length - 1]);
        }
        else {
            Cid = Cid + 1;                                                          //Increment CatDesc position
            HideFT(CatDesc[Cid - 1]);
        }
    }
    else if (catid == 'prev') {
    if (Cid <= 1) {                                                                 //Tests for EOR (EOR for previous is 1, as 0 is INTro)
            Cid = CatDesc.length - 1;
            HideFT(CatDesc[1]);
            HideFT(CatDesc[0]);
        }
        else {
            Cid = Cid - 1;                                                          //Decrement CatDesc position
            HideFT(CatDesc[Cid + 1]);
        }
    }                                                                               //If function is to move to a specific category (Previously called from SelectCat, now redundant but left in in case requirements change)
    else {
        var i;
        for (i = 0; i <= CatDesc.length - 1; i++) {                                 //Hide all text. A workaround because globals prove unreliable
            HideFT(CatDesc[i]);
        }
        Cid = parseInt(catid);
    }
    FadeInImage('fpanelpicimg', 'http://www.etps.co.uk/images/masterpage2/' + CatPics[Cid], 'fpanelpic', Cid);  //Append URL to filename
    ShowFT(CatDesc[Cid]);
    SetLink(CatHref[Cid]);
    if (iPause == 0) {
        Swap = window.setTimeout("NextCat('next','" + floattextID + "')", Delay);   //Set up for the next timer event
    }
}

function ShowFT(id) {                                                               //Shows 'floating' text
    var chk = document.getElementById(id);
    if (chk) {                                                                      //Validate existance of element
        chk.style.visibility = "visible";                                           //Make it visible
    }
}

function HideFT(id) {
    var chk = document.getElementById(id);
    if (chk) {
        chk.style.visibility = "hidden";                                            //Hides 'floating' text
    }
}

function SetOpacity(object, opacityPct) {                                           //Sets opacity in multi-browser envs. Done this way to avoid replication
    object.style.filter = 'alpha(opacity=' + opacityPct + ')';                              //IE5-7
    object.style.MozOpacity = opacityPct / 100;                                             //Mozilla/Firefox
    object.style.opacity = opacityPct / 100;                                                //CSS 2.1 W3C (also IE 8+, Firefox, Safari)
}

function ChangeOpacity(id, msDuration, msStart, fromO, toO) {                       //This function controls opacity from 0-100%, executing the change via SetOpacity at each step
    var element = document.getElementById(id);
    var opacity = element.style.opacity * 100;                                      //Get current opacity
    var msNow = (new Date()).getTime();
    opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;               //Calculate opacity per step
    if (opacity > 50 && opacity < 55) {
    }
    if (opacity < 0) {
        SetOpacity(element, 0);
    }
    else if (opacity > 100) {
        SetOpacity(element, 100);
    }
    else {
        SetOpacity(element, opacity);
        element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")", 1);   //Prepare for next step
    }
}

function FadeInImage(foregroundID, newImage, backgroundID, floattext, floattextID)  //This function sets up transitions, and governs ChangeOpacity
{
    var foreground = document.getElementById(foregroundID);
    if (backgroundID) {
        var background = document.getElementById(backgroundID);
        if (background) {
            background.style.backgroundImage = 'url(' + foreground.src + ')';           //Set the background image to the same as the foreground image
            background.style.backgroundRepeat = 'no-repeat';
        }
    }
    SetOpacity(foreground, 0);                                                          //Hide the foreground (background (current image) looks unchanged)
    foreground.src = newImage;                                                          //Set the new image to the foreground (invisible)
    if (foreground.timer) window.clearTimeout(foreground.timer);                        //Clear timer
    var startMS = (new Date()).getTime();
    foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1000," + startMS + ",0,103,'" + "')", 10); //Begin opacity change to bring forground visible

}
