/**
* 公用函数库
* @ Author : iFiky , 780537@gmail.com
* @ version : 2008.9.12
*/
window.onerror=function(){ return true; };
var DomainUrl = "dj19.com";

$ = function(em) {
    if (document.getElementById){ return document.getElementById(em); }
    else if (document.all){ return document.all[em]; }
    else if (document.layers){ return document.layers[em]; }
    else{ return null; }
};

function show_select(_title, _options, _input){
    option_obj = $(_options);
    option_obj.style.display = (option_obj.style.display == '') ? 'none' : '';
    option_obj.onblur = function() { option_obj.style.display = 'none'; };
    option_obj.focus();
    for (var i = 0; i < option_obj.childNodes.length; i++){
        option_obj.childNodes[i].onmouseover = function(){ this.className = 'options_over pointer'; };
        option_obj.childNodes[i].onmouseout = function(){ this.className = 'options_out pointer'; };
        option_obj.childNodes[i].onclick = function(){
            $(_title).innerHTML = this.innerHTML;
            $(_input).value = this.id;
            option_obj.blur();
            option_obj.style.display = 'none';
        }
    }
 };

function get_tags(parentobj, tag){
    if (typeof parentobj.getElementsByTagName != 'undefined'){ return parentobj.getElementsByTagName(tag);
    }else if (parentobj.all && parentobj.all.tags){ return parentobj.all.tags(tag);
    }else{ return new Array(); }
};

function clk(_act, _id) {
    var v = [];
    var a = get_tags($(_id), 'input');

    for (var i = 0; i < a.length; i++) {
        if (_act == 'all' ) {a[i].checked = true;}
        else if (_act == 'un' ) { a[i].checked = (a[i].checked) ? false : true; }
        else { if (0 < a[i].value && a[i].checked){ v.push(a[i].value); } }
    }
    if ( 'play' == _act ) {
        if (0 == v.length){ do_alert('请先选择舞曲，再进行连播！'); setTimeout('close_alert()', 3000); return; }
        window.open('/p_' + v.join('_') + '.htm', 'play');
    }else if ( 'add' ==  _act){
        if (0 == v.length){ do_alert('请先选择舞曲，再加入列表！'); setTimeout('close_alert()', 3000); return; }
        if ( 1 == get_cookie('LPlay_open') ){
            SetCookie('LPlay_list', v.join('_'), null, "/", DomainUrl, false);
            do_alert('添加成功, 请查看播放列表！');
            setTimeout('close_alert()', 3000);
        }else{
            window.open('/p_' + v.join('_') + '.htm', 'play');
        }
    }
};

function mclk(_act, _id) {
    var v = [];
    var a = get_tags($(_id), 'input');

    for (var i = 0; i < a.length; i++) {
        if (_act == 'all' ) {a[i].checked = true;}
        else if (_act == 'un' ) { a[i].checked = (a[i].checked) ? false : true; }
        else { if (0 < a[i].value && a[i].checked){ v.push(a[i].value); } }
    }
    if ( 'play' == _act ) {
        if (0 == v.length){ do_alert('请先选择歌曲，再进行连播！'); setTimeout('close_alert()', 3000); return; }
        if (30 < v.length){ do_alert('最多可同时选择30首歌曲！'); setTimeout('close_alert()', 4000); return; }
        window.open('/m_' + v.join('_') + '.htm', 'play');
    }else if ( 'add' ==  _act){
        if (0 == v.length){ do_alert('请先选择歌曲，再加入列表！'); setTimeout('close_alert()', 3000); return; }
        if (30 < v.length){ do_alert('最多可同时选择30首歌曲！'); setTimeout('close_alert()', 4000); return; }
        if ( 1 == get_cookie('MPlay_open') ){
            SetCookie('MPlay_list', v.join('_'), null, "/", DomainUrl, false);
            do_alert('添加成功, 请查看播放列表！');
            setTimeout('close_alert()', 3000);
        }else{
            window.open('/m_' + v.join('_') + '.htm', 'play');
        }
    }
};

function add_one( _id ){
    if ( 1 == get_cookie('LPlay_open') ){
        SetCookie('LPlay_list', _id, null, "/", DomainUrl, false);
        do_alert('添加成功, 请查看播放列表！');
        setTimeout('close_alert()', 3000);
    }else{
        window.open('/p_' + _id + '.htm', 'play');
    }
};

function go_page( _obj, _total ){
    var to_page = $(_obj).value;
    if ( '' == to_page) { do_alert('请输入页码后，再进行跳转！'); setTimeout('close_alert()', 3000); return;}
    to_page = to_page > _total ? _total : to_page;
    location.href = to_page + '.htm';
};

function go_page_so( _url, _obj, _total ){
    var to_page = $(_obj).value;
    if ( '' == to_page) { do_alert('请输入页码后，再进行跳转！'); setTimeout('close_alert()', 3000); return;}
    to_page = to_page > _total ? _total : to_page;
    location.href = _url + 'p=' +to_page;
};

function user_login(){
    var user_name = $('user').value;
    var pwd = $('pwd').value;
    if( '' == user_name || '' == pwd ){
         do_alert('用户名或密码不允许为空！'); setTimeout('close_alert()', 5000);
        return false;
    }
    var user = new Dj97_ajax({method : 'POST', url : '../dj97_ajax.php', async : true, responseType : 'text'});
	user.onReady = function(_result){
        switch( _result ){
            case 'error' : do_alert('用户名或密码错误, 请重试!'); setTimeout('close_alert()', 3000); break;
            case 'ok' : is_login(); break;
        }
	};
	user.send('action=user_login&name='+escape(user_name)+'&pwd='+escape(pwd)+'&time='+new Date().getTime());
	user = null;
    return false;
};

function is_login(){
    var user_name = get_cookie('user_name');
    if( '' == user_name ){
        setTimeout('is_login()', 2000);
        return;
    }
    var nikename = get_cookie('user_nikename');
	var temp_qq = get_cookie('user_qq') ;
	if( '' != temp_qq )
		var qq_str = '| <a href="http://kl.qq258.com/info/'+ temp_qq +'" target="_blank"><font color="red">QQ克隆</font></a>';

    $('user_info').innerHTML = '<li class="i tR"><span class="tC2">'+ unescape(nikename) +'</span> , 欢迎您再次登陆! &nbsp;&nbsp;&nbsp;&nbsp;<a href="/user/" target="user">控制面板</a> ' + qq_str +' | <a href="/user/sms.php?action=inbox" target="user">短消息</a> | <a href="/u/'+ user_name +'/" target="_blank">个人主页</a> | <a href="javascript:user_logout()" class="pointer">退出登陆</a></li>';
};

function user_logout(){
    var user = new Dj97_ajax({method : 'POST', url : '../dj97_ajax.php', async : true, responseType : 'text'});
	user.onReady = function(_result){
        if( 'ok' ==  _result ){
             $('user_info').innerHTML = '<li class="t">用户名：</li><li><input type="text" name="user" id="user" value="" class="ico user i_name" /></li><li class="t">&nbsp;&nbsp;密码：</li><li><input type="password" name="pwd" id="pwd" value="" class="ico user i_pwd" /></li><li class="b">&nbsp;<input type="submit" value="" name="so" id="so" class="ico b_lgn" /></li><li class="b">&nbsp;<input type="button" name="so" id="so" onclick="window.open(\'/register.php\',\'reg\')" class="ico b_reg" /></li><li class="t">&nbsp;&nbsp;<a href="/getpwd.php">忘记密码</a></li>';
             is_login();
        }
	};
	user.send('action=user_logout&time='+new Date().getTime());
	user = null;
};

function get_cookie(_name){
	var Res=eval('/'+_name+'=([^;]+)/').exec(document.cookie);
	return Res==null?'':unescape(Res[1]);
};

function set_cookie(_name, _value, _hours){
    var expdate = new Date();
    if( null != _hours ) expdate.setTime(expdate.getTime() + _hours * 3600000);
   document.cookie = _name + '=' + escape (_value) + '; path=/;domain=.dj19.com;' +((_hours == null) ? '' : ('; expires='+ expdate.toGMTString()));
};

function SetCookie(name, value){
	var expdate = new Date();
	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;
	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))+((secure == true) ? "; secure" : "");
};

function create_div( _str, _msgw, _msgh ){
    //document.documentElement.style.overflow='hidden';
   // document.getElementsByTagName('body')[0].style.overflow='hidden';
    var bgObj=document.createElement('div');
    bgObj.setAttribute('id','bgDiv');
    bgObj.style.position='absolute';
    bgObj.style.top='0px';
    bgObj.style.left='0px';
    bgObj.style.background='#FFFFFF';
    bgObj.style.opacity='0.60';
    bgObj.style.filter = 'alpha(opacity=60)';
    bgObj.style.width='100%';
    bgObj.style.height=document.body.scrollHeight + 'PX';
    bgObj.style.zIndex = '10000';
    document.body.appendChild(bgObj);

    var msgObj = document.createElement('div');
    var _width = document.body.clientWidth;
	var _height = document.documentElement.clientHeight||document.body.clientHeight;
    var LeftPosition = (_width) ? (_width - _msgw)/2 : 0  ;
    var TopPosition = (_height) ? (_height - _msgh)/2 + document.documentElement.scrollTop : 0 ;
    msgObj.setAttribute('id','msgDiv');
    msgObj.setAttribute('align','center');
    msgObj.style.position = 'absolute';
    msgObj.style.border='0';
    msgObj.style.left = LeftPosition + 'px';
	msgObj.style.top  = TopPosition + 'px';
    msgObj.style.width = _msgw + 'px';
    msgObj.style.height = _msgh + 'px';
    msgObj.style.textAlign = 'center';
    msgObj.style.zIndex = '10001';
    msgObj.innerHTML= _str;
    document.body.appendChild(msgObj);
};
function close_div(){
    //document.documentElement.style.overflow='auto';
    document.body.removeChild($('bgDiv'));
    document.body.removeChild($('msgDiv'));
};
function open_fav( _id, _type ){
    create_div( '<iframe id="ifm" src="/user/chk_login.htm?action=fav&id='+_type+'_'+_id+'" frameborder="0" width="500" height="206" scrolling="no"></iframe>', 500, 206 );
};

function open_feedback( parameter ){
    create_div( '<iframe id="ifm" src="/feedback.php?'+ parameter +'" frameborder="0" width="500" height="275" scrolling="no"></iframe>', 500, 275 );
};

function do_alert( msg ){
    var _height = document.documentElement.clientHeight||document.body.clientHeight;
    var _top = (_height) ? (_height - 39)/2 + document.documentElement.scrollTop : 0;
    var msgObj=document.createElement('div');
    msgObj.setAttribute('id', 'msgInfo');
    msgObj.setAttribute('align', 'center');
    msgObj.style.position = 'absolute';
    msgObj.style.top = _top + 'px';
    msgObj.style.left = '0';
    msgObj.style.width = '100%';
    msgObj.style.height = '33px';
    msgObj.style.zIndex = '999';
    msgObj.innerHTML = '<span id="alert">'+ msg +'</span>';
    document.body.appendChild(msgObj);
};
function close_alert(){
    document.body.removeChild($('msgInfo'));
};
