

jQuery.validator.addMethod("phone", function(value, element) {
	return this.optional(element) || /^(\d{2,4})(\-{0,1})(\d{5,8})$/.test(value);
}, "Ange ett korrekt telefonnummer");  





var pageIndex = 0;
var subPageIndex = 0;

var activePage, activeSubPage;

var overMenu = false;
var menuMouseX = 0;


var activeForm;

function init() {
   
   
   //referenserSpecial();
   
   formatSubPageId();
   
   $( "#menu-container" )
      .hover( function() { overMenu = true; }, function() { overMenu = false; } )
      .mousemove( 
         function( page ) {
            menuMouseX = page.pageX;
         }
      );
   
   $( "form" ).each(
      function() {
      
         $( this ).validate({
   			submitHandler: function(form) {
   			   activeForm = $( form ).ajaxSubmit( removeForm );
   			}
   		})   
      }
   );
   
   
   $( ".page" ).each(
      function() {
         $( this ).find( ".sub-page:last" ).css({ "border-bottom": 0 });
      }
   );
   
   
   $( window ).hashchange( navigation ).hashchange();
   $( window ).resize( resize );
   
   resize();
   
   
   setInterval( loopMarker, 40 );
   
   
   
   // fulo för att ta bort typekit-loggan.
   setTimeout( function() { $( ".typekit-badge, #typekit-badge-vha0diw" ).css({ "z-index": -1 }) }, 50 );
   
   
   var bigLogo = $( "#big-logo img" );
   var bigLogoY = $( window ).height() * 0.5 - 50;
   
   bigLogo.css({ "margin-top": bigLogoY });
   
   setTimeout( fadeToPage, 3000 );
   
}
$( document ).ready( init );



function removeForm( data ) {   				     
   
   if( !data ) {
      
      alert( "Fel i formuläret. Försök igen." );
      return;
      
   }
   
   var two = data.split( ";" );
   
   var html = $( "<div>" ).addClass( "form-feedback" ).append( $( "<h3>" ).addClass( "red" ).html( two[ 0 ] ) );
   
   if( two.length > 1 ) html.append( $( "<p>" ).html( two[1] ) );
   activeForm.replaceWith( html );
}





var subPageAnimateTime = 400;
var subPageContentAnimateTime = 300;

function resize() {

   if( activePage == null || activeSubPage == null ) return;
   
   var bodyHeight = $( window ).height();
   var menuHeight = $( "#menu-container" ).outerHeight();
   
   var mainHeight = ( bodyHeight - menuHeight );
   
   var subPageHeaderHeight = 30;
   var subPageBodyHeight = mainHeight - subPageHeaderHeight * ( $( ".page-active .sub-page" ).length - 1 ) - 15;
   
   //$( "#main-container" ).css({ height: mainHeight });
   
   
   
   
   var subPageMarginLeft = Math.max( 150, $( window ).width() * 0.5 - $( ".inner-sub-page:eq(1)" ).innerWidth() * 0.5 );
   $( ".inner-scroll-container" ).css( { "padding-left": subPageMarginLeft } );
   
   $( ".sub-page" ).each( 
      function() {
      
         var active = $( this ).hasClass( "sub-page-active" );
         var scrollContainer = $( this ).find( ".scroll-container" );
         
         
         
         
         if( !active ) {
            scrollContainer
               .find( ".inner-scroll-container" ).animate({ "padding-top": 0 }, subPageContentAnimateTime );
         }
         
         scrollContainer
            .clearQueue()
            .animate(
               { height: ( active ? subPageBodyHeight : subPageHeaderHeight ) }, 
               subPageAnimateTime, 
               'swing',
               function() {
                  
                  var active = $( this ).parent().hasClass( "sub-page-active" );
                  $( this ).css({ "overflow-y": ( active ? "auto" : "hidden" ) });
                  if( !active ) return;
                  
                  var content = $( this ).find( ".inner-scroll-container" );
                  var ty = 
                  ( 
                     active && subPageBodyHeight > content.outerHeight() ? 
                     ( subPageBodyHeight - content.outerHeight() ) * 0.5 : 0 
                  );
                  
                  content.animate({ "padding-top": ty }, subPageContentAnimateTime );
               } 
               
            );

         
      }
   );
   
   
}


function navigation() {
   var hash = document.location.hash;
   
   var newHash = false;
   
   
   if( hash.length <= 1 || $( "#sub-page-" + hash.substr( 1 ) ).length == 0 ) {
      newHash = "#" +  $( ".page:eq(0) .sub-page:eq(0)" ).attr( "id" ).substr( 9 );
   }
   if( hash.match( /^\#[0-9]\-[0-9]/gi) ) {
      
      var t = hash.substr( 1 ).split( "-" );
      var pi = ( parseInt( t[0] ) - 1 );
      var spi = ( parseInt( t[1] ) - 1 );
      
      var temp = "#" +  $( ".page:eq(" + pi + ") .sub-page:eq(" + spi + ")" ).attr( "id" ).substr( 9 );
      
      newHash = temp;
   }
   
   
   //if( hash.length <= 2 ) newHash = "#1-1";
   //else if( hash.indexOf( "-" ) == -1 ) newHash = hash.substr( 0, 2 ) + "-1";
   
   if( newHash ) {  
      location.hash = newHash;
      return;
   }
   
   
   var spId = "#sub-page-" + hash.substr( 1 );
   var spIndex = $( spId ).index();
   var pIndex = $( spId ).parent().index();
      
   loadPage( pIndex, spIndex );
   
   //var indexes = hash.substr( 1 ).split( "-" );
   //loadPage( parseInt( indexes[0] ) - 1, parseInt( indexes[1] ) - 1 );
   
   return false;
}


function loadPage( index, subIndex ) {
   
   pageIndex = index;
   subPageIndex = subIndex;
   
   $( ".menu" ).removeClass( "menu-active" );
   $( ".menu-" + ( pageIndex + 1 ) +":not( .no-marker )" ).addClass( "menu-active" );
   
   
   var pId = "#" + pageIndex;
   var sId = "#" + subPageIndex;
   
   $( ".page:not( .page-"+ pageIndex +")" ).removeClass( "page-active" );
   activePage = $( ".page-" + pageIndex ).addClass( "page-active" );
   
   $( ".sub-page:not( .sub-page-" + subPageIndex + " ) " ).removeClass( "sub-page-active" );
   activeSubPage = activePage.find( ".sub-page-" + subPageIndex ).addClass( "sub-page-active" );
   
   resize();
}






function addParticipant() {

   var participant = $( "#course-form .participant:last" );
   var newParticipant = participant.clone( true );
   participant.after( newParticipant );
   
   
   var numOfParticipants = $( ".participant" ).length;
   
   newParticipant.find( ".participants-changes" ).each(
      function() {
         
         if( $( this ).is("label") ) {
            $( this ).remove();
            return;
         }
         
         var id = $( this ).attr( "id" );
         var newId = id.substr( 0, id.length -1 ) + ( numOfParticipants - 1 );
         $( this ).attr( { id: newId });
      
      }
   );
}





function loopMarker() {
   
   var marker = $( "#marker" );
   var offset = marker.offset();
 
   if( offset.left == undefined ) return;
   
   var activeMenuCenter = ( $( ".menu-active" ).length > 0 ? $( ".menu-active" ).offset().left + $( ".menu-active" ).innerWidth() * 0.5 : 0 );
   
   var oldX = offset.left;
   var toX = ( overMenu ? menuMouseX : activeMenuCenter ) - marker.innerWidth() * 0.5; 
   
   var newX = oldX + ( toX - oldX ) * 0.3;
   marker.css({ left: newX });
   
}



function fadeToPage() {
   
   $( "#big-logo" ).fadeOut( 500, function() { $( this ).remove(); } );
   
}





function referenserSpecial() {

   var dom = $( "#referenser" );
   
   var html = dom.html();
   html = html.replace( /\<br\s{0,1}\/{0,1}\>/gi, '<br>' );
   
   var list = html.split( "<br>" );
   
   var listDom = $( "<div>" ).css({ "width": "100%" });
   for( var i=0; i<list.length; i++ ) {
      list[i] = list[i].replace(/^\s+|\s+$/g, '');
      listDom.append( $( "<span>" ).text( list[i] ).css({ "float": "left", "width": "30%" }) );
   }
   
   listDom.append( $( "<div>" ).addClass( "clearer" ) );
   
   dom.empty().append( listDom ).css({ "-webkit-column-count": "1", "-moz-column-count": "1" });
}


function formatSubPageId() {
   
   
   $( ".sub-page" ).each( 
      function() {
         var id = $( this ).attr( "id" )
            .replace( /[åäÅÄ]/g, 'a' )
            .replace( /[öÖ]/g, 'o' )
            .replace( /\s/g, '_' )
            .replace( /\&/g, 'och' )
            .toLowerCase();
            
         $( this ).attr({ id: id });
      }
   );
   
   
   
   $( "a" ).each( 
      function() {
         
         
         var to = $( this ).attr( "href" );
         if( to.match( /^\#[0-9]\-[0-9]/gi) ) {
      
            var t = to.substr( 1 ).split( "-" );
            var pi = ( parseInt( t[0] ) - 1 );
            var spi = ( parseInt( t[1] ) - 1 );
            
            var temp = "#" +  $( ".page:eq(" + pi + ") .sub-page:eq(" + spi + ")" ).attr( "id" ).substr( 9 );
            
            $( this ).attr( { href: temp } );
         }
      
      }
   );
   
   
}
