function check(obj) {
	return !!(obj || obj === 0);
}

/* Viewport */
function Viewport(win) {
	this.win = win || window;
	this.$win = $(this.win);
	this.doc = this.win.document;
	this.width = 0;
	this.height = 0;
	this.scroll = { x: 0, y: 0 };
}
Viewport.prototype.getViewport = function () {
	var win = this.win,
		doc = this.doc;
	if (doc.documentElement && (check(doc.documentElement.clientWidth) || check(doc.documentElement.clientHeight))) {
		this.width = doc.documentElement.clientWidth;
		this.height = doc.documentElement.clientHeight;
	} else if (doc.body && (check(doc.body.clientWidth) || check(doc.body.clientHeight))) {
		this.width = doc.body.clientWidth;
		this.height = doc.body.clientHeight;
	} else if (check(win.innerWidth) || check(win.innerHeight)) {
		this.width = win.innerWidth;
		this.height = win.innerHeight;
	}
};
Viewport.prototype.getScroll = function () {
	var win = this.win,
		doc = this.doc;
	if (doc.documentElement && (doc.documentElement.scrollLeft || doc.documentElement.scrollTop)) {
		this.scroll.x = doc.documentElement.scrollLeft;
		this.scroll.y = doc.documentElement.scrollTop;
	} else if (doc.body && (check(doc.body.scrollLeft) || check(doc.body.scrollTop))) {
		this.scroll.x = doc.body.scrollLeft;
		this.scroll.y = doc.body.scrollTop;
	} else if (check(win.pageXOffset) || check(win.pageYOffset)) {
		this.scroll.x = win.pageXOffset;
		this.scroll.y = win.pageYOffset;
	} else if (check(win.scrollX) || check(win.scrollY)) {
		this.scroll.x = win.scrollX;
		this.scroll.y = win.scrollY;
	}
};

/* Quick Menu */
function quickLayer(selector, speed) {
	if (!window.VP) {
		return false;
	}
	speed = speed || 500
	var qm = $(selector),
		init_top = parseInt(qm.css('top')),
		init_offtop = qm.offset().top,
		img_holder = $('div.quick-img img', qm),
		imgs = $('ul img', qm);
	imgs.bind('mouseover', function () {
		var src = this.getAttribute('src');
		imgs.each(function () {
			var src = this.getAttribute('src');
			this.setAttribute('src', src.replace('_on.', '_off.'));
		});
		this.setAttribute('src', src.replace('_off.', '_on.'));
		img_holder.attr('src', '/images/quick0' + (imgs.index(this) + 1) + '_img.gif');
	});
	function move() {
		var new_top = (VP.scroll.y > init_offtop) ? (VP.scroll.y - init_offtop + init_top + 10) : init_top;
		qm.stop().animate({
			'top': new_top + 'px'
		}, speed);
	}
	VP.$win.bind('scroll', move);
	VP.$win.bind('resize', move);
}

$(document).ready(function () {
	window.$win = $(window);
	window.VP = new Viewport();
	VP.getViewport();
	VP.getScroll();
	VP.$win.bind('scroll', function () {
		VP.getScroll();
	});
	VP.$win.bind('resize', function () {
		VP.getViewport();
		VP.getScroll();
	});
	quickLayer('#dvquick');
});
