function MM_openBrWindow_(theURL, winName, features) {
    window.open(theURL, winName, features);
}

var FastIsi = {
    init: function() {
        this.navs = $$("#fastNav .item");
        this.contents = $$("#fastContent .contentItem");
        this.reference = $$("#fastRef .refItem");

        if (this.navs.length != this.contents.length) return;

        this.navs.each(function(el, i) {
            el.getElement("a").set("href", "javascript:void(0);");
            el.addEvent("click", function() {
                this.show(i);
                //s.tl(this, 'o', el.getElement('span').get('text'));
            } .bind(this));

            if (i == 0) el.addClass("active");
        }, this);

        this.contents.each(function(el, i) {
            if (i != 0) el.setStyle("display", "none");
        });

        this.pre = 0;
        this.curr = 0;
    },

    show: function(index) {
        if (index == this.curr) return;

        this.navs[this.curr].removeClass("active");
        this.contents[this.curr].setStyle("display", "none");

        //this.navs[this.pre].removeClass("preActive");

        var pre = (index == 0) ? 0 : index - 1;
        var hres = (index == 5) ? 0 : index - 1;
        if (index == 5) {
            this.reference.setStyle("display", "none");
        } else {
            this.reference.setStyle("display", "");
        }


        this.navs[index].addClass("active");
        this.contents[index].setStyle("display", "");
        //this.navs[pre].addClass("preActive");


        this.pre = pre;
        this.curr = index;
    }
}
window.addEvent("domready", function() {
    FastIsi.init();
});

var SplashPresent = {
    curr: 0,

    init: function() {
        // create slider
        var slider = this.slider = new SliderShow({
            autoRotate: false
        });

        // attach events to slider
        slider.addEvents({
            "slide.out": this.slideOut.bind(this),
            "slide.in": this.slideIn.bind(this)
        });

        // map bubble index to slide index
        this.bubbleSliderMap = {
            0: 0,
            1: 1,
            2: 2
        };

        // bubble elements
        var bubbleToggles = $$(".bubbleToggle"),
			onBubbles = $$(".onState"),
			offBubbles = $$(".offState"),
			closeBtn = document.id("closeCarousel");

        // bubble events
        bubbleToggles.each(function(el, i) {
            el.addEvents({
                "mouseenter": function() {
                    offBubbles[i].hide();
                    onBubbles[i].show();
                },
                "mouseleave": function() {
                    onBubbles[i].hide();
                    offBubbles[i].show();
                },
                "click": function() {
                    this.showFor(i);
                    var omnitureText = (i == 0) ? 'Newly Diagnosed' : ((i == 1) ? 'New to KALETRA' : 'Receiving Patient Support');
                    s.linkTrackVars = 'eVar22,prop21,events'; s.linkTrackEvents = 'event25'; s.events = 'event25'; s.eVar22 = omnitureText; s.prop21 = omnitureText; s.tl(this, 'o', 'Homepage Expanded Patient Views');
                } .bind(this)
            });
        }, this);

        closeBtn.addEvent("click", function(e) {
            e.preventDefault();
            document.id(slider).hide();
            this.curr = null;
        } .bind(this));

        // create handlers for each slide's elements
        var slideTweens = this.slideTweens = [];

        var $clone = function(obj) {
            var f = function() { };
            f.prototype = obj;
            return new f;
        };

        var selectorMap = {
            ".slidePhoto": [[-479, 57], [940, 57], [50, 750]],
            ".slideHeadline": [[-479, 57], [940, 57], [250, 50]],
            ".slideCopy": [[-479, 57], [940, 57], [500, 250]],
            ".button": [[-285, 70], [940, 57], [750, 500]]
        };

        slider.slides.each(function(slide, i) {
            var tweens = [];
            for (var key in selectorMap) {
                var el = slide.getElement(key),
					val = selectorMap[key],
					twn = $clone(SlideTweener);

                twn.init.apply(twn, [el].concat(val));
                tweens.push(twn);
            }
            slideTweens.push(tweens);
        });

    },

    showFor: function(index) {
        var target = this.bubbleSliderMap[index],
			slider = this.slider;


        if (slider.curr !== null) slider.fireEvent("slide.out", slider.curr);
        slider.show(target, "right", true)

        document.id(slider).show();
        slider.fireEvent("slide.in", slider.curr);

        this.curr = index;
    },

    slideOut: function(index) {
        var tweens = this.slideTweens[index];
        for (var i = 0, l = tweens.length; i < l; i++) {
            tweens[i].reset();
        }
    },

    slideIn: function(index, direction) {
        var tweens = this.slideTweens[index];

        for (var i = 0, l = tweens.length; i < l; i++) {
            tweens[i].start(direction);
        }

    }
}


var SlideTweener = {
	timer: null,
	init: function(element, left, right, delay) {
		this.element = element;		
		var positions = this.positions = {
			left: left,
			right: right
		};
		
		this.fx = new Fx.Tween(element, {
			property: "left",
			link: "cancel",
			duration: 300,
			transition: Fx.Transitions.Sine.easeOut
		});
		
		this.delay = delay;
		this.fx.set(positions.left[0]);
		
		return this;
	},
	
	reset: function() {
		clearTimeout(this.timer);
		this.fx.set(this.positions.left[0]);
		return this;
	},
	
	start: function(direction) {
		clearTimeout(this.timer);
		var position = this.positions[direction === "left" ? "left" : "right"],
			delay = this.delay[direction === "left" ? 0 : 1],
			fx = this.fx;
		
		this.timer = setTimeout(function(){
			this.fx.start(position[0], position[1]);
		}.bind(this), delay);
		
		return this;
	}
	
};


var SliderShow = new Class({
    Implements: [Events, Options],

    options: {
        carouselId: "carousel",
        slideWrapId: "carouselSlides",
        slideSelector: ".slide",

        navSelector: "#carouselNav li a",
        navActiveClass: "active",

        autoRotate: true,
        rotateSpeed: 7000,

        fxOpts: {
            duration: 700
        }
    },

    inProgress: false,
    curr: null,

    initialize: function(opts) {

        this.setOptions(opts);

        this.element = document.id(this.options.carouselId);
        this.wrap = document.id(this.options.slideWrapId) || new element("div");
        this.slides = this.wrap.getElements(this.options.slideSelector);
        this.nextBtn = document.id("rightArrow");
        this.prevBtn = document.id("leftArrow");

        if (this.slides.length < 2) return; // 0 or 1 slides total

        this.wrap.setStyle("position", "relative");
        this.slides.each(function(slide, i) {
            if (i != 0) slide.hide();
            slide.setStyles({
                position: "absolute",
                top: 0
            });
        });

        this.sizes = {};
        this.sizes.wrap = this.wrap.getSize();

        if (!this.sizes.wrap.x) this.sizes.wrap = { x: parseInt(this.wrap.getStyle("width")), y: parseInt(this.wrap.getStyle("height")) }

        var fxEvents = {
            "onStart": function() {
                this.inProgress = true;
            }.bind(this),
            "onComplete": function() {
                this.inProgress = false;
            }.bind(this)
        };

        this.slideOut = new Fx.Tween(this.slides[0], $merge({ duration: 500, link: "cancel", property: "left" }, fxEvents, this.options.fxOpts));
        this.slideIn = new Fx.Tween(new Element("div"), $merge({ duration: 500, link: "cancel", property: "left" }, fxEvents, this.options.fxOpts));


        this.curr = 0;

        this.bound = {};
        this.bound.next = function(e) {
            e.preventDefault();
            this.next();
        } .bind(this);
        this.bound.prev = function(e) {
            e.preventDefault();
            this.prev();
        } .bind(this);

        this.attach();

    },

    toElement: function() {
        return this.element;
    },

    attach: function() {
        this.nextBtn.addEvent("click", this.bound.next);
        this.prevBtn.addEvent("click", this.bound.prev);

        return this;
    },

    detach: function() {
        this.nextBtn.removeEvent("click", this.bound.next);
        this.prevBtn.removeEvent("click", this.bound.prev);

        return this;
    },

    show: function(index, direction, immediate) {
        var slideOut = this.slideOut,
			slideIn = this.slideIn,
			curr = this.curr;
		
		if (index == curr) return;
		
        var x = this.sizes.wrap.x;

        if (this.inProgress) {
            // stop current tweens and set to end positions
            slideOut.cancel().set(-x);
            slideIn.cancel().set(0);
        }

        slideOut.element = this.slides[curr];
        slideIn.element = this.slides[index];
        slideIn.set(direction === "left" ? -x : x);
		
		// fx events
		slideOut.removeEvents("complete").addEvent("complete", function() {
			this.fireEvent("slide.out", curr);
		}.bind(this));
		slideIn.removeEvents("complete").addEvent("complete", function() {
			this.fireEvent("slide.in", [index, direction]);
		}.bind(this));		
		
        slideIn.element.show();

        if (immediate) {
            slideOut.set(-x);
            slideIn.set(0);
			
			// fire events
			//slidein.fireEvent("complete");
			//slideout.fireEvent("complete");
        }
        else {
            slideOut.start(0, direction === "left" ? x : -x);
            slideIn.start(0);
        }

        this.curr = index;

        return this;
    },

    next: function() {
        var curr = this.curr,
			next = (curr + 1 > this.slides.length - 1) ? 0 : curr + 1;

        return this.show(next, "right");
    },

    prev: function() {
        var curr = this.curr,
			prev = (curr - 1 < 0) ? this.slides.length - 1 : curr - 1;

        return this.show(prev, "left");
    }
});


function MM_openBrWindow_(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}

function openDisclaimer(url) {
    MM_openBrWindow_(window.root + 'utility/leaving.aspx?url=' + url, 'winDisclaimer', 'width=370,height=525');
}
