jQuery(function() {
	initBgGallery();
	initSlideBlock();
	if("#switcher"){
		jQuery("#switcher").css("marginTop","0px").delay(1000).animate({marginTop:"0px"}, 1000, "easeInOutExpo");	
	}	
});
function initBgGallery() {
	var holder = document.getElementById('bg-body');
	if(holder) {
		jQuery('#bg-body img').each(function() {
			backgroundStretcher.stretchImage(this);
		});
		backgroundStretcher.setBgHolder(holder);
	}
	var set = jQuery('#bg-body img'),
		switcher = jQuery('#switcher > li'),
		activeClass = 'active',
		animSpeed = 900,
		delay = 9999999,
		curInd = 0,
		prevInd = 0,
		timer,
		autorotate = true;
	set.hide().eq(curInd).show();
	function nextSlide() {
		prevInd = curInd;
		if(curInd < set.length-1) curInd++; else curInd = 0;
		switchSlide();
	}
	function autoSlide() {
		if(timer) clearTimeout(timer);
		if(autorotate) timer = setTimeout(nextSlide,delay);
	}
	autoSlide();
	function switchSlide() {
		set.eq(prevInd).fadeOut(animSpeed);
		set.eq(curInd).fadeIn(animSpeed);
		switcher.removeClass(activeClass).eq(curInd).addClass(activeClass);
		autoSlide();
	}
	switcher.each(function(i) {
		var btn = jQuery(this);
		btn.click(function() {
			prevInd = curInd;
			curInd = i;
			switchSlide();
			return false;
		})
	});
	jQuery(window).focus(function() {
		autorotate = true;
		autoSlide();
	}).blur(function() {
		if(timer) clearTimeout(timer);
		autorotate = false;
	})
}
function initSlideBlock() {	
	jQuery('.slide-block').each(function() {
		var slideBox = jQuery('> .block', this),
			opener = jQuery('.open-close', this),
			close = jQuery('.close', this),
			animate = false;
	});
}
backgroundStretcher = {
	images: [],
	holders: [],
	viewWidth: 0,
	viewHeight: 0,
	stretchByWindow: false, // or entire page
	init: function(){
		this.addHandlers();
		this.resizeAll();
		return this;
	},
	stretchImage: function(obj) {
		var img = new Image();
		img.onload = this.bind(function(){
			obj.iRatio = img.width / img.height;
			obj.iWidth = img.width;
			obj.iHeight = img.height;
			obj.style.msInterpolationMode = 'bicubic'; // IE7 fix
			this.resizeImage(obj);
		});
		img.src = obj.src;
		this.images.push(obj);
	},
	setBgHolder: function(obj) {
		this.holders.push(obj);
		this.resizeAll();
	},
	getWindowWidth: function() {
		return typeof window.innerWidth === 'number' ? window.innerWidth : document.documentElement.clientWidth;
	},
	getWindowHeight: function() {
		return typeof window.innerHeight === 'number' ? window.innerHeight : document.documentElement.clientHeight;
	},
	getPageWidth: function() {
		if(!document.body) return 0;
		return Math.max(
			Math.max(document.body.clientWidth, document.documentElement.clientWidth),
			Math.max(document.body.offsetWidth, document.body.scrollWidth)
		);
	},
	getPageHeight: function() {
		if(!document.body) return 0;
		return Math.max(
			Math.max(document.body.clientHeight, document.documentElement.clientHeight),
			Math.max(document.body.offsetHeight, document.body.scrollHeight)
		);
	},
	resizeAll: function() {
		for(var i = 0; i < this.holders.length; i++) {
			this.holders[i].style.width = '100%'; 
		}
		clearTimeout(this.resizeTimer);
		this.resizeTimer = setTimeout(this.bind(function(){
			for(var i = 0; i < this.holders.length; i++) {
				this.holders[i].style.display = 'none';
			}
			this.viewWidth = this[this.stretchByWindow ? 'getWindowWidth' : 'getPageWidth']();
			this.viewHeight = this[this.stretchByWindow ? 'getWindowHeight' : 'getPageHeight']();

			for(i = 0; i < this.holders.length; i++) {
				this.holders[i].style.display = 'block';
				this.resizeHolder(this.holders[i]);
			}
			for(i = 0; i < this.images.length; i++) {
				this.resizeImage(this.images[i]);
			}
		}),10);
	},
	addHandlers: function() {
		if (window.addEventListener) window.addEventListener('resize', this.bind(this.resizeAll), false);
		else if (window.attachEvent) window.attachEvent('onresize', this.bind(this.resizeAll));
	},
	bind: function(fn, scope, args) {
		var newScope = scope || this;
		return function() {
			return fn.apply(newScope, args || arguments);
		}
	} 
}.init();
