$(document).ready(function() {
  Galleria.loadTheme('/javascripts/themes/classic/galleria.classic.min.js');
  $('#gallery').galleria({
    width: 540,
    height: 400
  });
  
  $(".anchor").click(function() {
  	var anchor = $(this).attr('href').replace('#', '');
  	var offset = $('a#'+anchor).offset().top;
  	// if($.browser.msie && $.browser.version=="7.0") {
  	//     } 
  	//     else {
  	offset = offset - 165
  	// }
  	$('html, body').animate({
      scrollTop: offset
    });
  	return false;
  });
  
  $(".back-to-top").click(function() {
  	var anchor = $(this).attr('href').replace('#', '');
  	var offset = $('#'+anchor).offset().top;
  	if($.browser.msie && $.browser.version=="7.0") {
  	} 
  	else {
  	  offset = offset - 165
  	}
  	$('html, body').animate({
      scrollTop: offset
    });
  	return false;
  });
  
  $('#search-projects').click(function() {
    value = $('#searchform').val();
    window.location = "/project-ideas/project-ideas?search="+value;
  });
  
  $(".moreinfo").click(function() {
    $('#show-comments-'+this.id).hide();
  	$('#show-info-'+this.id).toggle();		
  	return false;
  });
  
  $(".comment").click(function() {
    $('#show-info-'+this.id).hide();
  	$('#show-comments-'+this.id).toggle();		
  	return false;
  });
  
  $("#faq .question").each(function() {
    $(this).next().hide();
    spanObj = $(this).children()[0];
    $(spanObj).attr('class', 'collapsed');
    $(spanObj).html('+');
  })
  
  $("#faq .question").click(function() {
    $(this).next().toggle();
    spanObj = $(this).children()[0];
    if ($(spanObj).attr('class') == 'expanded') {
      $(spanObj).attr('class', 'collapsed');
      $(spanObj).html('+');
    } else {
      $(spanObj).attr('class', 'expanded');
      $(spanObj).html('–');
    }
  })
  
  $('.toggle').each(function() {
    $(this).next().hide();
    spanObj = $(this).children()[0];
    $(spanObj).attr('class', 'collapsed');
    $(spanObj).html('+');
  })
  
  $(".toggle").click(function() {
    $(this).next().slideToggle('fast');
    spanObj = $(this).children()[0];
    if ($(spanObj).attr('class') == 'expanded') {
      $(spanObj).attr('class', 'collapsed');
      $(spanObj).html('+');
    } else {
      $(spanObj).attr('class', 'expanded');
      $(spanObj).html('–');
    }
    return false;
  });
  
});


var DocumentRepository = {
  createEditForm: function(id, page_id) {
    
    var lang = $('html').attr('lang');
    var documentName = "";
    var editFile = "";
    var description_name = "";
    var updateLink = "";
    
    if (lang == 'fr') {
      documentName = 'Nom du document';
      editFile = 'Editer';
      description_name = 'Description';
      updateLink = 'Mettre à jour';
    } else if (lang == 'nl') {
      documentName = 'Naam van het document';
      editFile = 'Aanpassen';
      description_name = 'Beschrijving';
      updateLink = 'Aanpassen';
    } else {
      documentName = 'Document name';
      editFile = 'Edit file';
      description_name = 'Description';
      updateLink = 'Update';
    }
    
    var name = $('.'+id+'-name').html();
    var csrf = $('#csrf-token').html();
    var description = $('.'+id+'-description').html().replace(/(<([^>]+)>)/ig,"");
    var form = "<tr id='show-edit-attachment-"+id+"' class='more-info-box'><td colspan=5>";
    form += "<form id='edit-attachment-"+id+"' action='/attachments/"+id+"' enctype='multipart/form-data' method='post'>";
    form += '<span style="margin:0;padding:0;display:inline"><input type="hidden" name="attachment[page_id]" value="'+page_id+'" /><input type="hidden" value="put" name="_method"><input type="hidden" value="'+csrf+'" name="_csrf_token"></span>';
    form += "<table class='doc'>";
    form += '<tr><td class="label">'+documentName+'</td><td class="doc"><input type="text" id="attachment_name" name="attachment[name]" value="'+name+'" /></td></tr>';
    form += '<tr><td class="label">'+editFile+'</td><td class="doc"><input type="file" id="attachment_file" name="attachment[file]" /></td></tr>';
    form += '<tr><td class="label">'+description_name+'</td><td class="doc"><textarea id="attachment_description" name="attachment[description]">'+description+'</textarea></td></tr>';
    form += "</table><button id='submit-"+id+"'>"+updateLink+"  <img src='/images/icons/arrow.png' /></button></form></td></tr>";
    $('#show-info-'+id).after(form);
    $('#edit-attachment-link-'+id).hide();
  }
}

