//global vars

//startApp function
function startApp()
{

	$('.featureImage img, .navContainer li img, .shape img').pngFix();
//
	$('nav li img').css('opacity', 0).show();
	
	if ( !$.browser.msie ) {
		$('nav a').mouseover(function(){
			$(this).parent().parent().find('img').stop().animate({
			    opacity: 1
			  }, 600);
		});
		$('nav a').mouseout(function(){
			$(this).parent().parent().find('img').stop().animate({
			    opacity: 0
			  }, 350);
		});
	} else {
		if($.browser.version != 6)
		{
			$('nav a').mouseover(function(){
				$(this).parent().parent().find('img').stop().animate({
				    opacity: 1
				  }, 600);
			});
			$('nav a').mouseout(function(){
				$(this).parent().parent().find('img').stop().animate({
				    opacity: 0
				  }, 350);
			});
		}
	}
	
	//
	
	$('.featureInfo').css({marginLeft: '-140px', opacity: 0})
	$('.homeBg').css({marginLeft: '140px', opacity: 0})
	$('a.findOutMore, ul.nav').css({opacity: 0});
	$('.sh1, .sh2, .sh3, .sh4, .sh5, .sh6, .sh7, .sh8').css({opacity: 0, marginLeft: '-39px', marginTop: '70px'})
	
	$('.featureImage').css({marginLeft: '-70px', opacity: 0});
	
	//
	
	$('nav li:first-child').addClass('first')
	
	//
	
	
	$nav = '';

	$('ul.text li').each(function(i){
	
		$nav += '<li><a href="#" class="sprite">'+i+'</a></li>';
	});
	$('ul.nav').append($nav);
	$('ul.nav a').click(function(e){
		e.preventDefault();
		clearInterval(autoplayTimer);
		autoplayTimer = setInterval("autoplay()", 7000);
		$id = parseInt($(this).text());
		slideTextOut($id);
	})
	
	/*

	movementAmount = 40;
	pWidth = $('.personalTrainingLink').width();
	cWidth = $('.chiropracticLink').width();
	
	$('.personalTrainingLink').mouseover(function(){
		$('.personalTraining').stop().animate({
			    width: pWidth + movementAmount,
			    marginLeft: -(movementAmount/2)
		}, 350);
		//$('.chiropractic').stop().animate({
		//	    width: cWidth-(movementAmount/2) + 2
		//}, 350);
	});
	$('.personalTrainingLink').mouseout(function(){
		$('.personalTraining').stop().animate({
			    width: pWidth,
			    marginLeft: 0
		}, 350);
		$('.chiropractic').stop().animate({
			    width: cWidth + 2
		}, 350);
	});
	//
	$('.chiropracticLink').mouseover(function(){
		$('.chiropractic').stop().animate({
			    width: pWidth+movementAmount
		}, 350);
		$('.personalTraining').stop().animate({
			    width: pWidth -(movementAmount/2),
			    marginLeft: (movementAmount/2)
		}, 350);
	});
	$('.chiropracticLink').mouseout(function(){
		$('.chiropractic').stop().animate({
			    width: cWidth
		}, 350);
		$('.personalTraining').stop().animate({
			    width: pWidth,
			    marginLeft: 0
		}, 350);
	});
*/
	$('.personalTrainingLink').mouseover(function(){
		$('.personalTraining img').animate({
			    marginLeft: '-20px'
		}, 350);
	});
	$('.personalTrainingLink').mouseout(function(){
		$('.personalTraining img').animate({
			    marginLeft: '0px'
		}, 350);
	});
	$('.chiropracticLink').mouseover(function(){
		$('.chiropractic img').animate({
			    marginLeft: '20px'
		}, 350);
	});
	$('.chiropracticLink').mouseout(function(){
		$('.chiropractic img').animate({
			    marginLeft: '0px'
		}, 350);
	});
	

  $(".backToTop").click(function(){
    $('html, body').animate({scrollTop:0}, 300);
  });

	changeText(0, true);
	fadeInBg();

}

//common functions

//functions
var timer,
	currentTextId,
	buttonTimer,
	bgTimer,
	imgTimer,
	textBgTimer,
	autoplayTimer,
	counter = 0;
	
function changeText(id)
{
	slideTextIn(id, true);
	autoplayTimer = setInterval("autoplay()", 7000);
}

function autoplay()
{
	var len = $('ul.text li').length;
	counter++
	if(counter >= len)
	{
		counter = 0;
	}
	slideTextOut(counter)
	//
}
function slideTextIn(id, firstTime)
{
  var href = $('ul.text li:eq('+id+')').find("span").html();
  $(".featureButtons .findOutMore").attr("href", href);
	$('ul.nav li:eq('+id+')').addClass('selected');
	if(firstTime)
	{
		$('ul.text li:eq('+id+')').css({opacity: 0, marginLeft: '-140px'}).show().animate({
			    opacity: 1,
			    marginLeft: 0
		}, 900);
	} else {
		$('ul.text li:eq('+id+')').css({opacity: 0, marginLeft: '-140px'}).show().animate({
			    opacity: 1,
			    marginLeft: 0
		}, 600);
	}
  
	currentTextId =  id;
	counter = id;
}



function slideTextOut(id)
{
	$('ul.nav li:eq('+currentTextId+')').removeClass('selected');
	$('ul.nav li:eq('+id+')').addClass('selected');
	$('ul.text li:eq('+currentTextId+')').animate({
		    opacity: 0,
		    marginLeft: '-100px'
	}, 350, function(){
		$(this).hide();
		slideTextIn(id);
	});

}

function fadeInButton(){
	clearInterval(buttonTimer)
	$('a.findOutMore, ul.nav').animate({
		opacity: 1
	}, 800);
}

function fadeInBg(){
	fadeInShapes();
	$('.homeBg').animate({
		opacity: 1,
		marginLeft: 0
	}, 600);
	fadeInImg();
}

function fadeInImg(){
	clearInterval(imgTimer);
	$('.featureImage').css({marginLeft: '-70px', opacity: 0}).animate({
		    marginLeft: 0,
		    opacity: 1
	}, 850);
	
	textBgTimer = setInterval ( "fadeInTextBg()", 200 );
}

function fadeInTextBg()
{
	clearInterval(textBgTimer)
	$('.featureInfo').animate({
		    marginLeft: 0,
		    opacity: 1
	}, 600);
	
	buttonTimer = setInterval ( "fadeInButton()", 450 );
}

var fade1,
	fade2,
	fade3,
	fade4,
	fade5,
	fade6,
	fade7,
	fade8;
function fadeInShapes(){
	$('.sh2').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
	
	fade2 = setInterval ( "sh2()", 300 );
	fade3 = setInterval ( "sh3()", 100 );
	fade4 = setInterval ( "sh4()", 300 );
	fade5 = setInterval ( "sh5()", 000 );
	fade6 = setInterval ( "sh6()", 100 );
	fade7 = setInterval ( "sh7()", 300 );
	fade8 = setInterval ( "sh8()", 000 );
}
function sh2(){
	clearInterval(fade2);
	
	$('.sh1').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}
function sh3(){
	clearInterval(fade3);
	
	$('.sh3').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}
function sh4(){
	clearInterval(fade4);
	
	$('.sh4').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}
function sh5(){
	clearInterval(fade5);
	
	$('.sh5').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}
function sh6(){
	clearInterval(fade6);
	
	$('.sh6').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}
function sh7(){
	clearInterval(fade7);
	
	$('.sh7').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}
function sh8(){
	clearInterval(fade8);
	
	$('.sh8').animate({
		    opacity: 1,
		    marginLeft: 0,
		    marginTop: 0
	}, 500);
}


//static functions
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

//ie fix, black aliasing on faded images, be sure to have the right image path
$.fn.pngFix = function() {
  if (!$.browser.msie || $.browser.version >= 9) { return $(this); }

  return $(this).each(function() {
    var img = $(this),
        src = img.attr('src');

    img.attr('src', 'resources/images/x.gif')
        .css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src='" + src + "')");
  });
};


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
var contactValidation = {
  firstName: null,
  email: null,
  phone: null,
  cb1: null,
  cb2: null,
  btn: null,
  init: function(){
    contactValidation.btn.click(contactValidation.on_submit);     
  },
  on_submit: function(e){
    var isValid = true;
    $("form .errorDes").hide();
    if(contactValidation.firstName.val().trim().length == 0){
      isValid = false;
      contactValidation.firstName.parent().find(".errorDes").show();
    }
    if(contactValidation.phone.val().trim().length == 0){
      isValid = false;
      contactValidation.phone.parent().find(".errorDes").show();
    }
    if(contactValidation.email.val().trim().length > 0){
      var re = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
      if(!contactValidation.email.val().match(re)){
        isValid = false;
        contactValidation.email.parent().find(".errorDes").show();
      }
    }
    if( (!contactValidation.cb1.is(":checked")) && (!contactValidation.cb2.is(":checked")) ){
      isValid = false;
      contactValidation.cb1.parent().parent().find(".errorDes").show();
    }

    if(!isValid){
      e.preventDefault();
    }
  }
}
