//预置用户设定参数
var clock_max=3;
var clock_id=0;
var oldTabId = "1";
var preMusic = { '1':'梁山伯与祝英台' , '2':'风中有朵雨做的云' , '3':'四季之春天'  , '4':'江南春早','5':'秋天不回来' ,'6':'该死的温柔' ,'7':'菊花台' ,'8':'童话' ,'9':'夜曲' ,'10':'土耳其进行曲','11':'致爱丽丝' ,'12':'爱的罗曼史' ,'13':'莫斯科郊外的晚上' ,'14':'泰坦尼克号' ,'15':'雨中旋律' }
var preRepeat = {'week':'周' , 'month':'月' , 'quarter':'季度'  , 'halfYear':'半年'  , 'year':'一年' }
var vdomain = "";
var setting_clock = false;
var newD = new Date();
var clock_day = {'1':'','2':'','3':'','4':'','5':''};
var server_hour = newD.getHours();
var server_m = newD.getMinutes();


d_preHourValue = { '1':server_hour , '2':server_hour , '3':server_hour } ;
d_preMinutesValue = { '1':server_m , '2':server_m , '3':server_m } ;
d_preIsRepeatValue = {'1':'0' , '2':'0' , '3':'0' };
d_preRepeatValue = { '1':'week' , '2':'week' , '3':'week' } ;
d_preKgValue = { '1':'0' , '2':'0' , '3':'0' } ;
d_preTitleValue = { '1':'不早了，该休息了！' , '2':'得陪女友逛街了！' , '3':'球赛直播开始了！' } ;
d_preMidValue = { '1':'1' , '2':'2' , '3':'4' } ;
var preHourValue = [] , preMinutesValue = [] , preIsRepeatValue = [] , preRepeatValue = [] , preKgValue = [] , preTitleValue = [] , preMidValue = [];
function $(obj){return document.getElementById(obj)}
function tabChange(tabId)
{
	if(oldTabId==tabId)return;
	SetCookie( 'clock_hotTab' , tabId );
	var tb = $('tab'+tabId);
	var b = $('tabBody'+tabId);
	if( oldTabId !='' )
	{
		var otb = $('tab'+oldTabId);
		var ob = $('tabBody'+oldTabId);
		otb.innerHTML = "<a href='###'><img onclick=\"tabChange('"+oldTabId+"');\" src='i/clock_"+oldTabId+".gif'></a>";
		ob.style.display = "none";
		oldTabId = tabId;
	}
	tb.innerHTML = "<a href='###'><img onclick=\"tabChange('"+tabId+"');\" src='i/clock_cur_"+tabId+".gif'></a>";
	b.style.display="block";
}
 
function musicSelectList(formId)
{
	var str = '<select name="music" onchange="loadMusic(this.value,'+formId+')">';
	for( var v in preMusic )
	{
		str += '<option value="'+v+'"';
		if( preMidValue[formId]==v)
			str +=' selected';
		str +='>'+preMusic[v]+'</option>';
	}
	str +='</select>';
	//alert(str);
	document.write( str );
}

function numSelectList( minN , maxN , selectName , preValue )
{
	var str = '<select name="'+selectName+'">';
	for( var i=minN;i<=maxN;i++ )
	{
		str += '<option value="'+i+'"';
		if( preValue==i)
			str +=' selected';
		str +='>'+i+'</option>';
	}
	str +='</select>';
	document.write( str );
}
function dealtime(time1,time2){
    var ss1=time1.split("-");
    var ss2=time2.split("-");
    var btime=new Date(ss1[0],ss1[1],ss1[2]);
    var etime=new Date(ss2[0],ss2[1],ss2[2]);
    time=(etime.getTime()-btime.getTime())/(1000*24*60*60);
    return time ;
    }
function daySelectList(formId)
{
    var btime=new Date();
	var etime= new Date((new Date().getTime())+60*24*60*60*1000);
    time=(etime.getTime()-btime.getTime())/(1000*24*60*60);
	var str = '<select name="day">';
	for( var i=0;i<=60;i++ )
	{
		nt= new Date((new Date().getTime())+(i*86400*1000));
		ss = nt.getYear()+'-'+(nt.getMonth()+1)+'-'+nt.getDate();
		str += '<option value="'+ss+'"';
		if(0==i)	str +=' selected';
		str +='>'+ss+'</option>';
	}
	str +='</select>';
	document.write( str );
}

function hourSelectList(formId)
{
    numSelectList(0,23,'hour',preHourValue[formId]);
}

function minutesSelectList(formId)
{
    numSelectList(0,59,'minutes',preMinutesValue[formId]);
}

function repeatSelectList(formId)
{
	if( preRepeatValue[formId]!='week' )
	{
		var fm = eval ("document.clockForm"+formId);
		fm.isRepeat[1].checked = true;
	}
	var str = '<select name="repeat">';
	for( var v in preRepeat )
	{
		str += '<option value="'+v+'"';
		if( preRepeatValue[formId]==v)
			str +=' selected';
		str +='>'+preRepeat[v]+'</option>';
	}
	str +='</select>';
	document.write( str );
}


function SetCookie( name , v ) 
{ 
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length; 
	var expires = (argc > 2) ? argv[2] : null; 
	var path = (argc > 3) ? argv[3] : null; 
	var domain = (argc > 4) ? argv[4] : null; 
	var secure = (argc > 5) ? argv[5] : false; 

	var v = escape( v );
	var vcook =name+"="+v;
	if(typeof(expires)=='object')vcook +=  (expires == null) ? "" : "; expires=" + expires.toGMTString();
	else if(expires>0)
	{
		var tmpD = new Date(expires*1000);
		vcook += "; expires=" + tmpD.toGMTString();
		//alert( tmpD.toGMTString() );
	}
	if(path!='')vcook +=  (path == null) ? "" : ("; path=" + path) ;
	if(domain!='')vcook +=  (domain == null) ? "" : ("; domain=" + domain) ; 
	vcook +=  (secure == true) ? "; secure" : "";
	document.cookie = vcook;
}
 

function GetCookie(sName)
{
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])
		return unescape(aCrumb[1]);
	}
	return null;
}

function GetCookieObj()
{
	var o = [];
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		o[aCrumb[0]] = aCrumb[1];
	}
	return o;
}

function DeleteCookie (name) 
{ 
	var exp = new Date(); 
	exp.setTime (exp.getTime() - 100000 ); 
	SetCookie( name , 0 , exp , "/" , vdomain );
}

//获取用户设定参数
function setPreValue()
{
	for( var i=1;i<=3;i++ )
	{
		var fm = eval ("document.clockForm"+i);
		fm.title.value = preTitleValue[i];
		fm.kg.value = preKgValue[i];

		if(preRepeatValue[i]=='0' || preIsRepeatValue[i]=='0')
			fm.isRepeat[0].checked = true;
		
		if(fm.kg.value=='1')
		{
			lockForm( i , true , true );
		}
		else
		{
			lockForm( i , false , true );			
		}
	}
}

function lockForm( formId , lock , isInit )
{
	var fm = eval ("document.clockForm1");
	var vexp = new Date();
	ot = vexp.getTime()+365*86400000; //毫秒
	vexp.setTime( ot );
	if(lock==true)
	{
		SetCookie( 'clock_kg_'+formId , 1 , vexp , "/" , vdomain );
		SetCookie( 'clock_run_'+formId , '-1' , vexp , "/" , vdomain );
		if(!isInit)
		{
			fm.kg.value = '1';
		}
	}
	else 
	{
		if( !isInit )
		{
			fm.kg.value = '0';
		}
		SetCookie( 'clock_kg_'+formId , 0 , vexp , "/" , vdomain );
	}
    //$('clockParams'+formId).disabled = true;
}

function getUserSetting()
{
	var co = GetCookieObj();
 	for( var i=1;i<=3;i++ )
	{
		//alert((co['clock_kg_'+i]));
		var clock_title=co['clock_t_'+i];
		if(clock_title!='undefined')
		{
			clock_title =  decodeURIComponent( unescape(clock_title) );
		}
		preHourValue[i] = ( typeof(co['clock_h_'+i])=="undefined" )?d_preHourValue[i]:co['clock_h_'+i];
		preMinutesValue[i] = ( typeof(co['clock_m_'+i])=="undefined" )?d_preMinutesValue[i]:co['clock_m_'+i];
		preIsRepeatValue[i] = ( typeof(co['clock_isRepeat_'+i])=="undefined" )?d_preIsRepeatValue[i]:co['clock_isRepeat_'+i];
		preRepeatValue[i] = ( typeof(co['clock_r_'+i])=="undefined" )?d_preRepeatValue[i]:co['clock_r_'+i];
		preKgValue[i] = ( typeof(co['clock_kg_'+i])=="undefined" )?d_preKgValue[i]:co['clock_kg_'+i];
		preTitleValue[i] = ( typeof( co['clock_t_'+i] )=="undefined" )?d_preTitleValue[i]:clock_title;
		preMidValue[i] = ( typeof( co['clock_mid_'+i] )=="undefined" )?d_preMidValue[i]:co['clock_mid_'+i];
		//alert( preKgValue[i] );
		//break;
	}
}
function putClock()
{
p='',k=1;

for(var i=1;i<=clock_max;i++){
	if( clock_day[i] !='' && clock_day[i] !='undefined' ){
	if( subdate(clock_day[i])<=0 ){
		clock_day[i]='';
		SetCookie( 'clock_kg_'+i , 0 , 0 , "/" , vdomain );
		continue
	}
	var t = co["clock_t_"+i];
	t = decodeURIComponent( unescape(t) );
	p+='<div class="xms_kk" id="clock'+i+'"><table width="100%" border="0" cellspacing="0" cellpadding="0">'
	p+='<tr><td class="xms_kk1"><table width="100%" border="0" cellspacing="0" cellpadding="0">'
	p+='<tr><td width="112">&nbsp;</td><td align="left">设置'+k+'(<span id="set'+i+'">'+clock_day[i]+'</span>)</td><td width="15"><a href="javascript:cls('+i+');"><img src="clock/images/close2.gif" border="0" /></a></td></tr>'
	p+='</table></td></tr><tr><td class="xms_kk2">距离提醒还有： <span id="exp'+i+'"><span class="xms_orange">0</span>天<span class="xms_orange">00</span>小时<span class="xms_orange">00</span>分钟<span class="xms_orange">00</span>秒</span></td></tr>'
	p+='<tr><td class="xms_kk2" id=ax>您的提示文字： <span id="alts">'+t+'</span></td></tr></table></div>'
	k++
	}
}
$('sclock').innerHTML=p;
runTime()
setHei()
}
function cls(formId)
{
	//lockForm(formId,false,false);
	clock_day[formId]='';
	SetCookie( 'clock_kg_'+formId , 0 , 0 , "/" , vdomain );
	putClock();
	if( iTimerId[formId]!=null )clearTimeout( iTimerId[formId] );
}
function runTime()
{
	for(i=1;i<=clock_max;i++)
	{
		if(clock_day[i]!='')
		{
			subtime(subdate(clock_day[i]),i);
		}
	}
}
function subdate(date)
{
	var now=new Date();
	s = (date.replace(/[ :]/g,"-")).split('-')
	t = new Date(s[0],(s[1]-1),s[2],s[3],s[4])
	return (t.getTime()-now.getTime())
}
function subtime(etime,id){
etime = etime/1000
d = Math.floor(etime/(60*60*24)); //日
h = Math.floor(etime/3600)%24; //时
m = Math.floor(etime/60)%60; //分
s = Math.floor(etime%60); //秒
d < 0 ? d = 0 :'';
h < 0 ? h = 0 : h = h;
m < 0 ? m = 0 : m = m;
s < 0 ? s = 0 : s = s;
h.toString().length < 2 ? hstr = "0" + h.toString() : hstr = h;
m.toString().length < 2 ? mstr = "0" + m.toString() : mstr = m;
s.toString().length < 2 ? sstr = "0" + s.toString() : sstr = s;
timestr = '<span class="xms_orange">'+d+'</span>天<span class="xms_orange">'+hstr+'</span>小时<span class="xms_orange">'+mstr+'</span>分钟<span class="xms_orange">'+sstr+'</span>秒</span>'
if( $('exp'+id)) $('exp'+id).innerHTML=timestr
}
setInterval(runTime,1000)
//设定闹钟
function setClock()
{
	if(arguments.length==1)
	{
		formId = arguments[0];
	}
	var fm = eval ("document.clockForm1");
	for(i=1;i<=clock_max;i++)
	{
		if( clock_day[i] == '' )
		{
			clock_day[i] = fm.day.value+' '+fm.hour.value+':'+fm.minutes.value;
			if( subdate(clock_day[i]) <= 0 )
			{
				alert('您设置的时间，必须大于当前时间...');
				return
			}
			formId=i;
			break;
		}
	}
	if( i > clock_max ){
		alert('当前最多可设置'+clock_max+'小秘书提示！\n\n请删除一个，再设置');
		return;
	}
	putClock();
	stopMusic();
	//fm.kgTitle.disabled = true;
	var kg_value = 1;
	var isRepeat = 0;

	//获取cookie过期时间
	var vexp = new Date();
	var dexp = new Date();
	vdate = vexp.getFullYear() + "-"+(vexp.getMonth()+1)+"-"+vexp.getDate() ;
	vtime = vexp.getHours() + ":"+vexp.getMinutes()+":"+vexp.getSeconds() ;
	vTimeStamp = vexp.getTime()/1000;
	ot = vexp.getTime()+365*86400000;
	vexp.setTime( ot );
	dexp.setTime( dexp.getTime()-10000 );
	//系统默认值
	SetCookie( 'clock_userTimeStamp_'+formId , vTimeStamp , vexp , "/" , vdomain );
	SetCookie( 'clock_usertime_'+formId , vtime , vexp , "/" , vdomain );
	SetCookie( 'clock_userdate_'+formId , vdate , vexp , "/" , vdomain );
	SetCookie( 'clock_done_'+formId , '' , vexp , "/" , vdomain );
	SetCookie( 'clock_running' , '0' , dexp , "/" , vdomain );
	//用户值
	SetCookie( 'clock_kg_'+formId , kg_value , vexp , "/" , vdomain );
	SetCookie( 'clock_h_'+formId , fm.hour.value , vexp , "/" , vdomain );
	SetCookie( 'clock_day_'+formId , clock_day[formId] , vexp , "/" , vdomain );
	SetCookie( 'clock_m_'+formId , fm.minutes.value , vexp , "/" , vdomain );
	SetCookie( 'clock_mid_'+formId , fm.music.value , vexp , "/" , vdomain );
	SetCookie( 'clock_t_'+formId , encodeURIComponent( fm.title.value ) , vexp , "/" , vdomain );
	SetCookie( 'clock_isRepeat_'+formId , isRepeat , vexp , "/" , vdomain );
	//SetCookie( 'clock_r_'+formId , fm.repeat.value , vexp , "/" , vdomain );
	SetCookie( 'clock_run_'+formId , '0' , vexp , "/" , vdomain );
	clockStart();
	//alert(document.cookie);
	window.status ='设置闹钟'+formId+"成功";
	//lockForm(formId,true,false);
	//fm.kgTitle.disabled = false;
}

function stopMusic()
{
	$('clock_mid').src = "";
}

function playMusic( formId )
{
	var fm = eval ( "document.clockForm"+formId );
	var v = fm.testMusicButton.value;
	var mid = ((v=="试听")?fm.music.value:"unknown");

	$('clock_mid').src = "clock/music/"+ mid +".mid";
	fm.testMusicButton.value=((v=="试听")?"停止":"试听");
}


//预载音乐
function loadMusic(mid,formId)
{
	//alert(formId);
	var fm = eval ( "document.clockForm"+formId );
	fm.testMusicButton.value = '试听';
	//alert( fm.testMusicButton.value );
	$('clock_mid').src = "clock/music/"+ mid+".mid";
	$('clock_mid').src = "";
}

getUserSetting();