﻿LM = {};

LM.Index = {

    CreateAccordion: function() {
        new Accordion($$('.processSelect'), $$('.processForms'), {
            show: 0,
            alwaysHide: true,
            fps: 300,
            onActive: function(toggler, element) {
                toggler.getElement('h3').addClass('active');
            },
            onBackground: function(toggler, element) {
                toggler.getElement('h3').removeClass('active');
            }
        })
    },
    InputHelper: function(theInput) {
        var holdText = theInput.value;
        theInput.addEvents({
            'blur': function() {
                if (theInput.get('value') == '') {
                    theInput.set('value', holdText);
                }
            },
            'click': function() {
                if (theInput.get('value') == holdText)
                    theInput.set('value', '');
            }
        })
    },
    FontSizeChanger: function(elements, size, lineheight) {
        elements.setStyle('font-size', size);
        elements.setStyle('line-height', lineheight);
       
        Cookie.write("fontSize", size,5);
        Cookie.write("fontLineHeight", lineheight, 5);


    },
    AjaxCalculate: function() {
        $('calculator').addEvent('submit', function(e) {
            e.stop();
            new Request.HTML({
                url: path + '/home/ajaxcalculate.rails',
                update: 'costs'
            }).post(this);
        })
        $$('.sbaField input', '.laxField input').addEvent('keyup', function(e) {
            new Request.HTML({
                url: path + '/home/ajaxcalculate.rails',
                update: 'costs'
            }).post($('calculator'));
        })
    },
    InitAccordion: function(open) {
        var show = -1;
        if (open)
            show = 0;
        new Accordion($$('.toggler'), $$('.shade'), {
            alwaysHide: true,
            display: show,
            onActive: function(t, e) {
                t.addClass('active');
            },
            onBackground: function(t, e) {
                t.removeClass('active');
            }
        })
    },
    InitDropDown: function() {
        $$('#navigation ul li').addEvents({
            'mouseenter': function() {
                this.addClass('ieFix');
            },
            'mouseleave': function() {
                this.removeClass('ieFix');
            }
        })
    }
};
LM.Time = new Class({
	initialize: function(time) {
		this.GetTime.periodical(1000, time);
	},
	GetTime: function() {
		var gmtTime = this;
		var hr = gmtTime.getHours()
		var min = gmtTime.getMinutes()
		var sec = gmtTime.getSeconds()

		//	set am/pm
		var ampm = "AM";
		if (hr >= 12) {
			ampm = "PM";
			hr -= 12;
		}
		if (hr <= 0)
			hr = 12;

		if (sec < 10) {
			sec = "0" + sec;
		}
		if (min < 10) {
			min = "0" + min;
		}
		$('time').set('text', hr + ":" + min + " " + ampm + "/PST");

		this.setTime(gmtTime.getTime() + 1000);
	}
});

LM.PageFlipper = {
	NextPage: function(page)
	{
		var me = this;
		new Request.HTML({
		url: path + '/news_facts/ajaxpage.rails',
			onComplete: function(tree,elements,html,js)
			{
				tree[0].inject($('scrollMe'))
				new Fx.Tween('scrollMe',{
							transition: Fx.Transitions.Quad.easeOut,
							duration: 500
						}).start('left', 0, -500).chain(function(){
							$$('.blurbContainer')[0].destroy();
							$('scrollMe').setStyle('left',0);
						});
						
//				var pagination = tree[0].getElement('.newsPagination');
//				$$('.pagContainer').each(function(e) { e.empty() });
//				$$('.pagContainer').each(function(e) { e.grab(pagination.clone()) });
//				me.CreatePagination();
			}
		}).post({'page': page});
		
		new Request.HTML({
		url: path + '/news_facts/ajaxpagination.rails',
			onComplete: function(tree,elements,html,js)
			{
				$$('.pagContainer').set('html',html);
				me.CreatePagination();
			}
		}).post({'page': page});
	},
	PrevPage: function(page)
	{
		var me = this;
		new Request.HTML({
		url: path + '/news_facts/ajaxpage.rails',
			onComplete: function(tree,elements,html,js)
			{
				tree[0].inject($('scrollMe'), 'top');
				$('scrollMe').setStyle('left', -500);
				new Fx.Tween('scrollMe',{
							transition: Fx.Transitions.Quad.easeOut,
							duration: 500
						}).start('left', -500, 0).chain(function(){
							$$('.blurbContainer')[1].destroy();
							$('scrollMe').setStyle('left',0);
						});
						
//				var pagination = tree[0].getElement('.newsPagination');
//				$$('.pagContainer').each(function(e) { e.empty() });
//				$$('.pagContainer').each(function(e) { e.grab(pagination.clone()) });
//				me.CreatePagination();
			}
		}).post({'page': page});
		
		new Request.HTML({
		url: path + '/news_facts/ajaxpagination.rails',
			onComplete: function(tree,elements,html,js)
			{
				$$('.pagContainer').set('html',html);
				me.CreatePagination();
			}
		}).post({'page': page});
	},
	FlipPage: function(page)
	{
		if(!$chk(this.currentPage))
			this.currentPage = 1;
			
		if(page > this.currentPage)
			this.NextPage(page)
		else
			this.PrevPage(page)
			
		this.currentPage = page;
	},

	CreatePagination: function()
	{
		var me = this;
		$$('.newsPagination a').addEvent('click', function(e)
		{
			e.stop();
			
			var strLink = this.getProperty('href').split('/');
			
			me.FlipPage(strLink[strLink.length - 1]);
		});
	}
}
