/**
 *  base
 *
 *  Created by Alex Bilstein on 2010-01-06.
 *  Copyright (c) 2010 Blue Fish Development Group. All rights reserved.
 */
 
/**
 * function hideExpiredContent
 * Parses the DOM, finds any elements with a date attribute, and hides them
 * if they are older than today's date.
 */
 
function hideExpiredContent() {
    $("body").find("[expires]").each(function(i) {

        var expirationDate = $(this).attr("expires");
        var d = new Date(expirationDate);
        var expirationDate = d.getTime();
        
        var d = new Date();
        var todaysDate = d.getTime();

        if (expirationDate < todaysDate) {
            $(this).addClass("hidden");
        }
        
    })
}

/**
 * function popUp
 * Parses the DOM, finds any anchor links with class 'popLink', and adds a 
 * window.open onclick event listener
 * ** Ported from old site **
 */
 
function popUp() {
	var aLinks = $(".popLink");
	for (var iLength=aLinks.length-1;iLength>=0;iLength--){
		aLinks[iLength].onclick=function(){
			LeftPosition = (screen.width) ? (screen.width-700)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-400)/2 : 0;
			var newWin=window.open(this.href,'popUp','width=700,height=400,top='+TopPosition+',left='+LeftPosition+',location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no');
			if(window.focus){newWin.focus()}
			return false;
		}
	}
}

/**
 * function popupWebcasts
 * Parses the DOM, finds any anchor links with class 'popLinkWebcasts', and adds a 
 * window.open onclick event listener
 * ** Ported from old site **
 */
 
function popUpWebcasts(){
	var aLinks = $("popLinkWebcasts");
	for (var iLength=aLinks.length-1;iLength>=0;iLength--){
		aLinks[iLength].onclick=function(){
			LeftPosition = (screen.width) ? (screen.width-640)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-400)/2 : 0;
			var newWin=window.open(this.href,'popUpWebcasts','width=640,height=525,top='+TopPosition+',left='+LeftPosition+',location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no');
			if(window.focus){newWin.focus()}
			return false;
		}
	}
}

/**
 * function initGlossaryTerms
 * Parses the DOM for elements of tag 'dfn' with class 'glossary', 
 * gets the term from the element attribute, retrieves the contents
 * of the file of that term name, and creates a tooltip with that content.
 * If 1 or more glossary terms are found on the page, the glossary term
 * instructions will be displayed in the sidebar.
 * ** Replaces Sweet Titles from old site **
 */

var languageCode = "en"; // this variable needs to be created by the system in order to generate the correct path

function initGlossaryTerms() {
    $("dfn.glossary").each(function(i) {
        $(".glossary-instructions").css({'display' : 'block'});
        var term = $(this).attr("term");
        $(this).qtip({
            content: {
                url: '/' + languageCode + '/glossary/' + term + '.html'
            },
            position: {
                corner: {
                    target: 'topRight',
                    tooltip: 'bottomLeft'
                },
                adjust: {
                    screen: true,
                    scroll: true,
                    resize: true
                }
            },
            style: { 
                color: '#000',
                width: 220,
                border: {
                    radius: 5
                },
                tip: 'bottomLeft',
                name: 'blue'
            }
        });
    })
}

/**
 * function initToolTips
 * Parses the DOM for an elements of class 'tooltip'
 * and creates a tooltip with the title attribute content.
 * ** Replaces Sweet Titles from old site **
 */

function initToolTips() {
    $('.tooltip[title]').qtip({ 
        position: {
            corner: {
                target: 'topRight',
                tooltip: 'bottomLeft'
            },
            adjust: {
                screen: true,
                scroll: true,
                resize: true
            }
        },
        style: { 
            color: '#000',
            width: 220,
            border: {
                radius: 5
            },
            tip: 'bottomLeft',
            name: 'blue'
        } 
    })
}

/**
 * function initSuperfish
 * Initialize the drop down menus for language and country
 * ** Replaces UDM from old site **
 */

function initSuperfish() {
    $("ul.sf-menu").superfish({
        delay:       300,
        animation:   {opacity:'show'},
        speed:       'fast',
        dropShadows: false,                 // true causes border problems, do not enable
        disableHI:   true
    }).find('ul').bgIframe({opacity:false});
}

/**
 * function stripTables
 * parses the document, looks for tables, if it finds class 'tblDataStriped', it adds
 * row striping to the table.
 * ** Ported from old site **
 */
 
function stripeTables() {
    $(".tblDataStriped tr:nth-child(even)").addClass("odd");
}

// popMainWin - migrated from old site - is this still needed?
function popMainWin(url){
	window.opener.focus();
	window.opener.location.href = url;
}

// popNewWin - migrated from old site - is this still needed?
function popNewWin(url){
	window.open(url,'popNewWin');
}

/**
 * Call functions after the document is ready
 */

$(document).ready(function(){
    hideExpiredContent();
    popUp();
    initGlossaryTerms();
    initToolTips();
    initSuperfish();
    stripeTables();
});
