/*
//  Modules
 */
var tele2nl = {
    modules:{}
};
// Javascript for Homepage tabs
tele2nl.modules.tabhome = function()
{
    $(document).ready(function(){
        $('.home-tab-content').hide(); // Hide all divs
        $('#tab-1').show(); // Show the first div
        $('#home-tab-1').addClass('active'); // Set the class of the first link to active
    $('#tabs ul li a').click(function(){ //When any link is clicked
        $('#tabs ul li').removeClass('active'); // Remove active class from all links
        $(this).parent().addClass('active'); //Set clicked link class to active
        var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
        $('.home-tab-content').hide(); // Hide all divs
        $(currentTab).show(); // Show div with id equal to variable currentTab
        return true;
        });
    });
}
// Javascript for Content page tabs
tele2nl.modules.tab = function()
{
    $(document).ready(function(){
        $('.tab-content').hide(); // Hide all divs
        $('#tabs-tab-1').show(); // Show the first div
        $('#content-tab-1').addClass('active'); // Set the class of the first link to active
    $('#tabs-tabs ul li a').click(function(){ //When any link is clicked
        $('#tabs-tabs ul li').removeClass('active'); // Remove active class from all links
        $(this).parent().addClass('active'); //Set clicked link class to active
        var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
        $('.tab-content').hide(); // Hide all divs
        $(currentTab).show(); // Show div with id equal to variable currentTab
        return true;
        });
    });
}    
// Javascript for Content page tabs new, version 2
tele2nl.modules.c_tab = function()
{
    $(document).ready(function(){
        $('.c-tab').hide(); // Hide all divs
        $('#tab1').show(); // Show the first div
        $('#c-li-tab-1').addClass('active'); // Set the class of the first link to active
        var tabLink = location.hash; //check if a direct link was passed in the url .. like #tab2 
        if($(tabLink).length == 1){            
            var regEx = new RegExp("[0-9]");    // look for a number in the tabLink    
            var tabIndex = regEx.exec(tabLink); // get the number       
            if(tabIndex != undefined){
                $('.c-tab').hide(); // Hide all divs
                $(tabLink).show(); // Show the appropriate div
                $('.c-tabs ul li').removeClass('active'); // Remove active class from all links
                $('#c-li-tab-'+tabIndex).addClass('active'); // Set the class of the selected link to active
            }
        }
    $('.c-tabs ul li a').click(function(e){ //When any link is clicked
        e.preventDefault();
        $('.c-tabs ul li').removeClass('active'); // Remove active class from all links
        $(this).parent().addClass('active'); //Set clicked link class to active
        var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
        $('.c-tab').hide(); // Hide all divs
        $(currentTab).show(); // Show div with id equal to variable currentTab
        $('#showTab').val($(this).closest("li").attr("id"));
        return true;
        });
    });
    setTimeout('window.scrollTo(0, 0)',1);
}
// Javascript for carrousel
tele2nl.modules.carrousel = function()
{
    $(document).ready(function() {
        //Set Default State of each portfolio piece
        $(".carrousel-bottom").show();
        $(".carrousel-bottom a:first").addClass("active");
            
        //Get size of images, how many there are, then determin the size of the image reel.
        var imageWidth = $(".offers").width();//960;
        var imageSum = $(".offer").size();
        var imageReelWidth = imageWidth * imageSum;
        
        //Adjust the image reel to its new size
        $(".offers").css({'width' : imageReelWidth});
        
        //carrousel-bottom + Slider Function
        rotate = function(){    
            var triggerID = $active.attr("rel") - 1; //Get number of times to slide
            var offersPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
            $(".carrousel-bottom a").removeClass('active'); //Remove all active class
            $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
            
            //Slider Animation
            $(".offers").animate({ 
                left: -offersPosition
            }, 500 );
            
        }; 
        
        //Rotation + Timing Event
        rotateSwitch = function(){        
            play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
                $active = $('.carrousel-bottom a.active').next();
                if ( $active.length === 0) { //If carrousel-bottom reaches the end...
                    $active = $('.carrousel-bottom a:first'); //go back to first
                }
                rotate(); //Trigger the carrousel-bottom and slider function
            }, 7000); //Timer speed in milliseconds (3 seconds)
        };
        
        rotateSwitch(); //Run function on launch
        
        //On Hover
        $(".offers a").hover(function() {
            clearInterval(play); //Stop the rotation
        }, function() {
            rotateSwitch(); //Resume rotation
        });    
        
        //On Click
        $(".carrousel-bottom a").click(function() {    
            $active = $(this); //Activate the clicked carrousel-bottom
            //Reset Timer
            clearInterval(play); //Stop the rotation
            rotate(); //Trigger rotation immediately
            rotateSwitch(); // Resume rotation
            return false; //Prevent browser jump to link anchor
        });    
        
    });
}
    
// Javascript for FAQs
tele2nl.modules.faq = function()
{
    $(document).ready(function() {
            
        $('.faqs dd').hide(); // Hide all DDs inside .faqs
        $('.faqs dt').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')}).click(function(){ // Add class "hover" on dt when hover
        $(this).toggleClass('dohighlight');
        $(this).next().slideToggle('normal'); // Toggle dd when the respective dt is clicked
        }); 
        
    });
}
    
// Javascript settings for PrettyPhoto (lightbox)
tele2nl.modules.prettyphoto  = function()
{
    $(document).ready(function(){
        $("a[rel^='prettyphoto']").prettyPhoto({
            show_title: false, /* true/false */
            theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
        });
    });
}
    
// Javascript settings for zebra striping on tables
tele2nl.modules.zebra = function()
{
    $(document).ready(function(){ 
        $("tr:odd").addClass("odd");
    }); 
}
// Javascript settings for pngfix
tele2nl.modules.pngfix = function()
{
    $(document).ready(function(){
        $('div.not-available').pngFix( );
    });
}
// Javascript settings for togglebox
tele2nl.modules.togglebox = function()
{
    $(document).ready(function() {
     // hides the box as soon as the DOM is ready
      jQuery('#togglebox').hide();
     // toggles the box on clicking the noted link
      jQuery('a#slidetogglebox').click(function() {
        jQuery('#togglebox').slideToggle(400);
        return false;
      });
    });
}
// Javascript settings for managementchat
tele2nl.modules.managementchat = function()
{
   var chatOn= 'false';
   gochat = function(){
    if (chatOn == 'true'){
        window.open("http://www3.tele2.nl/chat/");
    }
    else {
        alert('Momenteel is de Management Chat niet actief');
    }
   };
}
// Javascript for countdown
tele2nl.modules.countdown = function()
{
(function($) {
  
  $.fn.countdown = function(toDate, callback) {
    var handlers = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'daysLeft'];
    
    function delegate(scope, method) {
      return function() { return method.call(scope) }
    }
    
    return this.each(function() {
      // Convert
      if(!(toDate instanceof Date)) {
        if(String(toDate).match(/^[0-9]*$/)) {
          toDate = new Date(toDate);
        } else if( toDate.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/) ||
            toDate.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/)
            ) {
          toDate = new Date(toDate);
        } else if(toDate.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})/) || 
                  toDate.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})/)
                  ) {
          toDate = new Date(toDate)
        } else {
          throw new Error("Doesn't seen to be a valid date object or string")
        }
      }
      
      var $this = $(this),
          values = {},
          lasting = {},
          interval = $this.data('countdownInterval'),
          currentDate = new Date(),
          secondsLeft = Math.floor((toDate.valueOf() - currentDate.valueOf()) / 1000);
      
      function triggerEvents() {
        secondsLeft--;
        if(secondsLeft < 0) {
          secondsLeft = 0;
        }
        lasting = {
          seconds : secondsLeft % 60,
          minutes : Math.floor(secondsLeft / 60) % 60,
          hours   : Math.floor(secondsLeft / 60 / 60) % 24,
          days    : Math.floor(secondsLeft / 60 / 60 / 24),
          weeks   : Math.floor(secondsLeft / 60 / 60 / 24 / 7),
          daysLeft: Math.floor(secondsLeft / 60 / 60 / 24) % 7
        }
        for(var i=0; i<handlers.length; i++) {
          var eventName = handlers[i];
          if(values[eventName] != lasting[eventName]) {
            values[eventName] = lasting[eventName];
            dispatchEvent(eventName);
          }
        }
        if(secondsLeft == 0) { 
          stop();
          dispatchEvent('finished');
        }
      }
      triggerEvents();
      
      function dispatchEvent(eventName) {
        var event = $.Event(eventName);
        event.date  = new Date(new Date().valueOf() + secondsLeft);
        event.value = values[eventName] || "0";
        event.toDate = toDate;
        event.lasting = lasting;
        switch(eventName) {
          case "seconds":
          case "minutes":
          case "hours":
            event.value = event.value < 10 ? '0'+event.value.toString() : event.value.toString();
            break;
          default:
            if(event.value) {
              event.value = event.value.toString();
            }
            break;
        }
        callback.call($this, event);
      }
      
      $this.bind('remove', function() {
        stop(); // If the selector is removed clear the interval for memory sake!
        dispatchEvent('removed');
      });
      
      function stop() {
        clearInterval(interval);
      }
      function start() {
        $this.data('countdownInterval', setInterval(delegate($this, triggerEvents), 1000));
        interval = $this.data('countdownInterval');
      }
      
      if(interval) stop();
      start();
    });
  }
  // Wrap the remove method to trigger an event when called
  var removeEvent = new $.Event('remove'),
      removeFunction = $.fn.remove;
  $.fn.remove = function() {
    $(this).trigger(removeEvent);
    removeFunction.apply(this, arguments);
  }
})(jQuery);}
// Javascript settings for countdown
tele2nl.modules.countdownsettings = function()
{
    $(function() {
      var currentDate = new Date();
      $('div.clock').countdown(new Date(2011, 11, 5), function(event) {
        $this = $(this);
        switch(event.type) {
          case "seconds":
          case "minutes":
          case "hours":
          case "days":
          case "daysLeft":
            $this.find('span#'+event.type).html(event.value);
            break;
          case "finished":
            $this.fadeTo('slow', .5);
            break;
        }
      });
    });
}

//Autocompletion for google search
var searchAsYouTypeConfiguration = {
    resourcesPath: "http://www.tele2.se/search1/",
    ajaxResponderUrl: "http://www.tele2.se/search1/Suggest.ashx",
    extendedArgs: "&x=nl",
    helpPageUrl: "",
    maxFullResults: 10,
    keystrokeDelay: 5,
    showResultsDelay: 10,
    bottomPageMargin: 10
};
$(document).ready(function () {
    if (typeof searchAsYouType != "undefined") {
        if ($('input[name="q"]').length) {
            searchAsYouType.initialize($('input[name="q"]').get(-1), true);
        }
    }
});
