/* Main Javascript file.
*  version 1.0
*/

/*Main variables*/

var folderLocation = location.host == 'localhost' ? '/' : '/';
var serverLocation = location.host == 'localhost' ? location.protocol + '//' + location.host + '/smart/johnjohn/www/': location.protocol + '//' + location.host + '/';
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var labels;
var user;

$(document).ready(function()
{
        $('.button-work').click(function() {_gaq.push(['_trackPageview', '/trabalhe-conosco']);closeTop();$('#tabs-work').animate( {bottom: 0, queue: true}, 100 );})
        $('.button-privacy').click(function() {closeTop();$('#tabs-privacy').animate( {bottom: 0, queue: true}, 100 );})
        $('.button-rights').click(function() {closeTop();$('#tabs-rights').animate( {bottom: 0, queue: true}, 100 );})
        $('.button-newsletter').click(function() {closeBottom();$('#tabs-newsletter').animate( {top: 0, queue: true}, 100 );})
        
        $('.tab .close').click(function()
        {
                $(this).parent().animate( {bottom: -(parseInt($(this).parent().css('height')) + 20), queue: true}, 100 );
        })
        
        $('.tab-top .close').click(function()
        {
                $(this).parent().animate( {top: -(parseInt($(this).parent().css('height')) + 20), queue: true}, 100 );
        })
        
	$('header .shop-phone .icon').mouseover(function()
	{
		$('header .shop-phone .phone-number').fadeIn('fast');
	}).mouseleave(function() 
	{
		$('header .shop-phone .phone-number').fadeOut('fast');
	})
	
	
       $('.content-block').jScrollPane(
       {
                showArrows: false,
                autoReinitialise: true,
		verticalDragMinHeight: 21,
		verticalDragMaxHeight: 20
       });
       
       /*
        * NEWSLETTER FORM
        */
       
        $("#news-submit").click(function()
        {
		if(validate_newsletter())
		{
			$.ajax({
				url: serverLocation + "services/contact/newsletter/",
				type: "POST",
				async: false,
				data: {name: $("#news-name").val(), email: $("#news-email").val()},
				success: function(data) {
					if(data == "1")
					{
						alert("Informações enviadas com sucesso");
						$("#news-name").val("Nome");
						$("#news-email").val("E-mail");
					}
                                        else
					{
						alert("Ocorreu um erro no envio de seus dados");
					}
				}
			});
		}
		return false;
	});
        
        /*
         * WORK
         */
        $("#work-attachment").change(function() {$("#work-file").html($(this).val());});
        
        $("#work-submit").click(function ()
        {
                if(validate_work())
		{
                        $("#work-form").ajaxSubmit
                        ({
                                url: serverLocation + "services/work/send/",
                                success: function(response)
                                {
                                        alert("Informações enviadas com sucesso");
                                        $("#work-name").val("");
                                        $("#work-email").val("");
                                        $("#work-phone").val("");
                                        $("#work-file").html("ANEXAR CURRICULO");
                                }
                        });
		}
                return false;
        });
        
        
})
	
var isPhone = function (phoneValue){
        exp = /\(\d{2}\)\\d{4}\-\d{4}/
        return exp.test(phoneValue);
};

var isEmail = function (value){
        var emailExpression = new RegExp(/^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i);
        return emailExpression.exec(value);
};

var validate_newsletter = function ()
{
        if($("#news-name").val() == "" || $("#news-name").val() == "Nome")
        {
                alert("Preencha seu nome corretamente");
                $("#news-name").focus();
                return false;
        }
        if(!isEmail($("#news-email").val()))
        {
                alert("Preencha seu e-mail corretamente");
                $("#news-email").focus();
                return false;
        }
        return true;
};

var validate_work = function ()
{
        if($("#work-name").val() == "" || $("#work-name").val() == "Nome")
        {
                alert("Preencha seu nome corretamente");
                $("#work-name").focus();
                return false;
        }
        if(!isEmail($("#work-email").val()))
        {
                alert("Preencha seu e-mail corretamente");
                $("#work-email").focus();
                return false;
        }
        if($("#work-area").val() == "Escolha o departamento")
        {
                alert("Escolha a área de atuação corretamente");
                $("#work-area").focus();
                return false;
        }
        
        if($("#work-message").val() == "" || $("#work-message").val() == "Mensagem")
        {
                alert("Preencha a mensagem corretamente");
                $("#work-message").focus();
                return false;
        }
        
        
        if($("#work-file").html() == "ANEXAR CURRICULO")
        {
                alert("Anexe seu currículo");
                $("#work-file").focus();
                return false;
        }
        return true;
};
        
        

function closeTop()
{
      $('#tabs-newsletter').animate( {top: -(parseInt($('#tabs-newsletter').css('height')) + 20), queue: true}, 100 );
}

function closeBottom()
{
      $('#tabs-work').animate( {bottom: -(parseInt($('#tabs-work').css('height')) + 20), queue: true}, 100 );
      $('#tabs-rights').animate( {bottom: -(parseInt($('#tabs-rights').css('height')) + 20), queue: true}, 100 );
      $('#tabs-privacy').animate( {bottom: -(parseInt($('#tabs-privacy').css('height')) + 20), queue: true}, 100 );
}


// Read a page's URL segments and return them as an associative array.
function getUrlSegments()
{
	var vars = [];
	var hashes = window.location.href.slice(window.location.href.indexOf('http://') + 7).split('/');
	for(var i = 0; i < hashes.length; i++)
	{
		vars.push(hashes[i]);
	}
	return vars;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

jQuery(function()
{
        $('input[type="text"], textarea').each(function()	{
                $(this).attr('title', $(this).val()).focus(function() {
                        if ($(this).val() == $(this).attr('title')) {
                                $(this).val('');
                        }
                }).blur(function() {
                        if ($(this).val() == '' || $(this).val() == '') {
                                $(this).val($(this).attr('title'));
                        }
                });
        });

});

