var animating = false;
function init_pad() {
    var screen_width = $(window).width();
    if(screen_width > 1200) {
        var space = (($(window).width()-1200)/2);
        $('.page_container').each(function() {
            var pos = ($(this).position().left+space)+'px';
            $(this).css('left', pos);
        });
        $('#right_pad').width(space);
        $('#left_shadow').width(space+500);
        $('#right_shadow').width(space+500);
    }
}

function nav_hover(element, on) {
    element = $(element);
    var fade_to_class = ' .hover';
    if(element.hasClass('current')) {
        fade_to_class = ' .current_hover';
    }
    var fade_to = $('#'+element.attr('id')+fade_to_class);
    var nav_opacity = 0;
    if(on) {
        nav_opacity = 1;
    }
    if(!animating) {
        fade_to.stop();
    }
    fade_to.animate({opacity: nav_opacity}, 300);
}

function scroll_page(link) {
    animating = true;
    $('#nav a').removeClass('current');
    $('#'+link.attr('id')+' .current_hover').stop().animate({opacity: 1}, 300, function() {
        $('#'+link.attr('id')+' .hover').css('opacity', '0');
        if(link.attr('id') == 'nav_who_we_are') {
            $('#'+link.attr('id')+' .current_hover').css('opacity', '0');
        }
        animating = false;
    });
    link.addClass('current');
    var scroll_to = ($('#'+link.attr('id').substr(4)).offset().left) - (($(window).width()-1200)/2);
    $("html,body").stop().animate({scrollLeft: scroll_to}, 500 );
   return false;
}

function tab_swap(element) {
    var new_link = $(element);
    var new_text = $('#text_'+new_link.attr('id').substr(5));
    var old_link = $('.tab.current');
    var old_text = $('.tab_text.current');
    old_link.removeClass('current');
    new_link.addClass('current');
    old_text.fadeOut(150, function() {
        old_text.removeClass('current');
        new_text.fadeIn(150, function() {
            new_text.addClass('current');
        });
    });
}

function show_email() {
    var a = 'tudio7';
    var b = '5.co.nz';
    var c = 'mail@thes';
    var email = c+a+b;
    $('.replace_email').html('<a href="mailto:'+email+'">'+email+'</a>');
}

function popup(box, show) {
    box = $('#'+box+'_box');
    var overlay = $('#overlay');
    if(show) {
        box.css('left', (($(window).width()/2)-220)+'px');
        box.fadeIn(200);
        overlay.fadeIn(200);

    } else {
        box.fadeOut(200);
        $('#success_box').fadeOut(200);
        overlay.fadeOut(200);
    }
}

function validate_on_blur(element) {
    element = $(element);
    var value = element.val();
    switch(element.attr('id')) {
        case 'name':
            show_valid_status(element, validate_text(value, 1, 50));
            break;
        case 'email':
            show_valid_status(element, validate_email(value));
            break;
        case 'message':
            show_valid_status(element, validate_text(value, 1, 1000));
            break;
    }
}

function validate_form(e) {
    var name_element = $('#name');
    var email_element = $('#email');
    var message_element = $('#message');
    var name_valid = validate_text(name_element.val(), 1, 50);
    var email_valid = validate_email(email_element.val());
    var message_valid = validate_text(message_element.val(), 1, 1000);
    if(!(name_valid && email_valid && message_valid)) {
        e.preventDefault();
    }
    if(name_valid) {
        name_element.removeClass('invalid');
        name_element.addClass('valid');
    } else {
        name_element.removeClass('valid');
        name_element.addClass('invalid');
    }
    show_valid_status(name_element, name_valid);
    show_valid_status(email_element, email_valid);
    show_valid_status(message_element, message_valid);
}

function validate_text(text, min, max) {
    var is_valid = false;
    if(min === undefined) {
        min = 0;
    }
    if(max === undefined) {
        max = 9999;
    }
    if(text.length >= min && text.length <= max) {
        is_valid = true;
    }
    return is_valid;
}

function validate_email(email) {
    var is_valid = false;
    var email_regex = /^[A-Z0-9._%-\+]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
    if(email_regex.test(email)) {
        is_valid = true;
    }
    return is_valid;
}

function show_valid_status(element, valid) {
    if(valid) {
        element.removeClass('invalid');
        element.addClass('valid');
    } else {
        element.removeClass('valid');
        element.addClass('invalid');
    }
}

function init_slideshow() {
    $('#slideshow').orbit({
        animation: 'fade',
        animationSpeed: 200,
        advanceSpeed: 6000,
        captions: false,
        bullets: true
    });
    var num_bullets = 0;
    $('.orbit-bullets li').each(function() {
        num_bullets += 1;
    });
    $('.orbit-bullets').css('width', num_bullets*20+'px');
}

function change_slide(element) {
    var selected = element.attr('id');
    $('#gallery_nav div a').removeClass('current');
    $('#'+selected).addClass('current');
    $('#gallery_nav > a').attr('class', selected);
    $('#gallery_nav div').slideUp();
    swap_gallery(selected);
}

function swap_gallery(selected) {
    var category = 'fashion';
    switch(selected) {
        case 'gallery_nav_1':
            category = 'fashion';
            break;
        case 'gallery_nav_2':
            category = 'product';
            break;
        case 'gallery_nav_3':
            category = 'food';
            break;
        case 'gallery_nav_4':
            category = 'annual_report';
            break;
        case 'gallery_nav_5':
            category = 'architecture';
            break;
    }
    $('#slideshow img').each(function(i, e) {
        if($(e).css('z-index') !== '3') {
            $(e).remove();
        }
    });
    $('div.orbit-wrapper').fadeOut(200, function() {
        $('div.orbit-wrapper').remove();
        $.ajax({
            url: 'gallery.php',
            data: 'category='+category,
            success: function(result) {
                $('#what_we_do > div').append(result);
                init_slideshow();
            }
        });
    });
}
