"use strict";
/****** ANIMATION *****/
var wow = new WOW(
  {
    animateClass: 'animated',
    offset:       100,
    callback:     function(box) {
      //console.log("WOW: animating <" + box.tagName.toLowerCase() + ">")
    }
  }
);
wow.init();

/****** BACK TO TOP *****/
$(window).scroll(function() {
    if ($(document).scrollTop() > 500) {
        $('#t-back_to_top').fadeIn();
    } else {
        $('#t-back_to_top').fadeOut();
    }
});

$(document).ready(function(){
    $('#t-back_to_top').on('click', function(){
       $('html, body').animate({scrollTop : 0},800);
		   return false;
    }); 

    $('#t-header_mobile_nav').on('click', function(){
        $('#header_nav').slideToggle('slow');
        return false;
    }); 

    ColorSelector();
});  

$(window).resize(function(){
  var width = $(window).width();
  if(width > 767)
  {
    $('#header_nav').css('display','block');
  }
  else
  {
    $('#header_nav').css('display','none');
  }
});

$(window).load(function() {
  setTimeout(
  function() 
  {
    $("body").removeClass('t-overlay');
  }, 1500);
});

function ColorSelector(){
      $("#t-color_selector_list > li > span").on('click', function(event) {
        $("#t-color_selector_list > li > span i.fa-check").removeClass('fa-check');
        $(this).find('i').addClass('fa-check');  
        $( "body" ).addClass('t-overlay');
        ChkIt(this);
        return false;
    });
}

function ChkIt(color_rels){
    var Themes = {
      "red":"assets/css/theme/main_red.css",
      "mid_blue":"assets/css/theme/main_mid_blue.css",
      "nephritis":"assets/css/theme/main_nephritis.css",
      "green_sea":"assets/css/theme/main_green_sea.css",
      "orange":"assets/css/theme/main_orange.css",
      "pink":"assets/css/theme/main_pink.css",
      "yellow":"assets/css/theme/main_yellow.css",
      "saddle":"assets/css/theme/main_saddle.css",
      "main":"assets/css/main.css"
    };
    var color_rel = $(color_rels).attr("data-rel");
      if(Themes.hasOwnProperty(color_rel)){ 
        $(document).find("link#ThemeRoller").remove();
        SwitchTheme(Themes[color_rel]);
        /*$(document).find("link#ThemeRoller").attr("href",Themes[color_rel]);*/
      }
    
}

function SwitchTheme(href){
  var wjs = document.createElement('link');
  wjs.type = 'text/css';
  wjs.id = 'ThemeRoller';
  wjs.async = true;
  wjs.rel="stylesheet";
  wjs.onload = function(){
    $("body").removeClass('t-overlay');
  };
  wjs.href = href;
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(wjs);
}

$(document).on("click","a[href^=#]",function(e){
  var str = document.URL;
      var i = str.lastIndexOf('/');
      if (i != -1) {
          str = str.substr(0, i) + "/";
      }

  e.preventDefault();
  var hashtag = jQuery(this).attr("href");
  if(hashtag.length>1){
    window.location.href = str+hashtag;
  }
}); 