jQuery(function(){
	clearInputs();
	initBgPage();
})

// clear inputs
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
};

function initBgPage() {
	jQuery('#bg-box img').each(function(){
		imageStretcher.stretch(this)
	})

	// rotation sample
	var set = jQuery('#bg-box img'),
		animSpeed = 800,
		delay = 5000,
		curInd = 0,
		prevInd = 0,
		timer;
	set.hide().eq(curInd).show();
	function nextSlide() {
		prevInd = curInd;
		if(curInd < set.length-1) curInd++; else curInd = 0;
		set.eq(prevInd).fadeOut(animSpeed);
		set.eq(curInd).fadeIn(animSpeed);
		autoSlide();
	}
	function autoSlide() {
		if(timer) clearTimeout(timer);
		timer = setTimeout(nextSlide,delay);
	}
	autoSlide();
}

// image stretcher module
var imageStretcher = (function(){
	var windowWidth, windowHeight, images = [];
	function addImage(img) {
		recalcSize();
		images.push(img);
		if(img.comlete) {
			getRatio(img, resizeImage);
		} else {
			img.onload = function(){
				getRatio(img, resizeImage);
				img.onload = null;
			}
			img.src = img.src; // IE Fix
		}
	}
	function removeImage(img) {
		for(var i=0; i<images.length; i++) {
			if(images[i] === img) {
				images.splice(i,1);
				return;
			}
		}
	}
	function getRatio(img, cb) {
		img.style['msInterpolationMode'] = 'bicubic'; // IE7 fix
		var newImg = new Image();
		newImg.onload = function() {
			img.ratio = newImg.width / newImg.height;
			if(typeof cb === 'function') cb.apply(img)
			newImg.onload = null;
		}
		newImg.src = img.src;
	}
	function resizeImage(img) {
		var _ratio = this.ratio;
		var _slideWidth = windowWidth;
		var _slideHeight = _slideWidth/_ratio;
		if(_slideHeight < windowHeight) {
			_slideHeight = windowHeight;
			_slideWidth = _slideHeight * _ratio;
		}
		this.style.width = _slideWidth+'px';
		this.style.height = _slideHeight+'px';
		this.style.top = (windowHeight-_slideHeight)/2+'px';
		this.style.left = (windowWidth-_slideWidth)/2+'px';
	}
	function recalcSize() {
		windowWidth = getClientWidth();
		windowHeight = getClientHeight();
	}
	function handleResize() {
		recalcSize();
		for(var i=0; i<images.length; i++) resizeImage.apply(images[i]);
	}
	addHandler(window, 'resize', handleResize);

	// util functions
	function addHandler(object, event, handler) {
		if (typeof object.addEventListener != 'undefined') object.addEventListener(event, handler, false);
		else if (typeof object.attachEvent != 'undefined') object.attachEvent('on' + event, handler);
	}
	function getClientWidth(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientWidth'] || window.document.body['clientWidth']}
	function getClientHeight(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientHeight'] || window.document.body['clientHeight']}

	// methods
	return {
		stretch: addImage,
		unstretch: removeImage
	}
})()
