// $Id: common.js..gtml,v 1.16 2012/01/09 01:53:22 amir Exp $ 
var ival_id;
var iframeheight;
var messagestyle;
var height_init;

function init() {
	init_roller();
	init_motto(0);
	entry_page();
	init_popup();
}
function init_motto(offset) {
	// put the motto in the middle of the RHS empty space after the text
	var bottom_motto = document.getElementById('bottom-motto');
	if (!bottom_motto) { // no bottom-motto in this page
		return;
	}
	var bottom_motto_style = bottom_motto.style;
	var lhs_height = document.getElementById('lhs').offsetHeight;
	var pictext_height = document.getElementById('pic-text').offsetHeight;
	var bottom_motto_style_height = parseInt(bottom_motto_style.height);
	var middle = (lhs_height - pictext_height +  bottom_motto_style_height + 7) / 2;
	var menu_height = document.getElementById('menu').offsetHeight;
	var top;
	if (middle > 0) {
		top = -middle;
		if (lhs_height - middle <  menu_height + 20) {
			top = menu_height - lhs_height + 20;
		}
		top += 20;
		bottom_motto_style.top =  top + offset + 'px';
	}
}
function init_roller() {
	if (!ival_id) {
		var rollerobj = document.getElementById('roller');
		if (!rollerobj) { // no roller in this page
			return;
		}
		var iframedoc = rollerobj.contentWindow.document;
		if (!iframedoc) {
			iframedoc = rollerobj.contentDocument;
		}
		var messageobj = iframedoc.getElementById('message');
		messagestyle = messageobj.style;
		iframeheight = messageobj.offsetHeight -15;
		height_init = parseInt(rollerobj.style.height) - 10;
	}
	ival_id = setInterval(tscroll, 50);
}
function tscroll() {
	if (parseInt(messagestyle.marginTop) > -iframeheight) {
		messagestyle.marginTop = (parseInt(messagestyle.marginTop) - 1) + 'px';
	} else {
		messagestyle.marginTop = height_init + 'px';
	}
}
function entry_page() {
	var hostname = location.hostname.replace(/^(?:www|test)?/, '');
	var referrer = document.referrer || '';
	var title;
	var referrer_hostname = referrer.replace(/^(?:https?:\/\/)?(?:www)?|[\/?#].*/g, '');
	if (referrer_hostname != hostname || !readCookie('enpage')) {
		title = document.title || '';
		if (title) {
			var p = title.indexOf('|');
			if (p > 3) {
				title = title.substr(0, p - 1);
			}
		}
		var enpage_cookie = location.href + "\x01" + title + "\x01" + referrer;
		createCookie('enpage', enpage_cookie, 7);
	}
}
function createCookie(name, value, days, path) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setDate(date.getDate() + days);
		expires = '; expires=' + date.toUTCString();
	}
	document.cookie = name + '=' + encodeURI(value) + expires + '; path=' + (path ? path : '/');
}
function readCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		if (!c.indexOf(nameEQ)) {
			return decodeURI(c.substring(nameEQ.length, c.length));
		}
	}
	return null;
}
function mouseleave(e, p) {
	var elem = e.relatedTarget || e.toElement || e.fromElement;
	while ( elem && elem !== p) {
		elem = elem.parentNode;
	}
	if ( elem !== p) {
		return true;
	}
}
function init_popup() {
	var ids = ['copyrightPopup', 'privacyPopup'];
	for (var idn = 0; idn < ids.length; idn++) {
		document.getElementById(ids[idn]).onmouseout = function(e) {
			if (mouseleave(e || event, this)) {
				this.style.display = 'none';
			}
		};
	}
}
function showPopup(id) {
	document.getElementById(id).style.display = 'block';
}

