/* Event Function */
var Event = new Object();

Object.prototype.addEvent=function(element,e,fn){
	if(element.addEventListener){
		element.addEventListener(e,fn,false);
	}
	else{
		element.attachEvent("on"+e,fn);
	}
}

Object.prototype.delEvent=function(element,e,fn){
	if(element.removeEventListener){
		element.removeEventListener(e,fn,false);
	}
	else if(element.detachEvent)
	{
		element.detachEvent('on'+e,fn);
	}
}

var $A = Array.from=function(iterable){
	if (!iterable)return [];
	if (iterable.toArray){
		return iterable.toArray();
	}
	else
	{
		var results=[];
		for(var i=0,length=iterable.length;i<length;i++)results.push(iterable[i]);
		return results;
	}
}

var IE = (document.getElementById && document.all) ? true : false;

Function.prototype.bind = function()
{
	var __method = this, args = $A(arguments), object = args.shift();
	return function(){return __method.apply(object, args.concat($A(arguments)));}
}

/* SystemInit */
function SystemInit()
{
	if(typeof(daily)!='undefined' && typeof(uid)!='undefined')
	{
		var done = getCookie('daily_'+uid);
		if(!done)
		{
			daily_draw();
		}
		else
		{
			ajax({url:"service/notice.php?act=getEvi",id:"evi",oncomplete:commonController});
		}
	}

	/* PageInit */
	if(typeof(PageInit)=='function')PageInit();
	if(typeof(StartInit)=='function')StartInit();
	if(typeof(pngHandler)!='undefined' && IE)pngHandler.init();
	if(IE)
	{
		linkFix();
	}

	if( $('schoolnotice_box') && typeof(notice_id)=='undefined' )
	{
		getSchoolNotice();
	}
	/*
	if(document.location.href.indexOf('hsfax.php')!=-1)
	{
		if (window.Event)
		{
			document.oncontextmenu = function(){return false;}
			document.captureEvents(Event.MOUSEUP);
		}
		Event.addEvent( document,'contextmenu',nocontextmenu);
		Event.addEvent( document,'mousedown',norightclick);
		Event.addEvent( document.body,'selectstart',function(){return false;});
	}
	*/
}
Event.addEvent( window,'load',SystemInit);

/* GetObject */
function $(str){return document.getElementById(str);}

/* Select Option */
function addItem(obj,text,value)
{
	obj.options.add( new Option(text, value) );
}

function linkFix()
{
	var objs = document.getElementsByTagName('A');
	for(var i in objs)
	{
		objs[i].onfous = function(){this.blur;}
	}
}

function checkImg(type)
{
	var tid = type ? type : 1;
	var path = typeof(resource_path) != 'undefined' ? resource_path : '.';
	var url = path + '/images/common/';
	switch(parseInt(tid))
	{
		case 1:
		{
			url += 'nopic.gif';
			break;
		}
		case 2:
		{
			url += 'album_noface.gif';
			break;
		}
		default:
		{
			url += 'nopic.gif';
			break;
		}
	}

	var imgs = document.images;
	for(var i = 0;i < imgs.length;i++){
		imgs[i].onerror = function(){
			this.src = url;
		}
	}
}

function openWin(type,id)
{
	var sh,sw;
	sh=window.screen.availHeight;
	sw=window.screen.availWidth-8;

	var url = '';
	var url_add = '';
	switch(type)
	{
		case 'course':
		{
			if( checkRight('B') ){
				url='reg.php';	
			}
		    else{
				url='course.php?id='+id;
			}
			break;
		}
		case 'knowledge':
		{
			if( checkRight('A') ){
				url='reg.php';
			}
			else{
				url='knowledge.php?id='+id;
			}
			break;
		}
		case 'game':
		{
			if( checkRight('D') ){
				url='reg.php';
			}
			else{
				url='game.php?id='+id;
			}
			break;
		}
		case 'evi':
		{
			url='evi.php?id='+id;
			sh=300;
			sw=420;
			break;
		}
		case 'expand':
		{
			if( checkRight('B') ){
				url='reg.php';
			}
			else{
				url='expand.php?id='+id;
			}
			break;
		}
		case 'article':
		{
			if( checkRight('C') ){
				url='reg.php';
			}
			else{
				url='article.php?id='+id;
			}
			break;
		}
		case 'sharezone':
		{
			url_add = '&type=share';
		}
		case 'photo':
		case 'video':
		{
			url='view.php?stuffId='+id+url_add;
			break;
		}
		case 'fathersday':
		{
			url_add = '&type=share';
			url='fathersdayview.php?stuffId='+id+url_add;
			break;
		}
		case 'upload':
		{
			url='SiriusFileUpload.php';
			break;
		}
		case 'paint_board':
		{
			url='./kidszone/funclub/paint_board/index.php';
			break;
		}
		case 'shareview':
		{
			url='shareview.php?pid='+id;
			break;
		}
		case 'pet':
		{
			url='pet.php?id='+id;
			break;
		}
		case 'activity':
		{
			url='activity.php?id='+id;
			break;
		}
		case 'kl':
		{
			url='kl.php?id='+id;
			break;
		}
		case 'fullscreen':
		{
			url=id;
			break;
		}
	}
    
	if(url.length>0)
	{
		window.open (url, "", "height="+sh+", width="+sw+", left=0, top=0, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
	}
}

function checkRight(r)
{
	var b = P.indexOf(r) != -1;
	if(b)Dialog.show('right_box');
	return b;
}

function fullScreen()
{
	var r=0.98;

	var p = getPageSize();
	var sh = p[1];
	var sw = p[0];

	var obj1 = $('player1');
	if(obj1)
	{
		obj1.width = sw*r+'px';
		obj1.height = sh*r+'px';
	}

	var obj2 = $('player2');
	if(obj2)
	{
		obj2.width = sw*r+'px';
		obj2.height = sh*r+'px';
	}

	var cobj = document.getElementById('flashcontent');
	cobj.style.width=sw*r+'px';
	cobj.style.height=sh*r+'px';
	cobj.style.marginLeft="-"+(sw*r/2)+'px';
	cobj.style.marginTop="-"+(sh*r/2)+'px';
}

function SetString(str,len)
{
	var strlen = 0;
	var s = "";
	for(var i = 0;i < str.length;i++)
	{
		if(str.charCodeAt(i) > 128)
			strlen += 2;
		else
			strlen++;

		s += str.charAt(i);
		if(strlen >= len)
			return s + "...";
	}
	return s;
}

function setCookie(name,value,days)
{
	var Days = typeof(days)!='undefined' ? days : 365;
	var exp  = new Date();
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)) return unescape(arr[2]);
	else return null;
}

function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

var Dialog = (function()
{
	var inner;
	var _id;
	var _fulldiv;
	var _show = false;
	return inner = {
		show : function(id,bg)
		{
			bg = typeof(bg)=='undefined' || bg ? true : false;
			if(_show)return;
			_id = $(id);
			if(_id)
			{
				var p = getPageSize();
				var st = getPageScroll()[1];
				var width = p[0];
				var height = p[3];

				_id.style.display = '';
				_show = true;

				var leftPos = width = (width-_id.offsetWidth)/2;
				var topPos = (height-_id.offsetHeight)/2 + st;

				if(leftPos<=0)leftPos=0;
				if(topPos<=0)topPos=0;

				_id.style.left = leftPos+'px';
				_id.style.top = topPos+'px';
				_id.style.zIndex = popup_index++;

				if(bg)
				{
					_fulldiv = $('fulldiv');
					if(!_fulldiv)
					{
						_fulldiv = document.createElement('DIV');
						_fulldiv.id = 'fulldiv';
						_fulldiv.style.position = 'absolute';
						_fulldiv.style.backgroundColor = '#000000';
						_fulldiv.style.left = '0px';
						_fulldiv.style.top = '0px';
						_fulldiv.style.zIndex = '10';
						document.body.appendChild(_fulldiv);
					}
					setOpacity(_fulldiv,60);
					_fulldiv.style.display="";
					_fulldiv.style.width='100%';
					_fulldiv.style.height=p[1]+'px';
				}
				hideSelect( getWindow() );
			}
		},
		hide : function()
		{
			if(_id)
			{
				_id.style.display = 'none';
				_show = false;
				_id = null;
			}
			if(_fulldiv)
			{
				_fulldiv.style.display = 'none';
				_fulldiv = null;
			}

			showSelect( getWindow() );
		}
	}
})();

function getPageSize()
{
	var xScroll, yScroll;
	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;
	}

	var winWidth, winHeight;
	if (self.innerHeight) // all except Explorer
	{
		winWidth = self.innerWidth;
		winHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	{
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	height = yScroll < winHeight ? winHeight : yScroll;
	width = xScroll < winWidth ? winWidth : xScroll;

	arrayPageSize = new Array(width,height,winWidth,winHeight)
	return arrayPageSize;
}

function getPageScroll()
{
	var yScroll;
	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;
	}
	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function setOpacity(obj, opacity) {
	if(obj){
		opacity = (opacity == 100)?99.999:opacity;
		obj.style.filter = "alpha(opacity:"+opacity+")";// IE/Win
		obj.style.KHTMLOpacity = opacity/100;// Safari<1.2, Konqueror
		obj.style.MozOpacity = opacity/100;// Older Mozilla and Firefox
		obj.style.opacity = opacity/100;// Safari 1.2, newer Firefox and Mozilla, CSS3
	}
}


function hideSelect(doc)
{
	var	slts = doc.getElementsByTagName('SELECT')
	for(var	i=0;i<slts.length;i++)
	{
		if(slts[i].getAttribute('status')!='show')slts[i].style.visibility = 'hidden';
	}
	var	objs = doc.getElementsByTagName('OBJECT')
	for(var	i=0;i<objs.length;i++)
	{
		if(objs[i].getAttribute('status')!='show')objs[i].style.visibility = 'hidden';
	}
	var	frms = doc.getElementsByTagName('IFRAME');
	for(var	i=0;i<frms.length;i++)
	{
		try{ hideSelect(frms[i].contentWindow.document); }
		catch(e){}
	}
}

function showSelect(doc)
{
	var	slts = doc.getElementsByTagName('SELECT')
	for(var	i=0;i<slts.length;i++)
	{
		slts[i].style.visibility = '';
	}
	var	objs = doc.getElementsByTagName('OBJECT')
	for(var	i=0;i<objs.length;i++)
	{
		objs[i].style.visibility = '';
	}
	var	frms = doc.getElementsByTagName('IFRAME');
	for(var	i=0;i<frms.length;i++)
	{
		try{ showSelect(frms[i].contentWindow.document); }
		catch(e){}
	}
}


function getWindow()
{
	var	topWindow =	window.parent ;
	while (	topWindow.parent &&	topWindow.parent !=	topWindow )
	{
		try
		{
			if ( topWindow.parent.document.domain != document.domain )
				break ;
			if ( topWindow.parent.document.getElementsByTagName( 'frameset'	).length > 0 )
				break ;
		}
		catch (	e )
		{
			break ;
		}
		topWindow =	topWindow.parent ;
	}
	return topWindow.document ;
}

function getXY(Obj)
{
	for(var sumTop=0, sumLeft=0; Obj!=document.body; sumTop+=Obj.offsetTop, sumLeft+=Obj.offsetLeft,Obj=Obj.offsetParent);
	return {left:sumLeft,top:sumTop};
}

var popup_index = 11;
function popup(type, html, w, h, clickToHide)
{
	if(type=='img')
	{
		html = '<img src="'+html+'" />';
	}else if(type=='html'){
		html = html;
	}

	w = typeof(w)=='undefined' ? 'auto' : w + 'px';
	h = typeof(h)=='undefined' ? 'auto' : h + 'px';

	popup_index += 2;
	var obj = document.createElement('DIV');
	obj.id = 'popup_' + popup_index;
	if(typeof(clickToHide)=='undefined'||clickToHide==true)obj.onclick = function(){this.parentNode.removeChild(this);Dialog.hide();}
	with(obj.style)
	{
		position = 'absolute';
		width = w;
		height = h;
		display = 'none';
		zIndex = popup_index;
		backgroundColor='#fff';
	}
	obj.innerHTML = html;
	document.body.appendChild(obj);
	Dialog.show(obj.id);
}

function getEviNotice(id)
{
	if(id)
	{
		ajax({url:"service/notice.php?act=getEviNotice&id="+id,id:"evinotice",oncomplete:commonController});
	}
}

function commonController(rs)
{
	switch(rs.id)
	{
		case 'evi':
		{
			try{ var json = eval(rs.data); }catch(e){window.status = rs.data;}
			var popid = 0;
			for(var i=0;i<json.data.length;i++)
			{
				var item = json.data[i];
				if(popid==0 && item.popup==1 && getCookie('i_am_read_nid_'+item.id)!='ok')popid=item.id;
			}
			if(popid>0 && document.location.href.indexOf('home.php') != -1)
			{
				getEviNotice(popid);
			}

			if($('home_icon_jet') && json.jet>0)
			{
				$('home_icon_jet').src = themepath + '/images/home_icon_7_f.gif';
			}
			break;
		}
		case 'evinotice':
		{
			try{ var json = eval(rs.data); }catch(e){window.status = rs.data;}
			$('evinotice_subject').innerHTML = json.subject;
			$('evinotice_date').innerHTML = json.date;
			$('evinotice_content').innerHTML = json.content;
			if(json.popup==1)
			{
				$('evinotice_read_tips').style.display = '';
				$('evinotice_read').value = json.id;
				$('notice_read_tips').innerHTML = json.tips;
				$('read_chk').checked = getCookie('i_am_read_nid_'+json.id)=='ok';
			}
			else
			{
				$('notice_read_tips').innerHTML = '';
				$('evinotice_read_tips').style.display = 'none';
			}
			Dialog.show('notice_box');
			break;
		}
		case 'password':
		{
			try{ var json = eval(rs.data); }catch(e){window.status = rs.data;}
			alert(json.msg);
			switch(json.status)
			{
				case 1:
				{
					$('oldpass').value ='';
					$('oldpass').focus();
					break;
				}
				case 2:
				{
					$('newpass1').value ='';
					$('newpass1').focus();
					break;
				}
				case 3:
				{
					$('newpass2').value ='';
					$('newpass2').focus();
					break;
				}
				case 4:
				{
					$('newpass1').value ='';
					$('newpass2').value ='';
					$('newpass1').focus();
					break;
				}
				case 'ok':
				{
					Dialog.hide();
					break;
				}
			}
		}
	}
}

function notice_read(obj)
{
	var id = $('evinotice_read').value;
	if(id>0)
	{
		if(obj.checked)
		{
			setCookie('i_am_read_nid_'+id,'ok',30);
		}
		else
		{
			delCookie('i_am_read_nid_'+id);
		}
	}
}

function changePassword()
{
	clearPasswd();
	Dialog.show('passwd_box');
}

function savePasswd()
{
	var oldpass = $('oldpass').value;
	var newpass1 = $('newpass1').value;
	var newpass2 = $('newpass2').value;
	var data = 'oldpass='+oldpass+'&newpass1='+newpass1+'&newpass2='+newpass2;
	var url = 'service/profile.php?act=changePasswd';
	ajax({url:url,id:'password',method:'post',content:data,oncomplete:commonController});
}

function clearPasswd()
{
	$('oldpass').value ='';
	$('newpass1').value ='';
	$('newpass2').value ='';
}

function getSchoolNotice()
{
	var url = 'service/notice.php?act=getList';
	ajax({url: url, oncomplete:function(rs){
		try{ var json = eval(rs.data); }catch(e){window.status = rs.data;}
		var NeverRead = '';
		var num = 0;
		for(var i=0;i<json.data.length;i++)
		{
			var item = json.data[i];
			if(item.r.length == 0)
			{
				NeverRead += '<div style="margin:5px 10px;"><a href="hsfax.php?tab=1&id='+item.id+'">' + item.date+'&nbsp;&nbsp;'+SetString(item.title,28) + '</a></div>';
				num++;
			}
		}
		if(NeverRead.length>0)
		{
			$('home_icon_1').src = resource_path + '/images/notice/'+num+'.gif';

			$('schoolnotice_list').innerHTML = NeverRead;
			Dialog.show('schoolnotice_box');
		}
	}});
}

function writeFitImage(url,imgwidth,imgheight,roundCorner,str)
{
	document.write('<object type="application/x-shockwave-flash" data="swfoto2.swf?image='+url+'&roundCorner='+str+'&link='+str+'" width="'+imgwidth+'" height="'+imgheight+'" id="swfoto2+'+imgwidth+'">');
	document.write('<param name="movie" value="swfoto2.swf?image='+url+'&roundCorner='+roundCorner+'&link='+str+'" />');
	document.write('<embed src="swfoto2.swf?image='+url+'&roundCorner='+roundCorner+'&link='+str+'" width="'+imgwidth+'" height="'+imgheight+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>');
	document.write('</object>');
}

function nocontextmenu(e)
{
	if(IE)
	{
		event.cancelBubble = true;
		event.returnValue = false;
	}
	return false;
}

function norightclick(e)
{
	if (window.Event)
	{
		if (e.which == 2 || e.which == 3)
		{
			return false;
		}
	}
	else if (event.button == 2 || event.button == 3)
	{
		event.cancelBubble = true;
		event.returnValue = false;
		return false;
	}
}

function daily_draw()
{
	var url = 'service/20110222.php?act=init';
	var baseUrl = document.location.href;
	baseUrl = baseUrl.substring(0,baseUrl.lastIndexOf('/')+1);
	ajax({url: url, oncomplete:function(rs){
		try{ var json = eval(rs.data); }catch(e){window.status = rs.data;}
		if(json.status==1)
		{
			popup('html','<div style="width:650px;height:400px;overflow:hidden;background:#FFF;padding:20px;"><object id="flash3" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" height="400" width="650"><param name="movie" value="./html/daily/images/scratchCard2.swf" /><param name="flashVars" value="url='+baseUrl+'" /><param name="quality" value="high" /><param name="menu" value="false" /><param name="wmode" value="opaque" /><embed id="flash3" src="./html/daily/images/scratchCard2.swf" flashvars="url='+baseUrl+'" wmode="opaque" quality="high" type="application/x-shockwave-flash" height="400" width="650" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object></div>',650,400,false);
			$('flash3').style.visibility = '';
			$('flash4').style.visibility = '';
		}
		else
		{
			ajax({url:"service/notice.php?act=getEvi",id:"evi",oncomplete:commonController});
		}
		
		if(json.status==0)
		{
			var done = getCookie('daily_'+uid);
			if(!done)
			{
				var exp  = new Date();
				exp.setTime(exp.getTime() + json.time*1000);
				document.cookie = 'daily_'+uid+'='+ json.time + ';expires=' + exp.toGMTString();
			}
		}
	}});
}
