// JavaScript Document

var pageWidth,pageHeight,windowWidth,windowHeight;
var xScroll, yScroll;
var windowWidth, windowHeight;
var Moz = navigator.userAgent.indexOf("Gecko") != -1;

function getObj(id) {
	if (document.all) {
		return document.all(id);
	} else if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.layers) {
		return document.layers[id];
	}
	return false;
}

function getPageSize(){
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	//return pageHeight;
}

function doOver(obj){
	var st = obj.style.backgroundImage;
	if( st=='')
		obj.style.backgroundImage="url(/common/images/menu_over.gif)"
	else
		obj.style.backgroundImage="";
		
	obj.style.cursor = 'pointer';
}

function getPageScroll(){

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
}

function scroll2Top() {
	
	getPageScroll();
	
	if (yScroll<=0) return;
	yScroll-=120;
	window.scrollTo(0, yScroll);
	
	var t;
	t=setTimeout('scroll2Top()',10);
}

function go2url(addr) {
	parent.window.location = addr;
}

var a = new Array();
	a[0]='url(/stories/images/common/child_banner.jpg)';
	a[1]='url(/stories/images/common/child_banner2.jpg)';
	a[2]='url(/stories/images/common/child_banner3.jpg)';
	a[3]='url(/stories/images/common/child_banner4.jpg)';
	a[4]='url(/stories/images/common/child_banner5.jpg)';
	a[5]='url(/stories/images/common/child_banner6.jpg)';
	
	// preload images (for faster loading)
	var bg = new Array();
	bg[0] = new Image(); bg[0].src = a[0];
	bg[1] = new Image(); bg[1].src = a[1];
	bg[2] = new Image(); bg[2].src = a[2];
	bg[3] = new Image(); bg[2].src = a[3];
	bg[4] = new Image(); bg[2].src = a[4];
	bg[5] = new Image(); bg[2].src = a[5];


function randomBg(id)
{
	
    var r = Math.floor(Math.random()*a.length);

	document.getElementById(id).style.backgroundImage = a[r];
	document.getElementById(id).style.backgroundRepeat = 'no-repeat';
}

function getQueryString(variable) { 
var query = window.location.search.substring(1); 
var vars = query.split("&"); 

for (var i=0;i<vars.length;i++) { 
	var pair = vars[i].split("="); 
	if (pair[0] == variable) return pair[1];  
} 

return 'nothing';

} 

function detectLang() {
	var st = document.URL;
	if (st.indexOf('_en.htm')!=-1) return 'en'
	else return 'vn';
}

function check_under(fname) {
	var under_files = new Array('/cartoons/index_en.htm',
								'/stories/1','/stories/2','/stories/3','/stories/4','/stories/5','/stories/6','/stories/7','/stories/8',
								'/others/',
								'/quiz/',
								'/saints/');

	for (var i=0;i<under_files.length;i++)
		if (fname.indexOf(under_files[i])>=0) return true;

	return false;
}

function switchLang(lang) {
	
	var st = document.URL;
	var tail = st.substr(st.length-1,1);
	if (tail=='/') st+='index.htm'
	
	//alert('Require: '+lang+' - Current:'+detectLang());
	if (lang=='en'&&detectLang()=='vn')	{
		st = st.replace('.htm','_en.htm');
		if (check_under(st)) go2url('/common/warnings/index.htm')
		else
			location.href=st;
	}
	
	if (lang=='vn'&&detectLang()=='en') {	
		st = st.replace('_en.htm','.htm');
		location.href=st;
	}
	
}

function setLang(lang) {
	switch (lang) {
		case 'en': 
			
			switchLang(lang);
			break;
		case 'vn':
			switchLang(lang);
			break;
	}
}



function myWindow(URL, params)
{
	var IE  = navigator.appName.indexOf("Microsoft Internet Explorer",0) != -1;
	
	if (IE)
		var newWin = window.showModelessDialog(URL,'',params)
	else
		var newWin = window.open(URL,'popup1',params);
}

/* jsHandler.js */
function inc(filename)
{
document.write('<script type="text/javascript" src="'+filename+'"></script>');
}


