/**
 * $Header: /back.com/js/main.js 9     3/03/09 4:10p Pricea2 $
 *
 * @name	main.js
 * @package	Back.com
 * @author	Phil Robinson <phil.robinson@medtronic.com>
 * @version	$Revision: 9 $
 */

// creates application namespace 
YAHOO.namespace('app');
YAHOO.app = function() {
	// private properties, methods

	// YUI shortcuts
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var $ = Dom.get;

	// text sizer cookie properties
	var cookie = jimAuld.utils.cookies;		// assumes Jim Auldridge's cookie library; located at /shared/javascript/cookies.min.js
	var textSizeCookieName = 'text-size';
	var textSizeCookie = cookie.get(textSizeCookieName);
	var textSizeDefault = 'normal';

	// headline rotator
	var headlineRotator = {
		elHeadline: null,	// headline element
		interval:	5,		// timeout interval in seconds
		timer:		null,	// holds the id of the timeout 
		lastIndex:	null,	// index of the last headline displayed
		headlines:	[
			{url: '/articles-trial.html',				text: 'DIAM&trade; Device<br>Back Pain Study Underway'},
			{url: '/findadoctor.html',					text: 'Find a Doctor in Your City'},
			{url: '/signup.html',						text: 'Sign up for New Technology Updates'},
			{url: '/articles-exercises.html',			text: 'Free Exercises for Your Back'},
			{url: '/articles-pilates.html',				text: 'Develop Your &lsquo;Powerhouse&lsquo; With Pilates'},
			{url: '/causes-mechanical-herniated.html',	text: 'What is a herniated disc?'},
			{url: '/patient.html?dis=dj',				text: 'Read Patient Stories'}
		],

		init: function(el) {
			var self = this;
			this.elHeadline = el;
			el.innerHTML = '';
			this.loadHeadline();
			this.timer = window.setInterval(function() { self.loadHeadline(); }, (this.interval * 1000));
		},

		loadHeadline: function() {
//Commenting this out to freeze the headlines on DIAM per Sara
			// get a random headline, but not the last one
//			do {
//				var index = Math.floor(Math.random() * this.headlines.length);
//			} while (index == this.lastIndex);
//
//			this.lastIndex = index;
//			this.elHeadline.innerHTML = '<a href="'+this.headlines[index]['url']+'">'+this.headlines[index]['text']+'</a>';

			do {
				var index = '0';
			} while (index == this.lastIndex);

			this.lastIndex = '999999';
			this.elHeadline.innerHTML = '<a href="'+this.headlines[index]['url']+'">'+this.headlines[index]['text']+'</a>';

		}
	};

	var toggleSearch = function(el) {
		var el = $(el);
		var val = el.defaultValue;

		var doFocus = function() {
			if (el.value == val) {
				el.value = '';
			}
		};

		var doBlur = function() {
			if (el.value == '') {
				el.value = val;
			}
		};

		// attach the search input events
		Event.on(el, 'focus', doFocus);
		Event.on(el, 'blur', doBlur);
	}

	var resizeText = function(e) {
		Event.preventDefault(e);

		var which = Event.getTarget(e);
		var newTextSize = which.id;
		var oldTextSize = (newTextSize != 'normal') ? 'normal' : 'big';

		// add text size class to body
		Dom.replaceClass(document.body, oldTextSize, newTextSize);

		// set text size cookie
		cookie.set(textSizeCookieName, newTextSize, 24000);
	}


	// public properties, methods
	return {
		init: function() {
			// check for text size cookie
			if (textSizeCookie != '' && textSizeCookie != 'normal') {
				Dom.replaceClass(document.body, 'normal', textSizeCookie);
			}

			// reset nav-main list items if user is browsing with images turned off
			Event.onAvailable('logo', function(el) {
				if ($(el).offsetWidth != 199) Dom.addClass($('nav-main'), 'visible');
			}, 'logo');

			// add headlines rotator to index page
			Event.onAvailable('module-headlines', headlineRotator.init, $('headline'), headlineRotator, true);

			// add handler to clear/populate search form input 
			Event.onAvailable('search', toggleSearch, 'search');

			// add text resizer handlers
			Event.on('text-resizer', 'click', resizeText);

			// attach onclick event to print icon link  
			Event.on('link-print', 'click', function(e, el) {
				Event.stopEvent(e);
				window.print();
			});

		}
	}
}();


// init the app when DOM is ready
YAHOO.util.Event.onDOMReady(YAHOO.app.init, YAHOO.app, true);




/**
 * FUNCTIONS
 * 
 * NOTE: This is the place to put random functions that you do not want to be
 * part of the app "class" defined above. 
 */

function popINTER(path) {
	myWin = window.open('','myWindow','scrollbars=yes,directories=no,status=no,resizable=yes,toolbar=no,copyhistory=no,location=no,menubar=no,width=340,height=255,top=15,left=15')
	myWin.location.href = path;
}

function interstitial(storyurl){
	window.open(storyurl,'mywindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=300,height=400');
}


/**
 * $History: main.js $
 * 
 * *****************  Version 9  *****************
 * User: Pricea2      Date: 3/03/09    Time: 4:10p
 * Updated in $/back.com/js
 * 4383059
 * 
 * *****************  Version 8  *****************
 * User: Pricea2      Date: 7/23/08    Time: 3:57p
 * Updated in $/back.com/js
 * Changes for DIAM
 * 
 * *****************  Version 7  *****************
 * User: Pricea2      Date: 7/11/08    Time: 8:01a
 * Updated in $/back.com/js
 * 3817599 - Rephrasing the DIAM headline.
 * 
 * *****************  Version 6  *****************
 * User: Pricea2      Date: 7/07/08    Time: 4:22p
 * Updated in $/back.com/js
 * Removing headline randomization.
 * 
 * *****************  Version 5  *****************
 * User: Pricea2      Date: 3/25/08    Time: 8:50a
 * Updated in $/back.com/js
 * Added interstitial function.
 * 
 * *****************  Version 4  *****************
 * User: Robinp2      Date: 3/13/08    Time: 4:53p
 * Updated in $/back.com/js
 * Fixed issue with print link onclick event.
 * 
 * *****************  Version 3  *****************
 * User: Robinp2      Date: 3/06/08    Time: 11:15a
 * Updated in $/back.com/js
 * Added onclick event for print icon link to init method.
 * 
 * *****************  Version 2  *****************
 * User: Robinp2      Date: 3/03/08    Time: 4:45p
 * Updated in $/back.com/js
 * Added onAvailable event to add .visible CSS class to #nav-main when
 * user is
 * browsing with images turned off.
 * 
 * *****************  Version 1  *****************
 * User: Robinp2      Date: 2/26/08    Time: 6:13p
 * Created in $/back.com/js
 * Added to VSS.
 */