﻿
// ----------------------------------------------------------------
// Jquery methods for content widgets such as LatestNews
// ----------------------------------------------------------------
adecco.contentwidget = {
    currentPopup: null,
    totalDivs: 0,
    contentClassName: 'rotate-content',
    transitionTimeClassName: 'input-transition-time',
    transitionTime: 10000,
 
    // --------------------------------------------------
    // Hides the existing html element and fades in the next element.
    // --------------------------------------------------
    rotateElement: function(x) {
        var cw = adecco.contentwidget;
    
        var y = x;
        if (x == cw.totalDivs) y = 1; else y++;
        $("#rotate" + x).hide();
        $("#rotate" + y).customFadeIn("fast");
        setTimeout(function() { cw.rotateElement(y)}, cw.transitionTime);
    },

    // --------------------------------------------------
    // Initializes the html element content rotation.
    // --------------------------------------------------
    initContentRotation: function() {
        var cw = adecco.contentwidget;
        
        // initialize number of div elements to rotate.
        $("." + cw.contentClassName).attr("id", function(arr) {
            cw.totalDivs++;
            return "rotate" + (arr + 1);
        })

        // hide all rotatable elements except the first one.
        $("." + cw.contentClassName + ":eq(0)").show();
        $("." + cw.contentClassName + ":eq(0)").siblings().hide();

        // get the transition time.
        cw.transitionTime = parseInt($("." + cw.transitionTimeClassName).val());

        cw.rotateElement(0);
    },
    
    initHoverOver: function() {
        $('.bubble-main').each(function() {
        
            // options
            var distance = 0;
            var time = 250;
            var hideDelay = 500;
            var showDelay = 500;

            // tracker
            var hideDelayTimer = null;
            var showDelayTimer = null;

            var beingShown = false;
            var shown = false;

            var trigger = $('.bubble-trigger', this);
            var popup = $('.bubble-content', this).css('opacity', 0);

            // set the mouseover and mouseout on both element
            $([trigger.get(0), popup.get(0)]).mouseover(function(e) {
                // stops the hide event if we move from the trigger to the popup element
                if (hideDelayTimer) clearTimeout(hideDelayTimer);

                // don't trigger the animation again if we're being shown, or already visible
                if (beingShown || shown) {
                    return;
                } 
                else {
                    showDelayTimer = setTimeout(function() {
                
                        beingShown = true;

                        // get height width of the browser window and the popup bubble
                        var windowWidth = $(window).width();
                        var windowHeight = $(window).height();
                        var popupWidth = (popup).width();
                        var popupHeight = (popup).height();

			// get the top/left coordinates of popup relative to the container
			var position = $(popup).parent().position(); // position relative to container
			var docOffset = $(popup).parent().offset(); // position relative to document

                        // calculate position of popup based on current mouse position
                        // also, prevent hover over from being cut off from edges of browser window.                                            
                        var popupLeftPos = docOffset.left; //e.clientX;
                        var popupTopPos = docOffset.top; //e.clientY;
                        var popupRightPos =  popupLeftPos + popupWidth;
                        var popupBottomPos =  popupTopPos + popupHeight;
 
			var posX = position.left;
			var posY = position.top + 20;

                        if (popupRightPos > windowWidth)
                        {
                            // the right of the popup bubble goes past the browser window
                            // popupLeftPos = windowWidth - popupWidth - 15;

			    // calculate negative left offset
			    posX += ((windowWidth - popupWidth - 15) - docOffset.left);
                        }
                        if (popupBottomPos > windowHeight)
                        {
                            // the bottom of the popup bubble goes past the browser window
                            // popupTopPos = windowHeight - popupHeight - 15;

			    // calculate negative top offset
			    posY += ((windowHeight - popupHeight - 15) - docOffset.top);
                        }

                        // hide any prev popup
                        if (adecco.contentwidget.currentPopup != null)
                        {
                            adecco.contentwidget.currentPopup.css('display', 'none');
                        }
                        adecco.contentwidget.currentPopup = popup;
                
                        // reset position of popup box
                        popup.css({
                            position: 'absolute',
                            top: posY, //top: popupTopPos,
                            left: posX, //left: popupLeftPos,
                            display: 'block' // brings the popup back in to view
                        })

                        // (we're using chaining on the popup) now animate it's opacity and position
                        .animate({
                            top: '-=' + distance + 'px',
                            opacity: 1
                        }, time, 'swing', function() {
                            // once the animation is complete, set the tracker variables
                            beingShown = false;
                            shown = true;
                        });
                    }, showDelay);
                }
         
            }).mouseout(function() {
                // reset the timer if we get fired again - avoids double animations
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (showDelayTimer) clearTimeout(showDelayTimer);

                // store the timer so that it can be cleared in the mouseover if required
                hideDelayTimer = setTimeout(function() {
                    hideDelayTimer = null;
                    popup.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function() {
                        // once the animate is complete, set the tracker variables
                        shown = false;
                        // hide the popup entirely after the effect (opacity alone doesn't do the job)
                        popup.css('display', 'none');
                    });
                }, hideDelay);
            });
        });
    },
    
    init: function()
    {    
        adecco.contentwidget.initHoverOver();
        adecco.contentwidget.initContentRotation();
    }
    
}; // end of adecco.contentwidget


$(document).ready(adecco.contentwidget.init);

