/**
 * projects.js
 * script file for the main projects page
 */
var projects = new Array();
projects[0] = {'name':'1976 Fluy House','url':'fluy/index.html','img':'s_100'};
projects[1] = {'name':'1979 Eagle Rock House','url':'eagle_rock/index.html','img':'s_151'};
projects[2] = {'name':'1981 La Villette: Bioclimatic Facades','url':'la_villette/index.html','img':'s_165'};
projects[3] = {'name':'1981 La Villette: Roof and Heliomirrors','url':'la_villette_roof/index.html','img':'s_165_2'};
projects[4] = {'name':'1983 3D Light Forms','url':'amenagement/index.html','img':'s_166'};
projects[5] = {'name':'1983 Louvre Pyramids','url':'louvre_pyramid/index.html','img':'s_171'};
projects[6] = {'name':'1984 Island Gardens - Boat Museum','url':'boat_museum/index.html','img':'s_219'};
projects[7] = {'name':'1985 Louvre Sculpture Courts','url':'louvre_court/index.html','img':'s_222'};
projects[8] = {'name':'1986 Pharmacie - Boves, France','url':'pharmacie/index.html','img':'s_229'};
projects[9] = {'name':'1987 Roy Square - The Watergardens','url':'roy_square/index.html','img':'s_230'};
projects[10] = {'name':'1987 Pearl of the Gulf, Dubai','url':'dubai/index.html','img':'s_276'};
projects[11] = {'name':'1987 Symbol France - Japan','url':'symbol/index.html','img':'s_277'};
projects[12] = {'name':'1988 Stockley Park, B8','url':'stockley_park/index.html','img':'s_298'};
projects[13] = {'name':'1989 Reina Sofia\'s Art Museum','url':'reina_sofia/index.html','img':'s_307'};
projects[14] = {'name':'1989 Ecology Gallery','url':'ecology/index.html','img':'s_313'};
projects[15] = {'name':'1990 J.L.E. Bermondsey Station','url':'bermondsey_station/index.html','img':'s_331'};
projects[16] = {'name':'1990 J.L.E. Mid Line Vents','url':'bermondsey_vents/index.html','img':'s_331_2'};
projects[17] = {'name':'1991 Ecole Maternelle - Daours, France','url':'ecole_maternelle/index.html','img':'s_335'};
projects[18] = {'name':'1991 Herne Sodingen Academy','url':'academy/index.html','img':'s_342'};
projects[19] = {'name':'1992 Terrasson','url':'terrasson/index.html','img':'s_356'};
projects[20] = {'name':'1992 Leipzig Glass Hall','url':'leipzig/index.html','img':'s_361'};
projects[21] = {'name':'1993 London Regatta Centre','url':'regatta/index.html','img':'s_369'};
projects[22] = {'name':'1994 EDF Pylons','url':'edf_pylons/index.html','img':'s_402'};
projects[23] = {'name':'1995 Royal Opera House','url':'opera_house/index.html','img':'s_416'};
projects[24] = {'name':'1996 Crystal Palace Concert Platform','url':'concert_platform/index.html','img':'s_457'};
projects[25] = {'name':'1997 Scotland\'s Home of Tomorrow','url':'home/index.html','img':'s_476'};
projects[26] = {'name':'1997 Plymouth Theatre Royal','url':'plymouth/index.html','img':'s_plymouth'};
projects[27] = {'name':'1997 Whitecity: Central Line Sidings','url':'wc_cls/index.html','img':'s_489'};
projects[28] = {'name':'1998 Spire of Dublin','url':'dublin/index.html','img':'s_496'};
projects[29] = {'name':'1999 Hawking Space Time Centre','url':'space/index.html','img':'s_space'};
projects[30] = {'name':'1999 Sonic Boom','url':'sonic/index.html','img':'s_son'};
projects[31] = {'name':'1999 Wood Lane Station','url':'hcl_station/index.html','img':'s_550'};
projects[32] = {'name':'2000 Alba Di Milano','url':'alba/index.html','img':'s_alba'};
projects[33] = {'name':'2000 Oberhausen Gasometer','url':'oberhausen/index.html','img':'s_oberhausen'};
projects[34] = {'name':'2000 Magna Carta','url':'magna/index.html','img':'s_magna'};
projects[35] = {'name':'2001 Potters Fields','url':'potters_fields/index.html','img':'s_528'};
projects[36] = {'name':'2002 BMW Plant Leipzig','url':'bmw/index.html','img':'s_bmw'};
projects[37] = {'name':'2002 Three Colt Street','url':'office/index.html','img':'s_473'};
projects[38] = {'name':'2004 RSC Courtyard Theatre','url':'rsc_theatre/index.html','img':'s_551'};
projects[39] = {'name':'2006 New River Avon Footbridge','url':'avon_footbridge/index.html','img':'s_566'};
projects[40] = {'name':'','url':'','img':''};
var projectWidth = 420;//200 + 10 + 200 + 10
/* write some styles into the page */
document.write('<style type="text/css">#projectscontent { overflow:hidden; } #projectslayer { width:' + ((projectWidth * projects.length) + 200) + 'px; left:-' + ((projectWidth * (projects.length - 1)) + 10) + 'px; }</style>');
var maxDragAreaWidth = 800;
var handleWidth = Math.floor(maxDragAreaWidth/projects.length);
var dragAreaX = 50;
var dragAreaY = 100;
var dragAreaHeight = 42;
var currentProject = (projects.length - 1);
var projectIdx = (projects.length - 1);
var active = false;
var scrolling = false;
var returning = false;
var disableMoveHandler = false;
var to = null;
window.onload = function() {
    /* add news link text */
    writeNewsLink();
    if (document.images) {
        //$('#handleimg').src('images/bg/handle-blue.gif');
    }
    $('#dragarea').width((projects.length * handleWidth) + 'px');
    $('#handle').left(((projects.length - 1) * handleWidth) + 'px');
    $('#handle').show();
    $('#handle').click(pointerClickHandler);
    $('#handle').css({cursor:'pointer'});
    $(document).mousemove(mouseMoveHandler);
    $(document).keydown(keyDownHandler);
    for (i = 0; i < projects.length; i++) {
        if (document.getElementById(projects[i].img)) {
            document.getElementById(projects[i].img).src = 'images/projects/blue/'+projects[i].img+'.gif';
        }
    }
}
function pointerClickHandler()
{
    $('#menu-window').css('visibility','hidden');
    //$('#menu').src("threads/blank.html");
    $('#data-window').css('visibility','hidden');
    //$('#data').src("threads/blank.html");
    handleX = $('#handle').left();
    currentProject = (Math.floor(parseInt(handleX)/handleWidth));
    totalDistance = (projectWidth * (projects.length - 1));
    distanceToTravel = Math.abs(Math.abs(parseInt($('#projectslayer').left())) - (currentProject * projectWidth));
    minTime = 1000;
    maxTime = 4000;
    time = minTime + (Math.floor((distanceToTravel / totalDistance) * (maxTime - minTime)));
    $('#projectslayer').animate({'left': (-(currentProject * projectWidth))}, time, returnHandle);
    //alert(projects[currentProject].url);
}
function mouseMoveHandler(event)
{
    if (disableMoveHandler) return;
    x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
    y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
    /* check to see if the mouse id over the drag strip */
    if (y >= dragAreaY && y <= (dragAreaY + dragAreaHeight) && x > dragAreaX && x < (dragAreaX + (projects.length * handleWidth))) {
        /* adjust for each end (mouse is positioned in the middle of the handle) */
        if (x > (dragAreaX + ((projects.length * handleWidth) - (handleWidth / 2)))) {
            handleX = ((projects.length - 1) * handleWidth);
        } else if (x < (dragAreaX + (handleWidth / 2))) {
            handleX = 0;
        } else {
            handleX = (x - dragAreaX) - (handleWidth / 2);
        }
        /* adjust position of handle */
        $('#handle').left(parseInt(handleX) + 'px');
        /* adjust position of project name */
        $('#projectname').left(parseInt(handleX + 30) + 'px');
        /* save current project index */
        projectIdx = (Math.floor(handleX/handleWidth));
        /* adjust content of project name */
        $('#projectname').html(projects[projectIdx].name);
        /* make sure other scripts know the navigation is active */
        active = true;
    } else {
        if (active) {
            /* only run this function once as the mouse leaves the active area */
            active = false;
            returnHandle();
        }
    }
}
function returnHandle()
{
    /* only return the handle to the current project if the navbar is inactive */
    if (!active) {
        /* disable the mouse move handler */
        disableMoveHandler = true;
        /* turn off the project name if not current */
        if (currentProject != projectIdx) {
            $('#projectname').html('');
        }
        /* animate the handle back to the current project */
        $('#handle').animate({'left': (currentProject * handleWidth)}, 500, handleReturned);
    }
    to = null;
}
function handleReturned()
{
    /* enable the mouse move hndler */
    disableMoveHandler = false;
    /* position and write the current project name */
    $('#projectname').left(parseInt((currentProject * handleWidth) + 30) + 'px');
    $('#projectname').html(projects[currentProject].name);
}
function keyDownHandler(e)
{
    switch(e.keyCode) {
    case 37:
        if (currentProject == 0) {
            return;
        } else {
            currentProject--;
        }
        break;
    case 39:
        if (currentProject == (projects.length - 1)) {
            return;
        } else {
            currentProject++;
        }
        break;
    }
    $(document).unbind("keydown");
    returnHandle();
    $('#menu-window').css('visibility','hidden');
    //$('#menu').src("threads/blank.html");
    $('#data-window').css('visibility','hidden');
    //$('#data').src("threads/blank.html");
    $('#projectslayer').animate({'left': (-(currentProject * projectWidth))}, 1000, restoreKey);
}
function restoreKey()
{
    $(document).keydown(keyDownHandler);
}