// Cufon

Cufon.replace('H1');
Cufon.replace('H2');
Cufon.replace('H3');
Cufon.replace('H4', {hover: true});
Cufon.replace('.subNav', {hover: true});
Cufon.replace('#subNavigation', { hover: true });
Cufon.replace('#searchNavigation', { hover: true });
Cufon.replace('.butArrowDown', { hover: true });
Cufon.replace('.butArrow', { hover: true });
Cufon.replace('.searchDefinition');
Cufon.replace('.faqContentTitle');

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/utf-8 [rev. #1]

UTF8 = {
    encode: function (s) {
        for (var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
            s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
        );
        return s.join("");
    },
    decode: function (s) {
        for (var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
            ((a = s[i][c](0)) & 0x80) &&
            (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
            o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
        );
        return s.join("");
    }
};

/*
 * JQuery Random Plugin
 * 
 * Adds two random number functions to jQuery -
 * one to find a random number and one to find a random number between a max and min limit.
 * 
 * Version 1.0
 * 
 * by Christian Bruun - 23. jan 2009
 * 
 * Like it/use it? Send me an e-mail: rockechris@rockechris.com
 * 
 * License: None. Use and abuse. Comes with no warranty, of course!
 * 
 * 
 * Usage:
 * $.random(int);
 * $.randomBetween(min, max);
 * 
 * Code found at:
 * http://www.merlyn.demon.co.uk/js-randm.htm
 * 
 */
jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

//Variables available to all pages that load common.js
var pagepath = window.location.pathname;
var pageLanguage;
var pageCulture;

$(document).ready(function () {

    /*** culture helper - based on the url ***/

    if (pagepath.indexOf("/de-ch/") != -1) {
        pageCulture = "de-ch";
        pageLanguage = "de";
    }
    if (pagepath.indexOf("/fr-ch/") != -1) {
        pageCulture = "fr-ch";
        pageLanguage = "fr";
    }
    if (pagepath.indexOf("/it-ch/") != -1) {
        pageCulture = "it-ch";
        pageLanguage = "it";
    }
    if (pagepath.indexOf("/en-gb/") != -1) {
        pageCulture = "en-gb";
        pageLanguage = "en";
    }

    //add a break after each 2 teasers
    $('#teasersPage .teaser:nth-child(2n)').after('<br clear="all" />');

    $(".faqQuestion").click(function () {
        var answer = 'answer' + $(this).attr("id");
        //console.log(answer);
        $('#' + answer).slideToggle();
    });

    // subnavigation
    $(".navItem A").hover(
		function () {
		    $(this).parent().children('.subNavWrapper').show();
		},
		function () {
		    $(this).parent().children('.subNavWrapper').hide();
		}
	);

    $(".subNavWrapper").hover(
		function () {
		    $(this).show();
		    $(this).siblings().addClass('hover');
		},
		function () {
		    $(this).hide();
		    $(this).siblings().removeClass('hover');
		}
	);


    //Teaser animate function
    $('DIV.overlay').hover(
		function () {
		    $(this).stop(true, true).animate({
		        top: '-40px'
		    }, 500)
		},
		function () {
		    $(this).stop(true, true).animate({
		        top: '0px'
		    }, 500);
		}
	);


    /*** Accessibility ***/

    // bind arrow down (40) on navigation
    $('.navItem A').bind('keypress', function (e) {
        if (e.keyCode == 40) {
            $(this).parent().children('.subNavWrapper').show();
            $(this).parent().children('.subNavWrapper A:first').focus();

            event.preventDefault();
        }
    });

    // bind arrow up (38) on navigation
    $('.navItem').bind('keypress', function (e) {
        if (e.keyCode == 38) {
            $(this).children('.subNavWrapper').hide();
            event.preventDefault();
        }
    });

    // bind escape key (27) and arrow up (38) on subnavwrapper
    $('.subNavWrapper').bind('keypress', function (e) {
        if (e.keyCode == 27 || e.keyCode == 38) {
            $(this).hide();
            $('.subNavWrapper A').blur();
        }
    });

    /*** footnotes addon ***/
    $("#bodyWrapper").append("<ol id=\"footnotes\"></ol>");
    footnote = 1;
    $("#content a[href]:not('.butArrow')").addClass("footnote");
    $(".footnote").each(function () {
        $(this).append("<sup class='supLink'>" + footnote + "</sup>");
        cite = "<li>";
        url = $(this).attr("href");
        title = $(this).attr("title");
        //        if (title && url) {
        //            cite += "<a href=\"" + url + "\">" + title + "</a>";
        //        } else if (title) {
        //            cite += title;
        //        } else if (url) {
        //            cite += "<a href=\"" + url + "\">" + url + "</a>";
        //        }
        cite += "<a href=\"" + url + "\">" + url + "</a>";
        cite += "</li>";
        $("#footnotes").append(cite);
        footnote++;
    });
    /*** end footnotes addon ***/

    /*** check urls: add fancybox-iframe class to the publication previews ***/
    $("#content a[href]").each(function () {
        var urlHref = $(this).attr("href");

        if (urlHref.indexOf("/pub/") != -1) {

            $(this).attr("href", urlHref + "/" + pageCulture);

            //we make sure that a double slash is converted to one slash.
            $(this).attr("href", $(this).attr("href").replace("//", "/"));

            if (urlHref.indexOf("d") != -1) {
                //dossier
                //$(this).addClass("fancybox-iframe-510x510");
                $(this).attr("rel","fancybox-iframe-510x700");
            }
            else {
                //publication
                $(this).attr("rel","fancybox-iframe-510x230");
            }

            //console.log(urlHref);
        }
    });
    /*** end check urls ***/

    /*** fancybox support ***/

    $(".fancybox-inline").fancybox();

    $(".fancybox-iframe").fancybox();

    $(".fancybox-iframe").fancybox({
        'autoScale': false,
        //				'transitionIn'		: 'none',
        //				'transitionOut'		: 'none',
        'type': 'iframe'
    });
});


//toggle (select all / deselect all) checkboxes of a checkboxlist

function selectAllCB(cbControl) {

    var chkBoxList = document.getElementById(cbControl);
    var chkBoxCount = chkBoxList.getElementsByTagName("input");
    var chkBoxLast = parseInt(chkBoxCount.length) - 1;

    if (chkBoxList.getElementsByTagName("input")[chkBoxLast].checked == false) {
        for (var i = 0; i < chkBoxCount.length; i++) {
            chkBoxCount[i].checked = false;
        }
    }
    else {
        for (var i = 0; i < chkBoxCount.length; i++) {
            chkBoxCount[i].checked = true;
        }
    }

    return false; 
}

//function to parse querystring parameters

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
