// Graaph.arq Software
// (C) 2008, 2009
// http://graaph.arq.br

// 2008.11.16 - silvio almeida - inicio
// 2009.01.08 - silvio almeida - VERSION 1.0
// 2009.01.27 - silvio almeida - menus: mudança de idioma

var DEBUG = 0;

var menu_en = {
  'home': 'Home',
  'important_dates': 'Important Dates',
  'local_committee': 'Local Organizing Committee',
  'national_committee': 'National Organizing Committee',
  'international_committee': 'International Scientific Committee',
  'scientific_programme': 'Scientific Programme',
  'complete_programme': 'Complete Scientific Programme',
  'pre_congress': 'Pre-Congress Activities',
  'satellitemeeting': 'Satellite Meeting on T. cruzi nomenclature',
  'vazio': '',
  'pre_meeting_courses': 'Pre-meeting courses',
  'course_I': 'Course I',
  'course_II': 'Course II',
  'plenary_conferences': 'Plenary Lectures',
  'symposia': 'Symposia',
  'symposiumTcruzi': 'Special symposium on <i>Trypanosoma cruzi</i>',
  'mini_conferences': 'Mini-conferences',
  'platform_and_poster_sessions': 'Platform and Poster Sessions',
  'high_studies': 'School of High Studies',
  'poster': 'Poster',
  'registration': 'Registration',
//   'abstract_guidelines': 'Abstract Guidelines',
  'abstract_submission': 'Restricted Area/Abstract Guidelines',
  'support_conference': 'Support for Conference Attendees',
  'other_congress': 'Others Congress and Meetings',
  'travel_lodging': 'Travel/Lodging',
  'contact_us': 'Contact Us'
  

};

var menu_pt = {
  'home': 'Home',
  'important_dates': 'Datas Importantes',
  'local_committee': 'Comitê Executivo Nacional',
  'national_committee': 'Comitê Científico Nacional',
  'international_committee': 'Comitê Científico Internacional',
  'scientific_programme': 'Programa Científico',
  'complete_programme': 'Programa Científico Completo',
  'pre_congress': 'Atividades Pré-Congresso',
  'satellitemeeting': 'Satellite Meeting on T. cruzi nomenclature',
  'vazio': '',
  'pre_meeting_courses': 'Curso Pré-Congresso',
  'course_I': 'Course I',
  'course_II': 'Course II',
  'plenary_conferences': 'Conferências Plenárias',
  'symposia': 'Simpósios',
  'symposiumTcruzi': 'Simpósio especial sobre <i>Trypanosoma Cruzi</i>',
  'mini_conferences': 'Mini-conferences',
  'platform_and_poster_sessions': 'Plataformas e Painéis', 
  'high_studies': 'Escola de Altos Estudos',
  'poster': 'Cartaz',
  'registration': 'Inscrições',
//   'abstract_guidelines': 'Instruções para Participantes',
  'abstract_submission': 'Área Restrita/Instruções',
  'support_conference': 'Support for Conference Attendees',
  'travel_lodging': 'Hospedagem',
  'other_congress': 'Outros Congressos e Reuniões',
  'contact_us': 'Contato'
};

var lang = 'en';
var page = 'home';

function doLang(language, menu_base) {
  lang = language;
  var menu_texts = (lang == 'en' ? menu_en : (lang == 'pt' ? menu_pt : undefined));
  var menu_id = menu_base == undefined ? 'menu_base' : menu_base;
  $('#' + menu_id).children().each( function(i){
      var child_text = undefined;
      var child_menu = undefined;
      for (var i = 0; i<this.childNodes.length; i++) {
        var child = this.childNodes.item(i);
        if ((child.nodeType == this.ELEMENT_NODE || child.nodeType == 1) && child.nodeName.toUpperCase() == 'SPAN') {
           child_text = child;
        } else if ((child.nodeType == this.ELEMENT_NODE || child.nodeType == 1) && child.nodeName.toUpperCase() == 'UL') {
            child_menu = child;
      } }
      if (child_text != undefined && child_text.childNodes.length > 0) {
        child_text.innerHTML = menu_texts[this.id.substr(2)];
      }
      if (child_menu != undefined) {
        doLang(lang, child_menu.id);
  } } );
  if (menu_id == 'menu_base') {
    $('#m_' + page).trigger('click');
    $('#header img').attr('src', 'img/header_' + lang + '.png');
    $('#footer img').attr('src', 'img/footer_' + lang + '.png');
  }
}


function frame_ready() {

  $('a').css('color', '#4a4262');
  $('p').css('text-align', 'justify');
  $('h1').css('font-size', '1.22em');
  $('h2').css('font-size', '1.18em');
  $('h3').css('font-size', '1.08em');
  $('h1').add('h2').add('h3').add('h4').css('font-weight', 'bold');

  $('input [type=radio], input [type=text], textarea').css('width', '200px');
  $('input [type=submit]').css('text-weight', 'bold').css('background-color', '#c7ccdc').css('width', '80px');
  $('input [type=submit]').hover(
    function(e){ $(this).css('cursor', 'pointer'); },
    function(e){ $(this).css('cursor', 'default'); }
  );

  $('.center').css('text-align', 'center');
  $('.right').css('text-align', 'right');
  $('.top').css('vertical-align', 'top');

  $('.column_box').css('width', '570px');
  $('.column_left').css('width', '270px').css('text-align', 'left').attr('valign', 'bottom');
  $('.column_right').css('width', '270px').css('text-align', 'left').attr('valign', 'bottom');

  $('ul.lined_item li').css('font-size', '0.9em').css('line-height', '1.1em');
  $('ol.no_number, ol.no_number li ul').css('list-style-type', 'none');

  $('div.iframe').css('width', '550px').css('border-width', '0px').css('padding', '0px 0px').css('margin', '4px 28px 7px auto').css('display', 'block');

  $('div.message').css('overflow', 'auto').css('font-weight', 'bold').css('font-size', '1.1em').css('text-align', 'center').css('margin', '10px').css('padding', '4px').css('background-color', '#c7ccdc').hide();

  $('#alert').ajaxError(function(event, request, settings) {
    if(DEBUG) $(this).show().append("<li>Error requesting page " + settings.url + "</li>");
  });

  $('form').ajaxForm({ target:'#form_result', beforeSubmit:check_data, success:show_result, cache:false });

}


function showDiv(id) { $('#' + id).toggle(); }

function form_input_error() {
  alert((lang == 'pt' ? 'Dados incompletos ou incorretos.' : 'Missing or incorrect info.'));
  return false;
}

function check_data(data_arr, form_obj, opts) {
  for (i=0; i<data_arr.length; i++) {
    a=data_arr[i].value;
    data_arr[i].value = encodeURIComponent(a);
    b=data_arr[i].value;
  }
  var form = form_obj[0];
  var id = form_obj.attr('id');
  window.bad_data = 0;
  if (id == 'form_intention') {
    $.each(data_arr, function() {
      switch (this.name) {
        case "name":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "institution":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "title":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "abstract":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "letter":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "city":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "country":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "email":
          if (this.value.length == 0 || ! /^[-\w.]+%40[-\w.]+$/.test(this.value)) window.bad_data++;
          break;
        default:
          break;
      }
    });
  } else if (id == 'form_contact') {
    $.each(data_arr, function() {
      switch (this.name) {
        case "name":
          if (this.value.length == 0) window.bad_data++;
          break;
        case "message":
          if (this.value.length == 0 || this.value.length > 1000) {
            if (this.value.length > 1000) alert('1000 chars max!');
            window.bad_data++;
          }
          break;
        case "email":
          if (this.value.length == 0 || ! /^[-\w.]+@[-\w.]+$/.test(this.value)) window.bad_data++;
          break;
        default:
          break;
      }
    });
  }
  if (window.bad_data > 0) {
    return form_input_error();
  } else {
    $('input[name=hash]').attr('value', (new Date()).getTime());
    $('input[type=submit]').hide();
    $('#form_container').hide();
    $('#form_result').show().html('please wait...');
    return true;
  }
}

function show_result(data, status) {
  $('#form_container').hide();
  $('#form_result').show();
  if ($('#form_result').html().length > 0 && ! /Erro/.test($('#form_result').html())) {
    $('form').resetForm();
  }
  $('input[type=submit]').show();
}



function document_ready() {

  $('body *').css('color', '#4a4262')
             .css('font-family', 'trebuchet, trebuchet ms, arial, sans-serif').css('font-weight', 'normal').css('font-size', '11px');

  $('a').css('color', '#4a4262');
  $('p').css('text-align', 'justify');
  $('h1').css('font-size', '1.22em');
  $('h2').css('font-size', '1.18em');
  $('h3').css('font-size', '1.08em');
  $('h1').add('h2').add('h3').add('h4').css('font-weight', 'bold');

  $('.center').css('text-align', 'center');
  $('.right').css('text-align', 'right');
  $('.top').css('vertical-align', 'top');

  $('div#bounding').css('width', '832px').css('margin-left', '-416px').css('padding-left', '50%');

  $('div#header').css('margin', '9px 0px 0px 0px');
  $('area').filter('.lang').click(
    function(e){
      doLang($(this).attr('id').substr(2));
    })
    .hover(
      function(e){ $(this).css('cursor', 'pointer'); },
      function(e){ $(this).css('cursor', 'default'); }
    );

  $('div#photo').css('margin', '4px 0px 0px 0px');

  $('div#content').css('display', 'block').css('float', 'right').css('overflow', 'auto').css('position', 'relative')
                  .css('width', '606px').css('height', '376px').css('margin', '4px 0px').css('padding', '0px')
                  .css('background-color', '#b5bbce').css('list-style', 'none').css('z-index', '4');

  $('div#footer').css('clear', 'both').css('margin', '4px 0px 0px 0px');

  $('#sidemenus').css('display', 'block').css('float', 'left').css('position', 'relative').css('z-index', '800');

  $('ul.menu').css('position', 'absolute').css('z-index', '100')
    .css('width', '200px').css('margin', '2px 0px 0px 0px').css('padding', '0px');
  $('ul.menu ul.menu').css('position', 'absolute').css('z-index', '300')
    .css('top', '-5px').css('left', '190px')
    .css('width', '250px').css('background-color', '#b5bbce')
    .children().css('background-color', 'white').end()
    .hide();
  $('ul.menu ul.menu ul.menu').css('position', 'absolute').css('z-index', '700')
    .css('top', '-5px').css('left', '238px')
    .css('width', '250px').css('background-color', '#b5bbce')
    .children().css('background-color', 'white').end()
    .hide();
  $('ul.menu ul.menu ul.menu ul.menu').css('position', 'absolute').css('z-index', '1500')
    .css('top', '-5px').css('left', '188px')
    .css('width', '150px').css('background-color', '#b5bbce')
    .children().css('background-color', 'white').end()
    .hide();

  $('ul.menu li').css('position', 'relative')
    .css('height', '18px').css('margin', '3px 0px 3px 0px')
    .css('padding-left', '14px').css('padding-top', '6px')
    .css('background-color', '#b5bbce').css('list-style', 'none')
    .hover(
      function(e) {
        $(this).css('cursor', 'pointer');
	var par = $(this).parent().parent().parent();
        if (par.hasClass('menu')) {
          $(this).css('background-color', '#d9dded');
	} else {
          $(this).css('background-color', '#c7ccdc');
        }
	var inner_ul = $(this).children().eq(0);
        if (inner_ul && inner_ul.hasClass('menu')) {
          inner_ul.children().css('background-color', '#c7ccdc');
          inner_ul.show();
        }
      },
      function(e) {
        $(this).css('cursor', 'default');
	var par = $(this).parent().parent().parent();
        if (par.attr('class') && par.attr('class') == 'menu') {
          $(this).css('background-color', '#c7ccdc');
	} else {
          $(this).css('background-color', '#b5bbce');
        }
	var inner_ul = $(this).children().eq(0);
        if (inner_ul && inner_ul.attr('class') && inner_ul.attr('class') == 'menu') {
         inner_ul.hide();
	}
      })
    .click(
      function(e) {
        $(this).css('background-color', '#b5bbce');
	var id = $(this).attr('id');
        if (id.substr(2, 5) == 'empty') {
          return true;
        }
	var inner_ul = $(this).children().eq(0);
        if (inner_ul && inner_ul.hasClass('menu')) {
          inner_ul.hide();
	  return false;
        }
        if (id.substr(0, 2) == 'm_') {
          page = id.substr(2);
          if ($(this).hasClass('new_pdf')) {
	    window.open(lang + '/' + page + '.pdf');
          } else {
            $('#content').load(lang + '/' + page + '.html', {}, frame_ready);
          }
        }
      });

  $('#alert').ajaxError(function(event, request, settings) {
    if (DEBUG) $(this).show().append("<li>Error requesting page " + settings.url + "</li>");
  });

  $('#content').load(lang + '/' + page + '.html', {}, frame_ready);

}

$('document').ready(document_ready);


