var cHeadlineNews = new Class({
	Implements: Options,
	initialize: function(element) {
		this.container = $(element);
		this.container.setStyles({
			'display': 'block'
		}).set('tween', {
			property: 'opacity',
			duration: 400
		}).addEvents({
			mouseenter: function(e) {this.isrun = false;}.bind(this),
			mouseleave: function(e) {this.isrun = true;}.bind(this)
		});
	
	  this.liHeight = this.container.getFirst('li').getHeight();
	  this.liAmount = this.container.getChildren('li').length;
	  this.Index = 0;
	
		this.isrun = true;
		this.timer = this.run.periodical(4000, this);
	},
	run: function() {
		if(this.isrun == true) {
			if(++this.Index > this.liAmount-1) this.Index = 0;

			var fx = this.container.get('tween');
			fx.start(0).chain(function(){
				this.container.setStyle('margin-top', (this.liHeight * this.Index * -1));
				fx.start(1);
			}.bind(this));

			this.isrun = true;
		}	
	}
});

var cBrandTips = new Class({
	initialize: function(elcon) {
		var options = {
			offsetY: -10,
			offsetX: 42
		};

		elcon = $(elcon);
		elcon.getChildren('a').each(function(el) {
			
			var pos = el.getPosition($('mainContent'));
			var tip = new Element('div', {
				'class': 'brandsTooltip',
				'styles': {
					'top': pos.y + options.offsetY,
					'left': pos.x + options.offsetX
				},
				'opacity': 0
			}).inject(el);
			new Element('div', {'html': 'Заказать'}).inject(tip);
			
			el.addEvents({
				mouseenter: function(event) {
					tip.fade(1);
				},
				mouseleave: function(event) {
					tip.fade(0);
				}
			})	
		});
	}
});

var cStyleSW = new Class({
	initialize: function() {
		var trigs = $('set-styles').getChildren();
		this.styles = new Array();
		this.selected = '';
		for(i=0; i<trigs.length; i++) {
			var styleId = trigs[i].get('id').substr(4);
			if($('style-' + styleId)) {
				var styleCss = $('style-' + styleId);
				trigs[i].addClass('active');
				this.selected = styleId;
			}
			else {
				var styleCss = new Asset.css('/scripts/skin-' + styleId + '.css', {id: 'style-' + styleId, 'title': styleId});
				styleCss.disabled = true;
			}
			this.styles.push({
				'id': styleId,
				'li': trigs[i],
				'style': styleCss
			});
			trigs[i].addEvent('click', this.setStyle.bind(this, styleId));
		}
		
		var hour = new Date().get('hours');
		if(hour > 22 || hour <= 8) this.setStyle('night');
		else if(hour > 8 && hour <= 12) this.setStyle('morning');
		else if(hour > 12 && hour <= 17) this.setStyle('day');
		else this.setStyle('evening');
	},
	setStyle: function(styleId) {
		if(this.selected != styleId) {
			$each(this.styles, function(obj) {
				 if(obj.id == styleId) {
					 obj.li.addClass('active');
					 obj.style.disabled = false;
				 }
				 else {
				 	 obj.li.removeClass('active');
				 	 obj.style.disabled = true;	 
				 }
			});
			this.selected = styleId;
		}
		
	}
});



/* main */
window.addEvent('domready', function() {
	new cHeadlineNews('headline-news');
	new cBrandTips('brands');
	new cStyleSW();
	
	$$("#contactForm input[type=text]", "#contactForm textarea").each(function(el){
		var color = el.getStyle('backgroundColor');
		el.addEvents({
			focus: function(e) {
				this.morph({'background-color': '#fcffa6'});
			},
			blur: function(e) {
				this.morph({'background-color': color});
			}
		});
	});
	
	$('topLink').addEvent('click', function() {
		new Fx.Scroll(window).toTop();
		return false;
	});

    $$('.link-contactform').addEvent('click', function() {
        new Fx.Scroll(window).toElement('lnconform');
    });
    
//    $('contactForm').addEvent('submit', function() {
//        this.send();
//        return false;
//    });

});

