function advanceTabs(el1,el2){
	if(!$(this).hasClass('active')){
			
			$('#menu a.active').removeClass('active');
			el1.addClass('active');
			if(!$('#content').hasClass('open'))
				{
				$('html, body').animate({
					scrollTop: $("#menu").offset().top 
					}, 1000, function() {
					   		
							el2.slideDown(1500, function() {
							$('#content').addClass('open');
							el2.addClass('active');
							if(((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) && el2.hasClass("bg"))
								$('#goToTop').fadeIn('slow');
  							});
  				});
  						

				}
			else
				{
				$('#content .panel.active').fadeOut('fast',function(){
					$(this).removeClass('active');
					el2.fadeIn();
					el2.addClass('active');
					if(((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) && el2.hasClass("bg"))
						$('#goToTop').fadeIn('slow');
  					else
  						$('#goToTop').fadeOut('slow');

				});
				}
		}
};



$(document).ready(function(){

	$('#bgSlider').nivoSlider({
        effect:'fade',
        controlNav:false, // 1,2,3... navigation
        manualAdvance:true // Force manual transitions
    });
    
    $('#goToTop').click(function(evt){
		$('html, body').animate({
			scrollTop: '500px'
			}, 1000
		);
	});
		   
    if ($.browser.webkit) {
		$(document).keydown(function(e){
    		if (e.keyCode == 37) { 
       			$('#bgSlider a.nivo-prevNav').trigger("click");
      			return false;
    		}
    		else if (e.keyCode == 39) { 
       			$('#bgSlider a.nivo-nextNav').trigger("click");
      			return false;
    		}
		});
  	}
  	
  	$('#bgSlider').wipetouch({
			wipeLeft: function(result) { $('#bgSlider a.nivo-nextNav').trigger("click"); },
			wipeRight: function(result) { $('#bgSlider a.nivo-prevNav').trigger("click"); }
	});
	
	$('a.bg').click(function(evt){
		evt.preventDefault();
		advanceTabs($(this),$('#bg'));
	});
	
	$('a.change').click(function(evt){
		evt.preventDefault();
		advanceTabs($(this),$('#change'));
	});
	
	$('a.contact').click(function(evt){
		evt.preventDefault();
		advanceTabs($(this),$('#contact'));
	});
	

   if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))|| (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/android/i))) {
      	$('#bgSliderLegend').hide();
      	$('#bgSliderLegendiTouch').show();
     	$("#goToTop").removeClass('classic');
     	$("#goToTop").addClass('touch');
   }
   
  
   if((!navigator.userAgent.match(/iPhone/i)) && (!navigator.userAgent.match(/iPod/i)) && (!navigator.userAgent.match(/iPad/i))) {
	  var top = $('#menu').offset().top - parseFloat($('#menu').css('marginTop').replace(/auto/, 0));
	  
	  $(window).scroll(function (event) {
	    // what the y position of the scroll is
	    var y = $(this).scrollTop();
	  
	    // whether that's below the form
	    if (y >= top) {
	      // if so, ad the fixed class
	      $('#menu').addClass('fixed');
	      $('#goToTop').fadeIn('slow');
	    } else {
	      // otherwise remove it
	      $('#menu').removeClass('fixed');
	      $('#goToTop').fadeOut('slow');

	    }
	  });
   }
   

	
});

