/* vim:set autoindent shiftwidth=4 tabstop=4 noexpandtab cindent: */

// copied from quicktuour.js

ui.notice = {};

// constructor
ui.notice.Notice = function(path_prefixi, width, height) {
	this.scroll_checker = null;
	this.path_prefix = path_prefix;
	this.is_closed = true;

	this.onQTStart = null;
	this.onQTClose = null;
	this.onQTMovePage = null;

	// init the iframe player
	this.player = { max_opacity: 0.99, width: 899, height: 355, layer: std.dom.element('NoticePlayer'), control: std.dom.element('NControlPanel'), iframe: std.dom.element('NPlayer'), fader: new std.ani.Interpolator() }
	this.player.fader.setOnEvent(std.lang.bind(this, this._onPlayerFaderEvent));
	if (width) { this.player.width = width; }
	if (height) { this.player.height = height; }

	// init the black-screen background
	this.bg = { max_opacity: 0.6, layer: std.dom.element('NoticeScreen'), fader: new std.ani.Interpolator() };
	this.bg.fader.setOnEvent(std.lang.bind(this, this._onBgFaderEvent));

	// close notice when the black-screen is clicked
	std.event.observe(this.bg.layer, 'click', std.lang.bind(this, this.close));
}

// load a notice page to the iframe
ui.notice.Notice.prototype.loadPage = function() {
	this.player.iframe.src = this.path_prefix + 'notice';
}

// callbacks for faders
ui.notice.Notice.prototype._onPlayerFaderEvent = function(fader) {
	std.dom.opacity(this.player.layer, fader.getCurPoint());
}

ui.notice.Notice.prototype._onBgFaderEvent = function(fader) {
	std.dom.opacity(this.bg.layer, fader.getCurPoint());
}

// adjust notice player size according to screen width/height
ui.notice.Notice.prototype.setPlayerSize = function() {
	var player = this.player;
	var sw = window.innerWidth ? window.innerWidth : document.body.clientWidth;
	var sh = window.innerHeight ? window.innerHeight : document.body.clientHeight;
//	alert("sw = " + sw + ", sh = " + sh + "/ player.width = " + player.width + ", player.height = " + player.height);
	if (sw < player.width + 50) { // when the screen size is smaller than the player size
		player.layer.style.width = (sw - 50) + 'px';
		player.iframe.style.width = (sw - 50) + 'px';
	} else {
		player.layer.style.width = player.width + 'px';
		player.iframe.style.width = player.width + 'px';
	}
	if (sh < player.height + 30) { // when the screen size is smaller than the player size
		player.iframe.style.height = (sh - 30) + 'px';
	} else {
		player.iframe.style.height = player.height + 'px';
	}
}

// hook the notice player to screen center position
ui.notice.Notice.prototype.setPlayerPosition = function() {
	var player = this.player;
	var sw = window.innerWidth ? window.innerWidth : document.body.clientWidth;
	var sh = window.innerHeight ? window.innerHeight : document.body.clientHeight;
	var pw = (sw < player.width + 50) ? (sw-50) : player.width; // when the screen size is smaller than the player size
	var ph = (sh < player.height + 30) ? (sh-30) : player.height; // when the screen size is smaller than the player size
	player.top =  parseInt((sh-ph)/3.0);  // less space on the top
	player.left = parseInt((sw-pw)/2.4);  // less space on the left
	if (std.browser.isSafariMobile) {  // quick fix for incorrect positioning in iPhone Safari
		player.top = 10;
	}
	player.layer.style.top = player.top + 'px';
	player.layer.style.left= player.left + 'px';
}

// callback to adjust player size/position on window resize
ui.notice.Notice.prototype._resizeHandler = function() {
	// adjust the black-screen properly on window resize for in IE
	if (std.browser.isIE) {
		std.css.bulkset('width', [document.body.clientWidth + 'px', this.bg.layer]); 
		std.css.bulkset('height', [document.body.scrollHeight + 'px', this.bg.layer]);
	}
	this.setPlayerSize();
	this.setPlayerPosition();
}

// start and showup the notice
ui.notice.Notice.prototype.start = function() {
	// load page if the iframe hasn't get any page loaded
	if (!this.player.iframe.src) {
		this.loadPage();
	}

	// add onresize handler to update the iframe position properly
	std.event.observe(window, 'resize', std.lang.bind(this, this._resizeHandler));

	// adjust the black-screen properly on window resize for non-IE browsers
	if (!std.browser.isIE) {
		this.bg.layer.style.right = '0px';
		this.bg.layer.style.bottom = '-100px'; // play safe, cover more than neccessary
	}

	// disallow any scroll attempt
	if (std.browser.isFF) {
		document.body.style.overflow = 'scroll';
		document.body.style.position = 'fixed';
		document.body.style.width = '100%';
	} else {
		this.scroll_checker = setInterval(function() {
			if (document.documentElement.scrollTop != 0) document.documentElement.scrollTop = 0;
			if (document.body.scrollTop != 0) document.body.scrollTop = 0;
		}, 10);
	}

	// set unhide the black-screen and the iframe, then snap to correct position
	std.css.bulkset('display', ['block', this.bg.layer, this.player.layer]);
	this._resizeHandler();
	this.is_closed = false;

	// play fader to show up the black-screen and the iframe
	this.bg.fader.reset(0);
	this.player.fader.reset(0);
	this.bg.fader.start(0,this.bg.max_opacity,300,15);
	this.player.fader.start(0,this.player.max_opacity,600,15);

	// trigger an onQTStart event
	if (this.onQTStart && typeof this.onQTStart=='function') {
		this.onQTStart();
	}
}

// close and hide the notice
ui.notice.Notice.prototype.close = function() {
	// hide the iframe, without fading
	std.css.bulkset('display', ['none', this.player.layer]);
	this.is_closed = true;

	// play fader to hide the black-screen slowly
	this.bg.fader.start(this.bg.max_opacity,0,300,15);
	// set display-none of the black-out screen layer after finished playing the fader
	setTimeout("std.css.bulkset('display', ['none', getNotice().bg.layer])", 300);

	// unset resize handler and allow scrolling again
	if (std.browser.isIE) {
		window.onresize = null;
	}
	if (std.browser.isFF) {
		document.body.style.overflow = '';
		document.body.style.position = '';
	}
	if (this.scroll_checker) {
		clearInterval(this.scroll_checker);
	}

	// trigger an onQTClose event
	if (this.onQTClose && typeof this.onQTClose=='function') {
		this.onQTClose();
	}
}

ui.notice.Notice.prototype.isClosed = function() {
	return this.is_closed;
}


