/*
Video Controls
by: Adrian Gyuricska @ Lift Interactive
*/
try{Typekit.load();}catch(e){}

var scrolltop
var url = $.url();

function secondarynav(){
  if($('#secondarynav-holder').length){
    var scrolltop = $(window).scrollTop();
    var position = $('#secondarynav-holder').position();
    
    if(scrolltop > position.top+20){
      $('#secondarynav:not(.fixed)').addClass('fixed').animate({'opacity':0.95}, 500);
    }else{
      $('#secondarynav.fixed').removeClass('fixed').attr('style','');
    };
  };
};

function bannerSize(){
  var w = $(window).width();
  $('#banner, #banner ul, #banner li').css({'width':w});
  
/*
  $('#bannerimages img').each(function(){
    var iW = $(this).width();
    if(window.console) window.console.log(iW);
    $(this).css({'margin-left':-iW/2+'px'});
  });
*/
};

$(window).resize(function(){
  bannerSize();
});

$(window).scroll(function(){
  secondarynav();
});

$(function(){
  bannerSize();
  secondarynav();
  
  var availSegment = ['global', 'canada', 'india', 'indonesia', 'south-korea', 'thailand', 'vietnam', 'czech-republic', 'hungary', 'poland', 'romania'];
  
  if(jQuery.inArray(url.segment(1), availSegment) >= 0 ){
    console.log('set cookie', url.segment(1))
    $.cookie('country', url.segment(1), { expires: 7, path: '/', domain: 'scholarshipandmore.org' });
  };
  
  var country = $.cookie('country');
  var page = url.segment(2);
    
  if(jQuery.inArray(url.segment(1), availSegment) < 0){
    $('nav#mainnav ul a, #footernav a').each(function(){
      var href = $(this).attr('href')
      $(this).attr('href', href.replace('global', country));
    });
    
    if(url.segment(1) == 'cee'){
      $('nav#mainnav ul a[title="FAQ"], #footernav a[title="FAQ"]').each(function(){
        var href = $(this).attr('href')
        $(this).attr('href', href.replace(country, 'cee'));
      })
    }
  };
  
  if(url.attr('path') == '/global/home/'){
    if(url.param('modal') == 'true'){
      $('#country').after('<img src="/media/images/select-tip.png" id="select-tip" />');
      $('#select-tip').delay('6000').fadeOut();
/*       $('#country-select').modal(); */
    }
  }
  
  $('a[href^="http://"]').addClass('ext').attr('target', '_blank');

  $('#search .text').defaultText();
  
  $('nav#mainnav ul li:first-child').children('a').html('<img src="/media/images/icon-home.png" class="icon" />');
  
  $('#country-list li a[href*='+country+']').addClass('current');
  
  $('#country-list li a, #country-select a.global').each(function(){
    var org = $(this).attr('href');
    
    $(this).attr('href', org.replace('home', page));
  });
  
  if(country == 'global'){
    $('nav#mainnav a#country').html('<img src="/media/images/icon-select.png" class="select" /> Select a Country');
  }else{
    var text = country.replace('-', ' ');
    $('nav#mainnav a#country').html('<img src="/media/images/flag-'+ country +'.jpg" class="icon" /> '+ text);
  }
  
  if($('#country_list').length){
    $('#country_list a').each(function(){
      var link = $(this).url();
      var what = link.segment(1);
      $(this).prepend('<img src="/media/images/flag-'+ what +'.jpg" class="icon" /> ');
    });
  };
  
  if($('#secondarynav-holder').length){
    $('#secondarynav a').click(function(scroll){
      $(window).scrollTo( $(this).attr('href'), 350, { offset:-110 } );      
      return false;
    });
    
    $('#main-content h1, #main-content h2').waypoint(function() {
      var id = $(this).attr('id');
      $('#secondarynav a[href*='+id+']').addClass('current').parents().siblings().children('a').removeClass('current');
    }, {
      offset: 200
    });
    
    //  override default named anchor on page load; activate scroll to point with proper offset
    if ( url.attr('fragment') ) {
      $('#secondarynav a[href=#'+url.attr('fragment')+']').trigger('click');
      return false;
    };
    
  };
  
  if($('#faq').length){
    $('#faq a').click(function(scroll){
      $(window).scrollTo( $(this).attr('href'), 350, { offset:-70 } );      
      return false;
    });
  };
  
  if($('#banner').length){
    $('#banner ul').cycle({ 
      fx:     'fade',
      slideResize: 0,
      before: function(){
        $(this).find('.bg').css({'opacity':0}).animate({'opacity':1}, 750, 'linear');
        $(this).find('.kid').css({'left':350}).animate({'left':50}, 750, 'linear');
        $(this).find('.left').css({'left':450}).animate({'left':0}, 750, 'linear');
        $(this).find('.right').css({'right':'-400px'}).animate({'right':'-100px'}, 750, 'linear');
        $(this).find('.text').css({'right':'-50px'}).animate({'right':'0px'}, 750, 'linear');
      },
      after:  function(){
        $(this).find('.bg').delay('5000').animate({'opacity':'0'}, 750, 'linear');
        $(this).find('.kid').delay('5000').animate({'left':'-200px'}, 750, 'linear');
        $(this).find('.left').delay('5000').animate({'left':'-500px'}, 750, 'linear');
        $(this).find('.right').delay('5000').animate({'right':'-10px'}, 750, 'linear');
        $(this).find('.text').delay('5000').animate({'right':'50px'}, 750, 'linear');;
      },
      delay   : -2000,
      timeout : 6000,
      sync    : 0
    });
  };
  
  $('#country-list li a.disabled').click(function(){
    return false;
  });
  
});

