/*
(function($){
 
})(jQuery);
*/

var gparam={}
//try{ if( gparam == undefined) gparam={};}catch(e){var gparam={};}
  

/*   sign in, out, revoke  start    */
function facebook_signin(param){ 
  window.location.href ='/sns/facebook/signin?'+jQuery.param(param);     
}
 
function facebook_signout(homeurl){ 
  param={ homeurl:homeurl};
  window.location.href ='/sns/facebook/signout?'+jQuery.param(param);     
}
 
function facebook_revoke(homeurl,title, message){ 
  param={ homeurl:homeurl};
  $.alerts.confirm(message, 'notitle', function(r) {
    //alert(r);
    //alert('/sns/twitter/revoke?'+jQuery.param(param));
    if(r) window.location.href ='/sns/facebook/revoke?'+jQuery.param(param);  
  });
  /*
  jConfirm(message, title, function(r) {
    if(r) window.location.href ='/sns/facebook/revoke?'+jQuery.param(param);     
  });
  */
  
}

function twitter_signin(param){ 
  window.location.href ='/sns/twitter/signin?'+jQuery.param(param);     
}

function twitter_signout(homeurl){ 
  param={ homeurl:homeurl};
  window.location.href ='/sns/twitter/signout?'+jQuery.param(param);     
}

function twitter_revoke(homeurl, title, message){ 
  param={ homeurl:homeurl};
  $.alerts.confirm(message, 'notitle', function(r) {
    //alert(r);
    //alert('/sns/twitter/revoke?'+jQuery.param(param));
    if(r) window.location.href ='/sns/twitter/revoke?'+jQuery.param(param);
  });
  /*
  jConfirm(message, title, function(r) {
    alert(r);
    alert('/sns/twitter/revoke?'+jQuery.param(param));
    if(r) window.location.href ='/sns/twitter/revoke?'+jQuery.param(param);
  });
  */
 
}
/*   sign in, out, revoke  end    */

 
/* connection start  */
function bind_connection_twitter(id, callback){
 
  $('#'+id ).click(function(){ 
    //alert(jQuery.param( callback));
    twitter_signin(callback);
  });
}

function bind_connection_facebook(id, callback){
  $('#'+id ).click(function(){ 
    //alert(jQuery.param( callback));
    facebook_signin( callback);
  });
}
/* connection end  */


/* connectionbox bind start  */
function bind_connectionbox_close(id, others){
 $('#'+id + ' .close').click(function(){ 
	   try{ 
	  	 if( gparam == undefined) gparam={};
	   }catch(e){
	  	 var gparam={};
	   }
	 		 
	 	
     $($(this).parent()).slideUp('slow');
     if(others){
    	 if(others.cookie){
    		 $.cookie(others.cookie, 'hide', { expires: 9999999, path: '/' });
    	 }else if(others.callback){
    		 others.callback();
    	 }
     }
     return false;
 });
}
  


function bind_connectionbox_twitter(id, callback){
 
  bind_connection_twitter(id + ' .twitter_connection', callback);
  /*
  $('#'+id + ' input.twitter_connection').click(function(){ 
    twitter_signin(callback);
  });
  */
}

function bind_connectionbox_facebook(id, callback){
  bind_connection_facebook(id + ' .facebook_connection', callback);
  /*
  $('#'+id + ' input.facebook_connection').click(function(){ 
    facebook_signin( callback);
  });
  */
}

/* connectionbox bind end  */




/* settting revoke start  */
function bind_twitter_revoke(id, homeurl, title, message){
 
  $('#'+id ).click(function(){ 
    twitter_revoke(homeurl, title, message);
    return false;
  });
}

function bind_facebook_revoke(id, homeurl, title, message){
  $('#'+id ).click(function(){ 
    facebook_revoke(homeurl, title, message);
    return false;
  });
}
/* settting revoke end  */

  
 

function shorturl(longurl){
  /*
  $.getJSON('/sns/fn/shorturl','longurl='+longurl,function(data){
    ret= data.data.url;
    alert(ret);
  });
  */
   var url;
   $.ajax({
      url: '/sns/fn/shorturl',
      async: false,
      type: 'GET',
      data: 'longurl='+longurl,
      timeout: 5000,
      error: function(){
        //alert('Error loading XML document');
      },
      success: function(jsontext){
      json=eval('(' + jsontext + ')');
      url=json.data.url;
      }
   });  
   return url;
}
 
/* goodbad bind end  */






/* tweet start  */
 

var blocksize=5;
var replyinfo=[0,'@@@@@@@@@@'];
var debug=function(msg){
    if(false)
        alert(msg);
}
 
function bind_revisedcss(){ 
  $('.block-comments .comment-holder-wrapper').css('height','100%');
  $('.block-comments .comment-holder').css('height','100%');
  /*
  $('<span> logout</span>').insertBefore('.form-tweet .row-right label');
  $('.form-tweet .row-right span').css('float','right');
  */
}
 
function bind_istweet(param){
	
	$('#twitter_istweet').click(
			function(){
				if($('#twitter_istweet').attr('checked') == false){
					//$('#tweet_connection_box .close').trigger('click');
				}else{
				  if(param.facebook_connected == false ){
				  	$('#twitter_tweet').trigger('focus');
				  }
				}
 
			}
		
	);  
	/*
  try{ 
    $('input[name=foo]').attr('checked', true);
    $('#twitter_istweetwrap').click     (function(){_istweet(param) });   
    
    function _istweet(twitter_connected){
      if($('#twitter_istweet').is(':checked') ){
        $('input[name=foo]').attr('checked', false);
      }else{
        $('input[name=foo]').attr('checked', true);
      }
    } 
  }catch(e){
    //alert(e);
  }
  */
}   
 
function bind_logout(homeurl){
  try{
    $('#twitter_logout').click(function(){ 
      twitter_signout(homeurl);
      return false;
      //debug('here');
    });  
  }catch(e){}
}   

function bind_tweettextarea(param){
  $('#twitter_tweet').focus(function(event) {
    if($('#twitter_istweet').attr('checked') == true ){
      $('#tweet_connection_box').slideDown('slow'); 
      return;
    }
    if( !( param.islogin || param.facebook_connected) ){
    	//$("#twitter_istweet").attr("checked", true);
    	$('#tweet_connection_box').slideDown('slow');
    }
    
  });

  /*
  $('#twitter_tweet').keyup(function(event) {
     var htmlStr = $('#twitter_typingcount').html();
     $('#twitter_typingcount').html(140-$('#twitter_tweet').val().length);
     if( $('#twitter_tweet').val().length > 130) {
       $('#twitter_typingcount').css('color','red');
     }else{
       $('#twitter_typingcount').css('color','#787878');
     }      
  });
  */
} 
 
function bind_morebutton(param){ 
  $('#twitter_more').click(function(){ 
      var seqdown=$("div.comment-holder div.comment:last-child").attr('id').substring(8);
      $.ajax({
        url: '/sns/tweetlist',
        type: 'GET',
        data: 'ownerid='+param.ownerid+'&seqdown='+seqdown+'&categoryid='+ param.categoryid +'&categorytype='+ param.categorytype,
        dataType:'html',
        timeout: 5000,
        error: function(){
            debug('#twitter_more');
        },
        success: function(html){
         $("div.comment-holder div.comment:last-child").after(html);
         bind_status();
         $.ajax({
           url: '/sns/daofn/SnsSQL/getTwitterLogCount',
            type: 'GET',
            data: 'ownerid='+param.ownerid+'&seqdown='+seqdown+'&categoryid='+ param.categoryid +'&categorytype='+ param.categorytype,
            timeout: 5000,
            error: function(){
                debug('Error loading XML document');
            },
            success: function(xml){
              if ( parseInt(xml) < blocksize) {
                 $('#twitter_more').css('visibility','hidden');
                //$('#twitter_more').hide()
              }
            }
         });               
       }//success
      }); 
      return false;
  });   

  $.ajax({
    url: '/sns/daofn/SnsSQL/getTwitterLogCount',
    type: 'GET',
    data: 'ownerid='+param.ownerid+'&categoryid='+ param.categoryid +'&categorytype='+ param.categorytype,
    timeout: 5000,
    error: function(){
    },
    success: function(xml){
       if ( parseInt(xml) <= blocksize ) {
        $('#twitter_more').css('visibility','hidden');
      }
    }
  }); 
}

function bind_sendbutton(param){
 
  $('#twitter_sendbutton').click(function(){ 
    if(  $.trim($('#twitter_tweet').val()).length == 0  ){
      return false;
    } 
    var istweet='false';
    if( $('#twitter_istweet').is(':checked')){
      istweet='true';
    }   
    replystatusid=0;
    replysnsname='';
    replysnsurl='';
    replysnstype='';
    replyseq=0;
    parentthread=0;
    parentdepth=0;
    //bind_reply에서 작성됨
    if ( $('#twitter_tweet').val().indexOf(replyinfo[0]) == 0 ){
      replysnsname =  replyinfo[0].substr(1);
      replystatusid = replyinfo[1];
      parentthread = replyinfo[2];
      parentdepth = replyinfo[3];
      replysnsurl= replyinfo[4];
      replysnstype= replyinfo[5];
      replyseq= replyinfo[6];
    }
    if(param.snstypes.length==0) return;
 
    var oldcomment_cnt=  $("div.comment-holder div.comment").length ;
 
    if (param.fanid){
	    $.post('/sns/fn/addfan', 
	        {
	    			fanid    : 			 param.fanid
	          ,categoryid :    param.categoryid
	          ,categorytype  : param.categorytype
	        } 
	        ,function(data){
	        }
	    ); 
    }

    jQuery.post( '/sns/fn/comment'
      ,
      {
        istweet       : istweet
       ,ownerid       : param.ownerid
       ,tweet         : $('#twitter_tweet').val()
       ,categoryid    : param.categoryid
       ,categorytype  : param.categorytype
       ,replystatusid : replystatusid
       ,replysnsname  : replysnsname
       ,replysnsurl   : replysnsurl
       ,replysnstype  : replysnstype
       ,replyseq      : replyseq      
       ,parentthread  : parentthread
       ,parentdepth   : parentdepth
       ,snstypes      : param.snstypes
       ,homeurl       : param.homeurl
      }
      , function(html){
        //window.location.reload();
        $('#twitter_tweet').val('');
        $('#twitter_tweet').trigger('keydown');
        $('#twitter_typingcount').text(140);
        $('.comment-holder').load('/sns/tweetlist', 'ownerid='+param.ownerid+'&categoryid='+ param.categoryid +'&categorytype='+ param.categorytype, function(data){
          if( oldcomment_cnt == 5 && $('#twitter_more').css('visibility')=='hidden' ){
            $('#twitter_more').css('visibility','visible');
          };
          bind_status();
        });
      }
    );
    return false;
  }); //click
}

function bind_status(){
  $("div.comment-holder div.comment div.comments-inner  div.text-comment div.menu span.trash a").each(bind_deletebutton);
  $(".reply").each(bind_reply);
   //   $("#twitter_list li").each(_bindlist);
}

function bind_fade(){
  //$('.block-comments .twtr-fade').css('border', '1px solid #000000');
  if($(".comment").length  <  1 ){
   $('.block-comments .twtr-fade').hide();
  }
 
  //$('.block-comments .twtr-fade').css('bottom', '56px'); 156
 
  
}
 
 
var bind_reply=function (){
  $(this).click(function(){ 
    if ( $('#twitter_tweet').val().indexOf($(this).attr('id')) == -1 ){
      $('#twitter_tweet').focus();
      //$('#twitter_tweet').val($(this).attr('id')+' '+$('#twitter_tweet').val() );
     
      replyinfo = eval($(this).attr('id'));
      replyinfo[0]=replyinfo[0].split(" ").join('');
      replyinfo[0]=replyinfo[0].split(",").join('');
      $('#twitter_tweet').val(replyinfo[0]+' ');
    }
    return false;
  });
}

var bind_deletebutton=function () {
 
    $(this).click(function(){ 
      p = $(this).parent().parent().parent().parent();
      $.ajax({
        url: '/sns/fn/delete',
        type: 'POST',
        data: $(this).attr('id'),
        timeout: 5000,
        error: function(xhr, ajaxOptions, thrownError){ 
          //alert('xxx');
          //debug('Error loading XML document');
          $(p).hide();
        },
        success: function(xml){
          $(p).hide();
        }
      }); 
      return false;
    });
    
}
/* tweet end  */   


/*
//bind_sendbutton
//최상위1개만 추가
$.ajax({
    url: '/sns/twitter/tweetlist',
    type: 'GET',
    //data: 'ownerid='+ownerid,
    data: 'ownerid='+ownerid+'&sequp='+$("div.comment-holder div.comment:first-child").attr('id').substring(8),
    dataType:'html',
    timeout: 5000,
    error:function (xhr, ajaxOptions, thrownError){ 
        //debug(xhr.statusText+xhr.responseText ); 
        //debug(xhr.statusText); 
        //debug(thrownError); 
    },
    success: function(html){
       $('div.comment-holder div.comment:first-child').before(html);
       //alert(html);
       //$('div.comment-holder').html(html);
       bind_status();
    }//success
});//ajax
*/

