﻿/// <reference path="/includes/scripts/jquery-1.4.1-vsdoc.js"/>

$(document).ready(function () {
    EnablePrintLink();
    EnableQuickSearchHintText();
    EnableToolTipsOnForms();
    HideLastDivider();
    HideEmptyDownloadListOnVideos();
    RegisterPDFClickSiteStat();
    RegisterClickOutClickSiteStat();
});

// Only show the print link when the user has support for JavaScript
function EnablePrintLink() {
    $("#printLink, #btnPrintDown").toggleClass("hide");
    $("#printLinkBottom, #btnPrintDown").toggleClass("hide");

    $(".printIcon").click(function (e) {
        window.print();
        return false;
    });
}

// quicksearch is the search field on top of the page
function EnableQuickSearchHintText() {

    $(":text.enableHintText").each(function (i) {
        if ($(this).hasClass("Invoerveld") == false) {
            $(this).addClass("initial")

            $(this).focus(function () {
                $(this).addClass("focus");
                if ($(this).attr("value").toLowerCase() == $(this).attr("title").toLowerCase()) { $(this).attr("value", "").removeClass("initial"); }
            });

            $(this).blur(function () {
                $(this).removeClass("focus");
                if ($(this).attr("value") == "") { $(this).attr("value", $(this).attr("title")); }
                if ($(this).attr("value").toLowerCase() == $(this).attr("title").toLowerCase()) { $(this).addClass("initial"); }
            });

            $(this).click(function (e) {
                // TODO: bubbling event in IE treedt op

                $(this).focus();
            });

            if ($(this).attr("value") == "") { $(this).attr("value", $(this).attr("title")); }
        }
    });

    // Fix voor Chrome en Safari.
    // Safari en chrome valideren input velden op hun 'value', 'maxlength' etc.
    // Door de hinttekst worden velden gevuld met een waarde die mogelijk langer
    // is dan de maxlength. Vervolgens wordt het formulier niet verstuurd (geen submit event).
    // Door een novalidate attribuut op true te zetten wordt het formulier niet
    // langer door Safari of Chrome gevalideerd. 
    $("form").attr("novalidate", "true");

    $("form").submit(function () {
        $(":input").each(function (i) { if ($(this).attr("title").toLowerCase() == $(this).attr("value").toLowerCase()) { $(this).attr("value", ""); } });
    });
}

// enables the '?' tooltip functionality on webforms
var counterTmp = 0;
function EnableToolTipsOnForms() {

    // icon is clicked
    $(".tooltipIcon").click(function (e) {
        // hide all other messages which are shown
        $(".tooltipMsg").hide();
        // show the one clicked
        $(".tooltipMsg", $(this).parent()).show();
        counterTmp = 0;
    });
   
    // when the center div is clicked and the tooltip is already shown, hide it
    $(".GCCenter").live("click", function (e) {
        if ($(".tooltipMsg:visible").length > 0) {
            counterTmp++;
            if (counterTmp > 1) {
                $(".tooltipMsg").hide();
            }
        }
    });
}

// Hides on GSA results pages the last divider line
function HideLastDivider() {
    $(".tabContent .divider:last, .searchContent .divider:last").hide();
}

// Hide empty download list on video pages
function HideEmptyDownloadListOnVideos() {
    if ($(".moviecontent").length > 0) {
        if ($(".moviedownloads ul li").length < 1) {
            $(".moviedownloads").hide();
        }
    }
}

//////****** sitestat script ******//////

function RegisterPDFClickSiteStat() {
    $("a[href$=.pdf], a[href$=.doc], a[href$=.xls], a[href$=.ppt], a[href$=.docx], a[href$=.xlsx], a[href$=.pptx]").click(function (e) {

        var tmpfile = $(this).attr("href");
        tmpfile = tmpfile.replace("/images/", ".");
        tmpfile = tmpfile.substring(0, tmpfile.lastIndexOf("."));

        var pdfTrackingUrl = siteCounterName + tmpfile + "&amp;ns_type=pdf";

        try {
            sitestat(pdfTrackingUrl);
        } catch (e) { }

        return true;
    });
}

function RegisterClickOutClickSiteStat() {
    $("a[href^=http]").click(function (e) {

        var tmpfile = $(this).attr("href");
        tmpfile = tmpfile.replace("http://", ".");
        
        var siteTrackingUrl = siteCounterName + tmpfile + "&amp;ns_type=clickout";

        try {
            sitestat(siteTrackingUrl);
        } catch (e) { }

        return true;
    });
}

//////****** slideshow scripts ******//////

var currentHomeTeaser = 0;
var timerHomeTeaser = false;
var shortHomeTeaserTimeout = 10 * 1000;
var longHomeTeaserTimeout = 20 * 1000;

function __selectHomeTeaser(number) {
    currentHomeTeaser = number;

    t = homeTeasers[currentHomeTeaser];

    // Instead of changing background image --> change src of img in teaserImg
    //$('#teaserHome').css('background-image', 'url("' + t.image + '")');
    $('#teaserHome #teaserImg').attr('src', t.image);
    $('#teaserHome #teaserHeading').html(t.title);
    $('#teaserHome #teaserText').html(t.text);    
    $('#teaserHome #teaserPLeesmeer #teaserLeesmeer').attr('href', t.href);
    if (t.href.length == 0)
        $('#teaserHome #teaserPLeesmeer').css('display', 'none');
    else
        $('#teaserHome #teaserPLeesmeer').css('display', 'inline');

    var cnt = 0;
    $('#teaserHome .teaserLinks li').each(function () {
        if (cnt == number) {
            $(this).removeClass().addClass('teaserLinksSelected');
        }
        else {
            $(this).removeClass().addClass('teaserLinksNotSelected');
        }
        cnt++;
    });
}

function nextHomeTeaser() {
    next = currentHomeTeaser + 1;
    if (next >= homeTeasers.length) {
        next = 0;
    }

    __selectHomeTeaser(next);

    timerHomeTeaser = setTimeout('nextHomeTeaser()', shortHomeTeaserTimeout);
}

function clickHomeTeaser(number) {
    if (timerHomeTeaser !== false) {
        clearTimeout(timerHomeTeaser);
        timerHomeTeaser = false;
    }

    __selectHomeTeaser(number);

    timerHomeTeaser = setTimeout('nextHomeTeaser()', longHomeTeaserTimeout);
}

$(document).ready(function () {

    // Make paging div visible if JavaScript is enable (which is obviously the case)
    $('#teaserHome #teaserLinkDiv').css('display', 'inline');

    if ($("#teaserHome").length > 0) {
        if ($(".teaserLinks ul li").length > 1) {
            $(".teaserLinks ul").toggleClass("hide");
        }

        $(".teaserContent").css({ opacity: 0.9 });
        $(".teaserContent2").css({ opacity: 0.9 });

        // Immediately load the first teaser
        __selectHomeTeaser(0);

        // preloading images
        for (i = 0; i < homeTeasers.length; i++) {
            var cacheImage = document.createElement('img');
            cacheImage.src = homeTeasers[i].image;
        }

        // start the timer
        timerHomeTeaser = setTimeout('nextHomeTeaser()', shortHomeTeaserTimeout);

        // bind the navigation buttons
        $('#teaserHome .teaserLinks a').bind('click', function (e) {
            e.preventDefault();

            var number = parseInt($(this).text() - 1);
            clickHomeTeaser(number);
        });

        // stop slideshow when hovering and restart after hovering
        $('#teaserHome .teaserContent2').hover(
        function () {
            if (timerHomeTeaser !== false) {
                clearTimeout(timerHomeTeaser);
                timerHomeTeaser = false;
            }
        },
        function () {
            timerHomeTeaser = setTimeout('nextHomeTeaser()', shortHomeTeaserTimeout);
        }
    );
    }
});

