com_limelife_tools = function() {

	var e = YAHOO.util.Event;

	return {

		init: function(){

		this.isComplete = false;
		e.onContentReady('main', this.onContentReadyHandler, this );
	},		

	onContentReadyHandler: function( obj ) {
		// / current scope is element we were listening for
		// alert("onContentReadyHandler")
		obj.doInit();
		obj.isComplete = true;
	},
	
	isEmailValid : function(string){
    var acceptable=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    return acceptable.test(string);
	},


	doInit: function(){
		var IE6=(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
		this.setDiags();
		this.setComments();
		this.addAutoRollovers();			
		this.addAutoClearFields();
		this.addNotification();
//		this.addLinkTrack('llMainLoginTrack', 100);
//		this.addLinkTrack('llOnPageLoginTrack', 101);
		this.addLinkTrack('externLink', 201);
		this.addLinkTrack('holidayLink', 301);
		// 400 & 401 used directly in MobileUpdatesAd.html
		this.setCaptions();
		if (IE6){
			this.setMouseOverIE6();
			}
	},
	
	addLinkTrack: function(className, id){
		var els= YAHOO.util.Dom.getElementsByClassName(className, 'a');
		for (var i=0; i<els.length; i++) {
			els[i].llLinkTrackId = id;
			YAHOO.util.Event.addListener(els[i], 'click', this.onLinkTrack);
		}
		
	},
	
	onLinkTrack : function(evt)
	{
		srcObj = evt["target"] ? evt["target"] : evt["srcElement"];
		com_pagesociety_DoModule("ClickEventModule/createClickEvent", [srcObj.llLinkTrackId, srcObj.href],
				function(){}, function(){});
	},
	submitClick : function(type, url){
		com_pagesociety_DoModule("ClickEventModule/createClickEvent", [type, url],
				function(){}, function(){}) 
	},
	
	
	
	setCaptions: function(){
		var els = YAHOO.util.Dom.getElementsByClassName('f_caption', 'div');
		var image = new Image();
		for (var i=0; i<els.length; i++){
			image.src = els[i].parentNode.getElementsByTagName('img')[0].src;
			if (!image.complete){
				setTimeout(setCaptions(),100);
			}else{
				els[i].parentNode.style.width = els[i].parentNode.getElementsByTagName('img')[0].width+'px';
				els[i].style.display="block";
			}
		}

		els = YAHOO.util.Dom.getElementsByClassName('f_slideWrap', 'div');
		for (i=0; i<els.length; i++){
			image.src = els[i].parentNode.getElementsByTagName('img')[0].src;
			if(!image.complete){
				setTimeout(setCaptions(),100);
			}else{
				els[i].parentNode.style.width = els[i].getElementsByTagName('img')[0].width+'px';
				els[i].style.display="block";
			}
		}
	},
	
	setDiags: function(){
		

		// // IE 7 sees the default position.top of the BODY as 2px and NOT
		// 0px !!!!!!!!!!

		var x = YAHOO.util.Dom.getX( 'main' );
		var y = YAHOO.util.Dom.getY( 'main' );

		/*
		 * !!!!! IMPORTANT: the existence of an element with the id
		 * 'snaps_body' is needed to make non-tiling bg images align with
		 * diagonals:
		 */ 
		if( $('snaps_body') ){
			y *= -1;
			y += ( y % 4 );
			y -= ( YAHOO.util.Dom.getY( 'snaps_body' ) % 4);
		}			

		var els = YAHOO.util.Dom.getElementsByClassName('diag', 'div'); 
		for ( var i = 0; i < els.length; i++ ){
			// ie7 bug requires this check
			if (YAHOO.util.Dom.getStyle(els[i], "display")!="block")
				continue;

			var iX = YAHOO.util.Dom.getX( els[ i ] );
			var iY = YAHOO.util.Dom.getY( els[ i ] );

			// debug("els["+ i + "] id = " + els[i].id+" x: "+ iX+ " y: " +
			// iY +";");
			YAHOO.util.Dom.setStyle( els[ i ], 'backgroundPosition', (-iX+x) + "px "+ (-iY+y) +"px" );
		}

		var els = YAHOO.util.Dom.getElementsByClassName('diagBar', 'div'); 
		for ( var i = 0; i < els.length; i++ ){
			var iY = YAHOO.util.Dom.getY( els[ i ] );
			// debug("els["+ i + "] id = " + els[i].id+" x: "+ iX+ " y: " +
			// iY +";");
			YAHOO.util.Dom.setStyle( els[ i ], 'backgroundPosition', "0px "+ (-iY+y) +"px" );
		}	
	},

	setComments: function(){
		var els = YAHOO.util.Dom.getElementsByClassName('f_comments', 'div'); 
		for ( var i = 0; i < els.length; i++ ){
			var obj = new com_limelife_feature_comments( i, els[ i ] );
			// this.objs.push( obj );
		}				
	},

	addAutoRollovers: function(){
		var els = YAHOO.util.Dom.getElementsByClassName('auto_over', 'img'); 
		for ( var i = 0; i < els.length; i++ ){
			new com_limelife_ro_img( els[ i ] );
		}				
	},
	setMouseOverIE6: function(){
		var els = YAHOO.util.Dom.getElementsByClassName('barItem', 'li'); 
		for ( var i = 0; i < els.length; i++ ){
			els[i].onmouseover=function() {
				this.className+=" sfhover";
				var subEls = YAHOO.util.Dom.getElementsByClassName('menuFlyout','div', this); 
				if (subEls != ""){
					subEls[0].style.display='block';
				}
					
			}
			els[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
				var subEls = YAHOO.util.Dom.getElementsByClassName('menuFlyout','div', this); 
				if (subEls != ""){
					subEls[0].style.display='none';
				}
			}
		}				
	},
	
	addAutoClearFields: function(){
		// /!!! NEED TO CLEAR FIELD ON FIRST CLICK!!!!
		/*
		 * this.tagFlag=true; this.formTagsElem.OBJ = this;
		 * this.formTagsElem.onmousedown = function(){ if( this.OBJ.tagFlag ){
		 * this.value = ""; this.OBJ.tagFlag = false; } }
		 */
		var els = YAHOO.util.Dom.getElementsByClassName('auto_clear', 'input'); 
		for ( var i = 0; i < els.length; i++ ){
			els[i].clickFlag = true;
			els[i].onmousedown = els[i].onfocus = function(){
				if( this.clickFlag ){
					this.value = "";
					this.clickFlag = false;
					
					if (this.nextSibling!=null && this.nextSibling.id!=null && this.id.startsWith(this.nextSibling.id))
					{
						this.style.display = "none";
						this.nextSibling.style.display = "block";
						this.nextSibling.focus();
					}
					else
					{
						this.focus();
					}
					// alert('my class: '+ this.className + ""+
					// this.className.indexOf('pass'))
					/*
					 * if( this.className.indexOf('pass') != -1 ){
					 * alert("this.type: "+this.type) //this.type =
					 * "password"; //this.setAttribute("type","password"); }
					 */						

				}

			}
			// new com_limelife_ro_img( els[ i ] );
		}				
	},

	addSocial: function()
	{
		var _url;
		var o;
		var slide = false;
		var slideshow = document.getElementById('slideshow');

		if (!(slideshow == null)){
			var _offsetLeft = "-200px";
			var _offsetTop = "-290px";
			slide=true;
		}

		var urls = YAHOO.util.Dom.getElementsByClassName('permalink','a');
		var els = YAHOO.util.Dom.getElementsByClassName('snapSocial','div');
		if (els.length == 0)
			els = YAHOO.util.Dom.getElementsByClassName('blogSocial','div');
			
		var imgs = document.getElementsByTagName('img');
		var children;
		var _icon;
		var _summary = "";

		for (var i=0; i<els.length; i++){
			if (urls.length != 0){
				children = els[i].parentNode.parentNode.childNodes;
				
				_url = unescape(urls[i]);
				_icon = this.getIcon(children);
				_summary = urls[i].text;

				if (slide){
					o = SHARETHIS.addEntry({title:"From Limelife:", summary:_summary, url:_url, icon:_icon}, {offsetLeft:_offsetLeft, offsetTop:_offsetTop});
				}else{
					o = SHARETHIS.addEntry({title:"From Limelife:", summary:_summary, url:_url, icon:_icon},{button:false});
				}
			}else
			{
				if (slide){
					o = SHARETHIS.addEntry({title:"From Limelife:", summary:_summary, url:_url, icon:_icon},{offsetLeft:_offsetLeft, offsetTop:_offsetTop});
				}else{
					o = SHARETHIS.addEntry({title:"From Limelife:", summary:_summary, url:_url, icon:_icon},{button:false});
				}
			}
			o.attachButton(els[i]);

		}


	},

	getIcon: function (children){
		var _icon;
		for (var j=0; j<children.length; j++){
			if (children[j].nodeType == 1){
				if (children[j].getAttribute('id') == 'f_img'){
					_icon = children[j].childNodes[0].src;
					break;
				}
			}
		}

		if (_icon == undefined){
			_icon = "http://www.limelife.com/res/LimeLifeLogo.png"
		}
		return _icon;

	},

	addNotification: function()
	{
		var notification_message = this.getParameter("notification");
		if (notification_message == null)
			return;
		this.showNotification(notification_message);
	},

	showNotification: function(msg)
	{
		var notify = $("notify_bg");
		var notify_box = $("notify_box");
		//var notify_pad = $("notify_padding");
		notify_box.innerHTML = msg;
		notify.style.display = "block";
		//notify_pad.style.display = "block";
	},

	reloadWithNotification: function(msg)
	{
		var search = window.top.location.search.substring(1);
		var params = search.split("\&");

		var new_search = "";
		for (var i=0; i<params.length; i++)
		{
			var nv = params[i].split("=");
			if (nv[0]!="notification")
			{
				new_search += "&"+nv[0]+"="+nv[1];
			}
		}
		var loc = window.top.location;
		var href = loc.protocol+"//"+loc.host+":"+loc.port+loc.pathname
		+"?"+new_search+"&notification="+escape(msg);
		window.top.location.href = href;
	},
	
	doSMS: function(userID, target, message) {
		if (target.length < 10) return;
		
		if (userID == "-1"){
			var cookieToRead= "JSESSIONID=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				curCookie = ca[i];
				while (curCookie.charAt(0)==' ') 
					curCookie = curCookie.substring(1,curCookie.length);
				if (curCookie.indexOf(cookieToRead) == 0) 
					userID = curCookie.substring(cookieToRead.length,curCookie.length);
			}
			if (userID == "-1"){
				var date = new Date();
				userID = date.toString();
				var expires = "; expires=";
					date.setTime(date.getTime()+(3*60*60*1000)); //3 hour session limit
					expires = expires+date.toGMTString();
					}
					document.cookie = cookieToRead+userID+";"+expires+"; path=/";
				} 
		
		if (userID == "-1"){
			this.smsMsg("6:Unable to send sms - no Session ID could be found");
			return;
		}

		if (message == undefined){
			message="Tap into the LimeLife on your phone! To access the site, click on http://m.limelife.com or type this URL into your browser. Remember to bookmark us!"
		}
		com_pagesociety_DoModule("SmsModule/sendSMS", [ userID,
				target, message], this.smsMsg.bind(this), this.smsMsg.bind(this));
	},
	
	showSMSMsg: function(result){
		$("smsMessage").style.display = "block";
		$("smsMessage").style.visibility = "visible";
		$("promo_spot_text").style.display = "none";
		$("promo_spot_text").style.visibility = "hidden";
	},

	showSMS: function(){
		$("smsMessage").style.display = "none";
		$("smsMessage").style.visibility = "hidden";
		$("promo_spot_text").style.display = "block";
		$("promo_spot_text").style.visibility = "visible";
	},

	smsMsg: function(result){
		var aMsg = ' <a href=# onClick="javascript:com_limelife_tools.showSMS()">Click here</a> ';
		var lH = '43px';
		var mB = '0';
		result = result.toString();

		switch (result.charAt(0))
		{
		case "0":
			result = 'Your Message Has Been Sent!<br /> ';
			aMsg = aMsg+'to send to another phone';
			break;
			
		case "1":
			result = 'Unfortunately our system had a problem and your text message did not go through.<br /> ';
			aMsg = aMsg+'to try again';
			break;

		case "2":
			result = 'Unfortunately the servers burped and your text message did not go through.<br /> ';
			aMsg = aMsg+'to try again';
			break;
			
		case "3":
			result = 'Unfortunately the servers burped and your text message did not go through.<br /> ';
			aMsg = aMsg+'to try again';
			break;

		case "4":
			result = 'Unfortunately your text message did not go through.<p>You have already sent the maximum number of links possible</p>';
			aMsg = '';
			lH = '12px';
			mB = '9px';
			break;

		case "5":
			result = 'Unfortunately your text message did not go through.<p>That phone has already been sent the LimeLife link</p>';
			aMsg = aMsg+'to send to another phone';
			lH = '12px';
			mB = '9px';
			break;

		case "6":
			result = 'Unfortunately your text message did not go through.<p>There is no id associated with your browser session - please reload and try again</p>';
			aMsg = '';
			lH = '12px';
			mB = '9px';
			break;

		default:
			result = 'Your Message Has Been Sent!<br /> ';
			aMsg = aMsg+'to send to another phone';
			break;
		}
		
		$("smsMessage").innerHTML = result+aMsg;
		$("smsMessage").style.lineHeight = lH;
		$("smsMessage").style.marginBottom = mB;
		this.showSMSMsg();
	},

	getParameter: function( parameterName ) {

		var search = window.top.location.search.substring(1);
		var params = search.split("&");
		for (var i=0; i<params.length; i++)
		{
			var nv = params[i].split("=");
			if (nv[0]==parameterName)
			{
				return unescape(nv[1]);
			}
		}
		return null;
	} 	
	}
}();

com_limelife_ro_img = function( obj ){
	this.art = obj;
	this.artOn;
	this.artOff;
	this.init();
}

com_limelife_ro_img.prototype = {
		init: function(){

	var uri = this.art.src;
	if (this.art.style.filter)
	{
		var f = this.art.style.filter;
		var ie6src = f.substring(f.indexOf("(src=")+6);
		uri = ie6src.substring(0,ie6src.indexOf("\","));
	}

	this.artOff = new Image();
	this.artOff.src = uri;

	this.artOn = new Image();
	this.artOn.src = uri.substring( 0,  uri.lastIndexOf(".") ) + "_on" + uri.substring( uri.lastIndexOf("."), uri.length ) ;

	var obj = this;

	this.art.onmouseover = function(){
		obj.art.src = obj.artOn.src;
	}
	this.art.onmouseout = function(){
		obj.art.src = obj.artOff.src;
	}

}

}

com_limelife_feature_comments = function( i, obj ){
	this.el = obj;
	this.btn;
	this.block;
	// this.comments;
	this.art;
	this.arrowImg;
	this.arrowImgDown;
	this.isOpen;
	this.init();
}

com_limelife_feature_comments.prototype = {
		init: function(){

	this.btn = YAHOO.util.Dom.getElementsByClassName( 'f_comments_head', 'div', this.el )[0];
	// this.block = YAHOO.util.Dom.getElementsByClassName(
	// 'f_comments_block', 'div', this.el )[0];
	this.block = YAHOO.util.Dom.getElementsByClassName( 'f_comments_body', 'div', this.el )[0];

	this.art = YAHOO.util.Dom.getElementsByClassName( 'f_comments_arrow', 'img', this.el )[0];
	// this.comments = YAHOO.util.Dom.getElementsByClassName(
	// 'f_comment_post', 'div', this.el )[0];

	var uri = this.art.src;

	this.arrowImg = new Image();
	this.arrowImg.src = uri;

	this.arrowImgDown = new Image();
	this.arrowImgDown.src = uri.substring( 0,  uri.lastIndexOf(".") ) + "_down" + uri.substring( uri.lastIndexOf("."), uri.length ) ;		

	// ////// SHOULD I DO THIS VIA CSS OR SCRIPT??
	// YAHOO.util.Dom.setStyle( this.block, 'display', 'none' );
	this.isOpen = false;

	this.btn.style.cursor = "pointer";

	var obj = this;

	this.btn.onclick = function(){
		obj.toggleBox();
	}
		this.toggleBox();
},
toggleBox: function( indx ){
	// debug("toggleBox " + this.isOpen);
	if( this.isOpen){
		YAHOO.util.Dom.setStyle( this.block, 'display', 'none' );
		YAHOO.util.Dom.setStyle( this.block, '_display', 'block' );
		YAHOO.util.Dom.setStyle( this.block, 'visibility', 'hidden' );
		this.art.src = this.arrowImg.src;
		this.isOpen = false;
	} else {
		YAHOO.util.Dom.setStyle( this.block, 'display', 'block' );
		YAHOO.util.Dom.setStyle( this.block, '_display', 'block' );
		YAHOO.util.Dom.setStyle( this.block, 'visibility', 'visible' );

		this.art.src = this.arrowImgDown.src;
		this.isOpen = true;
	}
}

}



var slideWindow = null; // global variable

function openSlides( s_url )
{
	if(slideWindow == null || slideWindow.closed) {
		/*
		 * if the pointer to the window object in memory does not exist or if such
		 * pointer exists but the window was closed
		 */


		slideWindow = window.open( s_url,'slideWin','height=640,width=725,resizable=1,scrollbars=1,status=0');

		/*
		 * then create it. The new window will be created and will be brought on top
		 * of any other window.
		 */

	} else {
		slideWindow.location.href = s_url;
		slideWindow.focus();
		/*
		 * else the window reference must exist and the window is not closed;
		 * therefore, we can bring it back on top of any other window with the
		 * focus() method. There would be no need to re-create the window or to
		 * reload the referenced resource.
		 */
	};
}


com_limelife_tools.init();
