var records = Array();
function artists_gallery(data,year)
{
    data = data.replace(/\n/g,"");
    data = data.split("*");
    //alert(data); return;

    var fields = String(data[0]).split("|");

    for(var i in data) if(i>0)
    {
        var record_data = String(data[i]).split("|");
        if(record_data.length == fields.length)
        {
            var record = Array();
            //for(var a in fields) if(typeof(fields[a])=='string') alert(fields[a]);
            for(var a in fields) if(typeof(fields[a])=='string') record[fields[a]] = record_data[a];

            record['photos'] = String(record['photos']).split(",");

            records.push(record);
        }
    }

    artists_gallery_index(records,year);

    if(getUrlParam('artist')) artists_gallery_details(getUrlParam('artist'));

    return;
}

function artists_gallery_index(records,year)
{
    var res = "";

    for(var i in records) if(records[i]['photos'])
    {
        var record = records[i];
        var image = "/img/gallery-artists_"+year+"/100/"+record['photos'][0]+".png";

        var mover = "$(\"label"+i+"\").style.display=\"table\"";
        var mout = "$(\"label"+i+"\").style.display=\"none\"";

        var mclick = "document.location=\""+getBaseUrl()+"?m="+getUrlParam('m')+"&year="+year+"&artist="+i+"\"";

        res += "<div style='position: relative; float: left; border: 5px solid rgb(150, 179, 195); margin: 10px; cursor: pointer;' onmouseover='"+mover+"' onmouseout='"+mout+"' onclick="+mclick+">";

            // Obrazek
            res += "<div id='th"+i+"' style='background: white url("+image+") no-repeat center center; width:100px; height: 100px; float: left;'>"
            res += "</div>";

            // Opis
            res += "<table id='label"+i+"' style='width: 100px; height: 100px; background-color: white; position: absolute; left: 0; display: none;'>";
            res += "<tr><td style='text-align: center; vertical-align: middle; color: black;'>";
            res += String(record['name']).replace(/\//g,"<br>")+"<br>";
            res += "<hr style='border-top: solid 1px #a0a0a0; height: 0px;'/>";
            res += "<b>"+String(record['country'])+"</b>";
            res += "</td></tr>";
            res += "</table>";

        res += "</div>";
    }
    document.getElementById("artists_gallery_index_container").innerHTML = res;
    return;
}

function artists_gallery_details(record_no)
{
    var record = records[record_no];

    var artist_prev_no = Number(getUrlParam('artist')) - 1;
    var artist_next_no = Number(getUrlParam('artist')) + 1;
    if(artist_prev_no < 0 ) artist_prev_no = records.length-1;
    if(artist_next_no >= records.length ) artist_next_no = 0;

    nav = '<table cellspacing="0" align="center" style="width: 100%; border-bottom: solid 1px #96B1C4;"><tr>';
    nav += '<td style="text-align:left; white-space:nowrap;"><p><a href="'+getBaseUrl()+"?m="+getUrlParam('m')+'&year='+getUrlParam('year')+'&artist='+artist_prev_no+'">&laquo; Previous artist</a></p></td>';
    nav += '<td style="text-align:center; white-space:nowrap; width: 100%;"><p><a href="'+getBaseUrl()+"?m="+getUrlParam('m')+'">Back to index</a></p></td>';
    nav += '<td style="text-align:right; white-space:nowrap;"><p><a href="'+getBaseUrl()+"?m="+getUrlParam('m')+'&year='+getUrlParam('year')+'&artist='+artist_next_no+'">Next artist &raquo;</a></p></td>';
    nav += '</tr></table>';


    var res = '';

    res += nav;

    res += '<p style="font-size: 20px; line-height: 25px;"><b>'+record['name']+'</b><br/>'+record['country']+'</p>';
    if(String(record['winners']).length>0)
    {
        record['winners'] = String(record['winners']).substr(0,1)+'<sup>'+String(record['winners']).substr(1,2)+'</sup>';
        res += '<p style="font-size: 16px; margin: 0; padding-bottom: 2px;"><b>The Jury’s decision awarded</b></p>';
        res += '<p style="font-size: 24px; margin: 0; padding-bottom: 2px;"><b>'+record['winners']+'</b></p>';
        res += '<p style="font-size: 18px;"><b>Prize - '+record['prize']+'</b></p>';
    }
    if(String(record['special_mention']).length>0)
    {
        res += '<p style="font-size: 14px; margin: 0; padding-bottom: 2px;"><b>SPECIAL MENTION</b><br/>Chosen by</p>';
        res += '<p style="font-size: 16px; margin: 0; padding-bottom: 20px;"><b>'+record['special_mention']+'</b></p>';
    }

    res += '<table cellspacing="0" align="center"><tr>';
    for(var i in record['photos']) if(typeof(record['photos'][i])=='string')
    {
        var image = "/img/gallery-artists_"+getUrlParam('year')+"/150/"+record['photos'][i]+".png";

        var title = record['name'];
        if(record['technique_materials']) title += " - "+record['technique_materials'];

        res += '<td>';
        res += '<a title="'+title+'" rel="clearbox[gallery=Photos]" href="/img/gallery-artists_'+getUrlParam('year')+'/440/'+record['photos'][i]+'.png">';

        //res += '<img style="width: 150px; height: 150px; border: 5px solid rgb(150, 179, 195); margin: 5px;" src="/img/gallery-artists_'+getUrlParam('year')+'/150/'+record['photos'][i]+'.png">';

        res += "<div style='background: white url("+image+") no-repeat center center; width:150px; height: 150px; float: left; border: 5px solid rgb(150, 179, 195); margin: 10px'>"
        res += "</div>";

        res += '</a>';
        res += '</td>';
    }
    res += '</tr></table>';

    if(record['technique_materials']) res += '<p><b>Technique and materials:</b><br/>'+record['technique_materials']+'</p>';
    if(record['dimensions']) res += '<p><b>Dimensions:</b><br/>'+record['dimensions']+'</p>';

    document.getElementById("artists_gallery_index_container").style.display = 'none';
    document.getElementById('artists_gallery_details_container').innerHTML = res;
}

function getUrlParam(name)
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

function getBaseUrl()
{
    var url = window.location.href;
    url = url.substr(0,url.lastIndexOf("/"));
    return url;
}

