site={
	init: function () {
		// setup rotating background on homepage
		if ($('background'))	new Carousel($$('#background img'));
		// setup collapsing footer
		if ($('collapsingFooter'))	new CollapsedFooter($('collapsingFooter'));
		
		
	}
}
window.addEvent('domready', site.init);


var Carousel = new Class({
	initialize: function (screens) {
		this.screens = screens.setStyle('opacity', 0);
		var lastActiveScreenPosition = parseInt(Cookie.get('lastActiveScreenPosition'));
		if (lastActiveScreenPosition >= this.screens.length-1) lastActiveScreenPosition = 0;
		this.activeScreen = screens[lastActiveScreenPosition+1 || 0].setStyle('opacity', 1);
		this.start();
	},
	start: function () {
		this.timer = (function () {
			var fade = new Fx.Style(this.activeScreen, 'opacity', {duration:1000});
				fade.start(1,0);
			var pos = this.screens.indexOf(this.activeScreen);
				pos = (pos == this.screens.length-1) ? 0 : pos + 1;
			this.activeScreen = this.screens[pos];
			Cookie.set('lastActiveScreenPosition', pos);
			var fade2 = new Fx.Style(this.activeScreen, 'opacity', {duration:1000});
				fade2.start(0,1);
		}.bind(this)).periodical(7000);
	}
});

var CollapsedFooter = new Class({
	initialize: function (el) {
		this.element = $(el);
		this.collapsedHeight = 25;
		this.height = this.element.getSize().size.y;
		this.width = 974;
		var scrollHeight = window.getScrollHeight();
		var windowWidth = window.getWidth();
		this.element.setStyles({
			height:this.collapsedHeight,
			overflow:'hidden',
			position:'absolute',
			top:scrollHeight - this.collapsedHeight,
			left:0
//			left:windowWidth/2 - this.width/2
		});
		this.isOpen = false;
		if ($('footer_banner'))	$('footer_banner').setStyle('bottom', 50);
		this.fx = new Fx.Styles(this.element);
//		this.element.addEvents({
//			mouseenter:this.open.bindAsEventListener(this),
//			mouseleave:this.close.bindAsEventListener(this)
//		});
		this.element.addEvent('click', this.toggle.bindAsEventListener(this));
		window.addEvents({
			scroll:this.setPosition.bindAsEventListener(this),
			resize:this.setPosition.bindAsEventListener(this)
		});
	},
	setPosition: function () {
		var scrollHeight = window.getScrollHeight();
		var windowHeight = window.getHeight();
		var targetHeight = scrollHeight <= windowHeight ? scrollHeight : windowHeight;
		this.fx.set({
			height: this.isOpen ? this.height : this.collapsedHeight,
			top:targetHeight-this.collapsedHeight
		});
        
//		var windowWidth = window.getWidth();
//		this.element.setStyle('left', windowWidth/2 - this.width/2);
	},
	open: function () {
		var scrollHeight = window.getScrollHeight();
		this.fx.start({
			height:this.height,
			top:scrollHeight-this.height
		});
		this.isOpen = true;
		$$('.mc_home_footer_title_item').each(function (div) {div.addClass('open');});
	},
	close: function () {
		var scrollHeight = window.getScrollHeight();
		this.fx.start({
			height:this.collapsedHeight,
			top:scrollHeight-this.collapsedHeight
		});
		this.isOpen = false;
		$$('.mc_home_footer_title_item').each(function (div) {div.removeClass('open');});
	},
	toggle: function (e) {
		if ($(new Event(e).target).getTag() == 'input')	return;
		this[this.isOpen ? 'close' : 'open']();
	}
});

if (window.ie6) {
	window.addEvent('domready', function(){
		$extend(kina,{
			bgs: $$('#bg img'),
			timer:setInterval(kina.fix, 200)
		});
	});

	kina={
		doc:{x:0,y:0},
		fix:function(){
			kina.bgs.each(function (kinaBG) {
				if(kinaBG.complete && ($(document.body).offsetWidth != kina.doc.x || $(document.body).offsetHeight != kina.doc.y || kina.fix.src != kinaBG.src)){
					var bg=$(kinaBG).getSize();
					kina.doc={x:$(document.body).offsetWidth,y:$(document.body).offsetHeight};
					kina.fix.src==kinaBG.src||$extend(kina.fix,{p:bg.size.x/bg.size.y,src:kinaBG.src});
					$(kinaBG).setStyles({
						width:(bg.size.y=kina.doc.x>(bg.size.x=Math.round(kina.doc.y*kina.fix.p)))?kina.doc.x:bg.size.x,
						height:bg.size.y?Math.round(kina.doc.x/kina.fix.p):kina.doc.y
					})
				}
			});
		}
	}
}

Element.extend({
	getPositionedOffset: function () {
		var left = 0, top = 0, element = this;
		do {
			left += element.offsetLeft || 0;
			top += element.offsetTop  || 0;

			element = $(element.offsetParent);
			if (element) {
				if (element.tagName == 'BODY') break;
				var p = element.getStyle('position');
				if (p == 'relative' || p == 'absolute') break;
			}
		} while (element);
		return {'x':left, 'y':top};
	}
});
