var toolTipBG = new Image();
toolTipBG.src = 'assets/templates/bgcc/_style/_images/tooltips/bubble2.png';

function initToolTips(elements) {
    if (elements == null) {
        elements = $$('.toolTips');
    }
    var Tips2 = new Tips(elements, {
        initialize:function(){
            this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
        },
        onShow: function(toolTip) {
            this.fx.start(1);
        },
        onHide: function(toolTip) {
            this.fx.start(0);
        },
        offsets: {'x': 25, 'y': 15}
    });
}

function isInteger( string ) {
    return !isNaN( parseInt( string ) );
}
function confirmDelete(ev) {
    if(!confirm("Are you sure you want to delete the following: " + this.title + "?")) {
        new Event(ev).stop()
    }
}

function initCTASlider() {
    //morpher
    Fx.Morph = Fx.Styles.extend({
        start: function(className){
     
            var to = {};
     
            $each(document.styleSheets, function(style){
                var rules = style.rules || style.cssRules;
                $each(rules, function(rule){
                
                    if (!rule.selectorText || !rule.selectorText.test('\.' + className + '$')) return;
                    Fx.CSS.Styles.each(function(style){
                        if (!rule.style || !rule.style[style]) return;
                        var ruleStyle = rule.style[style];
                        to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
                    });
                });
            });
            return this.parent(to);
        }
    });
     
    Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
     
    Fx.CSS.Styles.extend(Element.Styles.padding);
    Fx.CSS.Styles.extend(Element.Styles.margin);
     
    Element.Styles.border.each(function(border){
        ['Width', 'Color'].each(function(property){
            Fx.CSS.Styles.push(border + property);
        });
    });
    //end morpher
    
    $$('.cta-toggler').each(function(toggler, i) {
        toggler.addEvent('click', function (e) {
            $$('.cta-toggler').each(function(newToggler) {
                newToggler.removeClass('cta-toggler-selected');
            });
                
            this.addClass('cta-toggler-selected');
            
            var thisSlide = this.getPrevious('div');
            
            //close all panes - except the one to open
            $$('.cta-slide').each( function (slide, j) {
                if (thisSlide != slide) {
                    slideMorph = new Fx.Morph(slide, {wait: false});
                    slideMorph.start('cta-slide-closed', {duration: 600});
                }
            });
            
            //open just this pane - if it's not already opened
            if (thisSlide.style.width == null || thisSlide.style.width == '' || thisSlide.style.width == '0px') {
                slideMorph = new Fx.Morph(thisSlide, {wait: false});
                window.setTimeout("slideMorph.start('cta-slide', {duration: 800})", 500);
            }
        });
    });
} 
var isFirstBanner = true;
var rotatingBannerIncrement = 0;
function rotateBanner(isFirst) {
    //show one at a time.
    currentBanner = $$('.rotating-banner-cta')[rotatingBannerIncrement];
//    alert(currentBanner);
    if (!isFirst) currentBanner.setStyle('opacity', 0);
    currentBanner.setStyle('display', '');
    
    if (!isFirst) {
        elementStyleFX = new Fx.Style(currentBanner, 'opacity', {
            duration: 1000,
            wait: false
        });
        elementStyleFX.start(1);
    }
    
    window.setTimeout("fadeBanner()", 2000);
}
function initRotatingBanner() {
    
    if ($$('.rotating-banner-cta').length > 1) {
        //hide all
        $$('.rotating-banner-cta').each(function(element, i){
            element.setStyle('display', 'none');
        });        
        if (isFirstBanner) {
            isFirstBanner = false;
            rotateBanner(true);
        }
        else {
            rotateBanner(isFirstBanner);
        }
    }
}
function hideBanner() {
    banner = $$('.rotating-banner-cta')[rotatingBannerIncrement];
    banner.setStyle('display', 'none');
    
    rotatingBannerIncrement++;
//    alert(rotatingBannerIncrement);
    if (rotatingBannerIncrement >= $$('.rotating-banner-cta').length) {
        rotatingBannerIncrement = 0;
    }
    rotateBanner(false);
}
function fadeBanner() {    
    banner = $$('.rotating-banner-cta')[rotatingBannerIncrement];
    elementStyleFX = new Fx.Style(banner, 'opacity', {duration: 2000, wait: false, onComplete: hideBanner});
    elementStyleFX.start(0);
}

window.addEvent('domready', function() {   
    initToolTips(null);
    initCTASlider();
    initRotatingBanner();
});
