﻿// Print button
$.fn.addPrintButton = function() {
	return this.each(function() {
		$('<a href="#" class="printButton">Print</a>').click(function() {
			window.print();
		}).insertBefore(this);
	});
};

$.fn.positionCenter = function() {
    $(this).css("top", (($(window).height() - $(this).height()) / 2 + $(window).scrollTop()) + "px");
    $(this).css("left", ($(window).width() - $(this).width()) / 2 + $(window).scrollLeft() + "px");
}

  //Popup-window
$.fn.popupWindow = function() {

    $.each($('.popUpMask, .close'), function() {
        $(this).click(function() {
            $('.popup-window, .popUpMask').hide();
            return false;
        });
    });

    return this.each(function() {
        $(this).click(function() {
            $(this).showModal();
            $('.popup-window').positionCenter();
            return false;
        });

        // Reposition the modal window when the window is scrolled or resized
        $(window).scroll(function() {
            $('.popup-window').positionCenter();
        }).resize(function() { $('.popup-window').positionCenter(); });

    });
}

$.fn.showModal = function() {

    var targetId = $(this).attr('rel');
    showSpecificModal(targetId);
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function showSpecificModal(contentId) {

    // Calling marketo with a click link
    if (getCookie('_mkto_trk')) {
        if (typeof mktoMunchkinFunction == 'function') {
            mktoMunchkinFunction('clickLink', {
                href: '/' + contentId + '/popup'
            });
        }
    };

    // Grab our modal window element
    var modalWindow = $('.popup-window');

    // Set the html from our target element
    modalWindow.find('.popup-content').html($('#' + contentId).html());
    modalWindow.find('.hider').click(function() { $('.popup-window, .popUpMask').hide(); return false; });
    modalWindow.show();
    modalWindow.positionCenter();

    // show the overlay
    $('.popUpMask').css('height', $(document).height() + 'px');
    $('.popUpMask').show();

    return false;
}



$(function() {
    if ($('body').hasClass('member_expired')) {
        $.each($('.exclusive'), function() {
            $(this)
  			.attr('href', '#')
  			.after('<span class="expired">members</span>')
  			.click(function() {
  			    $(this).attr('rel', 'expired-popup');
  			    $(this).showModal();
  			    //$('#header .popUpMask, #expired-popup').show();
  			    return false;
  			});
            var renew_choice;
            $('#renew_now a').click(function() {
                if ($(this).hasClass('purchase')) {
                    renew_choice = 'yes';
                } else {
                    renew_choice = 'no';
                }
                pageTracker._trackEvent("renew_now", renew_choice);
            });
        });
    }
});

$(function() {
    if ($('body').hasClass('bronze')) {
        $.each($('a.bronze'), function() {
            $(this)
  			.attr('href', '#')
  			.after('<span class="expired">members</span>')
  			.click(function() {
            $(this).attr('rel', 'upgrade-now-popup');
  			    $(this).showModal();
  			    //$('#header .popUpMask, #expired-popup').show();
  			    return false;
  			});
        });
    }
});

// External links
$(function() {
    $('a[target=_blank]').each(function() {
        $(this).addClass('external');
        $(this).click(function() {
            window.open(this.href);
            return false;
        });
    });
    $('a[target=_blank]').not('.short-externals a').each(function() {
        $(this).after('<span class="externalText">&nbsp;(opens in a new window)</span>');
    });
});

// extend jquery to include json post
$(function() {
  $.postJSON = function(url, data, callback) {
    $.post(url, data, callback, "json");
  };
});

// added in toggleText function
$.fn.toggleText = function(a, b) {
  return this.each(function() {
    $(this).text($(this).text() == a ? b : a);
  });
};

// Sidebar styling
$(function() {
    $('.sidebar ul li:has(p.sidebarHeader a)').each(function() {
        $(this).addClass('sidebarListItem');
        $(this).click(function() {
            var href = $(this).find('p.sidebarHeader a').attr('href');
            window.location = href;
        });
    });
});

$(function() {
    $('.sidebar ul li:has(p.sidebarExtraHeader a)').each(function() {
        $(this).addClass('sidebarExtraItem');
        $(this).click(function() {
            var href = $(this).find('p.sidebarExtraHeader a').attr('href');
            window.location = href;
        });
    });
});

//remove external link text from RHS image panel links
$(function() {
    $('.imagePanel').each(function() {
        $(this).find('span.externalText').remove();
    });
});

// Seminar Tab Panels
$(function() {
    $(".sessionContentContainer").hide();
    $("ul.sessionTabs").each(function() {
        var firstTab = $(this).find("li:first");
        $("#" + firstTab.attr("id") + "Panel").show();
    });
});

$(function() {
    $("ul.sessionTabs li").click(function() {
        var selectedTab = $(this).parent().find("li.sessionTabSelected");
        selectedTab.removeClass("sessionTabSelected");
        selectedTab.addClass("sessionTab");

        $(this).parent().find("li.sessionTabSelected").removeClass("sessionTabSelected");
        $(this).removeClass("sessionTab");
        $(this).addClass("sessionTabSelected");

        // Show appropriate tab panel
        var thePanel = $("#" + $(this).attr("id") + "Panel");
        thePanel.parent().find(".sessionContentContainer").hide();
        thePanel.show();

    });
  });


// Google Web Optimiser
gwoTracker = null;

// Google Analytics
pageTracker = null;
doneCallback = null;

function GAEvent(callback)
{
    doneCallback = callback;
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
   }

$(document).ready(function() {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function() {
        try {
            pageTracker = _gat._getTracker(lesGTC);
            pageTracker._setDomainName(lesGTD);
            if (lesUserDefined !== '') {
                pageTracker._setVar(lesUserDefined);
            }
            pageTracker._trackPageview();
        } catch (err) { }

        try {
            if (lesGWOTC != null && lesGWOTC != "") {
                gwoTracker = _gat._getTracker(lesGWOTC);
            }
        } catch (err) { }

        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;

        $('a').each(function() {
            var href = $(this).attr('href');

            if (href != null) {
                if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                    $(this).click(function() {
                        var extLink = href.replace(/^https?\:\/\//i, '');
                        pageTracker._trackEvent('External', 'Click', extLink);
                    });
                }
                else if (href.match(/^mailto\:/i)) {
                    $(this).click(function() {
                        var mailLink = href.replace(/^mailto\:/i, '');
                        pageTracker._trackEvent('Email', 'Click', mailLink);
                    });
                }
                else if (href.match(filetypes)) {
                    $(this).click(function() {
                        var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                        var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
                        pageTracker._trackPageview(filePath);
                    });
                }
            }
        });

        if (doneCallback != null) {
            doneCallback();
        }
    });
});

/* Disabled
// Clicktale
$(document).ready(function() {
    var isSSL = ("https:" == document.location.protocol);

    if (!isSSL) {
        $.getScript("http://s.clicktale.net/WRb.js", function() {
            try {
                if (typeof ClickTale == 'function') {
                    ClickTale(lesCTP, lesCTRP, "www02");
                }
            } catch (err) {}
        });
    }
});
*/

/* ********** listKeyPressEvents ********** */

// list name for function listKeyPressEvents
var listName = 'ul';

// set up list for traversing by key press
var listKeyPressEvents = function(elements, listId) {
    // set up key events
$(elements).bind('keyup', setListKeyEvents);
$(elements).trigger('keyup', [{ preventDefault: function() { }, keyCode: 38}]);
$(elements).trigger('keyup', [{ preventDefault: function() { }, keyCode: 40}]);

    // set list name of list to traverse with key press
    listName = 'ul#' + listId;
};


// redirect to selected page with id
var selectCompany = function() {
    if ($(listName).find('li.selected a').length != 0) {
        window.location.href = $(listName).find('li.selected a').attr('href');
    }
};

// move selected item up one place in the list
var moveUp = function() {
    if ($(listName).find('li.selected').prev().length != 0) {
        $(listName).find('li.selected').removeClass('selected').prev().addClass('selected');
    }
    else {
        $(listName).find('li.selected').removeClass('selected');
    }
};

// move selected item down one place in the list
var moveDown = function() {
    if ($(listName).find('li.selected').next().length != 0) {
        $(listName).find('li.selected').removeClass('selected').next().addClass('selected');
    }
    else if ($(listName).find('li.selected').length == 0) {
        $(listName + ' li').first().addClass('selected');
    }
};

// match key events to functions
var setListKeyEvents = function(e, customKey) {
    if (e) e.preventDefault();
    switch (e2key(customKey || e)) {
        case 'up': moveUp(); break;
        case 'down': moveDown(); break;
        case 'enter': selectCompany(); break;
    }
};

// match key events to key codes
var e2key = function(e) {
    if (!e) return '';
    var event2key = {
        '96': '0', '97': '1', '98': '2', '99': '3', '100': '4', '101': '5', '102': '6', '103': '7', '104': '8', '105': '9',
        // Chiffres clavier num         
        '48': 'm0', '49': 'm1', '50': 'm2', '51': 'm3', '52': 'm4', '53': 'm5', '54': 'm6', '55': 'm7', '56': 'm8', '57': 'm9',
        // Chiffres caracteres speciaux         
        '65': 'a', '66': 'b', '67': 'c', '68': 'd', '69': 'e', '70': 'f', '71': 'g', '72': 'h', '73': 'i', '74': 'j', '75': 'k',
        '76': 'l', '77': 'm', '78': 'n', '79': 'o', '80': 'p', '81': 'q', '82': 'r', '83': 's', '84': 't', '85': 'u', '86': 'v',
        '87': 'w', '88': 'x', '89': 'y', '90': 'z',
        // Alphabet         
        '37': 'left', '39': 'right', '38': 'up', '40': 'down', '13': 'enter', '27': 'esc', '32': 'space', '107': '+', '109': '-', '33': 'pageUp', '34': 'pageDown'
        // KEYCODES
    };

    return event2key[(e.which || e.keyCode)];
};

/* ********** end of listKeyPressEvents ********** */
