var x_lookup = [];
var intro = true;

var animator = new animator('animator');

function setupThumbs() {
    
    var thumbs = element('thumbs');
    thumbs.x_vel = 0;
    thumbs.decel = false;
    thumbs.style.overflow = 'hidden';
    thumbs.firstChild.style.marginLeft = '0px';
    
    if (navigator.appName == 'Microsoft Internet Explorer' &&
        navigator.userAgent.indexOf('Mac') != -1) {
        thumbs.style.overflow = 'auto';
        thumbs.style.visibility = 'visible';
        thumbs.style.paddingBottom = '30px';
        element('thumbs_loading').style.visibility = 'hidden';
        return;
    }
        
    
    var body = document.getElementsByTagName('body')[0];
    addEvent(body, 'onmousemove', thumbsHandler);
    
    var anchors = thumbs.getElementsByTagName('a');
    
    anchors[anchors.length - 1].style.marginRight = 0;
    var hover = document.getElementById('hover');
    width = 0;
    for (var i = 0; i < anchors.length; i++) {
        (function(i) {
            var href = anchors[i].getAttribute('href');
            var matches = href.match(/([^\/]+)$/);
            
            for (var x = anchors[i].offsetLeft;
                 x < anchors[i].offsetLeft + anchors[i].offsetWidth;
                 x++) {
                 x_lookup[x] = matches[1];
            }
            for (var x = anchors[i].offsetLeft + anchors[i].offsetWidth;
                 x < anchors[i].offsetLeft + anchors[i].offsetWidth + 20;
                 x++) {
                 x_lookup[x] = false;
            }
            width += anchors[i].offsetWidth + 20;
        })(i);
    }
    thumbs.firstChild.style.width = (width - 20) + 'px';
    thumbs.page_offset = width;
    
    setupWrapAround();
    
    var hover = document.getElementById('hover');
    hover.isbn = false;
    
    document.getElementById('thumbs_loading').style.display = 'none';
    
    var body = document.getElementsByTagName('body')[0];
    var field = thumbs.firstChild;
    field.style.marginLeft = (body.offsetWidth * -0.75) + 'px';
    
    var callback = function() {
        thumbs.x_vel = -2;
        window.setInterval('thumbsInterval();', 33);
    }
    
    field.animate = {
        marginLeft: [0, 2000, animator.ease.cubic_out, callback]
    };
    animator.register(field);
    
    thumbs.style.opacity = 0.1;
    thumbs.animate = {
        opacity: [0.99, 1000, animator.ease.linear]
    };
    animator.register(thumbs);
    
    thumbs.style.visibility = 'visible';
}

addEvent(window, 'onload', setupThumbs);


function setupWrapAround() {
    var thumbs = element('thumbs');
    var field = thumbs.firstChild;
    var anchors = field.getElementsByTagName('a');
    var n = 0;
    var width = field.offsetWidth;
    var extra_wide = width + self.screen.availWidth;
    var br_lookup = field.getElementsByTagName('br');
    var br = br_lookup[br_lookup.length - 1];
    
    field.style.width = (extra_wide + 400) + 'px';
    
    while (width < extra_wide) {
        
        var href = anchors[n].getAttribute('href');
        var matches = href.match(/([^\/]+)$/);
        for (var x = anchors[n].offsetLeft;
             x < anchors[n].offsetLeft + anchors[n].offsetWidth;
             x++) {
             x_lookup[x + thumbs.page_offset] = matches[1];
        }
        for (var x = anchors[n].offsetLeft + anchors[n].offsetWidth;
             x < anchors[n].offsetLeft + anchors[n].offsetWidth + 20;
             x++) {
             x_lookup[x + thumbs.page_offset] = false;
        }
        
        var cloned = anchors[n].cloneNode(true);
        field.insertBefore(cloned, br);
        n++;
        width += anchors[n].offsetWidth + 20;
    }
    
    
}

var counter = 0;
function thumbsHandler(event) {
    
    if (!event) {
        var event = window.event;
    }
    
    var thumbs = document.getElementById('thumbs');
    var hover = document.getElementById('hover');
    
    var x = event.clientX - thumbs.offsetLeft;
    var y = event.clientY - thumbs.offsetTop;
    
    var thresh = thumbs.offsetWidth / 3;
    var speed = thresh / 15;
    if (y < 0 || y > thumbs.firstChild.offsetHeight) {
        thumbs.x_vel = -2;
        thumbs.active = false;
    } else if (x > thumbs.offsetWidth - thresh) {
        thumbs.x_vel = (thumbs.offsetWidth - thresh - x) / speed;
        thumbs.decel = false;
        thumbs.x = event.clientX;
        thumbs.active = true;
    } else if (x < thresh) {
        thumbs.x_vel = (thresh - x) / speed;
        thumbs.decel = false;
        thumbs.x = event.clientX;
        thumbs.active = true;
    } else {
        thumbs.x_vel = 0;
        thumbs.decel = false;
        thumbs.x = event.clientX;
        thumbs.active = true;
    }
    if (x > thumbs.offsetWidth / 2) {
        thumbs.mode = 'right';
    } else {
        thumbs.mode = 'left';
    }
}

function thumbsInterval() {
    var thumbs = document.getElementById('thumbs');
    var hover = document.getElementById('hover');
    var field = thumbs.firstChild;
    
    if (thumbs.x_vel != 0) {
        var margin = parseInt(field.style.marginLeft) + thumbs.x_vel;
        if (margin > 0) {
            margin -= thumbs.page_offset;
        } else if (margin < -thumbs.page_offset) {
            margin += thumbs.page_offset;
        }
        field.style.marginLeft = margin + 'px';
    }
    
    if (thumbs.decel && Math.abs(thumbs.x_vel) < 0.2) {
        thumbs.x_vel = 0;
        thumbs.decel = false;
    } else if (thumbs.decel) {
        thumbs.x_vel = thumbs.x_vel * 0.8;
    }
    
    var x = thumbs.x - parseInt(thumbs.firstChild.style.marginLeft);
    if (thumbs.active && x_lookup[x] && Math.abs(thumbs.x_vel) < 5) {
        
        var isbn = x_lookup[x];
        if (hover.isbn != isbn) {
            var content = document.getElementById('hover-' + isbn);
            if (content) {
                var inner = cssQuery('#hover .inside div')[0];
                inner.innerHTML = content.innerHTML;
                hover.style.visibility = 'visible';
            }
            hover.isbn = isbn;
        }
        setTimeout("document.getElementById('hover').style.visibility = 'visible'", 33);
        
        hover.style.top = (thumbs.offsetTop - hover.offsetHeight - 10) + 'px';
        var bottom = cssQuery('#hover div.bottom')[0];
        if (thumbs.mode == 'left') {
            hover.style.left = (thumbs.x - 9) + 'px';
            bottom.style.left = '5px';
        } else {
            hover.style.left = (thumbs.x - hover.offsetWidth + 9) + 'px';
            bottom.style.left = (hover.offsetWidth - 25) + 'px'; 
        }
    } else {
        hover.style.visibility = 'hidden';
    }
    /*
    var x = thumbs.x - parseInt(thumbs.firstChild.style.marginLeft);
    if (thumbs.active && x_lookup[x] && Math.abs(thumbs.x_vel) < 5) {
        if (hover.timeout && x_lookup[x] != hover.isbn) {
            clearTimeout(hover.timeout);
            hover.isbn = x_lookup[x];
            hover.timeout = setTimeout('hoverTimeout();', 750);
        } else if (!hover.timeout) {
            hover.isbn = x_lookup[x];
            hover.timeout = setTimeout('hoverTimeout();', 750);
        }
        hover.style.top = (thumbs.offsetTop - hover.offsetHeight - 10) + 'px';
        if (thumbs.mode == 'left') {
            hover.style.left = (thumbs.x - 9) + 'px';
            hover.childNodes[1].childNodes[3].style.left = '5px';
        } else {
            hover.style.left = (thumbs.x - hover.offsetWidth + 9) + 'px';
            hover.childNodes[1].childNodes[3].style.left = (hover.offsetWidth - 16) + 'px'; 
        }
    } else {
        hover.style.visibility = 'hidden';
    }*/
}

function hoverTimeout() {
    
    var hover = document.getElementById('hover');
    var content = document.getElementById('hover-' + hover.isbn);
    
    if (content) {
        hover.childNodes[1].childNodes[1].innerHTML = content.innerHTML;
    }
    
    hover.timeout = window.setTimeout(function() {
        var hover = document.getElementById('hover');
        hover.style.visibility = 'visible';
        hover.timeout = false;
    }, 50);
    
}

function checkQuery() {
    var loc = '' + location.href;
    if (loc.indexOf('edit') != -1) {
        set_cookie('edit', 1);
    } else if (loc.indexOf('logout') != -1) {
        set_cookie('edit', 0);
    }
}

addEvent(window, 'onload', checkQuery);

