window.addEvent('domready', function() {

var scrollPos;

$$(".info_box").addEvents({
    'mouseover': function(){
        this.removeClass("info_box");
        this.addClass("info_box_hover");
    },
    'mouseleave': function(){
        this.removeClass("info_box_hover");
        this.addClass("info_box");
    }
  });
  
  if($('headline_click_to_play_image')) {
	  $('headline_click_to_play_image').addEvents({
	    'mouseenter':function(){
	        this.set('src','/css/i/PlayButton270x202.png').morph({'opacity':[0.5,1]});
	    },
	    'mouseleave':function(){
	        this.set('src', '/css/i/PlayButtonRolledOver270x202.png').morph({'opacity':1});
	    }
	  });
  }   

	setTimeout("rotateImages()",1);
   
	var mainFeatureBox = $('main_feature_box');
	if(mainFeatureBox)
		initFeatureBox();	
});

var currentImg = 0;
function rotateImages()
{
	var total;
	$each($$('#survey_box img', '.ad_box img'),function(img, i){
		var imgFx = new Fx.Morph(img,{duration:3000});
		if(currentImg == i) {
			img.setStyles({'display':'block','opacity':0});
			imgFx.start({'opacity':1});
		} else {
			img.setStyle('display','none');
		}
		total = i;
	});
	if(currentImg == total)
		currentImg = 0;
	else
		currentImg++;
	if(total >= 1)
		setTimeout("rotateImages()",8000);
}



var initFeatureBox = function() {
	//Load mooquee
//	loadJS('/js/mooquee.js');

    var runningBackandforth;    
	var totalTabs = $$('#main_feature_box .feature_tabs .tab').length,
		tabWidth = 476 / totalTabs - 20,
		tabs = $$('#main_feature_box .feature_tabs .tab'),
		slides = $$('#main_feature_box .feature_box'),
		forms = $$('#main_feature_box .feature_forms .form_holder'),
		slideCtr = 0;
		
	$$('#main_feature_box .feature_tabs .tab .tab_mid').setStyle('width',tabWidth+'px');
	
	if(Browser.Engine.trident && location.href.indexOf('www.healthguru.com') != -1)
 	   $('dd_forms').setStyle('left',$('main_feature_box').getPosition().x+'px');
 	   
	tabs.addEvent('click',function(e){
		if($type(e) == 'event')
			$clear(runShow);
		var id= this.get('id').substring(12);
		tabs.removeClass('tab-selected');
		tabs.addClass('tab-unselected');
		this.addClass('tab-selected');
		$$('a.related_right_link').fade(0);
        $$('a.related_left_link').fade(0);
		$$('a.related_right_link').setStyle('visibility', 'visible');
        $$('a.related_left_link').setStyle('visibility', 'visible');
        var mainRelatedLinks = $('feature_related_'+id);
        
        var minScroll = 0;
        var scrollStep = 1;
		var curScroll = 0;
        var direction = 0;
        var passFirst = 0;
        
		DDForm.hide();
        
        var backandforth = function() {
            var curScroll = mainRelatedLinks.getScroll().x;   
            var maxScroll = mainRelatedLinks.getScrollSize().x - mainRelatedLinks.getSize().x;  

            if( (curScroll < maxScroll) && (direction == 0) ) {
                passFirst = 0;
                mainRelatedLinks.scrollTo(curScroll+scrollStep, 0);
            } else {
                direction = 1;
            }
            
            if( (curScroll > minScroll) && (direction == 1) ) {
                if(passFirst < 3) {
                    passFirst += 1;
                } else {
                    mainRelatedLinks.scrollTo(curScroll-scrollStep, 0);
                }
             } else {
                direction = 0;
             }
        };
        if($defined(runningBackandforth)) {
            $clear(runningBackandforth);
        }
        runningBackandforth = backandforth.periodical(100);

        mainRelatedLinks.addEvents({
            'mouseenter':function(){
                $$('a.related_right_link').fade(1);
                $$('a.related_left_link').fade(1);
            },
            'mouseleave':function(){
              $$('a.related_right_link').fade(0);
              $$('a.related_left_link').fade(0);
            }
        });
		var slide = $('feature_box_'+id),
			form = $('feature_form_'+id),
			slideMorph = new Fx.Morph(slide.getElement('.feature_image'),{link:'chain'}),
			formMorph = new Fx.Morph(form);
			
		forms.addClass('hide');
		form.setStyle('opacity',0);
		form.removeClass('hide');
		formMorph.start({opacity:1});
			
		slides.addClass('hide');
		slide.getElement('.feature_image').setStyle('opacity',0);
		slide.removeClass('hide');
		slide.getElement('.feature_text').setStyle('opacity',0);
		slideMorph.start({opacity:1}).chain(function(){
			slide.getElement('.feature_text').morph({opacity:1});			
		});

	});
	
	// Scroll the Categories Listing when arrows are clicked.
    var scrollStep = 5;
    
    var startedScroll;
    $$('a.related_right_link').addEvents({
        'mousedown':function(e) {
            e.stop();
            var current = this;
    		var scrollHold = function(){
        		var relatedLinks = current.getPrevious('div.feature_related_links');
        	    var curScroll = relatedLinks.getScroll().x;
                var maxScroll = relatedLinks.getScrollSize().x;
                if(curScroll < maxScroll) {
                    relatedLinks.scrollTo(curScroll+scrollStep, 0);
                }
            };
            startedScroll = scrollHold.periodical(50);	
            $clear(runShow);
            //$clear(runningBackandforth);
            },
        'mouseup':function(){
            $clear(startedScroll);
        },
        'mouseenter':function(){
            this.fade(1);
        },
        'mouseleave':function(){
            this.fade(0);
        }
	});
	$$('a.related_left_link').addEvents({
        'mousedown':function(e) {
            e.stop();
            var current = this;
    		var scrollHold = function(){                
        		var relatedLinks = current.getNext('div.feature_related_links');
        	    var curScroll = relatedLinks.getScroll().x;
                var minScroll = 0;
                if(curScroll > minScroll) {
                    relatedLinks.scrollTo(curScroll-scrollStep, 0);
                };
            }
            $clear(runShow);
            //$clear(runningBackandforth);
            startedScroll = scrollHold.periodical(50);
            
        },
        'mouseup':function() {
            $clear(startedScroll);
        },
        'mouseenter':function(){
            this.fade(1);
        },
        'mouseleave':function(){
            this.fade(0);
            
        }
	});
	
	$$('.feature_related').addEvents({
	   'mouseenter':function(){
                $$('a.related_right_link').fade(1);
                $$('a.related_left_link').fade(1);
        },
        'mouseleave':function(){
          $$('a.related_right_link').fade(0);
          $$('a.related_left_link').fade(0);
        }
	});
    
    
	tabs[slideCtr].removeClass('tab-unselected');
	tabs[slideCtr].addClass('tab-selected');
	slides[slideCtr].removeClass('hide');
	forms[slideCtr].removeClass('hide');
	var slideShow = function(){
		if(slideCtr >= tabs.length - 1)
			slideCtr = 0;
		else
			slideCtr++;
		tabs[slideCtr].fireEvent('click');
	}
	var runShow = slideShow.periodical(10000);	
	
	var DDFormFx = new Fx.Morph($('dd_forms'));
	var initEvents = function() {
		$$('#dd_forms .form_text').addEvents({
			'focus':function(){
			    if(this.value == this.alt) {
			        this.value = '';
			    }
			    this.setStyle('color', '#000000');
			    if(this.hasClass('form_password')){
			        this.type = 'password';
			    }
			    tooltip.show(this,' &nbsp; &nbsp; &nbsp; Enter '+this.get('alt').capitalize(),170);   
			},
			'blur':function(){
				if(this.value.length == 0) {
					if(this.hasClass('form_password')){
					    this.type = 'text';
					}
					this.value = this.alt;
					this.setStyle('color', '#6E6F6F');   
				}
			    tooltip.hide();
			}
		});
		$('dd_register_form').addEvent('submit',function(e){
			e.stop();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') == v.get('value'))
					v.set('value','');
			});
			this.set('send',{
				onComplete:function(response) {
					if(response.indexOf('complete') > 0) {
						loginUser(response.split('-')[0]);
						DDForm.hide();
					} else 
						tooltip.show($('btnRegister'),response);
				}
			});
			this.send();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') && v.get('value') == '')
					v.set('value',v.get('alt'));
			});
		});
		
		$('dd_login_form').addEvent('submit',function(e){
			e.stop();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') == v.get('value'))
					v.set('value','');
			});
			this.set('send',{
				onComplete:function(response) {
					if(response.indexOf('success') > 0) {
						//location.href=location.href;
						loginUser(response.split('-')[0]);
						DDForm.hide();
					} else 
						tooltip.show($('btnLogin'),response);
				}
			});
			this.send();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') && v.get('value') == '')
					v.set('value',v.get('alt'));
			});
		});
		
		$$('a.forgot_link').removeEvents().addEvent('click',function(e){
			e.stop();
			initForgotLinks();
		});
		
		$('close_dd').addEvent('click',function(){
			DDForm.hide();
		});
	}	
	
	//Start events for DD RegForm
	initEvents();
	var selForm = null;
	var DDForm = {
		show : function(msg) {
			if(msg)
				$$('#dd_forms h3').set('html',msg);
			DDFormFx.start({top:'557px'});				
		},
		hide : function() {
			tooltip.hide();
			DDFormFx.start({top:'260px'});		
			if(selForm) {
				//selForm.fireEvent('submit');
				$$('.form_holder form').fireEvent('submit');		
				selForm = null;
			}
		}
	};
	
	$$('.form_holder').addEvent('click',function(){
		$clear(runShow);		
	});
	
	$$('.form_holder form').addEvent('submit',function(e){
		if($type(e) == 'event') {
			e.stop();
			$clear(runShow);
		}
		if($('right_user_log_name_label')) {
			var myForm = this;
			this.set('send',{
				onComplete:function(response) {
					if(response != '')
						myForm.getElement('.form_answer').set('html',response);
				}
			});
			this.send();
		} else {
			selForm = this;
			DDForm.show(this.getElement('.form_message').get('value'));
		}
	});

	if($('right_user_log_name_label')) {
		$$('.form_holder form').fireEvent('submit');
	}
		
    tabs[0].fireEvent('click');
}


