/**
 * pong.js 		Leah <apocalypser@hanmail.net>
 *
 * ver 1.0.20100224
 *
 * Jquery Need, facebox Need.
 *  - because of some old function. must be include.
 *  - jquery.js facebox.js facebox.css other_profile_view.css 
 */

var _pon_key = '<SPAN ';
var _pon_cnt = 0.2;			// open delay time
var _pon_rmv_cnt = 0.2;		// close delay time
var _pon_pre_id = null;		// prepare id
var _pon_open_id = null;	// opened flag
var _pon_area_ck = false;	// closing area check.
  
function _pong_area_set( boolvar ) {
  	if ( boolvar == true ) _pon_area_ck = true;
	else _pon_area_ck = false;
}
		 
function _ret_iframe(uid) {
 	var tmp = '<SPAN style="position:absolute;visibility:visible;margin-top:-20px;">'
	        + '<iframe width="267" height="129" marginwidth="0" marginheight="0" hspace="0" vspace="0" '
			+ 'frameborder="0" scrolling="no" src="'
			+ '/prog/pong.html?uid=' + uid + '"></iframe></SPAN>';
									 
    return tmp;
}

function _pong_rmv_str(obj_id) {
	//var str = document.getElementById(obj_id).innerHTML.toUpperCase();
	//var pos = str.indexOf(_pon_key);
	//if (pos != -1) {
	//	document.getElementById(obj_id).innerHTML = document.getElementById(obj_id).innerHTML.substr(0, pos);
	//}
	document.getElementById(obj_id).style.display = 'none';
}

function _pong_open( uid, obj_id ) {
	if ( obj_id == _pon_pre_id ) {
		if ( (_pon_open_id != null) && (_pon_open_id != obj_id) ) {
			_pong_rmv_str( _pon_open_id );
		}
								 
		if (_pon_open_id != obj_id) {
			_pon_open_id = obj_id;
			
			if (document.getElementById(obj_id).innerHTML == '') {
				document.getElementById(obj_id).innerHTML += _ret_iframe(uid);
			}
			document.getElementById(obj_id).style.display = 'inline';
		}
	}
}

function _pong_close( obj_id ) {
	var tmpstr = "_pong_close(\'" + obj_id + "\')";
	 
 	if ( _pon_area_ck == true ) {
		setTimeout( tmpstr, _pon_rmv_cnt * 1000 );
	} else {
		_pon_open_id = null;
		_pong_rmv_str(obj_id);
	}
}
										 
function _pong_close_all() {
 	_pon_area_ck == false;
	if (_pon_open_id != null) _pong_rmv_str(_pon_open_id);
	_pon_open_id = null;
	_pon_pre_id = null;
}





function pong_v1( uid, obj_id ) {
	//var str = document.getElementById(obj_id).innerHTML.toUpperCase();
	//var pos = str.indexOf(_pon_key);

	//if ( pos == -1 ) {
	if ( document.getElementById(obj_id).style.display == 'none' ) {
		var tmpstr = "_pong_open(" + uid + ", \'" + obj_id + "\')";
		if ( _pon_pre_id != obj_id ) {
			_pon_pre_id = obj_id;
			setTimeout( tmpstr, _pon_cnt * 1000 );
		}
	}
}

function pong_rmv(obj_id) {
	var tmpstr = "_pong_close(\'" + obj_id + "\')";
	_pon_pre_id = null;
	setTimeout( tmpstr, _pon_rmv_cnt * 1000 );
}


function pong_layerpop( mode, uid ) {
	if (mode == 1) {
		var urlstr = '/index.php?c=inbox_noten&m=facebox_noten_write_form&uid=' + uid
		jQuery.facebox({ ajax: urlstr })
		$('#facebox .body').removeClass('connectwrite_body').removeClass('group_invite_body').addClass("quickmail_body");
	} else if (mode == 2) {
		var urlstr = '/index.php?c=inbox_connect&m=facebox_connect_write_form&uid=' + uid
		jQuery.facebox({ ajax: urlstr })
		$('#facebox .body').removeClass('quickmail_body').removeClass('group_invite_body').addClass('connectwrite_body');
	} else if (mode == 3) {
		var urlstr = '/index.php?c=inbox_noten&m=facebox_openmail_writeform&uid=' + uid
		jQuery.facebox({ ajax: urlstr })
		$('#facebox .body').removeClass('connectwrite_body').removeClass('group_invite_body').addClass("quickmail_body");
	} else if (mode == 4) {
		var urlstr = '/index.php?c=inbox_noten&m=facebox_quickmail_writeform&uid=' + uid
		jQuery.facebox({ ajax: urlstr })
		$('#facebox .body').removeClass('connectwrite_body').removeClass('group_invite_body').addClass("quickmail_body");
	} else if (mode == 5){
        var urlstr = '/index.php?c=inbox_noten&m=facebox_noten_write_form&gs_uid=' + uid
        jQuery.facebox({ ajax: urlstr })
        $('#facebox .body').removeClass('connectwrite_body').removeClass('group_invite_body').addClass("quickmail_body");
    } 
}

function send_proper_mail(uid)
{
    $.post(
        '/index.php',
        {c:'inbox_main',m:'check_which_mail_send',uid:uid},
        function(data){
            switch(data.mail){
                case 'openmail':
                    pong_layerpop(3,uid);
                break;
                case 'quickmail':
                    pong_layerpop(4,uid)
                break;
                case 'buymorequick':
                    if(confirm('보낼 수 있는 퀵메일 개수가 소진되었습니다. 메시지를 보내기 위해 추가 구매하시겠습니까?'))
                        location.href='/index.php?c=main_main&m=biz_payquickmail';
                break;
                case 'quickconfirm':
                    location.href='/index.php?c=main_main&m=quickmail_confirm&member_uid='+uid;
                break;
                case '1chon':
                    pong_layerpop(1,uid)
                break;
            }
        },
        'json'
    );
}



/**
* 인맥 연결시 불러오는 스크립트
* uid : 연결하고자 하는 사람의 id
* redirect : 연결 후 보내고자 하는 페이지 주소. 이것을 공란으로 둘 때는 브라우저 주소를 그 값으로 가져 옴
*/
function pong_connect(uid,redirect){
    if(redirect=='' || typeof redirect=='undefined'){
        var thisurl = location.href;
        var parts = thisurl.split("/");
        var size = parts.length;
        for(i=0;i<size;i++){
            if(i>2){
                redirect += '/'+parts[i];
            }
        }
    }
    redirect = base64_encode(redirect);
    location.href = '/index.php?c=network_invite&m=connect_form&uid='+uid+'&redirect='+redirect;
}

/**
* 인맥 수락을 ajax 를 통해 함
* succss 일 때  대화 창 뜨고, 아닐 때 에러 창 뜸
* jQuery 및 colorBox 사용함
* connid - connect id
* uid - 나를 요청한 사람의 id
*/
//$('#allow_conn').colorbox({'href':'/index.php?c=inbox_connect&m=post_allow&uid='+uid});

function pong_allow_connect(connid){
    $.post(
        '/index.php',
        {c:'inbox_connect',m:'allow_connect_ajax',connid:connid},
        function(data){
            if(data.result == 'success'){

                if((BrowserDetect.browser=='Explorer') && (BrowserDetect.version < 8)){
                    var answer = confirm("인맥 요청을 수락했습니다. 답장을 보내시겠습니까?");
                    if(answer){
                       location.href ='/index.php?c=inbox_noten&m=noten_message_form&uid='+data.uid+'&from=connect';                         
                    }
                }
                else{
                    open_colorbox('/index.php?c=inbox_connect&m=post_allow&uid='+data.uid);
                }
            }
            if(data.result =='fail'){
                alert(data.reason);
            }
        },
        'json'
    );
}

/**
* colrobox 로 페이지 열기
*/
function open_colorbox(togo){
    jQuery.colorbox({href:togo});
}

/**
* pong_allow_connect 와 같으나, 해당 버튼을 없애줌
*/
function pong_allow_connect_2(connid,btn){
    $.post(
        '/index.php',
        {c:'inbox_connect',m:'allow_connect_ajax',connid:connid},
        function(data){
            if(data.result == 'success'){
                btn.parentNode.removeChild(btn);
                if((BrowserDetect.browser=='Explorer') && (BrowserDetect.version < 8)){
                    var answer = confirm("인맥 요청을 수락했습니다. 답장을 보내시겠습니까?");
                    if(answer){
                       location.href ='/index.php?c=inbox_noten&m=noten_message_form&uid='+data.uid+'&from=connect';                         
                    }
                }
                else{
                    open_colorbox('/index.php?c=inbox_connect&m=post_allow&uid='+data.uid);
                }            }
            if(data.result =='fail'){
                alert(data.reason);
            }
        },
        'json'
    );
}

