function now_conn() {
	var nowconn = $("#nowconn");
	
  $.ajax({
    type: "POST",
    url: "/coterie/now_conn.php",
    data: "url="+location.href+"&location="+document.title,
    cache: false,
    success: function(data) {
      nowconn.html(data);
    }
  });
}

function set_menu(id,mode) {
	var outlogin = $("#outlogin .member_menu_list > ul");
	
	var msg = "추가";
	if(mode=='del') {
		msg = "삭제";
	}
	
  $.ajax({
    type: "POST",
    url: "/coterie/mymenu_ok.php",
    dataType: "json",
    data: "bo_table="+id+"&mode="+mode,
    cache: false,
    beforeSend: function() {
    	if(!confirm("전용메뉴를 "+msg+" 하시겠습니까?")) {
    		return false;
    	}
    },
    success: function(data) {
    	if(data.result=="true") {
    		if(mode=="del") {
    			outlogin.find("."+id).remove();
    		} else {
    			outlogin.find(".nomenu").remove();
    			outlogin.append('- <li><span class="'+id+'"><a href="/board/board.php?bo_table='+id+'">'+data.name+'</a> <img src="/image/del_menu.png" alt="" style="cursor:pointer;" onclick="set_menu(\''+id+'\',\'del\')" /></span></li>');
    		}
    		alert(data.text);
    	} else {
    		alert(data.text);
    	}
    }
  });
}

function divpos(obj,e) {
  var sizeX;
  var sizeY;
  
  var win = $(window);
  
  if(win.width() > obj.width()+4) {
    if((win.scrollLeft() + win.width()) - e.pageX < obj.width()+4) {
      setsizeX = e.pageX - obj.width() - 6;
    } else {
      setsizeX = e.pageX;
    }
  } else {
    setsizeX = win.width() - obj.width() - 6 + win.scrollLeft();
  }
  if(win.height() > obj.height()+4) {
    if((win.scrollTop() + win.height()) - e.pageY < obj.height()+4) {
      setsizeY = e.pageY - obj.height() - 6;
    } else {
      setsizeY = e.pageY;
    }
  } else {
    setsizeY = win.height() - obj.height() - 6 + win.scrollTop();
  }

  obj.css({"left":setsizeX,"top":setsizeY}).show();
}

function member_info(no) {
   var nowurl = location.href;
   var board_id = nowurl.match(/bo_table\=[a-zA-Z0-9]*/);
   var body = $("body");
   var doc = $(document);
   var info = $("#info_div");

  if(no!='') {
    body.prepend('<div id="info_div"></div>');

    doc.one('click',function(e){
      $.ajax({
        type: "POST",
        url: "/coterie/member_info.php",
        data: "mb_no="+no+"&board_id="+board_id,
        cache: false,
        success: function(data) {
          info.html(data).trigger('onDiv');
        }
      });

      info.one('onDiv',function(){
      	divpos(info,e);
      }).bind('click',function(event){
        event.stopPropagation();
      });
      doc.one('click',function(event){
      	info.remove();
      });
    });
  } else {
    alert('존재하지 않는 회원입니다.');
  }
}

function board_report(board,no) {
  if(no!='') {
    $("body").prepend('<div id="report_div"></div>');

    $(document).one('click',function(e){
      $.ajax({
        type: "POST",
        url: "/board/board_report.php",
        data: "bo_no="+no+"&bo_table="+board,
        cache: false,
        success: function(data) {
          $("#report_div").html(data).trigger('onDiv');
        }
      });

      $('#report_div').one('onDiv',function(){
      	divpos($('#report_div'),e);
      }).bind('click',function(event){
        event.stopPropagation();
      });
      $(document).one('click',function(event){
        $("#report_div").remove();
      });
    });

  } else {
    alert('게시물이 존재하지 않습니다..');
  }
}

/*bit.ly를 이용해 단축주소 받아오기
function get_bitly_g4(tid, bo_table, wr_id) {

  // set up default options
  wr_url = g4_url + '/board/board.php?bo_table=' + bo_table + '&wr_id=' + wr_id;
  wr_url_encode = escape(wr_url);

  var defaults = {
    version:    '2.0.1',
    login:      bitly_id,
    apiKey:     bitly_key,
    history:    '0',
    longUrl:    wr_url_encode
  };

  // Build the URL to query
  var daurl = "http://api.bit.ly/shorten?"
    +"version="+defaults.version
    +"&longUrl="+defaults.longUrl
    +"&login="+defaults.login
    +"&apiKey="+defaults.apiKey
    +"&history="+defaults.history
    +"&format=json&callback=?";

    // Utilize the bit.ly API
    $.getJSON(daurl, function(data) {

        var bitly_url = data.results[wr_url].shortUrl;

        // Make a good use of short URL - 화면의 url 정보를 업데이트
        $(tid).html('<input type="text" name="bitly" class="bitly_input" value="'+bitly_url+'" readonly="readonly" />');
        
  			$(".bitly_div .bitly_input").click(function(){
 					$(this).select();
 				});

        url = g4_path +'/' + g4_bbs +'/g4_bitly_update.php';

        send  = 'bo_table=' + bo_table;
        send += '&wr_id=' + wr_id;
        send += '&bitly_url=' + bitly_url;

        $.ajax({
        type: 'POST',
        url: url,
        data: send,
        cache: false,
        success: function(result) {
            result      = result.split(',');
            msg_num     = result[0];
            if (msg_num !== '000') {
              alert('잘못된 접근입니다.\n\n'+result);
            }
          }
        });
    });
  };
  */
	function tw_info(mb) {
		if(mb!='') {
			$("body").prepend('<div id="twitter_info"></div>');
			
			$(document).one('click',function(e){
			  $.ajax({
			    type: "POST",
			    url: "/twitter/twitter_info.php",
			    data: "mb="+mb,
			    cache: false,
    			beforeSend: function() {
						
			    },
			    success: function(data) {
			      $("#twitter_info").html(data).trigger('onDiv');
			    }
			  });
			
			  $('#twitter_info').one('onDiv',function(){
    	  	divpos($('#twitter_info'),e);
	  	  }).bind('click',function(event){
	  	    event.stopPropagation();
	  	  });
	  	  $(document).one('click',function(event){
	  	    $("#twitter_info").remove();
	  	  });
	  	});
		} else {
			alert("정보가 존재하지 않습니다.");
		}
	}
	
	function chk_tw_limit() {
		$.getJSON("http://twitter.com/account/rate_limit_status.json?callback=?",
    function(data){
    	tw_limit = data.remaining_hits;
    	update_tw_info();
    });
	}
	
	function follow_menu(mb) {
		$.ajax({
		  type: "POST",
		  url: "/twitter/follow_menu.php",
		  data: "mb="+mb,
		  cache: false,
    	beforeSend: function() {
				$("#follow_menu_div").html('<img src="/image/tw_loading.gif" alt="" />');
		  	$(".tw_follow_menu").hide();
		  },
		  success: function(data) {
		  	$("#follow_menu_div").html(data);
		  }
		});
	}
	
function yfrog_upload() {
		$("body").prepend('<div id="yfrog_upload"></div>');
		
		$(document).one('click',function(e){
		  $.ajax({
		    type: "POST",
		    url: "/yfrog.php",
		    cache: false,
  			beforeSend: function() {
					
		    },
		    success: function(data) {
		      $("#yfrog_upload").html(data).trigger('onDiv');
		    }
		  });
		
		  $('#yfrog_upload').one('onDiv',function(){
  	  	divpos($('#yfrog_upload'),e);
  	  }).bind('click',function(event){
  	    event.stopPropagation();
  	  });
  	  $(document).one('click',function(event){
  	    $("#yfrog_upload").remove();
  	  });
  	});
}
