doAdsShuffle = true;

//
// Displays the links.
//
function DisplayLinks(links, type)
{
    var str = '';
    var linkId;
    var count;
    var url;
    var href;
    var text; 
    var ext;
    var isMedia; 
    var divIdName = 'List'+type;
    var isInnerUrl;
    var target;
    
    //
    // Display only three first items, in 
    // case of ads an non-admin.
    //
    var numItems = links.length;
    
    if (!isAdmin && (type == 'Add'))
    {
        numItems = Math.min(1, numItems);
    }

    //
    // Go over the links.
    //
    for (i=0; i < numItems; i++)
    {
        //
        // Parse link data.
        //
        linkId = links[i][0];
        count = links[i][1];
        url = links[i][2];
        text = links[i][3]; 
        ext = links[i][4];                
        isMedia = ((ext == 'jpg') || (ext == 'gif') || (ext == 'png') || (ext == 'swf'));
        isInnerUrl = (url.match(siteUrl) != null);

        //
        // Append URL.
        //
        if (isInnerUrl && isSwMode)
        {
            url = AppendUrl(url);
        }
    
        //
        // Set display body, based on link text 
        // and (if exists) file.
        //
        if ((ext == 'jpg') || (ext == 'gif') || (ext == 'png'))
        {
            display = '<div class="link-image handle"><img src="'+JS_LINKS_WLIB+linkId+'.'+ext+'" width="'+linkWidth+'" height="'+linkHeight+'" alt="'+text+'" title="'+text+'" /></div>';
        }
        
        else if (ext =='swf')
        {
            fileUrl = JS_LINKS_WLIB+linkId+'.swf';
            
            display = "<div class='link-image handle'>"+
                "<object "+
                "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " +
                "width=\""+linkWidth+"\" "+
                "height=\""+linkHeight+"\" " +
                "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,40,0\">"+
                " <param name=movie value=\""+fileUrl+"\">" +
                " <param name=play value=\"true\">" +
                " <param name=loop value=\"true\">" +
                " <param name=WMode value=\"Opaque\">" +
                " <param name=quality value=\"high\">" +
                " <param name=bgcolor value=\"white\">" +
                " <param name=align value=\"L\">\n" +
                "<embed src=\""+fileUrl+"\" width=\""+linkWidth+"\" height=\""+linkHeight+"\" play=\"true\" loop=\"true\" wmode=\"Opaque\" quality=\"high\" bgcolor=\"white\" align=\"L\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>\n"+
                "</object>" +
                "</div>";
        }
        
        else
        {
            display = text;
        }
        
        //
        // Set display. Case admin.
        //
        if (isAdmin)
        {
            //
            // Set delete icon.
            //
            delQtn = (type == 'Link') ? jsld["should I delete the link"] : jsld["should I delete the add"];
            delIcon = '<a href="javascript:if(confirm(\''+delQtn+' '+text+'\')){DelLink('+linkId+', \''+type+'\')}"><img class="icon" src="'+JS_SYSTEM_IMAGES_WLIB+'trashcan.gif" title="'+jsld["delete"]+'" /></a>';
            
            //
            // Build display LI.
            //
            if (isMedia)
            {
                str += '<li id="'+linkId+'">';
                str += '<p>';
                str += display + delIcon+' ';
                str += '<a href="'+url+'"><span class="link-image-text handle">'+text+'</span></a>';
                str += '</p>';
                str += '</li>';
                
            }
            
            else
            {                                                            
                str += '<li id="'+linkId+'"><p class="handle">'+delIcon+'<a href="'+url+'">'+display+'</a></p></li>';
            }            
        }
        
        //
        // Set display. Case non-admin.
        //
        else
        {
            target = (isInnerUrl) ? '' : 'target="_blank"';

            if (isMedia)
            {
                display += '<div class="link-image-text">'+text+'</div>';
                str += '<li><a href="'+url+'" '+target+'>'+display+'</a></li>';
            }
            else
            {
                str += '<p><a href="'+url+'" '+target+'>'+display+'</a></p>';
            }
                                                               
        }
    }

    //
    // Display the links.
    //
    if ( ! isAdmin && (type == 'Add'))
    {
        if (doAdsShuffle)
        {
            $("#"+divIdName).hide(400);                
            setTimeout("$('#ListAdd').html('"+str+"');", 390);
            $("#"+divIdName).show(400);
        }
    }
    
    else
    {
        $("#"+divIdName).html(str);
    }

    //
    // Enable drag and drop in case of admin.
    //
    if (isAdmin && ((type == 'Link') || (type == 'Add')))
    {
        $("#"+divIdName).sortable(
        { 
            handle : '.handle', 
            update : function () 
            { 
                var order = $("#"+divIdName).sortable('toArray'); 
                xajax_ajax_update_links_order(order);
            } 
        }); 
    } 
}

function SortLinksByCountAsc(x,y)
{
    cx = x[1];
    cy = y[1];
    return (cx - cy);
}

// Display Adds in a random order, while redisplaying every 10 seconds.
function DisplayAdds()
{
    adds = shuffle(adds);

    DisplayLinks(adds, "Add");
    
    if (!isAdmin && doAdsShuffle && adds.length > 1)
    {
        t = setTimeout("DisplayAdds()", 2 * JS_ADDS_TIME_GAP * 1000);
    }
}