var scrollHeight = new Array();
$(document).ready(function(){
    
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		window.location("http://www.certifiednutritionomaha.com/sorry-ie6");
	};

    certifiednutrition_accordion();
    $('#main .tab').hide();
    $('#main .services').show();
    
    scrollHeight['weight'] = $('#main .tab #weight').offset().top;
    scrollHeight['coaching'] = $('#main .tab #coaching').offset().top;
    scrollHeight['wellness'] = $('#main .tab #wellness').offset().top;
    scrollHeight['nutrition'] = $('#main .tab #nutrition').offset().top;
    scrollHeight['training'] = $('#main .tab #training').offset().top;
    scrollHeight['corporate'] = $('#main .tab #corporate').offset().top;
    scrollHeight['sports'] = $('#main .tab #sports').offset().top;

    $('#main .services').hide();
    
    certifiednutrition_tabs();
    certifiednutrition_scroll();
    certifiednutrition_sticky_scroll();
    
    certifiednutrition_select_service();
    certifiednutrition_request_appt();
    certifiednutrition_form();
});
/* --------------------------
 * FORM
 * -------------------------- */
function certifiednutrition_form(){

	$('form#contact').submit(function(){
		var isError = false;
		var link = $(".btnSubmit").attr('href');
		var required = '#name, #email';

		$(required).each(function(){
			if($(this).val() == ""){
				$(this).addClass('missing');
				isError = "Please fill in all required fields.";
			}
		});

		if(isError){
			window.alert(isError);
            return false; /*if errors don't submit form*/
		}
		else {
            var formArray = {
                'form[name]': $('#name').val(),
                'form[email]': $('#email').val(),
                'form[phone]': $('form #phone').val(),
                'form[subject]': $('#subject').val(),
                'form[message]': $('#message').val()
            };
			$.post(
                '/form-backend.php?js=enabled',
                formArray,
                function(data) {
                    if(data == "ok") {
                        window.alert("Thank you for filling out the form. We will contact you soon.");
                       /*$('input').each(function(){
                           $(this).val("");


                        });*/
                    }
                    else {

                        alert("There was an error. Please make sure you complete all required fields.");
                    }
                }
            );
		}
        return false;
	});
}

/* --------------------------
 * TABS
 * -------------------------- */

function certifiednutrition_tabs(){

    // Set up tabs
    $('#main .tab').hide();
    $('#main .contact-info').hide();
    $('ul#nav_primary li.home a').addClass('active');
    $('#main .home').show();
    $('#wrapper #container').css('top', '60%');

    // Tab functionality

    $('ul#nav_primary li a').click(function() {
        var self = this;
        if($(this).parent().hasClass('home')){
            $('body, html').css('background-image','url(/_ui/skin/img/jill-home.jpg)');
            $('#main .contact-info').hide();
            //$('#wrapper #container').css('top','40%');
            $('#wrapper #container').animate({top: '60%'}, 1000);
            certifiednutrition_accordion();
            finish_tab_setup($(this).attr('rel'));

        }
        else if($(this).parent().hasClass('blog')){
            //do nothing
        }
        else {
            setup_other_tabs();
            finish_tab_setup($(this).attr('rel'));
        }


    });
}

/* --------------------------
 * Setup tabs other than the home tab
 * -------------------------- */
function setup_other_tabs() {
	$('html, body').css('background-image','none');
	$('#main .contact-info').show();
	$('#wrapper #container').animate({top: '30px'}, 1000);
}

/* --------------------------
 * Any remaining setup when switching tabs
 * -------------------------- */
function finish_tab_setup(category) {
	$('.tab').hide();
	$('ul#nav_primary li a').removeClass('active');
	$('.tab.'+category).show();
	$('ul#nav_primary li.'+category +' a').addClass('active');
}

/* --------------------------
 * SMOOTH SCROLL
 * -------------------------- */

function certifiednutrition_scroll() {
    $('a[href*=#]','.nav-interior').click(function() {

        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);

            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');

             if ($target.length) {
                 var targetOffset = $target.offset().top;
                
                 $('html,body').animate({scrollTop: targetOffset}, 1000);
                 return false;
            }
        }
    });
}


/* --------------------------
 * INTERIOR STICKY NAV
 * -------------------------- */

function certifiednutrition_sticky_scroll(){

    var headerHeight= $('div#header').height()+30;

    var mainHeight= 106;

    var top = headerHeight + mainHeight;

    $('.links-container, .block-menu-container').css('top', mainHeight+"px");

    $(window).scroll(function(){
        if ($(window).scrollTop() >= top){

            $('.nav-container').css('position','absolute');
            $('.nav-interior').addClass('fixed');
        }
        else {

            $('.nav-container').css('position','absolute');
            $('.nav-interior').removeClass('fixed');
            $('.nav-container').css({
                position: 'absolute',
                top: top+"px"

            });
        }

    });
}

/* --------------------------
 * ACCORDION
 * -------------------------- */

function certifiednutrition_accordion() {

    // Show first pane and hide others by default
    $('.bundles .package').each(function() {
        if(!$(this).hasClass('first')){
            $(this).addClass('closed');
           $('div.content', this).hide();
        }
    });

    // Show or hide each pane
    $('.bundles .package .head').click(function() {
        if($(this).parent('.package').hasClass('closed')){
            $(this).parent('.package').removeClass('closed').addClass('open');
        }
        else {
            $(this).parent('.package').removeClass('open').addClass('closed');
        }
        if(!$(this).parent().hasClass('first')){
            $(this).next().slideToggle('slow', function() {

            });
            return false;
        }
    });
}

function certifiednutrition_select_service() {
	$('#submit').click(function() {
		var selected_service = $('#service').val();

		if (selected_service == '-1')
		{
                    // don't do anything when the default value is selected
                    return;
		}
		else
		{
                    setup_other_tabs();
                    finish_tab_setup('services');
                    var targetOffset2 = scrollHeight[selected_service];
                    $('html,body').animate({scrollTop: targetOffset2}, 1000);
                }
	});
}

function certifiednutrition_request_appt() {
	$('.appointment.button').click(function() {
		var selected_service = $(this).attr('rel');
                finish_tab_setup('contact');
                $('#subject').val(selected_service+' Inquiry');
	});
}
