var currentBelieveSlide = '#home';
var believeAnimating = false;
var currentOfferSlide = '';
var currentOfferWidth = 185;
var currentOfferItemSlide = '';
var slideTime = 300;
var timer = null;

$(document).ready(initialize);

function initialize() {
    $('#topMenu a').hover(handleTopMenuHover).click(handleTopMenuClick);
    $('#leadership dt a').click(handleLeadershipClick);
    $('#weThinkIcons li').hover(getSocialItem, closeSocialItem);
    $('#homeMenu a').click(handleBelieveClick);
    $('#weOfferMenu a').click(handleOfferClick).mouseover(handleOfferMouseover).mouseout(handleOfferMouseout);
    $('.weOfferMenu a').click(handleOfferItemClick);
    $('textarea').focus(function () {
        if ($(this).val() == "Want to know more? Share what's on your mind.")
            $(this).val('');
    }).blur(function () {
        if($(this).val() == '')
            $(this).val("Want to know more? Share what's on your mind.");
    });
    $('#Email').val('Enter email to submit');
    $('#Email').focus(function () {
        if ($(this).val() == 'Enter email to submit')
            $(this).val('');
    }).blur(function () {
        if ($(this).val() == '')
            $(this).val('Enter email to submit');
    });
    timer = setInterval(playThinkR4, 4000);
}

function sendRequest() {
    if ($('textarea').val() != "Want to know more? Share what's on your mind." && $('textarea').val() != '') {
        var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (emailRegex.test($('#Email').val())) {
			$('#emailValidation').html('');
            $.post('services/mail.php', '&message=' + $('textarea').html() + '&from=' + $('#Email').val(), handleSendResponse);
        } else
			$('#emailValidation').html('Please enter a valid email address');
    } else 
		$('#emailValidation').html('Please enter your request');
}
function handleSendResponse(data, status, jqXhr) {
	if(data == 'Thank you for your feedback') {
		$('textarea').val("Want to know more? Share what's on your mind.");
		$('#Email').val('Enter email to submit');
	}
    $('#emailValidation').html(data).fadeIn().delay(4000).fadeOut();
}

function getSocialItem(e) {
    var target = $(this).find('span');
    if ($(target).html() == '') {
        var url = 'services/social.php?source=' + $(this).attr('title');
        $.post(url, function (data, status, jqXhr) {
            $(target).html(data);
            if($(target).html() != 'error')
                showSocialItem(target);
        });
    } else if($(target).html() != 'error')
        showSocialItem(target);
}
function showSocialItem(target) {
    $(target).stop().animate({ width:150, "padding-left":10 }, slideTime);
}
function closeSocialItem(e) {
    $('#we-think li span').stop().animate({ width: 0, "padding-left": 0 }, slideTime);
}

function playThinkR4() {
    clearInterval(timer);
    $('#think-r4 li.active').animate({ opacity: 0, 'padding-top': '40px' }, slideTime, function () {
        var next = $('#think-r4 li.active').next();
        $('#think-r4 li').not(':visible').css({ 'padding-top': '0', 'opacity': '0' });
        $('#think-r4 li').removeClass('active');
        if ($(next).length == 0)
            $('#think-r4 li:first').addClass('active').animate({ opacity: 100, 'padding-top': '20px' }, slideTime);
        else
            $(next).addClass('active').animate({ opacity: 100, 'padding-top': '20px' }, slideTime);
        timer = setInterval(playThinkR4, 4000);
    });
}

function handleLeadershipClick(e) {
    var left = $(this).position().left - 30;
    var target = $(this).attr('href');
    if($(target).width() > 0) {
        $(target).stop().animate({ width: 0, opacity: 0 }, slideTime);
        $('#leadership').stop().animate({ left: 0 }, slideTime);
    } else {
        $(target).stop().animate({ width: 775, opacity: 100 }, slideTime);
        $('#leadership').stop().animate({ left: -left }, slideTime);
    }
    e.preventDefault();
}

function handleTopMenuClick(e) {
    $('#topMenu li').removeClass('active');
    $(this).parent().addClass('active');
    var target = $(this).attr('href');
    var padding = $('.page').height() * 2;
    $('#spacer').css('padding-bottom', padding + 'px');
    $('html,body').animate({ scrollTop: ($(target).offset().top - parseInt($(target).css('margin-top'))) - 100 }, slideTime * 2);
    e.preventDefault();
}

function handleTopMenuHover(e) {
    var text = $(this).attr('title');
    $(this).attr('title', $(this).find('.link').text());
    $(this).find('.link').text(text);
    if ($(this).hasClass('active'))
        $(this).removeClass('active');
    else
        $(this).addClass('active');
}

function handleBelieveClick(e) {
    handleBelieveAnimation($(this).attr('href'));
    e.preventDefault();
}

function handleBelieveAnimation(id) {
    if (id != currentBelieveSlide && !believeAnimating) {
        var source = $('#homeMenu a[href=' + id + ']');
        believeAnimating = true;
        $('#homeMenu a').removeClass('active');
        $('#homeMenu span').html('+');
        $(source).addClass('active');
        $(source).find('span').html('-');
        $('#leadership dd').animate({ width: 0, opacity: 0 });
        $(currentBelieveSlide).animate({ left: '-960px' }, slideTime, function () {
            $(this).css('left', '960px');
            currentBelieveSlide = id;
            $(id).animate({ left: '0' }, slideTime, function () { believeAnimating = false; });
        });
    }
}

function handleOfferMouseover(e) {
    var left = $(this).offset().left - $('#we-offer').offset().left + 153;
    $('#contentHeader').stop().animate({ width: left }, slideTime);
}
function handleOfferMouseout(e) {
    $('#contentHeader').stop().animate({ width: currentOfferWidth }, slideTime);
}

function handleOfferClick(e) {
    var id = $(this).attr('href');
    currentOfferWidth = $(this).offset().left - $('#we-offer').offset().left + 153;
    if (!$('#weOfferContent').is(':visible')) {
        $('#weOfferContent').slideDown(slideTime);
    }
    if (id != currentOfferSlide) {
        $('#weOfferMenu li').removeClass('active');
        $('.weOfferMenu > li').removeClass('active');
        $(this).parent().addClass('active');
        $('.we-offer:visible').fadeOut(slideTime / 2, function () {
            $(id).find('.weOfferContent > li').css('display','none');
            $(id).find('li.first').fadeIn(0);
            currentOfferSlide = id;
            $(id).fadeIn(slideTime / 2);
        });
    }
    e.preventDefault();
}

function handleOfferItemClick(e) {
    var target = $(this).attr('href');
    var menuItem = $(this);
    if (target != currentOfferItemSlide) {
        $('.weOfferMenu li').removeClass('active');
        $('.weOfferContent > li:visible').fadeOut(slideTime / 2, function () {
            currentOfferItemSlide = target;
            $(menuItem).parent().addClass('active');
            $(target).fadeIn(slideTime / 2);
        });
    }
    e.preventDefault();
}
