(function($) { $(function() {
    var maxheight = 0;      

    $('.subsection ul').each(function(i){
       if($(this).height()>maxheight)
          maxheight = $(this).height();
    });  
       
    $('.subsection ul').each(function(i){
        $(this).height(maxheight);
    }); 

    $('#issuu-links-add-a').click(function() {
        $('#issuu-new').addClass('yeah');
        $(this).hide();
        return false;
    });

    $('.issuu-selected-lang').live('click', function() {
        $(this).next().toggleClass('yeah');
    });

    $('.issuu-links-cancel').live('click', function() {
        if ( $(this).parent().attr('id') == 'issuu-new' ) {
            $(this).parent().removeClass('yeah');
            $('#issuu-links-add-a').removeAttr('style');
        } else {
            $(this).parent().prev().removeAttr('style');
            $(this).parent().remove();
        }
        
        return false;
    });

    $('.issuu-flag').live('click', function() {
        $(this).parent().parent().prev().attr('src', $(this).find('img').first().attr('src'));
        $(this).parent().parent().prev().prev().val($(this).attr('href').substr(11));
        $(this).parent().parent().removeClass('yeah');
        return false;
    });

    $('.issuu-save').live('click', function() {
        $.ajax({
            context:$(this).parent(),
            data:{
                action:'saveIssuuLink', 
                linkId:$(this).parent().find('input[name="issuu-id"]').val(),
                linkUrl:$(this).parent().find('input[name="issuu-url"]').val(),
                linkLang:$(this).parent().find('input[name="issuu-lang"]').val()
            },
            dataType:'json',
            success:function(data, status, req) {
                // Hide DIV.issuu-edit and update previous P
                $(this).prev().find('img').attr('src', "/images/flags/" + data.lang + ".gif").attr('alt', data.lang);
                $(this).prev().find('span').html(data.url);
                $(this).prev().show();
                $(this).remove();
            },
            type:'POST',
            url:'issuu.php'
        });

        return false;
    });

    $('#issuu-add').live('click', function() {
        $.ajax({
            context:$(this).parent(),
            data:{
                action:'addIssuuLink', 
                linkUrl:$(this).parent().find('input[name="issuu-url"]').val(),
                linkLang:$(this).parent().find('input[name="issuu-lang"]').val(),
                linkFileId:$(this).parent().find('input[name="issuu-file-id"]').val()
            },
            dataType:'json',
            success:function(data, status, req) {
                // Hide DIV#issuu-new and apprend new P
                $(this).before('<p title="Edit this link" class="issuu-desc" id="issuu-link-'+ data.id +'">\
                    <img src="/images/flags/'+ data.lang +'.gif" alt="'+ data.lang +'" /> <span>'+ data.url +'</span>\
                </p>');
                $(this).removeClass('yeah');
                $('#issuu-links-add-a').removeAttr('style');
            },
            type:'POST',
            url:'issuu.php'
        });

        return false;
    });

    $('.issuu-desc').live('click', function() {
        $(this).hide();
        $(this).after('\
            <div class="issuu-edit">\
                <input type="hidden" name="issuu-id" value="'+ $(this).attr('id').substr(11) +'" />\
                <input type="hidden" name="issuu-lang" value="'+ $('img', this).attr('alt') +'" />\
                <img class="issuu-selected-lang" src="/images/flags/'+ $('img', this).attr('alt') +'.gif" />\
                <ul>\
                    <li><a href="?issu-lang=cz" class="issuu-flag"><img src="/images/flags/cz.gif" alt="" /> Czech</a></li>\
                    <li><a href="?issu-lang=nl" class="issuu-flag"><img src="/images/flags/nl.gif" alt="" /> Dutch</a></li>\
                    <li><a href="?issu-lang=en" class="issuu-flag"><img src="/images/flags/gb.gif" alt="" /> English</a></li>\
                    <li><a href="?issu-lang=fr" class="issuu-flag"><img src="/images/flags/fr.gif" alt="" /> French</a></li>\
                    <li><a href="?issu-lang=de" class="issuu-flag"><img src="/images/flags/de.gif" alt="" /> German</a></li>\
                    <li><a href="?issu-lang=it" class="issuu-flag"><img src="/images/flags/it.gif" alt="" /> Italian</a></li>\
                    <li><a href="?issu-lang=pl" class="issuu-flag"><img src="/images/flags/pl.gif" alt="" /> Polish</a></li>\
                    <li><a href="?issu-lang=pt" class="issuu-flag"><img src="/images/flags/pt.gif" alt="" /> Portugese</a></li>\
                    <li><a href="?issu-lang=ru" class="issuu-flag"><img src="/images/flags/ru.gif" alt="" /> Russian</a></li>\
                    <li><a href="?issu-lang=es" class="issuu-flag"><img src="/images/flags/es.gif" alt="" /> Spanish</a></li>\
                </ul>\
                <input type="text" name="issuu-url" value="'+ $('span', this).text() +'" />\
                <input type="submit" name="issuu-save" value="Save" class="issuu-save" />\
                or <a href="#" class="issuu-links-cancel">Cancel</a>\
                <br><a href="?issu-link-id='+ $(this).attr('id').substr(11) +'" class="issuu-delete">Delete this link</a>\
            </div>');
    });

    $('.issuu-delete').live('click', function() {
       $.ajax({
            context:$(this).parent(),
            data:{
                action:'deleteIssuuLink', 
                linkId:$(this).attr('href').substring(14)
            },
            success:function(data, status, req) {
                // Hide DIV#issuu-new and apprend new P
                $(this).fadeOut('fast', function() { $(this).prev().remove(); $(this).remove() });
            },
            type:'POST',
            url:'issuu.php'
        });

        return false;
    });

    if( !$.support.changeBubbles ) {
        $('#downloads tbody:nth-child(even)').addClass('ie');
        $('#downloads tr:first-child td:not(:first-child)').addClass('ie');
        $('#downloads tr:last-child').addClass('ie');
    }
})})(jQuery);

function display_type_form(type)
{                     
  document.getElementById('cat_type').value=type;      
  switch(type)
  {
    case 'certificates':
           document.getElementById("certificates_box").style.display='block';           
           document.getElementById("col_label_type").innerHTML='Languages';           
           $("select.file_format").hide(); 
           $("select.file_lang").show();   
    break;
    case 'images':    
           document.getElementById("certificates_box").style.display='none';         
           document.getElementById("col_label_type").innerHTML='Format';               
           $("select.file_format").show(); 
           $("select.file_lang").hide();   
    break;
    default:
           document.getElementById("certificates_box").style.display='none';         
           document.getElementById("col_label_type").innerHTML='Languages';   
           $("select.file_format").hide(); 
           $("select.file_lang").show();            
  }         
}
             
function plier_deplier(id)
{       
    if(document.getElementById(id))
    {  
        if(document.getElementById(id).style.display=='block')
            document.getElementById(id).style.display='none';
        else
            document.getElementById(id).style.display='block';
    }  
}

function changeFile()
{
    if(document.getElementById('docIsChanged').value=='n')
    {                                                          
        document.getElementById('doc').style.display='block';        
        document.getElementById('docpath').style.display='none';        
        document.getElementById('display_doc').style.display='none';  
        document.getElementById('delete_'+label).style.display='none';
        document.getElementById('hidden_doc').style.display='block';
        document.getElementById('docIsChanged').value='y';
    }
    else
    {
        document.getElementById('doc').style.display='none';
        document.getElementById('docpath').style.display='block';
        document.getElementById('display_doc').style.display='block';
        document.getElementById('delete_'+label).style.display='block';
        document.getElementById('hidden_doc').style.display='none';  
        document.getElementById('docIsChanged').value='n';
    }
} 

function changeFile2(label)
{
    if(document.getElementById(label+'IsChanged').value=='n')
    {                                                          
        document.getElementById(label).style.display='inline';        
        document.getElementById(label+'path').style.display='none';        
        document.getElementById('display_'+label).style.display='none';  
        document.getElementById('delete_'+label).style.display='none';  
        document.getElementById('hidden_'+label).style.display='inline';
        document.getElementById(label+'IsChanged').value='y';
    }
    else
    {
        document.getElementById(label).style.display='none';
        document.getElementById(label+'path').style.display='inline';
        document.getElementById('display_'+label).style.display='inline';
        document.getElementById('delete_'+label).style.display='inline';
        document.getElementById('hidden_'+label).style.display='none';  
        document.getElementById(label+'IsChanged').value='n';
    }
}    

var nbFiles=0; 

function edit_file(id, visibleid, i)
{
    if(document.getElementById(visibleid).style.display=='none')
    {
        document.getElementById(id).style.display='none';
        document.getElementById(visibleid).style.display='inline';
        
        document.getElementById('editimg'+i).style.display='none';
        document.getElementById('cancelimg'+i).style.display='block';                 
        
        document.getElementById('input'+id).name='d'+id;   
        document.getElementById('input'+visibleid).name='files_upload'+nbFiles; 
        nbFiles++;
    }  
    else
    {      
        document.getElementById(id).style.display='inline';
        document.getElementById(visibleid).style.display='none';       
        
        document.getElementById('editimg'+i).style.display='block';
        document.getElementById('cancelimg'+i).style.display='none';
        
        document.getElementById('input'+id).name='old_files[]';   
        document.getElementById('input'+visibleid).name=visibleid+'file'; 
        nbFiles--;
    }
}

function add_file()
{      
    to=document.getElementById('files_list_body');                               
                                            
        var tr=document.createElement('tr');
                                         
            var th=document.createElement('th');
                //th.className='textcell';
                var thtext= document.createTextNode('image > ');
            th.appendChild(thtext);
            
            var td1=document.createElement('td');
            //td1.className='imagecell';
                var inputFile=document.createElement('input');
                inputFile.setAttribute("type","file"); 
                inputFile.setAttribute("name","files_upload"+nbFiles);
            td1.appendChild(inputFile);
            
            var td=document.createElement('td');
            td.className='actioncell';
                var a=document.createElement('a');
                a.setAttribute("href","#"); 
                    var img=document.createElement('img');
                    img.setAttribute("src","../images/delete.gif");                                        
                a.appendChild(img);                   
                a.onclick=function(){delete_file(a,'-1');return false;} 
            td.appendChild(a);
                             
        tr.appendChild(th);
        tr.appendChild(td1);
        tr.appendChild(td);
        
    to.appendChild(tr);
    nbFiles++;
}
                                                                                   
function delete_file(elt,path)
{   
    nbFiles--;
    if(nbFiles<5) document.getElementById('addimage').style.display='inline';                                                       
    elt.parentNode.parentNode.parentNode.removeChild(elt.parentNode.parentNode);       
}

var lang_options=new Array();


function add_file_admin()
{        
    var type=document.getElementById('cat_type').value;
    to=document.getElementById('files_list_body');                               
                                            
        var tr=document.createElement('tr');
                                     
            // input
            var td1=document.createElement('td');
            var inputFile=document.createElement('input');
            inputFile.setAttribute("type","file"); 
            inputFile.setAttribute("name","files_upload[]");
            inputFile.style.width = "450px";  
            td1.appendChild(inputFile);
            
            // select
            var tdlang=document.createElement('td');
             
            var selectlang=document.createElement('select');   
            selectlang.setAttribute("name","files_upload_lang[]");
            selectlang.setAttribute("class","file_lang"); 
            selectlang.style.display='none';       
            
            for(id in lang_options)
            {               
                var option=document.createElement('option'); 
                var optiontext = document.createTextNode(lang_options[id]);  
                option.setAttribute("value",id);                           
                option.appendChild(optiontext);         
                selectlang.appendChild(option); 
            }
                                               
                 
            var select=document.createElement('select');   
            select.setAttribute("name","file_format[]");  
            select.setAttribute("class","file_format");  
            select.style.display='none';     
                                                                
                var optionXL=document.createElement('option'); 
                var optionXLtext = document.createTextNode('XL'); 
                optionXL.setAttribute("value",'XL');                           
                optionXL.appendChild(optionXLtext);  
                
                var optionH=document.createElement('option'); 
                var optionHtext = document.createTextNode('H'); 
                optionH.setAttribute("value",'H');                           
                optionH.appendChild(optionHtext);  
                
                var optionL=document.createElement('option'); 
                var optionLtext = document.createTextNode('L'); 
                optionL.setAttribute("value",'L');                           
                optionL.appendChild(optionLtext);  
                
                var optionThumb=document.createElement('option'); 
                var optionThumbtext = document.createTextNode('THUMB');  
                optionThumb.setAttribute("value",'THUMB');                           
                optionThumb.appendChild(optionThumbtext);  
                                 
                select.appendChild(optionXL); 
                select.appendChild(optionH); 
                select.appendChild(optionL); 
                select.appendChild(optionThumb);       
            
            switch(type)
            {
              case 'images':      
                select.style.display='block';
              break;
              default:
                selectlang.style.display='block';
            }
              
            tdlang.appendChild(selectlang);                                       
            tdlang.appendChild(select); 
            
            // actions
            var td2=document.createElement('td');
            td2.className='actions';
            var a=document.createElement('a');
            a.setAttribute("href","#"); 
            var img=document.createElement('img');
            img.setAttribute("src","../../../../admin/images/delete_icon.gif");                                        
            img.setAttribute("title","delete");   
            a.appendChild(img);                   
            
            a.onclick=function(){delete_file(a,'-1');return false;} 
            td2.appendChild(a);
                             
        tr.appendChild(td1);
        tr.appendChild(tdlang);
        tr.appendChild(td2);
        
    to.appendChild(tr);
    nbFiles++;
}

function delete_file_admin(elt,path, file_id)
{   
    nbFiles--;
    elt.parentNode.parentNode.parentNode.removeChild(elt.parentNode.parentNode); 
    
    // hidden input
    var inputFile=document.createElement('input');   
    inputFile.setAttribute("type", "hidden");
    inputFile.setAttribute("value", file_id);
    inputFile.setAttribute("name", "files_remove[]");
    to=document.getElementById('files_list_body');     
    to.appendChild(inputFile);   
}

function edit_file_admin(id, visibleid, i)
{
    if(document.getElementById(visibleid).style.display=='none')
    {
        document.getElementById(id).style.display='none';
        document.getElementById(visibleid).style.display='inline';
        
        document.getElementById('editimg'+i).style.display='none';
        document.getElementById('cancelimg'+i).style.display='block';                 
        
        document.getElementById('input'+id).name='d'+id;   
        document.getElementById('input'+visibleid).name='files_upload[]'; 
        document.getElementById('old_files_lang'+i).name='files_upload_lang[]';           
        document.getElementById('old_files_format'+i).name='file_format[]';           
        
        // hidden input
        var inputFile=document.createElement('input');   
        inputFile.setAttribute("type", "hidden");
        inputFile.setAttribute("value", i);
        inputFile.setAttribute("id", "files_update_"+i); 
        inputFile.setAttribute("name", "files_remove[]");  
        to=document.getElementById('files_list_body');     
        to.appendChild(inputFile); 
        
        nbFiles++;
    }  
    else
    {      
        document.getElementById(id).style.display='inline';
        document.getElementById(visibleid).style.display='none';       
        
        document.getElementById('editimg'+i).style.display='block';
        document.getElementById('cancelimg'+i).style.display='none';
        
        document.getElementById('input'+id).name='old_files[]';   
        document.getElementById('old_files_lang'+i).name='old_files_lang[]';          
        document.getElementById('old_files_format'+i).name='old_file_format[]';  
        document.getElementById('input'+visibleid).name=visibleid+'file'; 
        
        // removing hidden input
        to=document.getElementById("files_update_"+i);     
        to.parentNode.removeChild(to);
        
        nbFiles--;
    }
}

function addAllLang(elts)
{   
    var l=new Array();           
    if(elts.type=='select-one')
    {     
        l.push(elts.value);
    }
    else    
    {     
        for(var i=0;i<elts.length;i++)                       
            l.push(elts[i].value);   
    }
    return l;  
}

function verif(type)
{   
    switch(type)
    {
      case 'images':
          var tab_format=new Array();
          for(id in $("select.file_format").get())
          {
             if(tab_format[$("select.file_format").get(id).value])
             {
              alert('Only one image per format please.');
              return false;
             }
             tab_format[$("select.file_format").get(id).value]="ok";     
          }
          
      break;
      default:
        var lang_selected=new Array();      
        if(document.forms['form'].elements['old_files_lang[]'])                                                                 
        {    
            lang_1 = addAllLang(document.forms['form'].elements['old_files_lang[]']);
            lang_selected=lang_1;   
        }
        
        if(document.forms['form'].elements['files_upload_lang[]'])
        {    
            lang_2 = addAllLang(document.forms['form'].elements['files_upload_lang[]']);
            lang_selected=lang_selected.concat(lang_2);    
        }
        
        var tab_lang=new Array();
        for(id in lang_selected)
        {
            if(tab_lang[lang_selected[id]])
            {
                alert('Only one file per language please.');
                return false;
            }
            tab_lang[lang_selected[id]]='ok';  
        } 
    }
    return true;                            
}

