function openWPpopup(sourceUrl) {
    window.open(sourceUrl,'WikiPharm', 'width=480,height=600,toolbars=no,menubar=no,location=no,scrollbars=yes,resizable=no,status=no');
}

function setCookie( name, value, expires, path, domain, secure )
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires ) {
        expires = expires * 1000 * 60 * 60; /* * 24; */
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function getCookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );

        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == check_name ) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 ) {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found ) {
        return null;
    }
}

function mainboxVisible(isVisible) {
    if (isVisible)
        $('#mainbox').show();
    else
        $('#mainbox').hide();
}

function search(formid, inputid) {
	if(checkKeyword(inputid))
	    $.history.load('/wikipharm/lista.php?' + $('#' + formid).serialize());
}

function openlinkAndScroll(link, divName) {
    $.history.load($(link).attr('href'));
    document.getElementById(divName).scrollIntoView(true);
}

function openlink(link) {   
    $.history.load($(link).attr('href'));
}

function scrollToDiv(divName){
    document.getElementById(divName).scrollIntoView(true);
}

function showPopup() {
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set height and width to mask to fill up the whole screen
	$('#mask').css({'width':460,'height':maskHeight});
	//$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	$('#mask').fadeIn(500);	
	$('#mask').fadeTo(500,0.2);	
	
	var winH = $(window).height();
	var winW = $(window).width();
	      
	//Set the popup window to center
	$('#dialog').css('top',  350);
	$('#dialog').css('left', winW/2-$('#dialog').width()/2);
	//$('#dialog').css('top',  winH/2-$('#dialog').height()/2);
	//$('#dialog').css('left', winW/2-$('#dialog').width()/2);
	
	//transition effect
	$('#dialog').fadeIn(500); 
}

function closePopup() {
	$('#mask, .window').hide();
}

function checkKeyword(idtext){
	var key = $('#' + idtext).val();
	if(key.length < 2)
	{
            //qualche azione per evidenziare la situazione
            $('#' + idtext).css({'border' : '2px solid red'});
            $('#alertText' + idtext.substring(10)).text('Inserire almeno due caratteri');
            return false;
	}
	else
	{
            //ripulire l'eventuale evidenziazione di cui sopra
            //questo in realta' si potrebbe anche fare sull'onkeyup del textbox
            $('#' + idtext).css({'border' : '1px solid black'});
            $('#alertText' + idtext.substring(10)).text('');
            return true;
	}
}

//Document OnReady Callback
$(function() {




    
	

    //Tabs
    $('#searchTabs').tabs();
    
    // Radio buttons
    $('#radioRIS').buttonset();
    $('#radioUMA').buttonset();
    $('#radioVET').buttonset();
    $('#radioDIS').buttonset();

    //Autocomplete
    //var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
    $("#searchtermRIS").autocomplete({
        minLength: 2,
        source: "suggest.php"
	//source: data
        //source: function(req, add){ $.get('suggest.php', function(data){ add(eval(data));	}); }
    });
    
    //History
    $.history.init(function(hash){
        if(hash == '') {
            //TODO : volendo si può mettere un'animazione del fadout del contenuto
            $('#mainbox').html('');
        } else {
            //showPopup();
            var timer = setTimeout('showPopup()', 1000);
            $.get(hash, function(data){
                var mainbox = $('#mainbox');
                clearTimeout(timer);
                    closePopup();
                mainbox.fadeOut(500, function(){
                    mainbox.html(data);
                    mainbox.effect('slide', 700);
                });
            });
        }
    },
    {unescape: ",/"});
	
	
});

