﻿function search(page) {

    

    var searchText = $('txt_search').get('value').trim();

    if (searchText == '' || searchText == 'Cerca nei comunicati stampa') {
        alert('Inserire delle parole di ricerca');
        return;
    }

    var query = 'k=' + searchText;

    if ($defined(page)) {
        query += '&p=' + page;
    }

    if ($defined($('searchYearSelection'))) {
        query += '&anno=' + $('searchYearSelection').value;
    }

    var xmlhttp = getXmlHTTP();
    var url = 'ashx/search.aspx?' + query + '&rnd=' + Math.random();
    xmlhttp.open("POST", url);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            var res = xmlhttp.responseText;

            $('wait').hide();
            $('content').set('html', res);
            $('content').show();

            //inizializzo smoothbox
            TB_init();

        }
    }

    var wait = $('wait');
    if ($defined(wait)) {
        wait.destroy();
    }

    wait = new Element('div', {'id':'wait'});
    wait.setStyle('position', 'fixed');
    wait.setStyle('top', $('content').getCoordinates().top);
    wait.setStyle('left', $('content').getCoordinates().left);
    wait.setStyle('width', $('content').getComputedSize().totalWidth + 'px');
    wait.setStyle('text-align', 'center');
    wait.setStyle('padding-top', '150px');
    
    wait.set('html', '<img src="images/wait.gif" /><br /><p style="padding-top:5px;font-style:italic;">Ricerca in corso nell\'archivio dei comunicati stampa...</p>');

    wait.inject($('body'));

    $('content').hide();
    
    xmlhttp.send('var=1');


}

function fade(el, options) {
    // el - the DOM element to fade
    //
    // options - an object with two properties:
    //
    //           fade - either 'in' or 'out', specifying the direction
    //           of the fade (required)
    //
    //           onComplete - a function to call after the fade
    //                        is done (optional)

    var current;
    var proxy = el.cloneNode(true);
    var direction = (options.fade == 'in') ? 1 : -1;

    el.parentNode.replaceChild(proxy, el);

    if (options.fade == 'in') {
        current = 0;
        proxy.style.visibility = 'visible';
    }
    else
        current = 1;

    setOpacity(proxy, current);
    var interval = window.setInterval(tick, 25);

    function tick() {
        current += 0.05 * direction;

        setOpacity(proxy, current);

        if (((direction == 1) && (current >= 1))
				|| ((direction == -1) && (current <= 0))) {
            el.style.visibility = (options.fade == 'in') ? 'visible' : 'hidden';
            proxy.parentNode.replaceChild(el, proxy);
            delete proxy;
            window.clearInterval(interval);

            if (options.onComplete)
                options.onComplete();
        }
    };

    function setOpacity(el, opacity) {
        var percent = Math.floor(opacity * 100);

        // IE
        el.style.zoom = 1;
        el.style.filter = 'alpha(opacity=' + percent + ')';

        // CSS 3
        el.style.opacity = opacity;
    };
};
