
$(document).ready(function() {

    //remove country-select submit button and add onchange event to select dropdrown to submit form

    //$("#language-submit").remove();
    $("#country").change(function() {
        this.form.submit();
    });

    //add submit, continue rollover states
    $(".submit").mouseover(function() {
        this.src = '/images/submit-rollover.gif';
    });
    $(".submit").mouseout(function() {
        this.src = '/images/submit.gif';
    });

    $(".submit").focus(function() {
        this.src = '/images/submit-rollover.gif';
    });
    $(".submit").blur(function() {
        this.src = '/images/submit.gif';
    });


    $(".continue").mouseover(function() {
        this.src = '/images/continue-rollover.gif';
    });
    $(".continue").mouseout(function() {
        this.src = '/images/continue.gif';
    });

    $(".continue").focus(function() {
        this.src = '/images/continue-rollover.gif';
    });
    $(".continue").blur(function() {
        this.src = '/images/continue.gif';
    });
    $("#pin").show();
});
 

/*********************************************************************************************************************
 * Tooltip JS
 *
 * History
 * -------
 *	2009-09-25	jdp	created
 *
 * To-do
 * -----
 *
 *********************************************************************************************************************/

$(function()
{
    $("body").addClass('js');
     
	var tips = $('.tooltip');
	tips.hide();
	
	for (i = 0; i < tips.length; i++)
	{
		new Tooltip(tips.get(i));
	}
});

function Tooltip(oEl) {
    /**
    * Handles the acquisition of a trigger.
    */
    this.acquireTrigger = function() {
        if (this.jqObj.hasClass('ttRemote') &&
			(this.jqObj.attr('rel') != undefined) &&
			($('#' + this.jqObj.attr('rel')).length != 0)) {
            this.oTriggerEl = $('#' + this.jqObj.attr('rel')).get(0);
            this.bRemote = true;
        }
        else {
            this.oTriggerEl = this.oTooltipEl.parentNode;
        }
        this.jqTrigger = $(this.oTriggerEl);
    }

    /**
    *
    */
    this.createHelpTip = function() {
        var oNew = document.createElement('span');
        oNew.innerHTML = '<img src=\"/members_images/icon-help.gif\" />';
        oNew.className = 'ttHelpTrigger';
        oNew.setAttribute('tabindex', '0');

        this.oTriggerEl.appendChild(oNew);
        this.oTriggerEl = oNew;
        this.jqTrigger = ($(this.oTriggerEl));

        this.bHelpTip = true;
    }

    /**
    *
    */
    this.positionTip = function(frommouseover) {
        // mz bug fix - added frommouseover boolean
        if (frommouseover == null) frommouseover = false;

        // get positioning element
        this.jqObj.css('position', 'absolute');
        var oPosElement = this.jqTrigger; // = (this.bRemote) ? this.jqObj.parent() : this.jqTrigger;

        // calculate offsets
        this.jqObj.show(); // have to show for offsetParent() to work correctly
        var offset = (this.jqObj.offsetParent().get(0) != oPosElement.get(0))
						? oPosElement.position()
						: { top: 0, left: 0 };
        if (frommouseover == false || this.bDelay == true) {
            // no need to hide if triggered from mouseover event
            // fixed flash bug
            this.jqObj.hide();
        }

        // now work out top and left... (hardcoded for helptips)
        var iTop = (this.bHelpTip) ? offset.top + 20 : offset.top + oPosElement.outerHeight();
        var iLeft = (this.bHelpTip) ? offset.left + 10 : offset.left;

        // if we're aligning right, move its x-coord
        if (this.bAlignRight)
            iLeft += oPosElement.width() - this.jqObj.outerWidth();

        // finally set out the positions
        this.jqObj.css('top', iTop + 'px');
        this.jqObj.css('left', iLeft + 'px');
    }

    /**
    * Handles the display of the tip
    */
    this.displayTip = function() {
        if (!this.isVisible) {
            clearTimeout(this.timeoutRef);
            this.positionTip(true);

            if (!this.bClick && this.bDelay) {
                // look for custom delay
                var delay = ((mat = this.jqObj.attr('class').match(/ttd\-(\d+)/)) != null) ? parseInt(mat[1]) : 200;

                // timeout
                var objRef = this;
                this.timeoutRef = setTimeout(function() { objRef.jqObj.fadeIn(100); }, delay);
            }
            else {
                this.jqObj.fadeIn(100);
            }

            if (this.bClick) {
                this.jqTrigger.unbind('click');
            }
        }

        this.isVisible = true;
    }

    /**
    * Handles hiding this tip. This is done on a timeout so 
    */
    this.hideTip = function() {
        if (!this.bSticky) {
            clearTimeout(this.timeoutRef); // in case there's a delay on the opening...
            var objRef = this;
            this.timeoutRef = setTimeout(function() { objRef.jqObj.fadeOut(100); }, 100);
            this.isVisible = false;
        }
    }

    /**
    * Handles closing this tip. Same as hideTip without sticky check.
    */
    this.closeTip = function() {
        clearTimeout(this.timeoutRef); // in case there's a delay on the opening...
        var objRef = this;
        this.timeoutRef = setTimeout(function() { objRef.jqObj.fadeOut(100); }, 100);
        this.isVisible = false;

        if (this.bClick) {
            setTimeout(function() { objRef.bindClick(); }, 150);
        }
    }

    this.bindClick = function() {
        this.jqTrigger.click(function() { objRef.displayTip(); });
    }

    /**
    * Constructor code.
    */
    /* Class variables */
    this.oTooltipEl = null;
    this.oTriggerEl = null;
    this.timeoutRef = null;

    /* other assorted stuff */
    this.jqObj = null;
    this.jqTrigger = null;
    this.bRemote = false;
    this.bHelpTip = false;
    this.bAlignRight = false;

    // mz stuff
    this.jqSticky = null;
    this.bSticky = false;
    this.bClick = false;
    this.bDelay = false;
    this.isVisible = false;

    /** ----------------- */
    /** Constructor logic */
    /** ----------------- */
    this.oTooltipEl = oEl;
    this.jqObj = $(oEl);

    // acquire triggers, any other processing
    this.acquireTrigger();
    if (this.jqObj.hasClass('ttHelp'))
        this.createHelpTip();
    if (this.jqObj.hasClass('ttRight'))
        this.bAlignRight = true;

    // position the tip
    this.positionTip();

    if (this.jqObj.hasClass('ttClick')) {
        this.bClick = true;
    }
    
    // link up triggers
    var objRef = this;
    if (!this.bClick) {
        this.jqTrigger.mouseenter(function() { objRef.displayTip(); });
    } else {
        this.bindClick();
    }
    this.jqObj.mouseenter(function() { objRef.displayTip(); });
    this.jqTrigger.mouseleave(function() { objRef.hideTip(); });
    this.jqObj.mouseleave(function() { objRef.hideTip(); });

    // if the trigger is an anchor tag, hook up focus and blur as well
    if ((this.oTriggerEl.nodeName.toLowerCase() == 'a') ||
		this.jqTrigger.hasClass('trigger') ||
		this.jqTrigger.hasClass('ttHelpTrigger')) {
        this.jqTrigger.focus(function() { objRef.displayTip(); });
        this.jqTrigger.blur(function() { objRef.hideTip(); });

        // look for tabbable elements in the tip, otherwise tabbing away from the trigger makes the tip go away
        /*		var selectable = $('a, input, textarea, select, button', this.jqObj);
        selectable.focus(function() { objRef.displayTip(); });
        selectable.blur(function() { alert('trig'); objRef.hideTip(); });*/

        // also look for pseudo-things that don't know about focus and blur...
    }

    if (this.jqObj.hasClass('ttSticky')) {
        this.bSticky = true;
        this.jqObj.html('<a href="javascript:void(0)" class="ttSticky" title="Close"><img src="/common_images/tt-close.png" border="0"/></a>' + this.jqObj.html());
        this.jqSticky = $('.ttSticky', this.oTooltipEl);
        this.jqSticky.click(function() { objRef.closeTip(); });

        this.jqSticky.css('position', 'absolute');
        this.jqSticky.css('top', '-10px');
        this.jqSticky.css('left', this.jqObj.width() - 18 + 'px');
    }

    if (this.jqObj.hasClass('ttDelay')) {
        this.bDelay = true;
    }
}
