﻿/* Copyright (c) 2010 by Adecco management & consulting S.A.
     Vendor: #5402, #0701 */
//*****************************************************************************
// jQuery additions
// Remove the 'filter' attribute after fading in/out
// otherwise IE7 will display ugly text using Windows' Cleartype rendering
// see bottom of http://blog.bmn.name/tag/jquery
//*****************************************************************************
jQuery.fn.customFadeIn = function(speed, callback) {
    return this.animate({ opacity: "show" }, speed,
		function() {
		    if (jQuery.browser.msie)
		        this.style.removeAttribute("filter");
		    if (jQuery.isFunction(callback)) {
		        this.callback = callback;
		        this.callback();
		    }
		}
	);
};

// ----------------------------------------------------------------
// Jquery methods for content widgets such as LatestNews
// ----------------------------------------------------------------
newscontentwidget = {
    currentPopup: null,
    totalDivs: 0,
    contentClassName: 'rotate-content',
    articleDurationClassName: 'input-article-duration',
    articleDuration: 10000,
    transitionTimeClassName: 'input-transition-time',
    transitionTime: 200,

    timeoutFunc: null,
    currentDiv: 0,

    // --------------------------------------------------
    // Hides the existing html element and fades in the next element.
    // --------------------------------------------------
    
    initHoverOver: function(IntID) {
        $('.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);
            /* Bug ID : #DE2190 Closed On : 9th-June-2011 Remarks :Added condition to check when to set bubble properties or not */
             if (popup.length > 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 ((e.offsetY + popupHeight + 20) > windowHeight) {
                            // the bottom of the popup bubble goes past the browser window
                            posY = (($(popup).parent().parent().parent().parent().parent().parent().height()) - popupHeight);
                        }

                        // hide any prev popup
                        if (newscontentwidget.currentPopup != null) {
                            newscontentwidget.currentPopup.css('display', 'none');
                        }
                        newscontentwidget.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(IntID) {
        newscontentwidget.initHoverOver(IntID);
    }
};      // end of newscontentwidget

function disableLinkTemporarily(elemId, timeout) {
    $(elemId).attr("disabled", "disabled");

    setTimeout("disableLink(" + elemId + ")", timeout);
}

function enableLink(elemId) {
    $(elemId).removeAttr("disabled");
}

function resizeNews(id) {
    
    var container = document.getElementById(id);
    var childDivs = new Array();
    childDivs = container.childNodes;
    
    var height = 0;
    var width = 0;
    
    for (var i = 0; i < childDivs.length; i = i+1) 
    {
        //alert("height: "+ childDivs[i].style.height + " width: " +childDivs[i].offsetWidth);
        if(childDivs[i].offsetHeight > height) 
        {
            height = childDivs[i].style.minHeight;
        }
        
        if(childDivs[i].offsetWidth > width) 
        {
            width = childDivs[i].offsetWidth;
        }
    }
    container.style.height = height;
    container.style.width = width;
}

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

