/*
 * Cliente: R. Assolari Assessoria Contábil
 * Projeto: Site institucional, 2010
 * Autor: Gustavo Straube, gustavo.straube@gmail.com
 */

$(document).ready(function () {

/*
 * Links externos.
 */
$('a[rel=external]').attr('target', '_blank');

/*
 * Efeitos no formulário de login.
 */
$('#login').focus(function () {
  if (this.value == 'Login') {
    this.value = '';
  }
}).blur(function () {
  if (this.value == '') {
    this.value = 'Login';
  }
});

$('#password').focus(function () {
  if (this.value == 'Senha') {
    this.value = '';
  }
}).blur(function () {
  if (this.value == '') {
    this.value = 'Senha';
  }
});

/*
 * Notícias.
 */
$('#highlights .news ul').height(60).css('overflow', 'hidden');
$('#highlights .news ul li:gt(0)').hide();
window.setInterval(function () {
  var li = $('#highlights .news ul li:visible');
  var next = li.next();
  if (next.size() == 0) {
	next = $('#highlights .news ul li:first');
  }
  li.fadeOut('slow', function () {
	  next.fadeIn('slow');
  });
}, 5 * 1000);

/*
 * :hover do menu para o IE6.
 */
if ($.browser.msie && $.browser.version < 7) {
  $('#main-menu li.servicos').mouseover(function () {
    $('ul', this).show();
    $('a:first', this).css('background-image', 'url(imagens/main-menu-servicos-ativo.gif)');
  }).mouseout(function () {
    $('ul', this).hide();
    $('a:first', this).css('background-image', 'url(imagens/main-menu-servicos.gif)');
  });
}

$('ul.popup').click(function () {
  $(this).fadeOut();
});

/*
 * Disable submit by pressing Enter/Return key.
 */
$('#content form').keypress(function(e) {
  if ($(e.target).is('#value_4')) {
    return;
  }
  if (e.keyCode == 13) {
    return false;
  }
});

$(':checkbox[name=all]').click(function () {
  var m = this.id.match(/^all_([a-z]+)$/i);
  if (m && m.length > 1) {
    $(':checkbox[name="' + m[1] + '[]"]').attr('checked', this.checked);
  }
});

$('input.datepicker').keyup(function () {
  $(this).val(datepickerFormat($(this).val()));
}).blur(function () {
  $(this).val(datepickerFormat($(this).val()));
});

function datepickerFormat(val) {
  if (!val || typeof val != 'string') {
    return '';
  }
  val = val.replace(/[^0-9]/ig, '');
  var newval = val.substring(0, 2);
  if (val.length > 2) {
    newval += '/' + val.substring(2, 4);
  }
  if (val.length > 4) {
    newval += '/' + val.substring(4);
  }
  return newval;
}

$('input.money').keyup(function () {
  $(this).val(moneyFormat($(this).val(), false));
}).blur(function () {
  $(this).val(moneyFormat($(this).val(), true));
});

function moneyFormat(val, decimal) {
  if (typeof val != 'string') {
    return '0,00';
  }
  val = val.split(',', 1)[0];
  val = val.replace(/[^0-9]/ig, '');
  val = val.replace(/^0+/ig, '');
  if (decimal) {
    if (val.length < 1) {
      val = '0';
    }
    val += ',00';
  }
  return val;
}

$('input.numeric').keyup(function () {
  $(this).val(numericFormat($(this).val()));
}).blur(function () {
  $(this).val(numericFormat($(this).val()));
});

function numericFormat(val) {
  if (typeof val != 'string') {
    return '';
  }
  val = val.replace(/[^0-9]/ig, '');
  return val;
}

$('a.delete,button.delete').click(function () {
  var txt = 'continuar';
  if (this.title) {
    var f = this.title.charAt(0).toLowerCase();
    txt = f + this.title.substr(1);
  }
  return window.confirm('Ao clicar em ok você apagará esse item. Tem certeza que deseja ' + txt + '?');
});

$(document.body).click(function (event) {
  var target = $(event.target);
  if (!target.is('.inner') && target.parents('.inner').length == 0) {
    $('#warn_message').remove();
  }
});

$('#warn_message .close').click(function () {
  $('#warn_message').remove();
  return false;
});

/*
 * Adicionar mais um documento, no admin.
 */
$('#add-file').click(function () {
  if (typeof types != 'object') {
    window.alert('Desculpe, mas não é possível incluir novos arquivos.');
    return false;
  }
  var files = $('input[name="file[]"]');
  var type = '<li>'
      + '<label for="type' + files.size() + '"><strong>Tipo*</strong>:</label>'
      + '<select id="type' + files.size() + '" name="type[]">'
      + '<option value="0">Selecione um tipo</option>';
  for (var i = 0, s = types.length; i < s; i++) {
    type += '<option value="' + types[i].id + '">' + types[i].name + '</option>';
  }
  type += '</select>';
  var date = '<li>'
      + '<label for="expiration' + files.size() + '">Validade:</label>'
      + '<input type="text" id="expiration' + files.size() + '" class="datepicker" name="expiration[]" value="" size="11" maxlength="10" />'
      + '<p><small>Formato dd/mm/aaaa.</small></p>'
      + '</li>';
  var file = '<li>'
      + '<label for="file' + files.size() + '"><strong>Arquivo*</strong>:</label>'
      + '<input type="file" id="file' + files.size() + '" name="file[]" />'
      + '</li>';
  $(files.get(files.size() - 1)).parent().after('<hr />' + type + date + file);
  $('input#expiration' + files.size() + '.datepicker').keyup(function () {
    $(this).val(datepickerFormat($(this).val()));
  }).blur(function () {
    $(this).val(datepickerFormat($(this).val()));
  });
  return false;
});

});

/*
 * Carrega a API do Maps.
 */
if (document.getElementById('contato')) {
  google.load("maps", "2");
}

google.setOnLoadCallback(init);

function init() {

/*
 * Abrir mapa.
 */
$('#openmap').click(function () {
  $(this).hide();
  var map = new google.maps.Map2($('div.gmaps').fadeIn().get(0));
  var point = new google.maps.LatLng(-25.475159,-49.298709);
  map.setCenter(point, 16);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  var marker = new GMarker(point);
  map.addOverlay(marker);
  var info = '<table cellspacing="0" cellpading="0" border="0"><tr>'
      + '<td style="padding-right:10px;vertical-align:top;"><strong>R. Assolari Assessoria Contábil</strong><br /><br />'
      + 'Rua João Bettega, 499, Sala 01<br />'
      + 'Portão - Curitiba, PR, Brasil<br />'
      + 'CEP 81070-000<br /><br />' 
      + '<strong>Telefone/Fax</strong>: 55 (41) 3329 9297</td>'
      + '<td><img src="images/escritorio_assolari.jpg" alt="Escritório R. Assolari" /></td>';
  marker.bindInfoWindowHtml(info);
  marker.openInfoWindowHtml(info);
  return false;
});

}

