﻿
$(document).ready(function() {
    //CheckSplashCookie();
    CheckTextSizeCookie();

    SetTechHover("Tech1", "hoverFirst");
    SetTechHover("Tech2", "hoverMiddles");
    SetTechHover("Tech3", "hoverMiddles");
    SetTechHover("Tech4", "hoverLast");

    SetTopMenuHover();

    DeleteBannerContentContainer();

    jQuery(".SlideOpen").next("div").hide();
    jQuery(".SlideOpen").bind("click", function() {
        jQuery(this).next("div").slideToggle(300);
    });

    var footer = jQuery("#Footer");
    var ul = jQuery("ul", footer);
    ul.each(function() {
        var li = jQuery("li:last", jQuery(this)).css("border-right", "0");        
    });
});

function CheckSplashCookie() {
    // A function for setting a splashcookie and showing the splashscreen if no splashcookie is present
    var cookie = readCookie('splashCookie');
    if (cookie == null) {
        ShowSplash();
        createCookie('splashCookie', '1', parseInt(hoursToShowAgain));
    }    
}

// Functions for managing cookies - create, read and delete
function createCookie(name, value, hours) {
    if (hours) {
        var date = new Date();
        //date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        date.setTime(date.getTime() + (1 * hours * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

// Functions for showing and hiding the splashscreen
function ShowSplash() {
    var time = parseInt(secondsToShow + "000");   
    var splash = document.getElementById('splash');
    $("#splash").show("fold", {}, 1500);
    splash.style.display = 'block';
    var timeOutId;    
    $("#splash").bind("click", function() {
        clearTimeout(timeOutId);
        $("#splash").hide("fold", {}, 1500);
    });
    timeOutId = setTimeout("$('#splash').hide('fold', {}, 1500)", time);    
}

function HideSplash()
{
    var splash = document.getElementById('splash');
    splash.style.display = 'none';
}

// Functions for hovering top menu images in the masterpage

function TopMenuItem(id, normal, hover) {
    this.id = id;
    this.normal = normal;
    this.hover = hover;
}

var items = [];
function SetTopMenuHover() {
    var menu = document.getElementById("TopMenu");
    var menuitems = $(".TopMenuItemHoverClass", menu);

    menuitems.each(function() {
        var currentMenuItem = this;
        var item = null;
        $.each(items, function() {
            if (this.id == currentMenuItem.id)
                item = this;
        });
        
        if (item != null) {
            $(currentMenuItem).hover(function() {
                currentMenuItem.src = item.hover;
            }, function() {
                currentMenuItem.src = item.normal;
            });
        }
    });
}

// Functions for hovering images on the frontpage in a tech selection context

function SetTechHover(hoverSubject, css) {    
    $(document.getElementById(hoverSubject)).hover(
      function () {
    $(this).addClass("hover "+css);
      },
      function () {
      $(this).removeClass("hover "+css);
      }
    );
  }

// Functions for increasing and decreasing text size

  var standard = 0.6875; // 0.6875em = 11px (11 / 16 = 0.6875);
  var minFontSize = 0.5;
  var maxFontSize = 1;
  var increment = 0.0625;

  function ChangeTextSize(increaseOrDecrease) {
      var fontsize = parseFloat(document.body.style.fontSize.replace("em", ""));      
      var realsize = GetRealFontSize(fontsize);
      
      if (increaseOrDecrease == "increase") {
          if (realsize < maxFontSize) {
              realsize = realsize + increment;
              document.body.style.fontSize = realsize + "em";
          }
      }
      else if (increaseOrDecrease == "decrease") {
          if (realsize > minFontSize) {
              realsize = realsize - increment;
              document.body.style.fontSize = realsize + "em";
          }
      }
      else if (increaseOrDecrease == "reset") {
          realsize = standard;
          document.body.style.fontSize = realsize + "em";
      }

    CheckServiceBarWidth(realsize);    
    createCookie("fontSizeCookie", realsize, 365);
}

function CheckTextSizeCookie() {
    // This functions checks for a fontsizecookie. If present, the fontsize is based of the cookievalue.
    var cookie = readCookie('fontSizeCookie');

    if (cookie != null) {
        var fontsize = cookie;
        var realsize = GetRealFontSize(fontsize);
        document.body.style.fontSize = realsize + "em";
        CheckServiceBarWidth(realsize);
    }    
}

function CheckServiceBarWidth(fontsize) {
    // This function makes sure that the servicebar is wide enough when a very large fontsize is chosen.
    var servicebar = document.getElementById('ServiceBar');
    if (fontsize >= 0.8125) {
        servicebar.style.width = "890px";
    }
    else {
        servicebar.style.width = "780px";
    }
}

// Hack for IE, since IE rounds of the fontsize returned from the body tag otherwise resulting in wrong calculations
function GetRealFontSize(fontsize) {

    if (fontsize < 0.5)
        return 0.5;
    if (fontsize > 1)
        return 1;
    
    var fontArray = new Array(0.55, 0.62, 0.68, 0.74, 0.81, 0.87, 0.93, 0.99);
    var realFontArray = new Array(0.5625, 0.625, 0.6875, 0.75, 0.8125, 0.875, 0.9375, 1);

    var size = fontsize;

    for (var i = 0; i < fontArray.length; i++) {        
        if (size == fontArray[i]) {
            size = realFontArray[i];            
            break;
        }
    }

    return size;
}

// Small hack to delete the BannerContent div in case there is no banner. It is necessary to make the print version look nice :)
// There might be a better way to solve this problem ;)
function DeleteBannerContentContainer() {
    var container = document.getElementById('BannerContent');
    if (container != null) {
        if (container.childNodes.length <= 1) {
            document.getElementById('Content').removeChild(container);
        }
    }    
}