var Site = {
    init: function() {
        Site.TextSize.init({ selector: '#utilText a', cookieName: 'whytextsize', targeted: $$('#siteBody', '#siteFoot') });        
    },

    TextSize: {
        cName: null,
        opts: { duration: 60, path: "/" },
        scale: ['0.75em', '1.0em', '1.1em'],
        myScale: null,
        els: null,
        curr: 0,
        init: function(obj) {
            this.els = $$(obj.selector)

            if (this.els.length == 0) return

            this.cName = obj.cookieName

            if (!$chk(Cookie.read(this.cName))) Cookie.write(this.cName, 0, this.opts) //if doesnt exist, set cookie to 0					

            this.myScale = parseInt(Cookie.read(this.cName)) || 0 //in case cookies are disabled use 0								

            //console.log(this.)

            this.els.each(function(el, i) {
                el.set('href', 'javascript:void(0);')
                el.addEvent('click', function() {
                    this.processScale(el, i)
                } .bind(this))
            }, this);

            this.targetEl = $$(obj.targeted);

            this.processScale(this.els[this.myScale], this.myScale.toInt());
        },
        setCookie: function() {
            Cookie.write(this.cName, this.myScale.toString(), this.opts);
        },
        processScale: function(el, index) {
            if (this.myScale != index) {
                this.els[this.myScale].removeClass("active");
            }

            this.els[index].addClass("active");

            this.myScale = index;
            this.setCookie();
            this.targetEl.setStyle('font-size', this.scale[this.myScale]);
        }
    },

    Myths: {
        init: function() {
            var element = document.id("mythReality"),
			frame1 = element.getElement(".mythClosed"),
			frame2 = element.getElement(".mythOpen"),
			toggle = element.getElement(".showAnswer"),
			close  = element.getElement(".closeBtn");
			
            toggle.addEvent("click", function() {
                frame1.hide();
				frame2.show();                
            });
            close.addEvent("click", function() {
                frame2.hide();
				frame1.show();
            });

        }
    },
	
	BloodCircle: {
		curr: null,
		init: function() {
			this.element = document.id("bloodCircle");
			this.toggles = this.element.getElements(".toggle");
			this.tips = this.element.getElements(".tooltip");
			
			this.toggles.each(function(tog, i) {
				tog.addEvents({
					"mouseenter": function() {
						$clear(this.timer);				
						this.show(i);
					}.bind(this),
					"mouseleave": function() {
					
					}
				})
			}, this);					
			
			this.resetBound = this.resetTimer.bind(this);
			this.hideTimerBound = this.hideTimer.bind(this);
			this.hideBound = this.hide.bind(this);
		},
		
		resetTimer: function() {
			$clear(this.timer);			
		},
		
		hideTimer: function() {
			this.timer = this.hide.delay(250, this);
		},
		
		hide: function() {
			if ($chk(this.curr)) {
				this.tips[this.curr].hide();
				this.tips[this.curr].removeEvent("mousenter", this.resetBound).removeEvent("mouseleave", this.hideTimerBound);
			}
		},
		
		show: function(index) {
			if (index !== this.curr && $chk(this.curr)) {
				this.tips[this.curr].hide();
				this.tips[this.curr].removeEvent("mousenter", this.resetBound).removeEvent("mouseleave", this.hideTimerBound);
			}
			
			this.tips[index].show();
			this.tips[index].addEvent("mouseenter", this.resetBound).addEvent("mouseleave", this.hideTimerBound);
			
			this.curr = index;
		}
	}

};

var TabGroup = new Class({
	Implements: [Events, Options],
	
	options: {
		activeClass: "active",
		showFirst: 0
	},
	
	curr: null,
	
	initialize: function(id, tabSelector, contentSelector, opts) {
		this.setOptions(opts);
		
		this.element = document.id(id);
		this.tabs = this.element.getElements(tabSelector);
		this.contents = this.element.getElements(contentSelector);
		
		this.show(this.options.showFirst);
		
		this.attach();
	},
	
	attach: function() {
		this.tabs.each(function(tab, i) {
			tab.getElement("a").set("href", "javascript:void(0);");
			tab.addEvent("click", function() {
				this.show(i);
			}.bind(this))
		}, this);
		
		
	},
	
	show: function(index) {
		if (index === this.curr) return this;
		
		if ($chk(this.curr)) {
			this.tabs[this.curr].removeClass(this.options.activeClass);
			this.contents[this.curr].hide();
		}
		
		this.tabs[index].addClass(this.options.activeClass);
		this.contents[index].show();		
		
		this.curr = index;
	}

});

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: 1000
		}
	},
	
	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.navs		= $$(this.options.navSelector);
		
		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();
		
		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.updateNav(0);
		
		this.curr = 0;
		this.attach();
				
		this.makeRotate();
		
		this.element.addEvents({
			"mouseenter": function() {
				$clear(this.rotateTimer);
			}.bind(this),
			"mouseleave": function() {
				this.makeRotate();
			}.bind(this)
		});		
		
	},
	
	toElement: function() {
		return this.element;
	},
	
	makeRotate: function() {
		if (!this.options.autoRotate) return;
		
		$clear(this.rotateTimer);
		
		this.rotateTimer = function() {
			var next = (this.curr + 1 > this.slides.length - 1) ? 0 : this.curr + 1;
			this.show(next);
		}.periodical(this.options.rotateSpeed, this);
	},
	
	attach: function() {
		this.navs.each(function(nav, i) {
			if (nav.get("tag") == "a") nav.set("href", "javascript:void(0);");
			nav.removeEvents("click").addEvent("click", function() {
				this.show(i);
			}.bind(this));
		}, this);
		
		return this;
	},
	
	detach: function() {
		this.navs.each(function(nav, i) {
			nav.removeEvents("click");
		});
		
		return this;
	},
	
	updateNav: function(index) {
		if ($chk(this.curr)) this.navs[this.curr].removeClass("active");
		this.navs[index].addClass("active");
	},
	
	show: function(index) {		
		if (index == this.curr) return;		
		
		if (this.inProgress) {
			// stop current tweens and set to end positions
			this.slideOut.cancel().set(-this.sizes.wrap.x);
			this.slideIn.cancel().set(0);
		}
		
		this.updateNav(index);
		
		this.slideOut.element = this.slides[this.curr];
		
		this.slideIn.element = this.slides[index];
		this.slideIn.set(this.sizes.wrap.x);
		this.slideIn.element.show();
		
		this.slideOut.start(0, -this.sizes.wrap.x);
		this.slideIn.start(0);
		
		this.curr = index;
		
		//this.makeRotate();
		
		return this;
	}	
});

window.addEvent("domready", function() {
	Site.init();
});


/* * * * * * * * * * * * * */
/* Spotlight PDF Tracking */
function trackclick() {

    var urlString = unescape(window.location);
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    document.getElementById('medicationGuide').innerHTML = '<IMG SRC="http://ad.doubleclick.net/activity;src=1089869;type=insuim10;cat=blood551;ord=1;num=' + a + '?" WIDTH=1 HEIGHT=1 BORDER=0>';   
   
   
}
/********************************/


function trackpdfClick(sptagName) {

    var urlString = unescape(window.location);
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    document.getElementById('spotTag').innerHTML = '<iframe src="https://fls.doubleclick.net/activityi;src=1089869;type=insuim10;cat=' + sptagName + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>';
    //document.write('<iframe src="http://fls.doubleclick.net/activityi;src=1089869;type=lantu530;cat=' + sptagName + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>');
}

//function trackClick(sptagName) {
//    var urlString = unescape(window.location);
//    var axel = Math.random() + "";
//    var a = axel * 10000000000000;
//    //document.getElementById('spotTag').innerHTML = '<iframe src="http://fls.doubleclick.net/activityi;src=1089869;type=lantu530;cat=' + sptagName + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>';
//    document.write('<iframe src="http://fls.doubleclick.net/activityi;src=1089869;type=lantu958;cat=' + sptagName + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>');
//}

function trackpdfClick2(sptagName) {

    var urlString = unescape(window.location);
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    document.getElementById('spotTag').innerHTML = '<iframe src="http://fls.doubleclick.net/activityi;src=3012733;type=insul333;cat=' + sptagName + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>';
    //document.write('<iframe src="http://fls.doubleclick.net/activityi;src=1089869;type=lantu530;cat=' + sptagName + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>');
}
