/*****************************************************************************/
/*
/* DOM Ready
/*
/*****************************************************************************/

// Event.onReady(callbackFunction);
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (Event._timer)  clearInterval(Event._timer);
    
    Event._readyCallbacks.each(function(f) { f() });
    Event._readyCallbacks = null;
    
  },
  onReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady;
      
      if (domReady.done) return f();
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});


/*****************************************************************************/
/*
/* Glider
/*
/*****************************************************************************/

Glider=Class.create();
Object.extend(Object.extend(Glider.prototype,Abstract.prototype),
{
    initialize:function(wrapper,options)
    {
        this.scrolling=false;
        this.wrapper=$(wrapper);
        this.scroller=this.wrapper.down('div.scroller');
        this.sections=this.wrapper.getElementsBySelector('div.section');
        this.options=Object.extend(
        {
            duration:1.0,frequency:3
        }
        ,options||
        {
        }
        );
this.sections.each(function(section,index)
{
    section._index=index;
}
);
this.events=
{
    click:this.click.bind(this)
};
this.addObservers();
if(this.options.initialSection)this.moveTo(this.options.initialSection,this.scroller,
{
    duration:this.options.duration
}
);
if(this.options.autoGlide)this.start();
}
,addObservers:function()
{
var controls=this.wrapper.getElementsBySelector('div.controls a');
controls.invoke('observe','click',this.events.click);
}
,click:function(event)
{
this.stop();
var element=Event.findElement(event,'a');
if(this.scrolling)this.scrolling.cancel();
this.moveTo(element.href.split("#")[1],this.scroller,
{
    duration:this.options.duration
}
);
Event.stop(event);
}
,moveTo:function(element,container,options)
{
this.current=$(element);
Position.prepare();
var containerOffset=Position.cumulativeOffset(container),elementOffset=Position.cumulativeOffset($(element));
this.scrolling=new Effect.SmoothScroll(container,
{
    duration:options.duration,x:(elementOffset[0]-containerOffset[0]),y:(elementOffset[1]-containerOffset[1])
}
);
return false;
}
,next:function()
{
if(this.current)
{
    var currentIndex=this.current._index;
    var nextIndex=(this.sections.length-1==currentIndex)?0:currentIndex+1;
}
else var nextIndex=1;
this.moveTo(this.sections[nextIndex],this.scroller,
{
    duration:this.options.duration
}
);
}
,previous:function()
{
if(this.current)
{
    var currentIndex=this.current._index;
    var prevIndex=(currentIndex==0)?this.sections.length-1:currentIndex-1;
}
else var prevIndex=this.sections.length-1;
this.moveTo(this.sections[prevIndex],this.scroller,
{
    duration:this.options.duration
}
);
}
,stop:function()
{
clearTimeout(this.timer);
}
,start:function()
{
this.periodicallyUpdate();
}
,periodicallyUpdate:function()
{
if(this.timer!=null)
{
    clearTimeout(this.timer);
    this.next();
}
this.timer=setTimeout(this.periodicallyUpdate.bind(this),this.options.frequency*1000);
}
}
);
Effect.SmoothScroll=Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype,Effect.Base.prototype),
{
initialize:function(element)
{
this.element=$(element);
var options=Object.extend(
{
    x:0,y:0,mode:'absolute'
}
,arguments[1]||
{
}
);
this.start(options);
}
,setup:function()
{
if(this.options.continuous&&!this.element._ext)
{
    this.element.cleanWhitespace();
    this.element._ext=true;
    this.element.appendChild(this.element.firstChild);
}
this.originalLeft=this.element.scrollLeft;
this.originalTop=this.element.scrollTop;
if(this.options.mode=='absolute')
{
    this.options.x-=this.originalLeft;
    this.options.y-=this.originalTop;
}
}
,update:function(position)
{
this.element.scrollLeft=this.options.x*position+this.originalLeft;
this.element.scrollTop=this.options.y*position+this.originalTop;
}
}
);

/*****************************************************************************/
/*
/* HBX.MLC & Doubleclick Code Generator
/*
/*****************************************************************************/

function hbxGenerator() {
	var pv = pageValues();
	var mlc = pv.outputDefaults + "/" + pv.subfeature1 + "/" + pv.subfeature2 + "/" + pv.subfeature3 + "/" + pv.channel + "/" + pv.subchannel + ';' + pv.channel + "/" + pv.subchannel + "/" + pv.outputDefaults + "/" + pv.subfeature1 + "/" + pv.subfeature2 + "/" + pv.subfeature3;
	return mlc;
}

function pageValues() {
sURL = new String;
	bit = new String;
	var x = 0;
	var stop = 0;
	var outputDefaults = "activetrainer/b2c/marketing";
	var subfeature1 = 'nosubfeature1';
	var subfeature2 = 'nosubfeature2';
	var subfeature3 = 'nosubfeature3';
	var channel = 'nochannel';
	var subchannel = 'nosubchannel';
	if (location.pathname != null) {
		if (location.pathname.length > 1) {
			sURL = location.pathname;
		} else if (location.pathname.length <= 1) {
			subfeature1 = 'home';
		} else {			
			sURL = '';
		}
	}

	function getHBXSubFeatureValue(bit,x) {
			if (x == 0)
			{
				switch (bit) {	
					case ('endurance'):
						return subfeature1 = 'endurance';
						break;
					case ('fitness'):
						return subfeature1 = 'fitness';
						break;
				} 
			}
			else if (x == 1)
			{
				switch (bit) {	
					case ('coaches'):
						return subfeature2 = 'coaches';
						break;
					case ('product'):
						return subfeature2 = 'online_log';
						break;
					case ('personal-coaching'):
						return subfeature2 = 'personal_coaching';
						break;
					case ('plans'):
						return subfeature2 = 'training_plans';
						break;
					case ('trainers'):
						return subfeature2 = 'coaches';
						break;
				}
			}				
			else if (x == 2)
			{
				switch (bit) {
					/*case ('at-basic'):
						return subfeature3 = 'pricing_basic';
						break;
					case ('at-plus'):
						return subfeature3 = 'pricing_plus';
						break;*/
					case ('learn-more'):
						return subfeature3 = 'pricing';
						break;
					case ('trainer'):
						return subfeature3 = 'trainer';
						break;
				}
				if (subfeature2 == 'training_plans'){  
					return channel = bit;    
				}   
			}
			else if (x == 3)
			{
			switch (bit) {
					case ('couch-to-5k'):
						return subfeature3 = 'ppc';
						break;
				}
			}
			else if (x == 4)
			{
				return subfeature3 = 'client';
			}
	}

	function getHBXSubChannelValue(bit,x) {
			if (x == 3)
			{
			subchannel = bit.gsub('-', '');	
			return subchannel;
			}
	}
	
	sURL = sURL.slice(1,sURL.length);
	finalChar = sURL.slice(sURL.length-1,sURL.length);
	chunkEnd = sURL.indexOf(".htm");
	if ((chunkEnd) > -1) sURL = sURL.slice('.htm',sURL.length-4);
	if (finalChar != "/") sURL += "/";
	if (sURL == 'endurance/product/at-basic/') {
			var subfeature1 = 'endurance';
			var subfeature2 = 'take_tour';
			var subfeature3 = 'pricing_basic';
		} else if (sURL == 'fitness/product/at-basic/') {
			var subfeature1 = 'fitness';
			var subfeature2 = 'take_tour';
			var subfeature3 = 'pricing_basic';
		} else if (sURL == 'endurance/product/at-plus/') { 
			var subfeature1 = 'endurance';
			var subfeature2 = 'take_tour';
			var subfeature3 = 'pricing_plus';
		} else if (sURL == 'fitness/product/at-plus/') {
			var subfeature1 = 'fitness';
			var subfeature2 = 'take_tour';
			var subfeature3 = 'pricing_plus';
		} else {
			while(!stop){
				chunkStart = sURL.indexOf("/");
				if (chunkStart != -1){
					bit = sURL.slice(0,chunkStart);
					sURL = sURL.slice(chunkStart+1,sURL.length);
					getHBXSubFeatureValue(bit,x);
					getHBXSubChannelValue(bit,x);
					x++;
				} else {
					stop = 1;
				}
			} 
		}

	var val = {};
	val.channel = channel;
	val.subchannel = subchannel;
	val.subfeature1 = subfeature1;
	val.subfeature2 = subfeature2;
	val.subfeature3 = subfeature3;
	val.outputDefaults = outputDefaults;
	return val;
}

/*****************************************************************************/
/*
/* DoubleClick Ad Generator
/*
/*****************************************************************************/
function doubleClick() {	
	var pv = pageValues();	
	var doubleClickValue = 'channel=' + pv.channel + ';subchannel=' + pv.subchannel + ';tanproduct=activetrainer;b2borb2c=b2c' + ';feature=marketing' + ';subfeature1=' + pv.subfeature1 + ';subfeature2=' + pv.subfeature2 + ';subfeature3=' + pv.subfeature3;
	$("banner").innerHTML = '<iframe id="leader_top" src="http://ad.doubleclick.net/adi/activetrainer.tan/' + pv.channel + '/marketing;tile=1;' + doubleClickValue + ';sz=728x90;dcopt=ist;position=leader_top;ord=' + ord + '?" width="728" height="90" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
	$("promo").innerHTML = '<iframe id="house_top" src="http://ad.doubleclick.net/adi/activetrainer.tan/' + pv.channel + '/marketing;tile=2;' + doubleClickValue + ';sz=250x90;position=house_top;ord=' + ord + '?" width="250" height="90" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
}

/*****************************************************************************/
/*
/* Random Int (used for DoubleClick Ad Generator above)
/*
/*****************************************************************************/

function randomInt( min, max ) { 
	return Math.floor(Math.random() * (max - min + 1) + min) ; } 
// var ord;  moved outside the function so it can run once per page
var ord; 
ord = randomInt( 100000, 1000000 ); 
/*****************************************************************************/
/*
/* HBX PageName Generator
/*
/*****************************************************************************/

function pageTitle() {
	metaData = document.getElementsByTagName('meta');
	hbx.pn = '';
	
	for (i=0; i<metaData.length; i++) {
		pnValue = metaData[i].name.search(/pntitle/);
		if (pnValue != -1) {
			hbx.pn = metaData[i].content;
		} 
	}
	return hbx.pn;
} 

/*****************************************************************************/
/*
/* Page Load
/*
/*****************************************************************************/
document.observe("dom:loaded", function() {
	randomInt();
	if ($("top_ads")) {
		doubleClick();
	}
});