﻿$.ajaxSetup({
    // Disable caching of AJAX responses */
    cache: false
});
function selectAll(checked)
{
    $('#searchresults input').attr('checked', checked);
}   
function showResults(selectedOnly) {
    if (selectedOnly)
        $('#searchresults div.result').each(function() { if (!$(this).find('input.resultselect:checked').val()) $(this).hide(); });
    else
        $('#searchresults div.result').each(function() { $(this).show(); });
}
function onSelectResult( sender, id )
{
    recalculateRelated(id);
    if (sender.checked)
        $(sender).parent().parent().addClass('selected');
    else
        $(sender).parent().parent().removeClass('selected');
}
function openSelected() 
{
    var selected = $('#searchresults input:checked');
    var count=0;
    $(selected).each(function() { count++; });
    if (count==0)
        alert('Please select one or more results first');
    else
    if (count<4 || confirm('Open all '+count+' results, each in a new window?'))
    {
        var max=10;
        if (count>max)
            alert('Eesh, '+count+' is too many for one poor browser at a time! No more than '+max);
        else
        $(selected).each(function() 
        { 
            id=this.id.substring(12);
            window.open('/visit/'+id,'doc'+id);    
        } );
    }
}
function emailSelected()
{
    var selected = $('#searchresults input:checked');
    var count=0;
    $(selected).each(function() { count++; });
    if (count==0)
        alert('Please select one or more results first');
    else
    if (count<30 || confirm('Email all '+count+' results?'))
    {
        var max=20;
        if (count>max)
        {
            alert('Eesh, '+count+' is too many to email at a time! No more than '+max);        
        }
        var idList='';
        $(selected).each(function() 
        { 
            id=this.id.substring(12);
            if (idList!='') idList+=',';
            idList+=id;
        } );
        window.location='/bulkemail?idList='+idList;
    }
}
function exportSelectedCSV()
{
    exportSelected('/bulkexportcsv');
}
function exportSelectedRIS()
{
    exportSelected('/bulkexportris');
}
function exportSelected(action)
{
    var selected = $('#searchresults input:checked');
    var count=0;
    $(selected).each(function() { count++; });
    if (count==0)
        alert('Please select one or more results first');
    else
    {
        var max=100;
        if (count>max)
        {
            alert('Eesh, '+count+' is too many to export at a time! No more than '+max);        
        }
        var idList='';
        $(selected).each(function() 
        { 
            id=this.id.substring(12);
            if (idList!='') idList+=',';
            idList+=id;
        } );
        window.location=action+'?idList='+idList;
    }
}
function onChooseAction(dd)
{
    if (dd.selectedIndex>0)
    {
        eval(dd.options[dd.selectedIndex].value);
        dd.selectedIndex=0;
    }
}
function togglePreview( documentId, href )
{
    var searchString = $('#textField').val();
    $('<div class="iframe2"><iframe src="/preview/'+documentId+'?criteria='+escape(searchString)+'" width="100%" height="100%"></iframe></div>').dialog( { title:"<a href='/visit/"+documentId+"' target=_blank>Visit Original</a>", modal:true, width:"70%", height:600 } );

}
function toggleConclusion( documentId, href )
{
    var element = $('#conclusion'+documentId);
    if (element.is(':visible'))
        element.hide();
    else
        element.text('...').show().load(href);
}
function onLoadImage( imgElem )
{
    var img=new Image();
    img.onload = function() 
    {
         var width=img.width;
        var height=img.height;
        var max=(width>height)?width:height;
        var ideal=130;
        var factor=max/ideal;
        var newwidth=width/factor;
        var newheight=height/factor;
        imgElem.style.width=newwidth+'px';
        imgElem.style.height=newheight+'px';
        if (newwidth<ideal)
        {
            imgElem.style.paddingLeft=((ideal-newwidth)/2)+'px';
            imgElem.style.paddingRight=((ideal-newwidth)/2)+'px';
        }
        if (newheight<ideal)
        {
            imgElem.style.paddingTop=((ideal-newheight)/2)+'px'; 
            imgElem.style.paddingBottom=((ideal-newheight)/2)+'px'; 
        }       
       
    }
    img.src=imgElem.src;
}
function explain(element,documentId)
{
    $(element).load('/search/explain?documentId='+documentId);
}
function mouseOverCpd(element,isQandA)
{
/*
    var menu=$("#cpd_menu");
    var parent=$(element).parent();
    menu.remove();
    menu.appendTo(parent);
    menu.show();
    menu.mouseleave( function() { menu.hide(); } );
    $("#cpd_default").attr('href',element.href);
    var qanda=$("#cpd_qanda");
    qanda.attr('href',element.href+'?qanda=true');
    if (isQandA)
        qanda.removeClass('disabled');
    else
        qanda.addClass('disabled');
        
    return false;
    */
}
var selecteds=new Array(1);
function recalculateRelated(id)
{
    selecteds.push(id);
    $('#related p').load('/related/'+selecteds.join(',')+'?max=5');
}
function recordView( sender, id, url )
{
    if ($(sender).parents('#related').length==0)
        recalculateRelated(id);
    $.getJSON("http://query.yahooapis.com/v1/public/yql?"+  
                "q=select%20*%20from%20html%20where%20url%3D%22"+  
                encodeURIComponent(url)+  
                "%22&format=xml&callback=?",  
        function(data){  
          if(data.results[0]){  
             //if the url passes the test, navigate to it  
          } else {  
            $.get("/record404/"+id+'?url='+encodeURIComponent(url));  
          }  
        } ); 

    $.get("/record/"+id);
    var existing=$('#tiltattribution').val();
    if (existing=='')
        $('#tiltattribution').val(url);
    else
        $('#tiltattribution').val(existing+'\n'+url);
}
$('li.hifa a').live('mouseover',function() { $(this).attr('title',"Is this article suitable for the developing world? For definitions see the 'Developing World Initiative' link at the foot of this page. Also, follow the same link for a discussion of the term 'developing world'. We welcome your input"); return false; } );
function filterByPub(pubId)
{
    window.location='/search?publicationid='+pubId+'&criteria='+gup('criteria');
    return true;
}




