var searched        = false;
var collectionopen  = false;

function toggleCollection() {
    
    new Effect.toggle('collection', 'slide');
    
    if (!collectionopen) {
        $("collectionopenlink2").style.display = "block";
        $("collectionopenlink1").style.display = "none";
        searchCollection();
    } else {
        $("collectionopenlink1").style.display = "block";
        $("collectionopenlink2").style.display = "none";
    }
    
    collectionopen = !collectionopen;
    
}

function searchCollection(_start) {
    
    $("collectionloading").style.display = "block";
    
    if (_start) {
        start = _start;
    } else {
        start = 0;
    }
    
    pb = [];
    pb.push("start=" + start);
    
    if ($("filter").value == 1) {
        pb.push("op_zaal=1");
    }
    
    if ($("characteristic_type").options[$("characteristic_type").selectedIndex].value) {
        pb.push("characteristic_type=" + $("characteristic_type").options[$("characteristic_type").selectedIndex].value);
    }
    
    if ($("artist_name").options[$("artist_name").selectedIndex].value) {
        pb.push("artist_name=" + $("artist_name").options[$("artist_name").selectedIndex].value);
    }
    
    if ($("date_year_begin").options[$("date_year_begin").selectedIndex].value) {
        pb.push("date_year_begin=" + $("date_year_begin").options[$("date_year_begin").selectedIndex].value);
    }
    
    if ($("date_year_end").options[$("date_year_end").selectedIndex].value) {
        pb.push("date_year_end=" + $("date_year_end").options[$("date_year_end").selectedIndex].value);
    }
    
    if ($("characteristic").options[$("characteristic").selectedIndex].value) {
        pb.push("characteristic=" + $("characteristic").options[$("characteristic").selectedIndex].value);
    }
    
    var ajaxRequest = new Ajax.Updater( "collectionOverview", 
                                        "/search-collection.php",
                                        {   
                                            method      : "post",
                                            postBody    : pb.join("&") + "&random=" + Math.random(),
                                            onComplete  : removeLoader
                                        }
                                       );
}

function removeLoader() {
    $("collectionloading").style.display = "none";
}

function showDetails(_object_id) {
    pb = "object_id=" + _object_id;
    
    $("collectionloading").style.display = "block";
    
    
    var ajaxRequest = new Ajax.Updater( "collectionDetailCon", 
                                        "/object-detail.php",
                                        {   
                                            method      : "post",
                                            postBody    : pb + "&random=" + Math.random(),
                                            onComplete  : showDetailsComplete
                                        }
                                       );
}

function showDetailsComplete() {
    $("collectionDetailCon").style.display = "block";
    removeLoader();
    hideTooltip();
}

//-----------------------//
//-- TOOLTIP FUNCTIONS --//
//-----------------------//

var mouseX;
var mouseY;

//-- retrieve mouse position
function getMousePos(e) {
    if (!e) {
        var e = window.event || window.Event;
    }
    
    if('undefined'!=typeof e.pageX) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    } else {
        mouseX = e.clientX + document.body.scrollLeft;
        mouseY = e.clientY + document.body.scrollTop;
    }
    
}

function setMouseCapture() {
    if (window.Event && document.captureEvents) {
        document.captureEvents(Event.MOUSEMOVE);
    }
    document.onmousemove = getMousePos;
}

addLoadEvent(function() {
    setMouseCapture();
});

function tooltip(_obj, _name, _artist) {
    
    tt          = document.getElementById('tooltip');
    ttname      = document.getElementById('tooltipname');
    ttartist    = document.getElementById('tooltipartist');
    
    ttname.innerHTML    = _name;
    ttartist.innerHTML  = _artist;
    
    containerend = document.getElementById('container').offsetLeft + document.getElementById('container').offsetWidth;
    
    if (mouseX + 200 > containerend) {
        newX = containerend - 225;
    } else {
        newX = mouseX;
    }
    
    newY = mouseY; 
    
    newX = newX + 10;
    newY = newY + 10;
    
    with (tt.style) {
        visibility  = "visible";
        left = newX + "px";
        top  = newY + "px";
    }
}

//-- hide tooltip
function hideTooltip() {
    document.getElementById('tooltip').style.visibility = "hidden";
}

function printDetail(_object_id) {
    printwindow = window.open("/object-detail.php?print=1&object_id=" + _object_id, "printWindow", "width=700,height=500,scrollbars=yes");
    printwindow.focus();
}

function toggleCheckbox() {
    if ($("filter").value == 1) {
        $("filter").value = "";
        $("checkthing").src = "/images/checkbox_unchecked";
    } else {
        $("filter").value = 1;
        $("checkthing").src = "/images/checkbox_checked";
    }
    searchCollection();
}
