Cufon('.showDescription', {
    hover: {
        color: 'white'
    },
    fontFamily: 'Helvetica TXT'
});
Cufon('div.menu .botones .first a', {
    hover: {
        color: '#00aeef'
    }
});
Cufon('div.menu .botones .second a', {
    hover: {
        color: '#6dbe45'
    }
});
Cufon('div.menu .botones .third a', {
    hover: {
        color: '#ffff00'
    }
});
Cufon('div.menu .botones .fourth a', {
    hover: {
        color: '#ec008c'
    }
});
Cufon('div.menu .botones .five a', {
    hover: {
        color: 'red'
    }
});
Cufon('ul.stylized li a', {
    hover: {
        color: 'white'
    },
    fontFamily: 'Helvetica TXT'
});

Cufon.replace('.description p', {
    fontFamily: 'Helvetica TXT'
});
Cufon.replace('.description ul', {
    fontFamily: 'Helvetica TXT'
});
Cufon.replace('.footer a.followUs', {
    fontFamily: 'Helvetica TXT',
    color: 'gray'
});
Cufon.replace('.section .title', {
    fontFamily: 'Helvetica Title'
});
Cufon.replace('.subtitle', {
    fontFamily: 'Helvetica Bajada'
});
Cufon.replace('.menu', {
    fontFamily: 'Helvetica Menu'
});
Cufon.replace('.showDescription',{
    fontFamily: 'Helvetica TXT'
});
Cufon('h2.acc_trigger a', {
    hover: {
        color: '#ccc',
        fontFamily: 'Helvetica Bajada'
    },
    fontFamily: 'Helvetica Bajada'
});
Cufon.replace('h2.acc_trigger a', {
    fontFamily: 'Helvetica Bajada'
});
Cufon.replace('.sendText', {
    fontFamily: 'Helvetica Bajada' 
});

var blocked = false;
var hidded = false;
var toSend = false;
var hideTitleCallback = null;
var showed = false;

function showTitle(margin){
    margin = margin + 1.5;
    $(".active.section .title").css("margin-left", margin + "%");
    
    var limit = 0;
    
    if(margin<limit){
        blocked = true;
        setTimeout("showTitle("+margin+")", 10);
    }else{
        $(".section.active .description").css("margin-left", margin + "%");
        subParagraph();
        hidded = false;
    }
}

function hideTitle(margin){
    if($(".active.section").attr('id') == 'contactanos'){
        end = -105;
    }else{
        end = -70;
    }
    if(!hidded){
        hidded = true;
        return subParagraph(function(){
            hideTitle(margin);
        });
    }
    
    margin = margin - 1.5;
    $(".active.section .title").css("margin-left", margin + "%");
    
    if(margin>end){
        setTimeout("hideTitle("+margin+")", 10);
    }else{
        hideTitleCallback();
    }
    
    
    return true;
}

function subParagraph(callback){
    var idActiveSection = $(".active.section").attr('id');
    var selector = $(".active.section .content");
    if(showed){
        if(idActiveSection == 'contactanos' || idActiveSection == 'hacemos'){
            $(".active.section").css('z-index','80');
        }
        
        selector.animate({
            width: 'toggle', 
            height: 'toggle'
        }, 1000, function(){
            $(".active.section .subtitle").slideUp("fast");
            showed = false;
            $(".section .underlineSection").hide("slide", "fast");
            if(typeof(callback) !== "undefined"){
                callback();
            }
        });
    }else{
        $(".section .underlineSection").show("slide", "fast");
        $(".active.section .subtitle").slideDown("fast", function(){
            selector.animate({
                width: 'toggle', 
                height: 'toggle'
            }, 1000, function(){
                if( idActiveSection == 'contactanos' || idActiveSection == 'hacemos'){
                    $(".active.section").css('z-index','91');
                }
                showed = true;
                blocked = false;
            });
        });
    }
}

function toggleSection(e, section){
    if(!blocked){
        blocked = true;
        hideTitleCallback = function(){
            $(".menuMask").css("visibility", 'visible').css('z-index', 99);
            $(".active.section").hide().removeClass('active');
            $(section).addClass('active');
            animateMenu(e);
        }
        
        hideTitle(0);
    }
}

function toggleGallery(e){
        
}

function animateMenu(e){
    $(".menu li").css('visibility', 'visible').animate({
        'margin-left' : 0
    }, 'fast');
    $(".menu").css('z-index', 90);
    
    if($(e).hasClass('fourth')){
        $(e).animate({
            'margin-left': 150
        }, 'fast', function(){
            $(e).css('visibility', 'hidden');
            $(".menuMask").css('visibility', 'hidden');
            $(".menu").css("z-index", 99);
            $(".section.active").css('margin-left', '-105').show();
            showTitle(-105);
        });
    }else{
        $(e).animate({
            'margin-left': 100
        }, 'fast', function(){
            $(e).css('visibility', 'hidden');
            $(".menuMask").css('visibility', 'hidden');
            $(".menu").css("z-index", 99);
            $(".section.active").css('margin-left', '-70').show();
            showTitle(-70);
        });
    }
}

function sendContactAjax(){
    if(!toSend){
        return alert("Please fill the fields");
    }
    
    var data = {};
    data['nombre'] = $(".nombre").val();
    data['email'] = $(".email").val();
    data['telefono'] = $(".telefono").val();
    data['mensaje'] = $(".mensaje").val();
    
    $.ajax({
        url: location.origin + location.pathname + 'sendMail.php',
        type: 'POST',
        dataType: 'json',
        data: data,
        success: function(response){
            if(response.sent){
                alert("El contacto se ha enviado");
            }else{
                alert(response.msg);
            }
        }
    });
    
    return true;
}

function accordion(){
    $('.acc_container').hide();
    $('.acc_trigger:first')
    .addClass('active')
    .next()
    .show();
 
    $('.acc_trigger').click(function(){
        new weDoAccordion().activeDescriptions();
        
        if( $(this).next().is(':hidden') ) {
            $('.acc_trigger').removeClass('active').next().slideUp();
            $(this).toggleClass('active').next().slideDown();
        }
        
        Cufon('h2.acc_trigger a', {
            hover: {
                color: '#ccc',
                fontFamily: 'Helvetica Bajada'
            },
            fontFamily: 'Helvetica Bajada'
        });
        Cufon.replace('h2.acc_trigger a', {
            fontFamily: 'Helvetica Bajada'
        });
        return false;
    });
}


function weDoAccordion() {
    this.activeDescriptions = function(){
        var activeDescriptions = $(".weDoDescription.activeWeDoDescription");
        var activeImgs = activeDescriptions.parent().find('img');
        $(".showDescription.active").removeClass('active');
        Cufon('.showDescription', {
            hover: {
                color: 'white'
            },
            fontFamily: 'Helvetica TXT'
        }).replace('.showDescription',{
            fontFamily: 'Helvetica TXT'
        });
        if(activeImgs.hasClass('arrow')){
            activeImgs.removeClass('arrowDown').addClass('arrow');
            activeDescriptions.toggle('blind', {}, 500);
            activeDescriptions.removeClass('activeWeDoDescription');
        }  
    };
    
    this.init = function(self){
        $(".weDoDescription").hide();
        
        $(".weDoTitle .showDescription").click(function(){
            var img = $(this).parent().find('img');
            
            self.activeDescriptions();
            $(this).addClass('active');
            $(this).parent().next('.weDoDescription').first().toggle('blind', {}, 500, function(){
                if(img.hasClass('arrow')){ //Esta oculto
                    Cufon.now();
                    $(this).addClass('activeWeDoDescription');
                    img.removeClass('arrow');
                    img.addClass('arrowDown');
                }else{ //Aca lo muestro
                    $(this).removeClass('activeWeDoDescription');
                    img.removeClass('arrowDown');
                    img.addClass('arrow');
                }
            });
            
            return false; 
        });
    };
    
    this.init(this);
}

function validateContact(){
        
    this.isValid = function(){
        var valid = true;
        var alertText = "Error: "
        
        if(!toSend){
            alertText += "\nPlease fill the fields\n";
            alert(alertText);
            return false;
        }
        if($(".nombre.inputField").val() == "" || $(".nombre.inputField").val() == 'NOMBRE'){
            alertText += "\nInvalid name\n"; 
            valid = false;
        }
        if(!this.validEmail($(".email.inputField").val())){
            alertText += "\nInvalid email\n";
            valid = false;
        }
        if($(".telefono.inputField").val() == "" || $(".telefono.inputField").val() == "TELEFONO"){
            alertText += "\nInvalid telephone\n";
            valid = false;
        }
        if($(".mensaje.textArea").val() == "" || $(".telefono.textArea").val() == "MENSAJE"){
            alertText += "\nInvalid message\n";
            valid = false;
        }
        
        if(!valid){
            alert(alertText);
        }
        
        return valid;
    };
    
    this.validEmail = function(email){
        filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        return filter.test(email);
    };
    
}

$(document).ready(function(){
    $(".section").hide();
    $(".section .content").hide();
    $(".section .subtitle").hide();
    $("#somos").addClass('active').show();
    animateMenu($(".menu li").first());
    
    var contactForm = new validateContact();
    $(".sendText").click(function(){
        if(contactForm.isValid()){
            sendContactAjax();
        }
    });
    
    
    var cleaned = [];
    $("form input, form textarea").focus(function(){
        if(typeof(cleaned[$(this).attr('id')]) == 'undefined' || cleaned[$(this).attr("id")] == false){
            switch ($(this).attr('id')) {
                case "nameField":
                    cleaned[$(this).attr('id')] = "NOMBRE";
                    break;
                case "mailField":
                    cleaned[$(this).attr('id')] = "E-MAIL";
                    break;
                case "telField":
                    cleaned[$(this).attr('id')] = "TELEFONO";
                    break;
                case "msgTextArea":
                    cleaned[$(this).attr('id')] = "MENSAJE";
                    break;
            }

            $(this).val("");
            $(this).css("color", "white");
        }
        toSend = true;
    });
    
    $("form input, form textarea").blur(function(){
       if($(this).val() == ""){
           $(this).val(cleaned[$(this).attr("id")]);
           $(this).css("color", "black");
           cleaned[$(this).attr("id")] = false;
       } 
    });
    
    accordion();
    
    new weDoAccordion();
    
    //    $(".stylized a").fancybox({
    //        'transitionIn'	:	'elastic',
    //        'transitionOut'	:	'elastic'
    //    });
    
    
    $(".menu li").click(function(){
        if(!blocked){
            __callSection(this, $(this).attr('class'));
        }
    });
    
    function __callSection(e, eClass){
        switch (eClass) {
            case 'first':
                toggleSection(e, '#somos');
                break;
            case 'second':
                toggleSection(e, '#hacemos');
                break;
            case 'third':
                toggleSection(e, '#creemos');
                break;
            case 'fourth':
                toggleSection(e, '#contactanos');
                break;
            case 'five':
                toggleGallery(e);
                break;
        }
    }
    
    Cufon.now();
});
