$(document).ready(function () {
   Accordion.init();

   $("#telefone").mask('(99) 9999-9999', {placeholder:" "});

   $('a[rel=external]').each(function() {
        $(this).attr('target', '_blank');
    });

    $('#mycarousel').jcarousel({
        scroll: 5
    });

    jQuery('.openfancy').fancybox({
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'titlePosition' 	: 'over',
        'overlayOpacity'        : 0.75,
        'overlayColor'          : '#000'
    });
});

var Accordion = {
    init : function () {
      $('.areabutton').each(function () {
        if (!$(this).hasClass('static'))
            $(this).click(function () {
               var parent = $(this).parent('.areaitem');
                Accordion.toggle(parent);
            });
      });
      //$('.areaitem:first').addClass('opened');
    },
    toggle : function (object) {
        if (object.hasClass('opened'))
            Accordion.close(object);
        else Accordion.open(object);
    },
    open : function (object) {
        Accordion.closeAll();
        object.addClass('opened');
    },
    close : function (object) {
        object.removeClass('opened');
    },
    closeAll : function () {
        $('.areaitem').each(function () {
           if ($(this).hasClass('opened'))
               $(this).removeClass('opened');
        });
    }
}

var Webmail = {
    submit : function () {
        var msg = "";

	if((window.document.getElementById('webmail_nome').value == "") || (window.document.getElementById('webmail_nome').value == "nome")) {
		msg = "Informe seu nome\n";
	}

	if((window.document.getElementById('webmail_email').value == "") || (window.document.getElementById('webmail_email').value == "e-mail")) {
		msg = msg + "Informe seu e-mail\n";
	}

	if (msg) {
            alert("Atenção, para entrar no webmail:\n\n" + msg);
            return false;
	}
        else {
            window.document.getElementById('webmail_nome').value = escape(window.document.getElementById('webmail_nome').value);
            window.document.getElementById('webmail_email').value = escape(window.document.getElementById('webmail_email').value);
            window.document.webmailForm.submit();
            return true;
	}

    }
}

var Contato = {
    submit : function () {
        var msg = "";

        if(window.document.getElementById('nome').value == ""){
            msg = msg + " - Informe seu nome;\n";
	}
        if(window.document.getElementById('email').value == ""){
            msg = msg + " - Informe seu email;\n";
	}
        if(window.document.getElementById('endereco').value == ""){
            msg = msg + " - Informe seu endereco;\n";
	}
        if(window.document.getElementById('telefone').value == ""){
            msg = msg + " - Informe seu telefone;\n";
	}
	if(window.document.getElementById('mensagem').value == ""){
            msg = msg + " - Digite sua mensagem.\n";
	}

	if(msg) {
            alert("Atenção!\n\n" + msg);
            return false;
	}

        if (!Util.checkMail($('#email').val())) {
            $('#email').select();
            alert('E-mail com formato inválido.');
            return false;
        }

        document.formContato.submit();
    }
}

var Util = {
    checkMail : function(mail) {
        var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
        if (typeof(mail) == "string") {
            if (er.test(mail)) {
                return true;
            }
        } else if (typeof(mail) == "object") {
            if (er.test(mail.value)) {
                return true;
            }
        }
        return false;
    }
}
