/* -------------------------------------------------------------------

	Boondoggle - Hans Dreesen
	Dexia
	
	This JavaScript file collects all general functions used
	throughout the site, as well as some smaller specific
	functions.

------------------------------------------------------------------- */


// when the document is ready to be traversed and manipulated...
$(document).ready(function(){

	// LINK HANDLER
	// when you click on a link with class externalLink
	$("a.externalLink").click(function(){
		// open a new window with the link's location
		window.open($(this).attr("href"));
		// and do nothing after (to prevent the link from actually firing)
		return false;
	});
	
	
	
	// PRINT BUTTON
	// add print button
	$("<li id='navPrint'><span class='imgReplaced'>print</span></li>").prependTo("#functionalNav");
	// define print function
	$("#navPrint").click(function(){
		window.print();
	});
	
	
	// TEXT RESIZE
	// add text resize controls
	var textResizeControl = $("<li id='navFontSize'><a href='#' id='navFontSizeSmall'>A</a> <a href='#' id='navFontSizeMedium'>A</a> <a href='#' id='navFontSizeLarge'>A</a></li>");
	$("#navLanguageChoice").after(textResizeControl);
	// define text resize functions
	function resizeBaseFontTo(size){
		$("body").css("font-size", size);
		return false;
	};
	$("#navFontSizeSmall").click(function(){
		return resizeBaseFontTo("70%");
	});
	$("#navFontSizeMedium").click(function(){
		return resizeBaseFontTo("75%");
	});
	$("#navFontSizeLarge").click(function(){
		return resizeBaseFontTo("85%");
	});
	
	
	
	// DROPDOWN BUTTONS
	$(".btnDropdown, #segmentNav > li:not(#segmentNav > li.active)")
	    .click(function(event){
	        $(this).children("ul").slideDown("fast");
	        if ($(event.target).is(".btnDropdown > a")){
    	        return false;
            }
    	})
    	.mouseleave(function(event){
            $(this).children("ul").slideUp("fast");
    	});

	// SERVICES NAVIGATION
	var firstLevelItems = $("#column01 .localNavType01 > ul > li > a");
	var dropDownList = $("#column01 .localNavType01 > ul > li > a + ul");
	// set default state
	firstLevelItems.addClass("open");
	// open/close functions
	function openList(list){
        list.slideDown("fast", function(){
            $(this).siblings("a").removeClass("closed").addClass("open");
        })
	};
	function closeList(list){
	    list.slideUp("fast", function(){
		    $(this).siblings("a").removeClass("open").addClass("closed");
		})
	};
	// define click behaviour
	firstLevelItems.click(function(){
		if ($(this).hasClass("open")){
		    closeList($(this).next("ul"));
		}
		else if ($(this).hasClass("closed")){
		    openList($(this).next("ul"));
		}
		else { return false }
	});
	
	// MAIN NAVIGATION
	var windowWidth = $(window).width();
	$(window).resize(function(){
	    windowWidth = $(window).width();
	});
    var subNavUl;
    var subNavOffSet;
    
    function addMega(){
        $(this).addClass("mnHover");
        subNavUl = $(this).children("ul");
        subNavUl.show();
        if(subNavUl.width() + subNavUl.offset().left + 5 > windowWidth){
            subNavOffSet = windowWidth - subNavUl.width();
            subNavUl.css({"left": subNavOffSet});
        } else {
            subNavUl.css({"left": "auto"});
        }
    }
    
    function removeMega(){ 
        $(this).removeClass("mnHover").children("ul").hide();
    }
    
    var megaConfig = {
        interval: 10,
        sensitivity: 20,
        over: addMega,
        timeout: 0,
        out: removeMega
    };
    $("#mainNav > li").hoverIntent(megaConfig);

	// DOSSIER TARGET GROUP CTA-COLUMNS
	$("#targetGroupCTAs .grid_1, #targetGroupCTAs .grid_1_5").hover(
		function(){
			$(this).css("cursor", "pointer");
			$(this).find("a").addClass("hover");
		},
		function(){
			$(this).find("a").removeClass("hover");
		}
	);
	$("#targetGroupCTAs .grid_1, #targetGroupCTAs .grid_1_5").click(function(){
		window.location = $(this).find("a").attr("href");
	});


	// FAQ BOX
	var faqNav = $("<span id='faqNavPrev' class='imgReplaced'>previous</span><span id='faqNavNext' class='imgReplaced'>next</span>");
	$("#faqBox #faqNav").prepend(faqNav);
	$("#faqBox ul").cycle({
		speed:  0,
		timeout: 0,
		next:   "#faqNavNext", 
		prev:   "#faqNavPrev"
	});
	
	
	
	// PANELS
	$(".panels").attr("class", "panelsJS");
	$(".panelsJS h3").addClass("close").next("p").hide();
	$(".panelsJS h3").toggle(
		function(){
			$(this).attr("class", "open").next("p").slideDown(200);
		},
		function(){
			$(this).attr("class", "close").next("p").slideUp(200);
		}
	);

});

// -- added by DX -- //

var bannerids = [];
var bannerurls = [];
var bannernames = [];

function flashredirect(bannerkey)
{   
    var flashdata = bannerurls[0];
    if (bannerids.indexOf(bannerkey)>-1)
    {
        flashdata = bannerurls[bannerids.indexOf(bannerkey)];
        tagBannerClick(bannernames[bannerids.indexOf(bannerkey)]);
    }
    else
    {
        tagBannerClick(bannernames[1]);
    }
    window.open(flashdata[0],flashdata[1]);
}
function tagBannerViews()
{
    for (i = 0; i < bannerids.length; i++)
    {
        tagEvent("banner."+ bannernames[i] +".view");
    }
}
function tagBannerClick(bannerkey)
{
    tagEvent("banner."+bannerkey+".click");
}

function tagEvent(eventid)
{
    try
    {
        sitestat(eventid);
        ntptEventTag('ev='+eventid);
    }
    catch (e) {}
}

function RequestDexiaTickerValue()
{
    try
    {
        var result = ticker_id + ' ' + ticker_last + ' (' + ticker_var_pct + ') ';
        var stockprice = $('#stockPrice');
        var stockpriceAnchor = $('a', stockprice);
        stockpriceAnchor.text(result);
        stockpriceAnchor.addClass((ticker_var_pct.lastIndexOf('-') == 0) ? "down" : "up");
        stockprice.show();
    }
    catch (e)
    {
        // security settings do not allow, do not show the ticker
    }
}

function dx_onload()
{
    tagBannerViews();
}


// setup array indexOf for IE
if (!Array.indexOf)
{
    Array.prototype.indexOf = function (obj, start)
    {
        for (var i = (start || 0); i < this.length; i++)
        {
            if (this[i] == obj)
            {
                return i;
            }
        }
    }
}