function getBrowserWidth () {   
    if ( window.innerWidth ) { return window.innerWidth; }   
    else if ( document.documentElement && document.documentElement.clientWidth != 0 ) { return document.documentElement.clientWidth; }   
    else if ( document.body ) { return document.body.clientWidth; }   
    return 0;   
}  

function kero_box_position(e,el){
	var left=0;
	var top=0;
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	var sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
	var leftPos = e.clientX + 10;
	if(left==1) leftPos = e.clientX - 260;
	if(leftPos<0)leftPos = 0;
	el.style.top = e.clientY +10 + st + 'px';
	if(top==1)  el.style.top = e.clientY - el.offsetHeight -1 + st + 'px';

	w = getBrowserWidth();
	if(w>1 && w<leftPos + el.offsetWidth){
		el.style.left = (w - el.offsetWidth + sl) + 'px';
	}else{
		el.style.left = leftPos + sl + 'px';
	}
}

function kero_send_tag_on_load(){
	var a = arguments;
	if(a[0]=='0'){
		try{
			document.getElementById("tags"+a[1]).innerHTML=a[2];
		}catch(e){}
		hideTagAddBox();
	}else if(a[0]=='1'){
		try{
			document.getElementById("kero_tag_error_box").innerHTML=a[2];
		}catch(e){}
	}
}

function showTagAddBox(e,item_id,usr_id){
	if(document.all)e = event;
	var el = document.getElementById("kero_tag_add_box");
	if(el){}else{
	el = document.createElement('div');
	el.id = "kero_tag_add_box";
	el.setAttribute('class', 'kero_tag_add_box');
	el.setAttribute('className', 'kero_tag_add_box');
	document.body.appendChild(el);
	}
	el.innerHTML = '<form >'
	+'追加するタグを入力し、送信ボタンを押してください。<br />'
	+'<input class="add_tag" type="text" name="tag" />'
	+'<input type="button" value="送信" onclick="'
	+'new JSONP2(\''
	+'http://admin.voiceblog.jp/tagging.php'
	+'?method=add'
	+'&item_id='+item_id
	+'&usr_id='+usr_id
	+'&tag=\'+encodeURI(this.form.tag.value)'
	+')'
	+'"/>'
	+'<input type="text" style="position:absolute;visibility:hidden">'
	+'</form>'
	+'<div id="kero_tag_error_box"></div>'
	+'<p class="close"><a onclick="hideTagAddBox();">閉じる</a></p>';
	;
	kero_box_position(e,el);
	
	new JSONP2(
	'http://admin.voiceblog.jp/tagging.php'
	+'?method=check'
	+'&item_id='+item_id
	+'&usr_id='+usr_id
	);
}
function kero_tag_login_check_on_load(){
	var a = arguments;
	if(a[0]=='0'||a[0]=='1'){
		try{
			document.getElementById("kero_tag_add_box").innerHTML=
			'<div id="kero_tag_del_list">'+a[2]+'</div>'
			+'<div id="kero_tag_error_box"></div>'
			+'<p class="close"><a onclick="hideTagAddBox();">閉じる</a></p>';
		}catch(e){}
	}
}


function hideTagAddBox(){
	var el = document.getElementById("kero_tag_add_box");
	if(el) el.parentNode.removeChild(el);
}

function showTagDelBox(e,item_id,usr_id){
	if(document.all)e = event;
	var el = document.getElementById("kero_tag_add_box");
	if(el){}else{
	el = document.createElement('div');
	el.id = "kero_tag_add_box";
	el.setAttribute('class', 'kero_tag_add_box');
	el.setAttribute('className', 'kero_tag_add_box');
	document.body.appendChild(el);
	}
	el.innerHTML = '<div id="kero_tag_del_list">読み込み中</div>'
	+'<div id="kero_tag_error_box"></div>'
	+'<p class="close"><a onclick="hideTagAddBox();">閉じる</a></p>';
	;
	/*
	var d=[];
	d['method']= 'get';
	d['item_id']= item_id;
	d['usr_id']= usr_id;
	sendRequest(
	kero_load_tag_on_load,
	d,
	'POST',
	'/tagging.php',
	true,
	true
	);
	*/
	kero_box_position(e,el);
	new JSONP2(
	'http://admin.voiceblog.jp/tagging.php'
	+'?method=get'
	+'&item_id='+item_id
	+'&usr_id='+usr_id
	);
}

function kero_load_tag_on_load(){
	//var res  =  oj.responseText;
	//var a = res.split(',',3)
	var a = arguments;
	if(a[0]=='0'||a[0]=='1'){
		try{
			document.getElementById("kero_tag_del_list").innerHTML=a[2];
		}catch(e){}
	}
}

function kero_send_tag_del(item_id,usr_id,tag){
	/*
	var d=[];
	d['method']= 'del';
	d['item_id']= item_id;
	d['usr_id']= usr_id;
	d['tag']= tag;
	sendRequest(
	kero_send_tag_on_load,
	d,
	'POST',
	'/tagging.php',
	true,
	true
	);
	*/
	new JSONP2(
		'http://admin.voiceblog.jp/tagging.php'
		+'?method=del'
		+'&item_id='+item_id
		+'&usr_id='+usr_id
		+'&tag='+encodeURI(tag)
	);
}



/*******************************************
* JSONP call<br/>
* @auther futa23<br/>
* @version 2008/02/03
********************************************/
JSONP2.counter = 1;
function JSONP2(src){
  this.initialize.apply(this, arguments);
}
JSONP2.prototype = {
  scriptTag: '',
  headTag: '',
  initialize: function(src, callback, scope){
    this.scriptTag = document.createElement("script");
    this.headTag = document.getElementsByTagName("head").item(0);

    this.scriptTag.id = "jsonp" + JSONP2.counter++;
    this.scriptTag.setAttribute('type', 'text/javascript');
    this.scriptTag.setAttribute('charset', 'UTF-8');
    var noCache = '&_noCache=' + new Date().getTime();
    var url = src + noCache;
    
    switch(typeof callback) {
      case 'string':
        url += '&callback=' + callback;
        break;
      case 'function':
        var callbackName = 'callback' + this.scriptTag.id;
        var self = this;
        scope = scope || window;
        window[callbackName] = function(json) {
                    return function() {
                        callback.apply(scope, arguments);
                        window[callbackName] = undefined;
                        try{ delete window[callbackName]; } catch(e){}
                        if (self.headTag) self.remove();
                    }(json);//これで実行される
        }      
        url += '&callback=' + callbackName;
        break;
      default:
        break;
    }
    this.scriptTag.setAttribute('src', url);
    this.headTag.appendChild(this.scriptTag);
  },
  remove: function(){
    this.headTag.removeChild(this.scriptTag);
  }
}

